blob: c7777fd3928ae0de978e52edf04cb99f1ed9059d [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
Guido van Rossum27e177d1995-03-16 15:43:47 +000028- socket.getservbyname(servicename, protocolname) --> port number
Guido van Rossum83a072d2002-09-03 19:10:18 +000029- socket.socket([family[, type [, proto]]]) --> new socket object
Guido van Rossum006bf911996-06-12 04:04:55 +000030- socket.ntohs(16 bit value) --> new int object
31- socket.ntohl(32 bit value) --> new int object
32- socket.htons(16 bit value) --> new int object
33- socket.htonl(32 bit value) --> new int object
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000034- socket.getaddrinfo(host, port [, family, socktype, proto, flags])
35 --> List of (family, socktype, proto, canonname, sockaddr)
36- socket.getnameinfo(sockaddr, flags) --> (host, port)
Guido van Rossum27e177d1995-03-16 15:43:47 +000037- socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h>
Guido van Rossum47dfa4a2003-04-25 05:48:32 +000038- socket.has_ipv6: boolean value indicating if IPv6 is supported
Guido van Rossum5c9eb211999-08-20 18:21:51 +000039- socket.inet_aton(IP address) -> 32-bit packed IP representation
40- socket.inet_ntoa(packed IP) -> IP address string
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +000041- socket.getdefaulttimeout() -> None | float
42- socket.setdefaulttimeout(None | float)
Guido van Rossum6574b3e1991-06-25 21:36:08 +000043- an Internet socket address is a pair (hostname, port)
44 where hostname can be anything recognized by gethostbyname()
45 (including the dd.dd.dd.dd notation) and port is in host byte order
46- where a hostname is returned, the dd.dd.dd.dd notation is used
Guido van Rossum27e177d1995-03-16 15:43:47 +000047- a UNIX domain socket address is a string specifying the pathname
Jeremy Hylton22308652001-02-02 03:23:09 +000048- an AF_PACKET socket address is a tuple containing a string
49 specifying the ethernet interface and an integer specifying
50 the Ethernet protocol number to be received. For example:
Jeremy Hyltondbfb6622001-02-02 19:55:17 +000051 ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple
52 specify packet-type and ha-type/addr -- these are ignored by
Jeremy Hylton22308652001-02-02 03:23:09 +000053 networking code, but accepted since they are returned by the
54 getsockname() method.
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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000064#include "Python.h"
65
Guido van Rossum47dfa4a2003-04-25 05:48:32 +000066#undef MAX
67#define MAX(x, y) ((x) < (y) ? (y) : (x))
68
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000069/* Socket object documentation */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000070PyDoc_STRVAR(sock_doc,
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000071"socket([family[, type[, proto]]]) -> socket object\n\
72\n\
73Open a socket of the given type. The family argument specifies the\n\
74address family; it defaults to AF_INET. The type argument specifies\n\
75whether this is a stream (SOCK_STREAM, this is the default)\n\
76or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\
77specifying the default protocol. Keyword arguments are accepted.\n\
78\n\
79A socket object represents one endpoint of a network connection.\n\
80\n\
81Methods of socket objects (keyword arguments not allowed):\n\
82\n\
83accept() -- accept a connection, returning new socket and client address\n\
84bind(addr) -- bind the socket to a local address\n\
85close() -- close the socket\n\
86connect(addr) -- connect the socket to a remote address\n\
87connect_ex(addr) -- connect, return an error code instead of an exception\n\
88dup() -- return a new socket object identical to the current one [*]\n\
89fileno() -- return underlying file descriptor\n\
90getpeername() -- return remote address [*]\n\
91getsockname() -- return local address\n\
92getsockopt(level, optname[, buflen]) -- get socket options\n\
93gettimeout() -- return timeout or None\n\
94listen(n) -- start listening for incoming connections\n\
95makefile([mode, [bufsize]]) -- return a file object for the socket [*]\n\
96recv(buflen[, flags]) -- receive data\n\
97recvfrom(buflen[, flags]) -- receive data and sender's address\n\
98sendall(data[, flags]) -- send all data\n\
99send(data[, flags]) -- send data, may not send all of it\n\
100sendto(data[, flags], addr) -- send data to a given address\n\
101setblocking(0 | 1) -- set or clear the blocking I/O flag\n\
102setsockopt(level, optname, value) -- set socket options\n\
103settimeout(None | float) -- set or clear the timeout\n\
104shutdown(how) -- shut down traffic in one or both directions\n\
105\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000106 [*] not available on all platforms!");
Guido van Rossum3baaa131999-03-22 21:44:51 +0000107
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000108/* XXX This is a terrible mess of platform-dependent preprocessor hacks.
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000109 I hope some day someone can clean this up please... */
110
Guido van Rossum9376b741999-09-15 22:01:40 +0000111/* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure
112 script doesn't get this right, so we hardcode some platform checks below.
113 On the other hand, not all Linux versions agree, so there the settings
114 computed by the configure script are needed! */
115
116#ifndef linux
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000117# undef HAVE_GETHOSTBYNAME_R_3_ARG
118# undef HAVE_GETHOSTBYNAME_R_5_ARG
119# undef HAVE_GETHOSTBYNAME_R_6_ARG
Guido van Rossum9376b741999-09-15 22:01:40 +0000120#endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000121
Guido van Rossum7a122991999-04-13 04:07:32 +0000122#ifndef WITH_THREAD
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000123# undef HAVE_GETHOSTBYNAME_R
Guido van Rossum7a122991999-04-13 04:07:32 +0000124#endif
125
Guido van Rossume7de2061999-03-24 17:24:33 +0000126#ifdef HAVE_GETHOSTBYNAME_R
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000127# if defined(_AIX) || defined(__osf__)
128# define HAVE_GETHOSTBYNAME_R_3_ARG
129# elif defined(__sun) || defined(__sgi)
130# define HAVE_GETHOSTBYNAME_R_5_ARG
131# elif defined(linux)
Guido van Rossum9376b741999-09-15 22:01:40 +0000132/* Rely on the configure script */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000133# else
134# undef HAVE_GETHOSTBYNAME_R
135# endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000136#endif
137
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000138#if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \
139 !defined(MS_WINDOWS)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000140# define USE_GETHOSTBYNAME_LOCK
Guido van Rossum3baaa131999-03-22 21:44:51 +0000141#endif
142
Just van Rossum1040d2c2003-05-09 07:53:18 +0000143/* On systems on which getaddrinfo() is believed to not be thread-safe,
Just van Rossum09aecd72003-05-09 08:03:44 +0000144 (this includes the getaddrinfo emulation) protect access with a lock. */
Just van Rossum16e426b2003-05-09 08:12:00 +0000145#if defined(WITH_THREAD) && (defined(__APPLE__) || defined(__FreeBSD__) || \
146 defined(__OpenBSD__) || defined(__NetBSD__) || !defined(HAVE_GETADDRINFO))
Just van Rossum1040d2c2003-05-09 07:53:18 +0000147#define USE_GETADDRINFO_LOCK
148#endif
149
150#ifdef USE_GETADDRINFO_LOCK
151#define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1);
152#define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock);
153#else
154#define ACQUIRE_GETADDRINFO_LOCK
155#define RELEASE_GETADDRINFO_LOCK
156#endif
157
158#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000159# include "pythread.h"
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000160#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000161
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000162#if defined(PYCC_VACPP)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000163# include <types.h>
164# include <io.h>
165# include <sys/ioctl.h>
166# include <utils.h>
167# include <ctype.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000168#endif
169
Martin v. Löwis9e437302002-12-06 12:57:26 +0000170#if defined(__VMS)
171#if ! defined(_SOCKADDR_LEN)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000172# ifdef getaddrinfo
173# undef getaddrinfo
174# endif
175# include "TCPIP_IOCTL_ROUTINE"
176#else
177# include <ioctl.h>
178#endif
Martin v. Löwis9e437302002-12-06 12:57:26 +0000179#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000180
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000181#if defined(PYOS_OS2)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000182# define INCL_DOS
183# define INCL_DOSERRORS
184# define INCL_NOPMAPI
185# include <os2.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000186#endif
187
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000188#if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000189/* make sure that the reentrant (gethostbyaddr_r etc)
190 functions are declared correctly if compiling with
191 MIPSPro 7.x in ANSI C mode (default) */
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000192
193/* XXX Using _SGIAPI is the wrong thing,
194 but I don't know what the right thing is. */
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000195#define _SGIAPI 1
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000196
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000197#define HAVE_INET_PTON
198#include <netdb.h>
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000199#endif
200
Anthony Baxterbab23cf2003-10-04 08:00:49 +0000201/* Irix 6.5 fails to define this variable at all. This is needed
202 for both GCC and SGI's compiler. I'd say that the SGI headers
203 are just busted. */
204#if defined(__sgi) && !defined(INET_ADDRSTRLEN)
205#define INET_ADDRSTRLEN 16
206#endif
207
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000208/* Generic includes */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209#include <sys/types.h>
Guido van Rossum81194471991-07-27 21:42:02 +0000210#include <signal.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000211
Marc-André Lemburg976ade62002-02-16 18:47:07 +0000212/* Generic socket object definitions and includes */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000213#define PySocket_BUILDING_SOCKET
Marc-André Lemburgbb8b78b2002-02-16 18:44:52 +0000214#include "socketmodule.h"
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000215
216/* Addressing includes */
217
Guido van Rossum6f489d91996-06-28 20:15:15 +0000218#ifndef MS_WINDOWS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000219
220/* Non-MS WINDOWS includes */
221# include <netdb.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000222
Guido van Rossum9376b741999-09-15 22:01:40 +0000223/* Headers needed for inet_ntoa() and inet_addr() */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000224# ifdef __BEOS__
225# include <net/netdb.h>
226# elif defined(PYOS_OS2) && defined(PYCC_VACPP)
227# include <netdb.h>
Tim Peters603c6832001-11-05 02:45:59 +0000228typedef size_t socklen_t;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000229# else
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000230# include <arpa/inet.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000231# endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000232
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000233# ifndef RISCOS
234# include <fcntl.h>
235# else
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000236# include <sys/ioctl.h>
237# include <socklib.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000238# define NO_DUP
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000239int h_errno; /* not used */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000240# define INET_ADDRSTRLEN 16
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000241# endif
242
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000243#else
Guido van Rossum48a680c2001-03-02 06:34:14 +0000244
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000245/* MS_WINDOWS includes */
246# include <fcntl.h>
Guido van Rossum48a680c2001-03-02 06:34:14 +0000247
Jeremy Hylton22308652001-02-02 03:23:09 +0000248#endif
249
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000250#ifdef HAVE_STDDEF_H
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000251# include <stddef.h>
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000252#endif
253
254#ifndef offsetof
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000255# define offsetof(type, member) ((size_t)(&((type *)0)->member))
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000256#endif
257
Neal Norwitz39d22e52002-11-02 19:55:21 +0000258#ifndef O_NONBLOCK
259# define O_NONBLOCK O_NDELAY
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000260#endif
261
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000262#include "addrinfo.h"
263
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000264#ifndef HAVE_INET_PTON
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000265int inet_pton(int af, const char *src, void *dst);
Martin v. Löwisc925b1532001-07-21 09:42:15 +0000266const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000267#endif
268
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000269#ifdef __APPLE__
270/* On OS X, getaddrinfo returns no error indication of lookup
271 failure, so we must use the emulation instead of the libinfo
272 implementation. Unfortunately, performing an autoconf test
273 for this bug would require DNS access for the machine performing
274 the configuration, which is not acceptable. Therefore, we
275 determine the bug just by checking for __APPLE__. If this bug
276 gets ever fixed, perhaps checking for sys/version.h would be
277 appropriate, which is 10/0 on the system with the bug. */
Jack Jansen84262fb2002-07-02 14:40:42 +0000278#ifndef HAVE_GETNAMEINFO
279/* This bug seems to be fixed in Jaguar. Ths easiest way I could
280 Find to check for Jaguar is that it has getnameinfo(), which
281 older releases don't have */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000282#undef HAVE_GETADDRINFO
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000283#endif
Jack Jansen84262fb2002-07-02 14:40:42 +0000284#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000285
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000286/* I know this is a bad practice, but it is the easiest... */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000287#if !defined(HAVE_GETADDRINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000288/* avoid clashes with the C library definition of the symbol. */
289#define getaddrinfo fake_getaddrinfo
290#define gai_strerror fake_gai_strerror
291#define freeaddrinfo fake_freeaddrinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000292#include "getaddrinfo.c"
293#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000294#if !defined(HAVE_GETNAMEINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000295#define getnameinfo fake_getnameinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000296#include "getnameinfo.c"
297#endif
298
Guido van Rossumbcc20741998-08-04 22:53:56 +0000299#if defined(MS_WINDOWS) || defined(__BEOS__)
300/* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000301/* seem to be a few differences in the API */
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000302#define SOCKETCLOSE closesocket
Guido van Rossumbe32c891996-06-20 16:25:29 +0000303#define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000304#endif
305
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000306#ifdef MS_WIN32
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000307#define EAFNOSUPPORT WSAEAFNOSUPPORT
308#define snprintf _snprintf
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000309#endif
Fred Drakea04eaad2000-06-30 02:46:07 +0000310
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000311#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000312#define SOCKETCLOSE soclose
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000313#define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000314#endif
315
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000316#ifndef SOCKETCLOSE
317#define SOCKETCLOSE close
318#endif
319
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000320#ifdef __VMS
321/* TCP/IP Services for VMS uses a maximum send/revc buffer length of 65535 */
322#define SEGMENT_SIZE 65535
323#endif
324
Martin v. Löwise9416172003-05-03 10:12:45 +0000325/*
326 * Constants for getnameinfo()
327 */
328#if !defined(NI_MAXHOST)
329#define NI_MAXHOST 1025
330#endif
331#if !defined(NI_MAXSERV)
332#define NI_MAXSERV 32
333#endif
334
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000335/* XXX There's a problem here: *static* functions are not supposed to have
336 a Py prefix (or use CapitalizedWords). Later... */
337
Guido van Rossum30a685f1991-06-27 15:51:29 +0000338/* Global variable holding the exception type for errors detected
339 by this module (but not argument type or memory errors, etc.). */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000340static PyObject *socket_error;
341static PyObject *socket_herror;
342static PyObject *socket_gaierror;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000343static PyObject *socket_timeout;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000344
Guido van Rossum48a680c2001-03-02 06:34:14 +0000345#ifdef RISCOS
346/* Global variable which is !=0 if Python is running in a RISC OS taskwindow */
347static int taskwindow;
348#endif
349
Tim Peters643a7fc2002-02-17 04:13:21 +0000350/* A forward reference to the socket type object.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000351 The sock_type variable contains pointers to various functions,
352 some of which call new_sockobject(), which uses sock_type, so
Tim Peters643a7fc2002-02-17 04:13:21 +0000353 there has to be a circular reference. */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000354static PyTypeObject sock_type;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000355
Guido van Rossum30a685f1991-06-27 15:51:29 +0000356/* Convenience function to raise an error according to errno
357 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000358
Guido van Rossum73624e91994-10-10 17:59:00 +0000359static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000360set_error(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000361{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000362#ifdef MS_WINDOWS
Mark Hammond46a733d2000-07-24 01:45:11 +0000363 int err_no = WSAGetLastError();
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000364 static struct {
365 int no;
366 const char *msg;
367 } *msgp, msgs[] = {
368 {WSAEINTR, "Interrupted system call"},
369 {WSAEBADF, "Bad file descriptor"},
370 {WSAEACCES, "Permission denied"},
371 {WSAEFAULT, "Bad address"},
372 {WSAEINVAL, "Invalid argument"},
373 {WSAEMFILE, "Too many open files"},
374 {WSAEWOULDBLOCK,
375 "The socket operation could not complete "
376 "without blocking"},
377 {WSAEINPROGRESS, "Operation now in progress"},
378 {WSAEALREADY, "Operation already in progress"},
379 {WSAENOTSOCK, "Socket operation on non-socket"},
380 {WSAEDESTADDRREQ, "Destination address required"},
381 {WSAEMSGSIZE, "Message too long"},
382 {WSAEPROTOTYPE, "Protocol wrong type for socket"},
383 {WSAENOPROTOOPT, "Protocol not available"},
384 {WSAEPROTONOSUPPORT, "Protocol not supported"},
385 {WSAESOCKTNOSUPPORT, "Socket type not supported"},
386 {WSAEOPNOTSUPP, "Operation not supported"},
387 {WSAEPFNOSUPPORT, "Protocol family not supported"},
388 {WSAEAFNOSUPPORT, "Address family not supported"},
389 {WSAEADDRINUSE, "Address already in use"},
390 {WSAEADDRNOTAVAIL, "Can't assign requested address"},
391 {WSAENETDOWN, "Network is down"},
392 {WSAENETUNREACH, "Network is unreachable"},
393 {WSAENETRESET, "Network dropped connection on reset"},
394 {WSAECONNABORTED, "Software caused connection abort"},
395 {WSAECONNRESET, "Connection reset by peer"},
396 {WSAENOBUFS, "No buffer space available"},
397 {WSAEISCONN, "Socket is already connected"},
398 {WSAENOTCONN, "Socket is not connected"},
399 {WSAESHUTDOWN, "Can't send after socket shutdown"},
400 {WSAETOOMANYREFS, "Too many references: can't splice"},
401 {WSAETIMEDOUT, "Operation timed out"},
402 {WSAECONNREFUSED, "Connection refused"},
403 {WSAELOOP, "Too many levels of symbolic links"},
404 {WSAENAMETOOLONG, "File name too long"},
405 {WSAEHOSTDOWN, "Host is down"},
406 {WSAEHOSTUNREACH, "No route to host"},
407 {WSAENOTEMPTY, "Directory not empty"},
408 {WSAEPROCLIM, "Too many processes"},
409 {WSAEUSERS, "Too many users"},
410 {WSAEDQUOT, "Disc quota exceeded"},
411 {WSAESTALE, "Stale NFS file handle"},
412 {WSAEREMOTE, "Too many levels of remote in path"},
413 {WSASYSNOTREADY, "Network subsystem is unvailable"},
414 {WSAVERNOTSUPPORTED, "WinSock version is not supported"},
415 {WSANOTINITIALISED,
416 "Successful WSAStartup() not yet performed"},
417 {WSAEDISCON, "Graceful shutdown in progress"},
418 /* Resolver errors */
419 {WSAHOST_NOT_FOUND, "No such host is known"},
420 {WSATRY_AGAIN, "Host not found, or server failed"},
421 {WSANO_RECOVERY, "Unexpected server error encountered"},
422 {WSANO_DATA, "Valid name without requested data"},
423 {WSANO_ADDRESS, "No address, look for MX record"},
424 {0, NULL}
425 };
Mark Hammond46a733d2000-07-24 01:45:11 +0000426 if (err_no) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000427 PyObject *v;
Mark Hammond46a733d2000-07-24 01:45:11 +0000428 const char *msg = "winsock error";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000429
Mark Hammond46a733d2000-07-24 01:45:11 +0000430 for (msgp = msgs; msgp->msg; msgp++) {
431 if (err_no == msgp->no) {
432 msg = msgp->msg;
433 break;
434 }
435 }
436
437 v = Py_BuildValue("(is)", err_no, msg);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000438 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000439 PyErr_SetObject(socket_error, v);
Guido van Rossum73624e91994-10-10 17:59:00 +0000440 Py_DECREF(v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000441 }
442 return NULL;
443 }
444 else
445#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000446
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000447#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000448 if (sock_errno() != NO_ERROR) {
449 APIRET rc;
450 ULONG msglen;
451 char outbuf[100];
452 int myerrorcode = sock_errno();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000453
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000454 /* Retrieve socket-related error message from MPTN.MSG file */
455 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
456 myerrorcode - SOCBASEERR + 26,
457 "mptn.msg",
458 &msglen);
459 if (rc == NO_ERROR) {
460 PyObject *v;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000461
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000462 /* OS/2 doesn't guarantee a terminator */
463 outbuf[msglen] = '\0';
464 if (strlen(outbuf) > 0) {
465 /* If non-empty msg, trim CRLF */
466 char *lastc = &outbuf[ strlen(outbuf)-1 ];
467 while (lastc > outbuf && isspace(*lastc)) {
468 /* Trim trailing whitespace (CRLF) */
469 *lastc-- = '\0';
470 }
471 }
472 v = Py_BuildValue("(is)", myerrorcode, outbuf);
473 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000474 PyErr_SetObject(socket_error, v);
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000475 Py_DECREF(v);
476 }
477 return NULL;
478 }
479 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000480#endif
481
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000482#if defined(RISCOS)
483 if (_inet_error.errnum != NULL) {
484 PyObject *v;
485 v = Py_BuildValue("(is)", errno, _inet_err());
486 if (v != NULL) {
487 PyErr_SetObject(socket_error, v);
488 Py_DECREF(v);
489 }
490 return NULL;
491 }
492#endif
493
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000494 return PyErr_SetFromErrno(socket_error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000495}
496
Guido van Rossum30a685f1991-06-27 15:51:29 +0000497
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000498static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000499set_herror(int h_error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000500{
501 PyObject *v;
502
503#ifdef HAVE_HSTRERROR
504 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
505#else
506 v = Py_BuildValue("(is)", h_error, "host not found");
507#endif
508 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000509 PyErr_SetObject(socket_herror, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000510 Py_DECREF(v);
511 }
512
513 return NULL;
514}
515
516
517static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000518set_gaierror(int error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000519{
520 PyObject *v;
521
Martin v. Löwis272cb402002-03-01 08:31:07 +0000522#ifdef EAI_SYSTEM
523 /* EAI_SYSTEM is not available on Windows XP. */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000524 if (error == EAI_SYSTEM)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000525 return set_error();
Martin v. Löwis272cb402002-03-01 08:31:07 +0000526#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000527
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000528#ifdef HAVE_GAI_STRERROR
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000529 v = Py_BuildValue("(is)", error, gai_strerror(error));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000530#else
531 v = Py_BuildValue("(is)", error, "getaddrinfo failed");
532#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000533 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000534 PyErr_SetObject(socket_gaierror, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000535 Py_DECREF(v);
536 }
537
538 return NULL;
539}
540
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000541/* Function to perform the setting of socket blocking mode
542 internally. block = (1 | 0). */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000543static int
544internal_setblocking(PySocketSockObject *s, int block)
545{
546#ifndef RISCOS
547#ifndef MS_WINDOWS
548 int delay_flag;
549#endif
550#endif
551
552 Py_BEGIN_ALLOW_THREADS
553#ifdef __BEOS__
554 block = !block;
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000555 setsockopt(s->sock_fd, SOL_SOCKET, SO_NONBLOCK,
556 (void *)(&block), sizeof(int));
Guido van Rossum67f7a382002-06-06 21:08:16 +0000557#else
558#ifndef RISCOS
559#ifndef MS_WINDOWS
560#if defined(PYOS_OS2) && !defined(PYCC_GCC)
561 block = !block;
562 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000563#elif defined(__VMS)
564 block = !block;
565 ioctl(s->sock_fd, FIONBIO, (char *)&block);
566#else /* !PYOS_OS2 && !_VMS */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000567 delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
568 if (block)
Neal Norwitz39d22e52002-11-02 19:55:21 +0000569 delay_flag &= (~O_NONBLOCK);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000570 else
Neal Norwitz39d22e52002-11-02 19:55:21 +0000571 delay_flag |= O_NONBLOCK;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000572 fcntl(s->sock_fd, F_SETFL, delay_flag);
573#endif /* !PYOS_OS2 */
574#else /* MS_WINDOWS */
575 block = !block;
576 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
577#endif /* MS_WINDOWS */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000578#else /* RISCOS */
579 block = !block;
580 socketioctl(s->sock_fd, FIONBIO, (u_long*)&block);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000581#endif /* RISCOS */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000582#endif /* __BEOS__ */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000583 Py_END_ALLOW_THREADS
584
585 /* Since these don't return anything */
586 return 1;
587}
588
Guido van Rossum11ba0942002-06-13 15:07:44 +0000589/* Do a select() on the socket, if necessary (sock_timeout > 0).
590 The argument writing indicates the direction.
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000591 This does not raise an exception; we'll let our caller do that
592 after they've reacquired the interpreter lock.
593 Returns 1 on timeout, 0 otherwise. */
594static int
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000595internal_select(PySocketSockObject *s, int writing)
Guido van Rossum67f7a382002-06-06 21:08:16 +0000596{
597 fd_set fds;
598 struct timeval tv;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000599 int n;
Guido van Rossum11ba0942002-06-13 15:07:44 +0000600
Guido van Rossumad654902002-07-19 12:44:59 +0000601 /* Nothing to do unless we're in timeout mode (not non-blocking) */
Guido van Rossum11ba0942002-06-13 15:07:44 +0000602 if (s->sock_timeout <= 0.0)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000603 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000604
Guido van Rossumad654902002-07-19 12:44:59 +0000605 /* Guard against closed socket */
606 if (s->sock_fd < 0)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000607 return 0;
Guido van Rossumad654902002-07-19 12:44:59 +0000608
Guido van Rossum67f7a382002-06-06 21:08:16 +0000609 /* Construct the arguments to select */
610 tv.tv_sec = (int)s->sock_timeout;
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000611 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000612 FD_ZERO(&fds);
613 FD_SET(s->sock_fd, &fds);
614
615 /* See if the socket is ready */
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000616 if (writing)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000617 n = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000618 else
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000619 n = select(s->sock_fd+1, &fds, NULL, NULL, &tv);
620 if (n == 0)
621 return 1;
622 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000623}
624
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000625/* Initialize a new socket object. */
626
Tim Petersa12b4cf2002-07-18 22:38:44 +0000627static double defaulttimeout = -1.0; /* Default timeout for new sockets */
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000628
Mark Hammond62b1ab12002-07-23 06:31:15 +0000629PyMODINIT_FUNC
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000630init_sockobject(PySocketSockObject *s,
631 SOCKET_T fd, int family, int type, int proto)
632{
633#ifdef RISCOS
634 int block = 1;
635#endif
636 s->sock_fd = fd;
637 s->sock_family = family;
638 s->sock_type = type;
639 s->sock_proto = proto;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000640 s->sock_timeout = defaulttimeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000641
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000642 s->errorhandler = &set_error;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000643
644 if (defaulttimeout >= 0.0)
645 internal_setblocking(s, 0);
646
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000647#ifdef RISCOS
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000648 if (taskwindow)
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000649 socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000650#endif
651}
652
653
Guido van Rossum30a685f1991-06-27 15:51:29 +0000654/* Create a new socket object.
655 This just creates the object and initializes it.
656 If the creation fails, return NULL and set an exception (implicit
657 in NEWOBJ()). */
658
Guido van Rossum73624e91994-10-10 17:59:00 +0000659static PySocketSockObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000660new_sockobject(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000661{
Guido van Rossum73624e91994-10-10 17:59:00 +0000662 PySocketSockObject *s;
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000663 s = (PySocketSockObject *)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000664 PyType_GenericNew(&sock_type, NULL, NULL);
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000665 if (s != NULL)
666 init_sockobject(s, fd, family, type, proto);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000667 return s;
668}
669
Guido van Rossum30a685f1991-06-27 15:51:29 +0000670
Guido van Rossum48a680c2001-03-02 06:34:14 +0000671/* Lock to allow python interpreter to continue, but only allow one
Just van Rossum1040d2c2003-05-09 07:53:18 +0000672 thread to be in gethostbyname or getaddrinfo */
673#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
674PyThread_type_lock netdb_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000675#endif
676
677
Guido van Rossum30a685f1991-06-27 15:51:29 +0000678/* Convert a string specifying a host name or one of a few symbolic
679 names to a numeric IP address. This usually calls gethostbyname()
680 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000681 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000682 an error occurred; then an exception is raised. */
683
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000684static int
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000685setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000686{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000687 struct addrinfo hints, *res;
688 int error;
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000689 int d1, d2, d3, d4;
690 char ch;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000691
Guido van Rossuma376cc51996-12-05 23:43:35 +0000692 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
Guido van Rossum30a685f1991-06-27 15:51:29 +0000693 if (name[0] == '\0') {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000694 int siz;
695 memset(&hints, 0, sizeof(hints));
696 hints.ai_family = af;
697 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
698 hints.ai_flags = AI_PASSIVE;
Just van Rossum1040d2c2003-05-09 07:53:18 +0000699 Py_BEGIN_ALLOW_THREADS
700 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000701 error = getaddrinfo(NULL, "0", &hints, &res);
Just van Rossum1040d2c2003-05-09 07:53:18 +0000702 Py_END_ALLOW_THREADS
703 /* We assume that those thread-unsafe getaddrinfo() versions
704 *are* safe regarding their return value, ie. that a
705 subsequent call to getaddrinfo() does not destroy the
706 outcome of the first call. */
707 RELEASE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000708 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000709 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000710 return -1;
711 }
712 switch (res->ai_family) {
713 case AF_INET:
714 siz = 4;
715 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000716#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000717 case AF_INET6:
718 siz = 16;
719 break;
720#endif
721 default:
722 freeaddrinfo(res);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000723 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000724 "unsupported address family");
725 return -1;
726 }
727 if (res->ai_next) {
Martin v. Löwisf0b11d22001-11-07 08:31:03 +0000728 freeaddrinfo(res);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000729 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000730 "wildcard resolved to multiple address");
731 return -1;
732 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000733 if (res->ai_addrlen < addr_ret_size)
734 addr_ret_size = res->ai_addrlen;
735 memcpy(addr_ret, res->ai_addr, addr_ret_size);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000736 freeaddrinfo(res);
737 return siz;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000738 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000739 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000740 struct sockaddr_in *sin;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +0000741 if (af != AF_INET && af != AF_UNSPEC) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000742 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000743 "address family mismatched");
744 return -1;
745 }
746 sin = (struct sockaddr_in *)addr_ret;
747 memset((void *) sin, '\0', sizeof(*sin));
748 sin->sin_family = AF_INET;
749#ifdef HAVE_SOCKADDR_SA_LEN
750 sin->sin_len = sizeof(*sin);
751#endif
752 sin->sin_addr.s_addr = INADDR_BROADCAST;
753 return sizeof(sin->sin_addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000754 }
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000755 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
756 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
757 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
758 struct sockaddr_in *sin;
759 sin = (struct sockaddr_in *)addr_ret;
760 sin->sin_addr.s_addr = htonl(
761 ((long) d1 << 24) | ((long) d2 << 16) |
762 ((long) d3 << 8) | ((long) d4 << 0));
763 sin->sin_family = AF_INET;
764#ifdef HAVE_SOCKADDR_SA_LEN
765 sin->sin_len = sizeof(*sin);
766#endif
767 return 4;
768 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000769 memset(&hints, 0, sizeof(hints));
770 hints.ai_family = af;
Just van Rossum1040d2c2003-05-09 07:53:18 +0000771 Py_BEGIN_ALLOW_THREADS
772 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000773 error = getaddrinfo(name, NULL, &hints, &res);
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000774#if defined(__digital__) && defined(__unix__)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000775 if (error == EAI_NONAME && af == AF_UNSPEC) {
776 /* On Tru64 V5.1, numeric-to-addr conversion fails
777 if no address family is given. Assume IPv4 for now.*/
778 hints.ai_family = AF_INET;
779 error = getaddrinfo(name, NULL, &hints, &res);
780 }
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000781#endif
Just van Rossum1040d2c2003-05-09 07:53:18 +0000782 Py_END_ALLOW_THREADS
783 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000784 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000785 set_gaierror(error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000786 return -1;
787 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000788 if (res->ai_addrlen < addr_ret_size)
789 addr_ret_size = res->ai_addrlen;
790 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000791 freeaddrinfo(res);
792 switch (addr_ret->sa_family) {
793 case AF_INET:
794 return 4;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000795#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000796 case AF_INET6:
797 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +0000798#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000799 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000800 PyErr_SetString(socket_error, "unknown address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000801 return -1;
802 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000803}
804
Guido van Rossum30a685f1991-06-27 15:51:29 +0000805
Guido van Rossum30a685f1991-06-27 15:51:29 +0000806/* Create a string object representing an IP address.
807 This is always a string of the form 'dd.dd.dd.dd' (with variable
808 size numbers). */
809
Guido van Rossum73624e91994-10-10 17:59:00 +0000810static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000811makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000812{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000813 char buf[NI_MAXHOST];
814 int error;
815
816 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
817 NI_NUMERICHOST);
818 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000819 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000820 return NULL;
821 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000822 return PyString_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000823}
824
825
826/* Create an object representing the given socket address,
827 suitable for passing it back to bind(), connect() etc.
828 The family field of the sockaddr structure is inspected
829 to determine what kind of address it really is. */
830
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000831/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000832static PyObject *
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000833makesockaddr(int sockfd, struct sockaddr *addr, int addrlen)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000834{
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000835 if (addrlen == 0) {
836 /* No address -- may be recvfrom() from known socket */
Guido van Rossum73624e91994-10-10 17:59:00 +0000837 Py_INCREF(Py_None);
838 return Py_None;
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000839 }
840
Guido van Rossumbcc20741998-08-04 22:53:56 +0000841#ifdef __BEOS__
Guido van Rossum2c8bcb82000-04-25 21:34:53 +0000842 /* XXX: BeOS version of accept() doesn't set family correctly */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000843 addr->sa_family = AF_INET;
844#endif
845
Guido van Rossum30a685f1991-06-27 15:51:29 +0000846 switch (addr->sa_family) {
847
848 case AF_INET:
849 {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000850 struct sockaddr_in *a;
851 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
Barry Warsaw752300b1997-01-03 17:18:10 +0000852 PyObject *ret = NULL;
853 if (addrobj) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000854 a = (struct sockaddr_in *)addr;
Barry Warsaw752300b1997-01-03 17:18:10 +0000855 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
856 Py_DECREF(addrobj);
857 }
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000858 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000859 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000860
Andrew MacIntyre34d3e2d2003-01-02 12:45:34 +0000861#if defined(AF_UNIX) && !defined(PYOS_OS2)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000862 case AF_UNIX:
863 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000864 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000865 return PyString_FromString(a->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000866 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000867#endif /* AF_UNIX */
868
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000869#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000870 case AF_INET6:
871 {
872 struct sockaddr_in6 *a;
873 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
874 PyObject *ret = NULL;
875 if (addrobj) {
876 a = (struct sockaddr_in6 *)addr;
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000877 ret = Py_BuildValue("Oiii",
878 addrobj,
879 ntohs(a->sin6_port),
880 a->sin6_flowinfo,
881 a->sin6_scope_id);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000882 Py_DECREF(addrobj);
883 }
884 return ret;
885 }
Jeremy Hylton22308652001-02-02 03:23:09 +0000886#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +0000887
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +0000888#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +0000889 case AF_PACKET:
890 {
891 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
892 char *ifname = "";
893 struct ifreq ifr;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000894 /* need to look up interface name give index */
895 if (a->sll_ifindex) {
Jeremy Hylton22308652001-02-02 03:23:09 +0000896 ifr.ifr_ifindex = a->sll_ifindex;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000897 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
Jeremy Hylton22308652001-02-02 03:23:09 +0000898 ifname = ifr.ifr_name;
Jeremy Hylton22308652001-02-02 03:23:09 +0000899 }
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000900 return Py_BuildValue("shbhs#",
901 ifname,
902 ntohs(a->sll_protocol),
903 a->sll_pkttype,
904 a->sll_hatype,
905 a->sll_addr,
906 a->sll_halen);
Jeremy Hylton22308652001-02-02 03:23:09 +0000907 }
908#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000909
Guido van Rossum30a685f1991-06-27 15:51:29 +0000910 /* More cases here... */
911
912 default:
Guido van Rossumaa948df1997-05-07 17:41:48 +0000913 /* If we don't know the address family, don't raise an
914 exception -- return it as a tuple. */
915 return Py_BuildValue("is#",
916 addr->sa_family,
917 addr->sa_data,
918 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000919
Guido van Rossum30a685f1991-06-27 15:51:29 +0000920 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000921}
922
Guido van Rossum30a685f1991-06-27 15:51:29 +0000923
924/* Parse a socket address argument according to the socket object's
925 address family. Return 1 if the address was in the proper format,
926 0 of not. The address is returned through addr_ret, its length
927 through len_ret. */
928
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000929static int
Guido van Rossum48a680c2001-03-02 06:34:14 +0000930getsockaddrarg(PySocketSockObject *s, PyObject *args,
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000931 struct sockaddr **addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000932{
Guido van Rossum30a685f1991-06-27 15:51:29 +0000933 switch (s->sock_family) {
934
Andrew MacIntyre34d3e2d2003-01-02 12:45:34 +0000935#if defined(AF_UNIX) && !defined(PYOS_OS2)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000936 case AF_UNIX:
937 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000938 struct sockaddr_un* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000939 char *path;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000940 int len;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000941 addr = (struct sockaddr_un*)&(s->sock_addr).un;
Guido van Rossum7e488981998-10-08 02:25:24 +0000942 if (!PyArg_Parse(args, "t#", &path, &len))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000943 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000944 if (len > sizeof addr->sun_path) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000945 PyErr_SetString(socket_error,
Barry Warsaw752300b1997-01-03 17:18:10 +0000946 "AF_UNIX path too long");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000947 return 0;
948 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000949 addr->sun_family = s->sock_family;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000950 memcpy(addr->sun_path, path, len);
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000951 addr->sun_path[len] = 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000952 *addr_ret = (struct sockaddr *) addr;
Guido van Rossum65af28a1996-06-11 18:36:33 +0000953 *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000954 return 1;
955 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000956#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000957
Guido van Rossum30a685f1991-06-27 15:51:29 +0000958 case AF_INET:
959 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000960 struct sockaddr_in* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000961 char *host;
Martin v. Löwis5db099a2003-08-07 11:55:15 +0000962 int port, result;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000963 addr=(struct sockaddr_in*)&(s->sock_addr).in;
Guido van Rossume4dad902000-12-01 13:13:11 +0000964 if (!PyTuple_Check(args)) {
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000965 PyErr_Format(
966 PyExc_TypeError,
967 "getsockaddrarg: "
968 "AF_INET address must be tuple, not %.500s",
969 args->ob_type->tp_name);
Guido van Rossume4dad902000-12-01 13:13:11 +0000970 return 0;
971 }
Martin v. Löwis2548c732003-04-18 10:39:54 +0000972 if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
973 "idna", &host, &port))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000974 return 0;
Martin v. Löwis5db099a2003-08-07 11:55:15 +0000975 result = setipaddr(host, (struct sockaddr *)addr,
976 sizeof(*addr), AF_INET);
977 PyMem_Free(host);
978 if (result < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000979 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000980 addr->sin_family = AF_INET;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000981 addr->sin_port = htons((short)port);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000982 *addr_ret = (struct sockaddr *) addr;
983 *len_ret = sizeof *addr;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000984 return 1;
985 }
986
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000987#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000988 case AF_INET6:
989 {
990 struct sockaddr_in6* addr;
991 char *host;
Martin v. Löwis5db099a2003-08-07 11:55:15 +0000992 int port, flowinfo, scope_id, result;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000993 addr = (struct sockaddr_in6*)&(s->sock_addr).in6;
994 flowinfo = scope_id = 0;
Martin v. Löwis2548c732003-04-18 10:39:54 +0000995 if (!PyArg_ParseTuple(args, "eti|ii",
996 "idna", &host, &port, &flowinfo,
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000997 &scope_id)) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000998 return 0;
999 }
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001000 result = setipaddr(host, (struct sockaddr *)addr,
1001 sizeof(*addr), AF_INET6);
1002 PyMem_Free(host);
1003 if (result < 0)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001004 return 0;
1005 addr->sin6_family = s->sock_family;
1006 addr->sin6_port = htons((short)port);
1007 addr->sin6_flowinfo = flowinfo;
1008 addr->sin6_scope_id = scope_id;
1009 *addr_ret = (struct sockaddr *) addr;
1010 *len_ret = sizeof *addr;
1011 return 1;
1012 }
1013#endif
1014
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001015#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001016 case AF_PACKET:
1017 {
1018 struct sockaddr_ll* addr;
1019 struct ifreq ifr;
1020 char *interfaceName;
1021 int protoNumber;
1022 int hatype = 0;
1023 int pkttype = 0;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001024 char *haddr;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001025
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001026 if (!PyArg_ParseTuple(args, "si|iis", &interfaceName,
1027 &protoNumber, &pkttype, &hatype, &haddr))
Jeremy Hylton22308652001-02-02 03:23:09 +00001028 return 0;
1029 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
1030 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001031 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001032 s->errorhandler();
Jeremy Hylton22308652001-02-02 03:23:09 +00001033 return 0;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001034 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001035 addr = &(s->sock_addr.ll);
1036 addr->sll_family = AF_PACKET;
1037 addr->sll_protocol = htons((short)protoNumber);
1038 addr->sll_ifindex = ifr.ifr_ifindex;
1039 addr->sll_pkttype = pkttype;
1040 addr->sll_hatype = hatype;
1041 *addr_ret = (struct sockaddr *) addr;
1042 *len_ret = sizeof *addr;
1043 return 1;
1044 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001045#endif
1046
Guido van Rossum30a685f1991-06-27 15:51:29 +00001047 /* More cases here... */
1048
1049 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001050 PyErr_SetString(socket_error, "getsockaddrarg: bad family");
Guido van Rossum30a685f1991-06-27 15:51:29 +00001051 return 0;
1052
1053 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001054}
1055
Guido van Rossum30a685f1991-06-27 15:51:29 +00001056
Guido van Rossum48a680c2001-03-02 06:34:14 +00001057/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +00001058 Return 1 if the family is known, 0 otherwise. The length is returned
1059 through len_ret. */
1060
1061static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +00001062getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001063{
1064 switch (s->sock_family) {
1065
Andrew MacIntyre34d3e2d2003-01-02 12:45:34 +00001066#if defined(AF_UNIX) && !defined(PYOS_OS2)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001067 case AF_UNIX:
1068 {
1069 *len_ret = sizeof (struct sockaddr_un);
1070 return 1;
1071 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001072#endif /* AF_UNIX */
Guido van Rossum710e1df1992-06-12 10:39:36 +00001073
1074 case AF_INET:
1075 {
1076 *len_ret = sizeof (struct sockaddr_in);
1077 return 1;
1078 }
1079
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001080#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001081 case AF_INET6:
1082 {
1083 *len_ret = sizeof (struct sockaddr_in6);
1084 return 1;
1085 }
1086#endif
1087
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001088#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001089 case AF_PACKET:
1090 {
1091 *len_ret = sizeof (struct sockaddr_ll);
1092 return 1;
1093 }
1094#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001095
Guido van Rossum710e1df1992-06-12 10:39:36 +00001096 /* More cases here... */
1097
1098 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001099 PyErr_SetString(socket_error, "getsockaddrlen: bad family");
Guido van Rossum710e1df1992-06-12 10:39:36 +00001100 return 0;
1101
1102 }
1103}
1104
1105
Guido van Rossum30a685f1991-06-27 15:51:29 +00001106/* s.accept() method */
1107
Guido van Rossum73624e91994-10-10 17:59:00 +00001108static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001109sock_accept(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001110{
1111 char addrbuf[256];
Fred Drakea04eaad2000-06-30 02:46:07 +00001112 SOCKET_T newfd;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001113 socklen_t addrlen;
Barry Warsaw752300b1997-01-03 17:18:10 +00001114 PyObject *sock = NULL;
1115 PyObject *addr = NULL;
1116 PyObject *res = NULL;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001117 int timeout;
Barry Warsaw752300b1997-01-03 17:18:10 +00001118
Guido van Rossum710e1df1992-06-12 10:39:36 +00001119 if (!getsockaddrlen(s, &addrlen))
1120 return NULL;
Fred Drakefd168342001-05-09 19:11:33 +00001121 memset(addrbuf, 0, addrlen);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001122
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001123#ifdef MS_WINDOWS
1124 newfd = INVALID_SOCKET;
1125#else
1126 newfd = -1;
1127#endif
1128
Guido van Rossum73624e91994-10-10 17:59:00 +00001129 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001130 timeout = internal_select(s, 0);
1131 if (!timeout)
1132 newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf,
1133 &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001134 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001135
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001136 if (timeout) {
1137 PyErr_SetString(socket_timeout, "timed out");
1138 return NULL;
1139 }
1140
Fred Drakea04eaad2000-06-30 02:46:07 +00001141#ifdef MS_WINDOWS
1142 if (newfd == INVALID_SOCKET)
1143#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001144 if (newfd < 0)
Fred Drakea04eaad2000-06-30 02:46:07 +00001145#endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001146 return s->errorhandler();
Barry Warsaw752300b1997-01-03 17:18:10 +00001147
Guido van Rossum30a685f1991-06-27 15:51:29 +00001148 /* Create the new object with unspecified family,
1149 to avoid calls to bind() etc. on it. */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001150 sock = (PyObject *) new_sockobject(newfd,
1151 s->sock_family,
1152 s->sock_type,
1153 s->sock_proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001154
Barry Warsaw752300b1997-01-03 17:18:10 +00001155 if (sock == NULL) {
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001156 SOCKETCLOSE(newfd);
Barry Warsaw752300b1997-01-03 17:18:10 +00001157 goto finally;
1158 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001159 addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001160 addrlen);
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001161 if (addr == NULL)
Barry Warsaw752300b1997-01-03 17:18:10 +00001162 goto finally;
1163
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001164 res = PyTuple_Pack(2, sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00001165
Guido van Rossum67f7a382002-06-06 21:08:16 +00001166finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001167 Py_XDECREF(sock);
1168 Py_XDECREF(addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001169 return res;
1170}
1171
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001172PyDoc_STRVAR(accept_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001173"accept() -> (socket object, address info)\n\
1174\n\
1175Wait for an incoming connection. Return a new socket representing the\n\
1176connection, and the address of the client. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001177info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001178
Guido van Rossum11ba0942002-06-13 15:07:44 +00001179/* s.setblocking(flag) method. Argument:
1180 False -- non-blocking mode; same as settimeout(0)
1181 True -- blocking mode; same as settimeout(None)
1182*/
Guido van Rossume4485b01994-09-07 14:32:49 +00001183
Guido van Rossum73624e91994-10-10 17:59:00 +00001184static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001185sock_setblocking(PySocketSockObject *s, PyObject *arg)
Guido van Rossume4485b01994-09-07 14:32:49 +00001186{
1187 int block;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001188
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001189 block = PyInt_AsLong(arg);
1190 if (block == -1 && PyErr_Occurred())
Guido van Rossume4485b01994-09-07 14:32:49 +00001191 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001192
Guido van Rossum11ba0942002-06-13 15:07:44 +00001193 s->sock_timeout = block ? -1.0 : 0.0;
Guido van Rossume8008f02002-06-07 03:36:20 +00001194 internal_setblocking(s, block);
Guido van Rossume4485b01994-09-07 14:32:49 +00001195
Guido van Rossum73624e91994-10-10 17:59:00 +00001196 Py_INCREF(Py_None);
1197 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +00001198}
Guido van Rossume4485b01994-09-07 14:32:49 +00001199
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001200PyDoc_STRVAR(setblocking_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001201"setblocking(flag)\n\
1202\n\
1203Set the socket to blocking (flag is true) or non-blocking (false).\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001204setblocking(True) is equivalent to settimeout(None);\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001205setblocking(False) is equivalent to settimeout(0.0).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001206
Guido van Rossum11ba0942002-06-13 15:07:44 +00001207/* s.settimeout(timeout) method. Argument:
1208 None -- no timeout, blocking mode; same as setblocking(True)
1209 0.0 -- non-blocking mode; same as setblocking(False)
1210 > 0 -- timeout mode; operations time out after timeout seconds
1211 < 0 -- illegal; raises an exception
1212*/
Guido van Rossum67f7a382002-06-06 21:08:16 +00001213static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001214sock_settimeout(PySocketSockObject *s, PyObject *arg)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001215{
Guido van Rossum11ba0942002-06-13 15:07:44 +00001216 double timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001217
1218 if (arg == Py_None)
Guido van Rossum11ba0942002-06-13 15:07:44 +00001219 timeout = -1.0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001220 else {
Guido van Rossum11ba0942002-06-13 15:07:44 +00001221 timeout = PyFloat_AsDouble(arg);
1222 if (timeout < 0.0) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00001223 if (!PyErr_Occurred())
1224 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00001225 "Timeout value out of range");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001226 return NULL;
1227 }
1228 }
1229
Guido van Rossum11ba0942002-06-13 15:07:44 +00001230 s->sock_timeout = timeout;
1231 internal_setblocking(s, timeout < 0.0);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001232
1233 Py_INCREF(Py_None);
1234 return Py_None;
1235}
1236
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001237PyDoc_STRVAR(settimeout_doc,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001238"settimeout(timeout)\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001239\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001240Set a timeout on socket operations. 'timeout' can be a float,\n\
1241giving in seconds, or None. Setting a timeout of None disables\n\
1242the timeout feature and is equivalent to setblocking(1).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001243Setting a timeout of zero is the same as setblocking(0).");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001244
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001245/* s.gettimeout() method.
1246 Returns the timeout associated with a socket. */
Guido van Rossum67f7a382002-06-06 21:08:16 +00001247static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001248sock_gettimeout(PySocketSockObject *s)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001249{
1250 if (s->sock_timeout < 0.0) {
1251 Py_INCREF(Py_None);
1252 return Py_None;
1253 }
1254 else
1255 return PyFloat_FromDouble(s->sock_timeout);
1256}
1257
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001258PyDoc_STRVAR(gettimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001259"gettimeout() -> timeout\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001260\n\
1261Returns the timeout in floating seconds associated with socket \n\
1262operations. A timeout of None indicates that timeouts on socket \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001263operations are disabled.");
Guido van Rossume4485b01994-09-07 14:32:49 +00001264
Guido van Rossum48a680c2001-03-02 06:34:14 +00001265#ifdef RISCOS
1266/* s.sleeptaskw(1 | 0) method */
1267
1268static PyObject *
Martin v. Löwisa94568a2003-05-10 07:36:56 +00001269sock_sleeptaskw(PySocketSockObject *s,PyObject *arg)
Guido van Rossum48a680c2001-03-02 06:34:14 +00001270{
Guido van Rossum67f7a382002-06-06 21:08:16 +00001271 int block;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00001272 block = PyInt_AsLong(arg);
1273 if (block == -1 && PyErr_Occurred())
Guido van Rossum67f7a382002-06-06 21:08:16 +00001274 return NULL;
1275 Py_BEGIN_ALLOW_THREADS
1276 socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
1277 Py_END_ALLOW_THREADS
Guido van Rossum48a680c2001-03-02 06:34:14 +00001278
Guido van Rossum67f7a382002-06-06 21:08:16 +00001279 Py_INCREF(Py_None);
1280 return Py_None;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001281}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001282PyDoc_STRVAR(sleeptaskw_doc,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001283"sleeptaskw(flag)\n\
1284\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001285Allow sleeps in taskwindows.");
Guido van Rossum48a680c2001-03-02 06:34:14 +00001286#endif
1287
1288
Guido van Rossumaee08791992-09-08 09:05:33 +00001289/* s.setsockopt() method.
1290 With an integer third argument, sets an integer option.
1291 With a string third argument, sets an option from a buffer;
1292 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001293
Guido van Rossum73624e91994-10-10 17:59:00 +00001294static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001295sock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001296{
1297 int level;
1298 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001299 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +00001300 char *buf;
1301 int buflen;
1302 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001303
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001304 if (PyArg_ParseTuple(args, "iii:setsockopt",
1305 &level, &optname, &flag)) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001306 buf = (char *) &flag;
1307 buflen = sizeof flag;
1308 }
1309 else {
Guido van Rossum73624e91994-10-10 17:59:00 +00001310 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001311 if (!PyArg_ParseTuple(args, "iis#:setsockopt",
1312 &level, &optname, &buf, &buflen))
Guido van Rossumaee08791992-09-08 09:05:33 +00001313 return NULL;
1314 }
Thomas Wouters334fb892000-07-25 12:56:38 +00001315 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001316 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001317 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001318 Py_INCREF(Py_None);
1319 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001320}
1321
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001322PyDoc_STRVAR(setsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001323"setsockopt(level, option, value)\n\
1324\n\
1325Set a socket option. See the Unix manual for level and option.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001326The value argument can either be an integer or a string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001327
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001328
Guido van Rossumaee08791992-09-08 09:05:33 +00001329/* s.getsockopt() method.
1330 With two arguments, retrieves an integer option.
1331 With a third integer argument, retrieves a string buffer of that size;
1332 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001333
Guido van Rossum73624e91994-10-10 17:59:00 +00001334static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001335sock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001336{
1337 int level;
1338 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001339 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +00001340 PyObject *buf;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001341 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001342
Guido van Rossumbcc20741998-08-04 22:53:56 +00001343#ifdef __BEOS__
Fred Drake564a6cc2001-05-11 20:12:26 +00001344 /* We have incomplete socket support. */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001345 PyErr_SetString(socket_error, "getsockopt not supported");
Guido van Rossumbcc20741998-08-04 22:53:56 +00001346 return NULL;
1347#else
1348
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001349 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
1350 &level, &optname, &buflen))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001351 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001352
Guido van Rossumbe32c891996-06-20 16:25:29 +00001353 if (buflen == 0) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001354 int flag = 0;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001355 socklen_t flagsize = sizeof flag;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001356 res = getsockopt(s->sock_fd, level, optname,
Thomas Wouters334fb892000-07-25 12:56:38 +00001357 (void *)&flag, &flagsize);
Guido van Rossumaee08791992-09-08 09:05:33 +00001358 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001359 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001360 return PyInt_FromLong(flag);
Guido van Rossumaee08791992-09-08 09:05:33 +00001361 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001362#ifdef __VMS
1363 if (buflen > 1024) {
1364#else
Guido van Rossumaee08791992-09-08 09:05:33 +00001365 if (buflen <= 0 || buflen > 1024) {
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001366#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001367 PyErr_SetString(socket_error,
Barry Warsaw752300b1997-01-03 17:18:10 +00001368 "getsockopt buflen out of range");
Guido van Rossumaee08791992-09-08 09:05:33 +00001369 return NULL;
1370 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001371 buf = PyString_FromStringAndSize((char *)NULL, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001372 if (buf == NULL)
1373 return NULL;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001374 res = getsockopt(s->sock_fd, level, optname,
Fred Drake564a6cc2001-05-11 20:12:26 +00001375 (void *)PyString_AS_STRING(buf), &buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001376 if (res < 0) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001377 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001378 return s->errorhandler();
Guido van Rossumaee08791992-09-08 09:05:33 +00001379 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001380 _PyString_Resize(&buf, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001381 return buf;
Guido van Rossumbcc20741998-08-04 22:53:56 +00001382#endif /* __BEOS__ */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001383}
1384
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001385PyDoc_STRVAR(getsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001386"getsockopt(level, option[, buffersize]) -> value\n\
1387\n\
1388Get a socket option. See the Unix manual for level and option.\n\
1389If a nonzero buffersize argument is given, the return value is a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001390string of that length; otherwise it is an integer.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001391
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001392
Fred Drake728819a2000-07-01 03:40:12 +00001393/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001394
Guido van Rossum73624e91994-10-10 17:59:00 +00001395static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001396sock_bind(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001397{
1398 struct sockaddr *addr;
1399 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001400 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001401
Fred Drake728819a2000-07-01 03:40:12 +00001402 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001403 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001404 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001405 res = bind(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001406 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001407 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001408 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001409 Py_INCREF(Py_None);
1410 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001411}
1412
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001413PyDoc_STRVAR(bind_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001414"bind(address)\n\
1415\n\
1416Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00001417pair (host, port); the host must refer to the local host. For raw packet\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001418sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001419
Guido van Rossum30a685f1991-06-27 15:51:29 +00001420
1421/* s.close() method.
1422 Set the file descriptor to -1 so operations tried subsequently
1423 will surely fail. */
1424
Guido van Rossum73624e91994-10-10 17:59:00 +00001425static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001426sock_close(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001427{
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001428 SOCKET_T fd;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001429
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001430 if ((fd = s->sock_fd) != -1) {
1431 s->sock_fd = -1;
Guido van Rossum08481461996-10-12 14:07:22 +00001432 Py_BEGIN_ALLOW_THREADS
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001433 (void) SOCKETCLOSE(fd);
Guido van Rossum08481461996-10-12 14:07:22 +00001434 Py_END_ALLOW_THREADS
1435 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001436 Py_INCREF(Py_None);
1437 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001438}
1439
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001440PyDoc_STRVAR(close_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001441"close()\n\
1442\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001443Close the socket. It cannot be used after this call.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001444
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001445static int
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001446internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
1447 int *timeoutp)
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001448{
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001449 int res, timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001450
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001451 timeout = 0;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001452 res = connect(s->sock_fd, addr, addrlen);
1453
1454#ifdef MS_WINDOWS
1455
1456 if (s->sock_timeout > 0.0) {
1457 if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK) {
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001458 /* This is a mess. Best solution: trust select */
1459 fd_set fds;
1460 struct timeval tv;
1461 tv.tv_sec = (int)s->sock_timeout;
1462 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
1463 FD_ZERO(&fds);
1464 FD_SET(s->sock_fd, &fds);
1465 res = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001466 if (res == 0) {
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001467 res = WSAEWOULDBLOCK;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001468 timeout = 1;
1469 } else if (res > 0)
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001470 res = 0;
1471 /* else if (res < 0) an error occurred */
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001472 }
1473 }
1474
1475 if (res < 0)
1476 res = WSAGetLastError();
1477
1478#else
1479
1480 if (s->sock_timeout > 0.0) {
1481 if (res < 0 && errno == EINPROGRESS) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001482 timeout = internal_select(s, 1);
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001483 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum12e3c712002-08-08 20:39:30 +00001484 if (res < 0 && errno == EISCONN)
1485 res = 0;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001486 }
1487 }
1488
1489 if (res < 0)
1490 res = errno;
1491
1492#endif
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001493 *timeoutp = timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001494
1495 return res;
1496}
Guido van Rossum30a685f1991-06-27 15:51:29 +00001497
Fred Drake728819a2000-07-01 03:40:12 +00001498/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001499
Guido van Rossum73624e91994-10-10 17:59:00 +00001500static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001501sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001502{
1503 struct sockaddr *addr;
1504 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001505 int res;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001506 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001507
Fred Drake728819a2000-07-01 03:40:12 +00001508 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001509 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001510
Guido van Rossum73624e91994-10-10 17:59:00 +00001511 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001512 res = internal_connect(s, addr, addrlen, &timeout);
Guido van Rossum11ba0942002-06-13 15:07:44 +00001513 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001514
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001515 if (timeout) {
1516 PyErr_SetString(socket_timeout, "timed out");
1517 return NULL;
1518 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001519 if (res != 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001520 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001521 Py_INCREF(Py_None);
1522 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001523}
1524
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001525PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001526"connect(address)\n\
1527\n\
1528Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001529is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001530
Guido van Rossum30a685f1991-06-27 15:51:29 +00001531
Fred Drake728819a2000-07-01 03:40:12 +00001532/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001533
1534static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001535sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001536{
1537 struct sockaddr *addr;
1538 int addrlen;
1539 int res;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001540 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001541
Fred Drake728819a2000-07-01 03:40:12 +00001542 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001543 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001544
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001545 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001546 res = internal_connect(s, addr, addrlen, &timeout);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001547 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001548
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001549 return PyInt_FromLong((long) res);
1550}
1551
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001552PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001553"connect_ex(address) -> errno\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001554\n\
1555This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001556instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001557
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001558
Guido van Rossumed233a51992-06-23 09:07:03 +00001559/* s.fileno() method */
1560
Guido van Rossum73624e91994-10-10 17:59:00 +00001561static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001562sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00001563{
Fred Drakea04eaad2000-06-30 02:46:07 +00001564#if SIZEOF_SOCKET_T <= SIZEOF_LONG
Guido van Rossum73624e91994-10-10 17:59:00 +00001565 return PyInt_FromLong((long) s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +00001566#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001567 return PyLong_FromLongLong((PY_LONG_LONG)s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +00001568#endif
Guido van Rossumed233a51992-06-23 09:07:03 +00001569}
1570
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001571PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001572"fileno() -> integer\n\
1573\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001574Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001575
Guido van Rossumed233a51992-06-23 09:07:03 +00001576
Guido van Rossumbe32c891996-06-20 16:25:29 +00001577#ifndef NO_DUP
1578/* s.dup() method */
1579
1580static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001581sock_dup(PySocketSockObject *s)
Guido van Rossumbe32c891996-06-20 16:25:29 +00001582{
Fred Drakea04eaad2000-06-30 02:46:07 +00001583 SOCKET_T newfd;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001584 PyObject *sock;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001585
Guido van Rossumbe32c891996-06-20 16:25:29 +00001586 newfd = dup(s->sock_fd);
1587 if (newfd < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001588 return s->errorhandler();
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001589 sock = (PyObject *) new_sockobject(newfd,
1590 s->sock_family,
1591 s->sock_type,
1592 s->sock_proto);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001593 if (sock == NULL)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001594 SOCKETCLOSE(newfd);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001595 return sock;
1596}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001597
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001598PyDoc_STRVAR(dup_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001599"dup() -> socket object\n\
1600\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001601Return a new socket object connected to the same system resource.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001602
Guido van Rossumbe32c891996-06-20 16:25:29 +00001603#endif
1604
1605
Guido van Rossumc89705d1992-11-26 08:54:07 +00001606/* s.getsockname() method */
1607
Guido van Rossum73624e91994-10-10 17:59:00 +00001608static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001609sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001610{
1611 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001612 int res;
1613 socklen_t addrlen;
1614
Guido van Rossumc89705d1992-11-26 08:54:07 +00001615 if (!getsockaddrlen(s, &addrlen))
1616 return NULL;
Guido van Rossumca42b161996-01-12 01:36:05 +00001617 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001618 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001619 res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001620 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001621 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001622 return s->errorhandler();
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001623 return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001624}
1625
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001626PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001627"getsockname() -> address info\n\
1628\n\
1629Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001630info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001631
Guido van Rossumc89705d1992-11-26 08:54:07 +00001632
Guido van Rossumb6775db1994-08-01 11:34:53 +00001633#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001634/* s.getpeername() method */
1635
Guido van Rossum73624e91994-10-10 17:59:00 +00001636static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001637sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001638{
1639 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001640 int res;
1641 socklen_t addrlen;
1642
Guido van Rossumc89705d1992-11-26 08:54:07 +00001643 if (!getsockaddrlen(s, &addrlen))
1644 return NULL;
Fred Drakefd168342001-05-09 19:11:33 +00001645 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001646 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001647 res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001648 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001649 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001650 return s->errorhandler();
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001651 return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001652}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001653
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001654PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001655"getpeername() -> address info\n\
1656\n\
1657Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001658info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001659
Guido van Rossumb6775db1994-08-01 11:34:53 +00001660#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001661
1662
Guido van Rossum30a685f1991-06-27 15:51:29 +00001663/* s.listen(n) method */
1664
Guido van Rossum73624e91994-10-10 17:59:00 +00001665static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001666sock_listen(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001667{
1668 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001669 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001670
1671 backlog = PyInt_AsLong(arg);
1672 if (backlog == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001673 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001674 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +00001675 if (backlog < 1)
1676 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001677 res = listen(s->sock_fd, backlog);
Guido van Rossum73624e91994-10-10 17:59:00 +00001678 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001679 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001680 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001681 Py_INCREF(Py_None);
1682 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001683}
1684
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001685PyDoc_STRVAR(listen_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001686"listen(backlog)\n\
1687\n\
1688Enable a server to accept connections. The backlog argument must be at\n\
1689least 1; it specifies the number of unaccepted connection that the system\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001690will allow before refusing new connections.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001691
1692
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001693#ifndef NO_DUP
Guido van Rossum30a685f1991-06-27 15:51:29 +00001694/* s.makefile(mode) method.
1695 Create a new open file object referring to a dupped version of
1696 the socket's file descriptor. (The dup() call is necessary so
1697 that the open file and socket objects may be closed independent
1698 of each other.)
1699 The mode argument specifies 'r' or 'w' passed to fdopen(). */
1700
Guido van Rossum73624e91994-10-10 17:59:00 +00001701static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001702sock_makefile(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001703{
Tim Petersdbd9ba62000-07-09 03:09:57 +00001704 extern int fclose(FILE *);
Guido van Rossum6b144911995-03-14 15:05:13 +00001705 char *mode = "r";
1706 int bufsize = -1;
Fred Drakea04eaad2000-06-30 02:46:07 +00001707#ifdef MS_WIN32
Tim Peters79248aa2001-08-29 21:37:10 +00001708 Py_intptr_t fd;
Fred Drakea04eaad2000-06-30 02:46:07 +00001709#else
Guido van Rossum30a685f1991-06-27 15:51:29 +00001710 int fd;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001711#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001712 FILE *fp;
Guido van Rossum6b144911995-03-14 15:05:13 +00001713 PyObject *f;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001714#ifdef __VMS
1715 char *mode_r = "r";
1716 char *mode_w = "w";
1717#endif
Guido van Rossum6b144911995-03-14 15:05:13 +00001718
Guido van Rossum43713e52000-02-29 13:59:29 +00001719 if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001720 return NULL;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001721#ifdef __VMS
1722 if (strcmp(mode,"rb") == 0) {
1723 mode = mode_r;
1724 }
1725 else {
1726 if (strcmp(mode,"wb") == 0) {
1727 mode = mode_w;
1728 }
1729 }
1730#endif
Guido van Rossum8d665e61996-06-26 18:22:49 +00001731#ifdef MS_WIN32
Guido van Rossum82a5c661998-07-07 20:45:43 +00001732 if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) ||
1733 ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL))
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001734#else
Guido van Rossum82a5c661998-07-07 20:45:43 +00001735 if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL)
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001736#endif
Guido van Rossum82a5c661998-07-07 20:45:43 +00001737 {
Guido van Rossum6b144911995-03-14 15:05:13 +00001738 if (fd >= 0)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001739 SOCKETCLOSE(fd);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001740 return s->errorhandler();
Guido van Rossum6b144911995-03-14 15:05:13 +00001741 }
1742 f = PyFile_FromFile(fp, "<socket>", mode, fclose);
1743 if (f != NULL)
1744 PyFile_SetBufSize(f, bufsize);
1745 return f;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001746}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001747
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001748PyDoc_STRVAR(makefile_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001749"makefile([mode[, buffersize]]) -> file object\n\
1750\n\
1751Return a regular file object corresponding to the socket.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001752The mode and buffersize arguments are as for the built-in open() function.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001753
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001754#endif /* NO_DUP */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001755
Guido van Rossum48a680c2001-03-02 06:34:14 +00001756
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001757/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001758
Guido van Rossum73624e91994-10-10 17:59:00 +00001759static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001760sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001761{
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001762 int len, n = 0, flags = 0, timeout;
Guido van Rossum73624e91994-10-10 17:59:00 +00001763 PyObject *buf;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001764#ifdef __VMS
1765 int read_length;
1766 char *read_buf;
1767#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00001768
Guido van Rossum43713e52000-02-29 13:59:29 +00001769 if (!PyArg_ParseTuple(args, "i|i:recv", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001770 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001771
1772 if (len < 0) {
Martin v. Löwisfba64e12001-11-19 10:41:26 +00001773 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00001774 "negative buffersize in recv");
Martin v. Löwisfba64e12001-11-19 10:41:26 +00001775 return NULL;
1776 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00001777
Guido van Rossum73624e91994-10-10 17:59:00 +00001778 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001779 if (buf == NULL)
1780 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001781
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001782#ifndef __VMS
Guido van Rossum73624e91994-10-10 17:59:00 +00001783 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001784 timeout = internal_select(s, 0);
1785 if (!timeout)
1786 n = recv(s->sock_fd, PyString_AS_STRING(buf), len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001787 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001788
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001789 if (timeout) {
1790 Py_DECREF(buf);
1791 PyErr_SetString(socket_timeout, "timed out");
1792 return NULL;
1793 }
Guido van Rossum7c53b771995-09-13 18:39:47 +00001794 if (n < 0) {
1795 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001796 return s->errorhandler();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001797 }
Tim Peters5de98422002-04-27 18:44:32 +00001798 if (n != len)
1799 _PyString_Resize(&buf, n);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001800#else
1801 read_buf = PyString_AsString(buf);
1802 read_length = len;
1803 while (read_length != 0) {
1804 unsigned int segment;
1805
1806 segment = read_length /SEGMENT_SIZE;
1807 if (segment != 0) {
1808 segment = SEGMENT_SIZE;
1809 }
1810 else {
1811 segment = read_length;
1812 }
1813
1814 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001815 timeout = internal_select(s, 0);
1816 if (!timeout)
1817 n = recv(s->sock_fd, read_buf, segment, flags);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001818 Py_END_ALLOW_THREADS
1819
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001820 if (timeout) {
1821 Py_DECREF(buf);
1822 PyErr_SetString(socket_timeout, "timed out");
1823 return NULL;
1824 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001825 if (n < 0) {
1826 Py_DECREF(buf);
1827 return s->errorhandler();
1828 }
1829 if (n != read_length) {
1830 read_buf += n;
1831 break;
1832 }
1833
1834 read_length -= segment;
1835 read_buf += segment;
1836 }
1837 if (_PyString_Resize(&buf, (read_buf - PyString_AsString(buf))) < 0)
1838 {
1839 return NULL;
1840 }
1841#endif /* !__VMS */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001842 return buf;
1843}
1844
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001845PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001846"recv(buffersize[, flags]) -> data\n\
1847\n\
1848Receive up to buffersize bytes from the socket. For the optional flags\n\
1849argument, see the Unix manual. When no data is available, block until\n\
1850at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001851the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001852
Guido van Rossum30a685f1991-06-27 15:51:29 +00001853
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001854/* s.recvfrom(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001855
Guido van Rossum73624e91994-10-10 17:59:00 +00001856static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001857sock_recvfrom(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001858{
1859 char addrbuf[256];
Barry Warsaw752300b1997-01-03 17:18:10 +00001860 PyObject *buf = NULL;
1861 PyObject *addr = NULL;
1862 PyObject *ret = NULL;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001863 int len, n = 0, flags = 0, timeout;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001864 socklen_t addrlen;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001865
Guido van Rossum43713e52000-02-29 13:59:29 +00001866 if (!PyArg_ParseTuple(args, "i|i:recvfrom", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001867 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001868
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00001869 if (!getsockaddrlen(s, &addrlen))
1870 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001871 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00001872 if (buf == NULL)
1873 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001874
Guido van Rossum73624e91994-10-10 17:59:00 +00001875 Py_BEGIN_ALLOW_THREADS
Fred Drakefd168342001-05-09 19:11:33 +00001876 memset(addrbuf, 0, addrlen);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001877 timeout = internal_select(s, 0);
1878 if (!timeout)
1879 n = recvfrom(s->sock_fd, PyString_AS_STRING(buf), len, flags,
Guido van Rossum8d665e61996-06-26 18:22:49 +00001880#ifndef MS_WINDOWS
Andrew MacIntyreba43e872002-03-03 03:03:52 +00001881#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001882 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00001883#else
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001884 (void *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00001885#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001886#else
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001887 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001888#endif
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001889 );
Guido van Rossum73624e91994-10-10 17:59:00 +00001890 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001891
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001892 if (timeout) {
1893 Py_DECREF(buf);
1894 PyErr_SetString(socket_timeout, "timed out");
1895 return NULL;
1896 }
Guido van Rossum7c53b771995-09-13 18:39:47 +00001897 if (n < 0) {
1898 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001899 return s->errorhandler();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001900 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00001901
Guido van Rossum53a9bf81996-06-11 18:35:24 +00001902 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001903 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001904
Guido van Rossum67f7a382002-06-06 21:08:16 +00001905 if (!(addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001906 addrlen)))
Barry Warsaw752300b1997-01-03 17:18:10 +00001907 goto finally;
1908
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001909 ret = PyTuple_Pack(2, buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001910
1911finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001912 Py_XDECREF(addr);
1913 Py_XDECREF(buf);
Guido van Rossum6f5afc91993-02-05 09:46:15 +00001914 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001915}
1916
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001917PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001918"recvfrom(buffersize[, flags]) -> (data, address info)\n\
1919\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001920Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001921
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001922/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001923
Guido van Rossum73624e91994-10-10 17:59:00 +00001924static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001925sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001926{
Guido van Rossumff4949e1992-08-05 19:58:53 +00001927 char *buf;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001928 int len, n = 0, flags = 0, timeout;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001929#ifdef __VMS
1930 int send_length;
1931#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00001932
Guido van Rossum43713e52000-02-29 13:59:29 +00001933 if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001934 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001935
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001936#ifndef __VMS
Guido van Rossum73624e91994-10-10 17:59:00 +00001937 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001938 timeout = internal_select(s, 1);
1939 if (!timeout)
1940 n = send(s->sock_fd, buf, len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001941 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001942
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001943 if (timeout) {
1944 PyErr_SetString(socket_timeout, "timed out");
1945 return NULL;
1946 }
Guido van Rossum30a685f1991-06-27 15:51:29 +00001947 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001948 return s->errorhandler();
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001949#else
1950 /* Divide packet into smaller segments for */
1951 /* TCP/IP Services for OpenVMS */
1952 send_length = len;
1953 while (send_length != 0) {
1954 unsigned int segment;
1955
1956 segment = send_length / SEGMENT_SIZE;
1957 if (segment != 0) {
1958 segment = SEGMENT_SIZE;
1959 }
1960 else {
1961 segment = send_length;
1962 }
1963 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001964 timeout = internal_select(s, 1);
1965 if (!timeout)
1966 n = send(s->sock_fd, buf, segment, flags);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001967 Py_END_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001968 if (timeout) {
1969 PyErr_SetString(socket_timeout, "timed out");
1970 return NULL;
1971 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001972 if (n < 0) {
1973 return s->errorhandler();
1974 }
1975 send_length -= segment;
1976 buf += segment;
1977 } /* end while */
1978#endif /* !__VMS */
Guido van Rossum73624e91994-10-10 17:59:00 +00001979 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00001980}
1981
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001982PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001983"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001984\n\
1985Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001986argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001987sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001988
1989
1990/* s.sendall(data [,flags]) method */
1991
1992static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001993sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001994{
1995 char *buf;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001996 int len, n = 0, flags = 0, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001997
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001998 if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags))
1999 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002000
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002001 Py_BEGIN_ALLOW_THREADS
2002 do {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002003 timeout = internal_select(s, 1);
2004 if (timeout)
2005 break;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002006 n = send(s->sock_fd, buf, len, flags);
2007 if (n < 0)
2008 break;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002009 buf += n;
2010 len -= n;
2011 } while (len > 0);
2012 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002013
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002014 if (timeout) {
2015 PyErr_SetString(socket_timeout, "timed out");
2016 return NULL;
2017 }
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002018 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002019 return s->errorhandler();
Guido van Rossum67f7a382002-06-06 21:08:16 +00002020
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002021 Py_INCREF(Py_None);
2022 return Py_None;
2023}
2024
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002025PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002026"sendall(data[, flags])\n\
2027\n\
2028Send a data string to the socket. For the optional flags\n\
2029argument, see the Unix manual. This calls send() repeatedly\n\
2030until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002031to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002032
Guido van Rossum30a685f1991-06-27 15:51:29 +00002033
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002034/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002035
Guido van Rossum73624e91994-10-10 17:59:00 +00002036static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002037sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002038{
Guido van Rossum73624e91994-10-10 17:59:00 +00002039 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002040 char *buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002041 struct sockaddr *addr;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002042 int addrlen, len, n = 0, flags, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002043
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002044 flags = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002045 if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) {
Guido van Rossum73624e91994-10-10 17:59:00 +00002046 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002047 if (!PyArg_ParseTuple(args, "s#iO:sendto",
2048 &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002049 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002050 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002051
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002052 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002053 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002054
Guido van Rossum73624e91994-10-10 17:59:00 +00002055 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002056 timeout = internal_select(s, 1);
2057 if (!timeout)
2058 n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002059 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002060
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002061 if (timeout) {
2062 PyErr_SetString(socket_timeout, "timed out");
2063 return NULL;
2064 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002065 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002066 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002067 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002068}
2069
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002070PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002071"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002072\n\
2073Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002074For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002075
Guido van Rossum30a685f1991-06-27 15:51:29 +00002076
2077/* s.shutdown(how) method */
2078
Guido van Rossum73624e91994-10-10 17:59:00 +00002079static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002080sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002081{
2082 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002083 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002084
2085 how = PyInt_AsLong(arg);
2086 if (how == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002087 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002088 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002089 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +00002090 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002091 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002092 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002093 Py_INCREF(Py_None);
2094 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002095}
2096
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002097PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002098"shutdown(flag)\n\
2099\n\
Martin v. Löwis94681fc2003-11-27 19:40:22 +00002100Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\
2101of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002102
Guido van Rossum30a685f1991-06-27 15:51:29 +00002103
2104/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002105
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002106static PyMethodDef sock_methods[] = {
2107 {"accept", (PyCFunction)sock_accept, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002108 accept_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002109 {"bind", (PyCFunction)sock_bind, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002110 bind_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002111 {"close", (PyCFunction)sock_close, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002112 close_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002113 {"connect", (PyCFunction)sock_connect, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002114 connect_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002115 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002116 connect_ex_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00002117#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002118 {"dup", (PyCFunction)sock_dup, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002119 dup_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00002120#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002121 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002122 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002123#ifdef HAVE_GETPEERNAME
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002124 {"getpeername", (PyCFunction)sock_getpeername,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002125 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00002126#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002127 {"getsockname", (PyCFunction)sock_getsockname,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002128 METH_NOARGS, getsockname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002129 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002130 getsockopt_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002131 {"listen", (PyCFunction)sock_listen, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002132 listen_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002133#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002134 {"makefile", (PyCFunction)sock_makefile, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002135 makefile_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002136#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002137 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002138 recv_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002139 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002140 recvfrom_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002141 {"send", (PyCFunction)sock_send, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002142 send_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002143 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002144 sendall_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002145 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002146 sendto_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002147 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002148 setblocking_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002149 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
Guido van Rossum67f7a382002-06-06 21:08:16 +00002150 settimeout_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002151 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
Guido van Rossum67f7a382002-06-06 21:08:16 +00002152 gettimeout_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002153 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002154 setsockopt_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002155 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002156 shutdown_doc},
2157#ifdef RISCOS
Martin v. Löwisa94568a2003-05-10 07:36:56 +00002158 {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002159 sleeptaskw_doc},
2160#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00002161 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002162};
2163
Guido van Rossum30a685f1991-06-27 15:51:29 +00002164
Guido van Rossum73624e91994-10-10 17:59:00 +00002165/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00002166 First close the file description. */
2167
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002168static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002169sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002170{
Guido van Rossumfa972c92000-04-10 12:45:45 +00002171 if (s->sock_fd != -1)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00002172 (void) SOCKETCLOSE(s->sock_fd);
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002173 s->ob_type->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002174}
2175
Guido van Rossum30a685f1991-06-27 15:51:29 +00002176
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002177static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002178sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002179{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002180 char buf[512];
Fred Drakea04eaad2000-06-30 02:46:07 +00002181#if SIZEOF_SOCKET_T > SIZEOF_LONG
2182 if (s->sock_fd > LONG_MAX) {
2183 /* this can occur on Win64, and actually there is a special
2184 ugly printf formatter for decimal pointer length integer
2185 printing, only bother if necessary*/
2186 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002187 "no printf formatter to display "
2188 "the socket descriptor in decimal");
Fred Drakea04eaad2000-06-30 02:46:07 +00002189 return NULL;
2190 }
2191#endif
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002192 PyOS_snprintf(
2193 buf, sizeof(buf),
2194 "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>",
2195 (long)s->sock_fd, s->sock_family,
2196 s->sock_type,
2197 s->sock_proto);
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00002198 return PyString_FromString(buf);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002199}
2200
2201
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002202/* Create a new, uninitialized socket object. */
2203
2204static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002205sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002206{
2207 PyObject *new;
2208
2209 new = type->tp_alloc(type, 0);
Guido van Rossume1c478f2002-06-06 20:08:25 +00002210 if (new != NULL) {
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002211 ((PySocketSockObject *)new)->sock_fd = -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002212 ((PySocketSockObject *)new)->sock_timeout = -1.0;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002213 ((PySocketSockObject *)new)->errorhandler = &set_error;
Guido van Rossume1c478f2002-06-06 20:08:25 +00002214 }
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002215 return new;
2216}
2217
2218
2219/* Initialize a new socket object. */
2220
2221/*ARGSUSED*/
2222static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002223sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002224{
2225 PySocketSockObject *s = (PySocketSockObject *)self;
2226 SOCKET_T fd;
2227 int family = AF_INET, type = SOCK_STREAM, proto = 0;
2228 static char *keywords[] = {"family", "type", "proto", 0};
2229
2230 if (!PyArg_ParseTupleAndKeywords(args, kwds,
2231 "|iii:socket", keywords,
2232 &family, &type, &proto))
2233 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002234
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002235 Py_BEGIN_ALLOW_THREADS
2236 fd = socket(family, type, proto);
2237 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002238
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002239#ifdef MS_WINDOWS
2240 if (fd == INVALID_SOCKET)
2241#else
2242 if (fd < 0)
2243#endif
2244 {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002245 set_error();
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002246 return -1;
2247 }
2248 init_sockobject(s, fd, family, type, proto);
2249 /* From now on, ignore SIGPIPE and let the error checking
2250 do the work. */
2251#ifdef SIGPIPE
2252 (void) signal(SIGPIPE, SIG_IGN);
2253#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002254
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002255 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002256
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002257}
2258
2259
Guido van Rossumb6775db1994-08-01 11:34:53 +00002260/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002261
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002262static PyTypeObject sock_type = {
Guido van Rossum55558541996-05-23 22:54:50 +00002263 PyObject_HEAD_INIT(0) /* Must fill in type value later */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002264 0, /* ob_size */
Guido van Rossumca6dfa52001-10-28 12:31:33 +00002265 "_socket.socket", /* tp_name */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002266 sizeof(PySocketSockObject), /* tp_basicsize */
2267 0, /* tp_itemsize */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002268 (destructor)sock_dealloc, /* tp_dealloc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002269 0, /* tp_print */
2270 0, /* tp_getattr */
2271 0, /* tp_setattr */
2272 0, /* tp_compare */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002273 (reprfunc)sock_repr, /* tp_repr */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002274 0, /* tp_as_number */
2275 0, /* tp_as_sequence */
2276 0, /* tp_as_mapping */
2277 0, /* tp_hash */
2278 0, /* tp_call */
2279 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002280 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002281 0, /* tp_setattro */
2282 0, /* tp_as_buffer */
2283 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002284 sock_doc, /* tp_doc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002285 0, /* tp_traverse */
2286 0, /* tp_clear */
2287 0, /* tp_richcompare */
2288 0, /* tp_weaklistoffset */
2289 0, /* tp_iter */
2290 0, /* tp_iternext */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002291 sock_methods, /* tp_methods */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002292 0, /* tp_members */
2293 0, /* tp_getset */
2294 0, /* tp_base */
2295 0, /* tp_dict */
2296 0, /* tp_descr_get */
2297 0, /* tp_descr_set */
2298 0, /* tp_dictoffset */
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002299 sock_initobj, /* tp_init */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002300 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002301 sock_new, /* tp_new */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002302 PyObject_Del, /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002303};
2304
Guido van Rossum30a685f1991-06-27 15:51:29 +00002305
Guido van Rossum81194471991-07-27 21:42:02 +00002306/* Python interface to gethostname(). */
2307
2308/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002309static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002310socket_gethostname(PyObject *self, PyObject *args)
Guido van Rossum81194471991-07-27 21:42:02 +00002311{
2312 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00002313 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002314 if (!PyArg_ParseTuple(args, ":gethostname"))
Guido van Rossum81194471991-07-27 21:42:02 +00002315 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002316 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002317 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00002318 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002319 if (res < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002320 return set_error();
Guido van Rossum81194471991-07-27 21:42:02 +00002321 buf[sizeof buf - 1] = '\0';
Guido van Rossum73624e91994-10-10 17:59:00 +00002322 return PyString_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00002323}
Guido van Rossumff4949e1992-08-05 19:58:53 +00002324
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002325PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002326"gethostname() -> string\n\
2327\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002328Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002329
Guido van Rossumff4949e1992-08-05 19:58:53 +00002330
Guido van Rossum30a685f1991-06-27 15:51:29 +00002331/* Python interface to gethostbyname(name). */
2332
2333/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002334static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002335socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002336{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002337 char *name;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00002338#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002339 struct sockaddr_storage addrbuf;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00002340#else
2341 struct sockaddr_in addrbuf;
2342#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002343
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002344 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002345 return NULL;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002346 if (setipaddr(name, (struct sockaddr *)&addrbuf, sizeof(addrbuf), AF_INET) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002347 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002348 return makeipaddr((struct sockaddr *)&addrbuf,
2349 sizeof(struct sockaddr_in));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002350}
2351
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002352PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002353"gethostbyname(host) -> address\n\
2354\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002355Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002356
2357
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002358/* Convenience function common to gethostbyname_ex and gethostbyaddr */
2359
2360static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002361gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002362{
2363 char **pch;
2364 PyObject *rtn_tuple = (PyObject *)NULL;
2365 PyObject *name_list = (PyObject *)NULL;
2366 PyObject *addr_list = (PyObject *)NULL;
2367 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002368
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002369 if (h == NULL) {
Guido van Rossum48a680c2001-03-02 06:34:14 +00002370 /* Let's get real error message to return */
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002371#ifndef RISCOS
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002372 set_herror(h_errno);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002373#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002374 PyErr_SetString(socket_error, "host not found");
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002375#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002376 return NULL;
2377 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002378
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002379 if (h->h_addrtype != af) {
2380#ifdef HAVE_STRERROR
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002381 /* Let's get real error message to return */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002382 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002383 (char *)strerror(EAFNOSUPPORT));
2384#else
2385 PyErr_SetString(
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002386 socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002387 "Address family not supported by protocol family");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002388#endif
2389 return NULL;
2390 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002391
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002392 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002393
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002394 case AF_INET:
2395 if (alen < sizeof(struct sockaddr_in))
2396 return NULL;
2397 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002398
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002399#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002400 case AF_INET6:
2401 if (alen < sizeof(struct sockaddr_in6))
2402 return NULL;
2403 break;
2404#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002405
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002406 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002407
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002408 if ((name_list = PyList_New(0)) == NULL)
2409 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002410
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002411 if ((addr_list = PyList_New(0)) == NULL)
2412 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002413
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002414 for (pch = h->h_aliases; *pch != NULL; pch++) {
2415 int status;
2416 tmp = PyString_FromString(*pch);
2417 if (tmp == NULL)
2418 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002419
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002420 status = PyList_Append(name_list, tmp);
2421 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002422
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002423 if (status)
2424 goto err;
2425 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002426
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002427 for (pch = h->h_addr_list; *pch != NULL; pch++) {
2428 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002429
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002430 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002431
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002432 case AF_INET:
2433 {
2434 struct sockaddr_in sin;
2435 memset(&sin, 0, sizeof(sin));
2436 sin.sin_family = af;
2437#ifdef HAVE_SOCKADDR_SA_LEN
2438 sin.sin_len = sizeof(sin);
2439#endif
2440 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
2441 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002442
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002443 if (pch == h->h_addr_list && alen >= sizeof(sin))
2444 memcpy((char *) addr, &sin, sizeof(sin));
2445 break;
2446 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002447
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002448#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002449 case AF_INET6:
2450 {
2451 struct sockaddr_in6 sin6;
2452 memset(&sin6, 0, sizeof(sin6));
2453 sin6.sin6_family = af;
2454#ifdef HAVE_SOCKADDR_SA_LEN
2455 sin6.sin6_len = sizeof(sin6);
2456#endif
2457 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
2458 tmp = makeipaddr((struct sockaddr *)&sin6,
2459 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002460
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002461 if (pch == h->h_addr_list && alen >= sizeof(sin6))
2462 memcpy((char *) addr, &sin6, sizeof(sin6));
2463 break;
2464 }
2465#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002466
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002467 default: /* can't happen */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002468 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002469 "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002470 return NULL;
2471 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002472
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002473 if (tmp == NULL)
2474 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002475
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002476 status = PyList_Append(addr_list, tmp);
2477 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002478
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002479 if (status)
2480 goto err;
2481 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002482
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002483 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002484
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002485 err:
2486 Py_XDECREF(name_list);
2487 Py_XDECREF(addr_list);
2488 return rtn_tuple;
2489}
2490
2491
2492/* Python interface to gethostbyname_ex(name). */
2493
2494/*ARGSUSED*/
2495static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002496socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002497{
2498 char *name;
2499 struct hostent *h;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00002500#ifdef ENABLE_IPV6
2501 struct sockaddr_storage addr;
2502#else
2503 struct sockaddr_in addr;
2504#endif
Martin v. Löwis9db2f572001-07-23 01:30:10 +00002505 struct sockaddr *sa;
Guido van Rossum955becc1999-03-22 20:14:53 +00002506 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002507#ifdef HAVE_GETHOSTBYNAME_R
2508 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002509#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
2510 struct hostent_data data;
2511#else
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002512 char buf[16384];
2513 int buf_len = (sizeof buf) - 1;
2514 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002515#endif
2516#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002517 int result;
2518#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002519#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002520
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002521 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002522 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00002523 if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002524 return NULL;
2525 Py_BEGIN_ALLOW_THREADS
2526#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002527#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002528 result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
2529 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002530#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002531 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002532#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00002533 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002534 result = gethostbyname_r(name, &hp_allocated, &data);
2535 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002536#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002537#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002538#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002539 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002540#endif
2541 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002542#endif /* HAVE_GETHOSTBYNAME_R */
2543 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002544 /* Some C libraries would require addr.__ss_family instead of
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002545 addr.ss_family.
2546 Therefore, we cast the sockaddr_storage into sockaddr to
2547 access sa_family. */
Martin v. Löwis9db2f572001-07-23 01:30:10 +00002548 sa = (struct sockaddr*)&addr;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002549 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002550 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002551#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002552 PyThread_release_lock(netdb_lock);
Guido van Rossum955becc1999-03-22 20:14:53 +00002553#endif
2554 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002555}
2556
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002557PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002558"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
2559\n\
2560Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002561for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002562
2563
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002564/* Python interface to gethostbyaddr(IP). */
2565
2566/*ARGSUSED*/
2567static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002568socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002569{
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002570#ifdef ENABLE_IPV6
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002571 struct sockaddr_storage addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002572#else
Guido van Rossum48a680c2001-03-02 06:34:14 +00002573 struct sockaddr_in addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002574#endif
2575 struct sockaddr *sa = (struct sockaddr *)&addr;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002576 char *ip_num;
2577 struct hostent *h;
Guido van Rossum3baaa131999-03-22 21:44:51 +00002578 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002579#ifdef HAVE_GETHOSTBYNAME_R
2580 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002581#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
2582 struct hostent_data data;
2583#else
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002584 char buf[16384];
2585 int buf_len = (sizeof buf) - 1;
2586 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002587#endif
2588#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002589 int result;
2590#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002591#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002592 char *ap;
2593 int al;
2594 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002595
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002596 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002597 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00002598 af = AF_UNSPEC;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002599 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002600 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002601 af = sa->sa_family;
2602 ap = NULL;
2603 al = 0;
2604 switch (af) {
2605 case AF_INET:
2606 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
2607 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
2608 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002609#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002610 case AF_INET6:
2611 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
2612 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
2613 break;
2614#endif
2615 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002616 PyErr_SetString(socket_error, "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002617 return NULL;
2618 }
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002619 Py_BEGIN_ALLOW_THREADS
2620#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002621#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002622 result = gethostbyaddr_r(ap, al, af,
2623 &hp_allocated, buf, buf_len,
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002624 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002625#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002626 h = gethostbyaddr_r(ap, al, af,
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002627 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002628#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00002629 memset((void *) &data, '\0', sizeof(data));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002630 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002631 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002632#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002633#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002634#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002635 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002636#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002637 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002638#endif /* HAVE_GETHOSTBYNAME_R */
2639 Py_END_ALLOW_THREADS
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002640 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002641#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002642 PyThread_release_lock(netdb_lock);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002643#endif
2644 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002645}
2646
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002647PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002648"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
2649\n\
2650Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002651for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002652
Guido van Rossum30a685f1991-06-27 15:51:29 +00002653
2654/* Python interface to getservbyname(name).
2655 This only returns the port number, since the other info is already
2656 known or not useful (like the list of aliases). */
2657
2658/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002659static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002660socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002661{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002662 char *name, *proto;
Guido van Rossum30a685f1991-06-27 15:51:29 +00002663 struct servent *sp;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002664 if (!PyArg_ParseTuple(args, "ss:getservbyname", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002665 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002666 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002667 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00002668 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00002669 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002670 PyErr_SetString(socket_error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00002671 return NULL;
2672 }
Guido van Rossum73624e91994-10-10 17:59:00 +00002673 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002674}
2675
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002676PyDoc_STRVAR(getservbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002677"getservbyname(servicename, protocolname) -> integer\n\
2678\n\
2679Return a port number from a service name and protocol name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002680The protocol name should be 'tcp' or 'udp'.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002681
Guido van Rossum30a685f1991-06-27 15:51:29 +00002682
Guido van Rossum3901d851996-12-19 16:35:04 +00002683/* Python interface to getprotobyname(name).
2684 This only returns the protocol number, since the other info is
2685 already known or not useful (like the list of aliases). */
2686
2687/*ARGSUSED*/
2688static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002689socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00002690{
2691 char *name;
2692 struct protoent *sp;
Guido van Rossumbcc20741998-08-04 22:53:56 +00002693#ifdef __BEOS__
2694/* Not available in BeOS yet. - [cjh] */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002695 PyErr_SetString(socket_error, "getprotobyname not supported");
Guido van Rossumbcc20741998-08-04 22:53:56 +00002696 return NULL;
2697#else
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002698 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
Guido van Rossum3901d851996-12-19 16:35:04 +00002699 return NULL;
2700 Py_BEGIN_ALLOW_THREADS
2701 sp = getprotobyname(name);
2702 Py_END_ALLOW_THREADS
2703 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002704 PyErr_SetString(socket_error, "protocol not found");
Guido van Rossum3901d851996-12-19 16:35:04 +00002705 return NULL;
2706 }
2707 return PyInt_FromLong((long) sp->p_proto);
Guido van Rossumbcc20741998-08-04 22:53:56 +00002708#endif
Guido van Rossum3901d851996-12-19 16:35:04 +00002709}
2710
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002711PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002712"getprotobyname(name) -> integer\n\
2713\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002714Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002715
Guido van Rossum3901d851996-12-19 16:35:04 +00002716
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002717#ifndef NO_DUP
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002718/* Create a socket object from a numeric file description.
2719 Useful e.g. if stdin is a socket.
2720 Additional arguments as for socket(). */
2721
2722/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002723static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002724socket_fromfd(PyObject *self, PyObject *args)
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002725{
Guido van Rossum73624e91994-10-10 17:59:00 +00002726 PySocketSockObject *s;
Fred Drakea04eaad2000-06-30 02:46:07 +00002727 SOCKET_T fd;
2728 int family, type, proto = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002729 if (!PyArg_ParseTuple(args, "iii|i:fromfd",
2730 &fd, &family, &type, &proto))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002731 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +00002732 /* Dup the fd so it and the socket can be closed independently */
2733 fd = dup(fd);
2734 if (fd < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002735 return set_error();
2736 s = new_sockobject(fd, family, type, proto);
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002737 /* From now on, ignore SIGPIPE and let the error checking
2738 do the work. */
Guido van Rossum48a680c2001-03-02 06:34:14 +00002739#ifdef SIGPIPE
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002740 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum48a680c2001-03-02 06:34:14 +00002741#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00002742 return (PyObject *) s;
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002743}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002744
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002745PyDoc_STRVAR(fromfd_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002746"fromfd(fd, family, type[, proto]) -> socket object\n\
2747\n\
2748Create a socket object from the given file descriptor.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002749The remaining arguments are the same as for socket().");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002750
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002751#endif /* NO_DUP */
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002752
Guido van Rossum82a5c661998-07-07 20:45:43 +00002753
Guido van Rossum006bf911996-06-12 04:04:55 +00002754static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002755socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00002756{
2757 int x1, x2;
2758
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002759 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00002760 return NULL;
2761 }
2762 x2 = (int)ntohs((short)x1);
2763 return PyInt_FromLong(x2);
2764}
2765
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002766PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002767"ntohs(integer) -> integer\n\
2768\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002769Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002770
2771
Guido van Rossum006bf911996-06-12 04:04:55 +00002772static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002773socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00002774{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002775 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00002776
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002777 if (PyInt_Check(arg)) {
2778 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002779 if (x == (unsigned long) -1 && PyErr_Occurred())
2780 return NULL;
Guido van Rossum006bf911996-06-12 04:04:55 +00002781 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002782 else if (PyLong_Check(arg)) {
2783 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002784 if (x == (unsigned long) -1 && PyErr_Occurred())
2785 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002786#if SIZEOF_LONG > 4
2787 {
2788 unsigned long y;
2789 /* only want the trailing 32 bits */
2790 y = x & 0xFFFFFFFFUL;
2791 if (y ^ x)
2792 return PyErr_Format(PyExc_OverflowError,
2793 "long int larger than 32 bits");
2794 x = y;
Tim Peters58141872002-08-06 22:25:02 +00002795 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002796#endif
2797 }
2798 else
Tim Peters58141872002-08-06 22:25:02 +00002799 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002800 "expected int/long, %s found",
2801 arg->ob_type->tp_name);
2802 if (x == (unsigned long) -1 && PyErr_Occurred())
2803 return NULL;
2804 return PyInt_FromLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00002805}
2806
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002807PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002808"ntohl(integer) -> integer\n\
2809\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002810Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002811
2812
Guido van Rossum006bf911996-06-12 04:04:55 +00002813static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002814socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00002815{
Hye-Shik Changa7b673f2003-12-17 09:40:23 +00002816 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00002817
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002818 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00002819 return NULL;
2820 }
2821 x2 = (int)htons((short)x1);
2822 return PyInt_FromLong(x2);
2823}
2824
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002825PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002826"htons(integer) -> integer\n\
2827\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002828Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002829
2830
Guido van Rossum006bf911996-06-12 04:04:55 +00002831static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002832socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00002833{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002834 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00002835
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002836 if (PyInt_Check(arg)) {
2837 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002838 if (x == (unsigned long) -1 && PyErr_Occurred())
2839 return NULL;
Guido van Rossum006bf911996-06-12 04:04:55 +00002840 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002841 else if (PyLong_Check(arg)) {
2842 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002843 if (x == (unsigned long) -1 && PyErr_Occurred())
2844 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002845#if SIZEOF_LONG > 4
2846 {
2847 unsigned long y;
2848 /* only want the trailing 32 bits */
2849 y = x & 0xFFFFFFFFUL;
2850 if (y ^ x)
2851 return PyErr_Format(PyExc_OverflowError,
2852 "long int larger than 32 bits");
2853 x = y;
Tim Peters58141872002-08-06 22:25:02 +00002854 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002855#endif
2856 }
2857 else
Tim Peters58141872002-08-06 22:25:02 +00002858 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002859 "expected int/long, %s found",
2860 arg->ob_type->tp_name);
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002861 return PyInt_FromLong(htonl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00002862}
2863
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002864PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002865"htonl(integer) -> integer\n\
2866\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002867Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002868
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002869/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002870
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002871PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002872"inet_aton(string) -> packed 32-bit IP representation\n\
2873\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002874Convert 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 +00002875binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002876
2877static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002878socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002879{
Guido van Rossuma2e48551999-09-09 15:42:59 +00002880#ifndef INADDR_NONE
2881#define INADDR_NONE (-1)
2882#endif
Neal Norwitz88f115b2003-02-13 02:15:42 +00002883#ifdef HAVE_INET_ATON
Guido van Rossumad05cdf2003-02-12 23:08:22 +00002884 struct in_addr buf;
Tim Peters1df9fdd2003-02-13 03:13:40 +00002885#else
2886 /* Have to use inet_addr() instead */
2887 unsigned long packed_addr;
2888#endif
2889 char *ip_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002890
Tim Peters1df9fdd2003-02-13 03:13:40 +00002891 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002892 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00002893
Tim Peters1df9fdd2003-02-13 03:13:40 +00002894
2895#ifdef HAVE_INET_ATON
Guido van Rossumad05cdf2003-02-12 23:08:22 +00002896 if (inet_aton(ip_addr, &buf))
2897 return PyString_FromStringAndSize((char *)(&buf),
2898 sizeof(buf));
2899
2900 PyErr_SetString(socket_error,
2901 "illegal IP address string passed to inet_aton");
2902 return NULL;
2903
Tim Peters1df9fdd2003-02-13 03:13:40 +00002904#else /* ! HAVE_INET_ATON */
Guido van Rossumad05cdf2003-02-12 23:08:22 +00002905 /* XXX Problem here: inet_aton('255.255.255.255') raises
2906 an exception while it should be a valid address. */
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002907 packed_addr = inet_addr(ip_addr);
2908
2909 if (packed_addr == INADDR_NONE) { /* invalid address */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002910 PyErr_SetString(socket_error,
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002911 "illegal IP address string passed to inet_aton");
2912 return NULL;
2913 }
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002914 return PyString_FromStringAndSize((char *) &packed_addr,
2915 sizeof(packed_addr));
Guido van Rossumad05cdf2003-02-12 23:08:22 +00002916#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002917}
2918
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002919PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00002920"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002921\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002922Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002923
2924static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002925socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002926{
2927 char *packed_str;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002928 int addr_len;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002929 struct in_addr packed_addr;
2930
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002931 if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) {
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002932 return NULL;
2933 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00002934
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002935 if (addr_len != sizeof(packed_addr)) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002936 PyErr_SetString(socket_error,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002937 "packed IP wrong length for inet_ntoa");
2938 return NULL;
2939 }
2940
2941 memcpy(&packed_addr, packed_str, addr_len);
2942
2943 return PyString_FromString(inet_ntoa(packed_addr));
2944}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002945
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00002946#ifdef HAVE_INET_PTON
2947
2948PyDoc_STRVAR(inet_pton_doc,
2949"inet_pton(af, ip) -> packed IP address string\n\
2950\n\
2951Convert an IP address from string format to a packed string suitable\n\
2952for use with low-level network functions.");
2953
2954static PyObject *
2955socket_inet_pton(PyObject *self, PyObject *args)
2956{
2957 int af;
2958 char* ip;
2959 int retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00002960#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00002961 char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00002962#else
2963 char packed[sizeof(struct in_addr)];
2964#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00002965 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
2966 return NULL;
2967 }
2968
Martin v. Löwis10649092003-08-05 06:25:06 +00002969#ifndef ENABLE_IPV6
2970 if(af == AF_INET6) {
2971 PyErr_SetString(socket_error,
2972 "can't use AF_INET6, IPv6 is disabled");
2973 return NULL;
2974 }
2975#endif
2976
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00002977 retval = inet_pton(af, ip, packed);
2978 if (retval < 0) {
2979 PyErr_SetFromErrno(socket_error);
2980 return NULL;
2981 } else if (retval == 0) {
2982 PyErr_SetString(socket_error,
2983 "illegal IP address string passed to inet_pton");
2984 return NULL;
2985 } else if (af == AF_INET) {
2986 return PyString_FromStringAndSize(packed,
2987 sizeof(struct in_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00002988#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00002989 } else if (af == AF_INET6) {
2990 return PyString_FromStringAndSize(packed,
2991 sizeof(struct in6_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00002992#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00002993 } else {
2994 PyErr_SetString(socket_error, "unknown address family");
2995 return NULL;
2996 }
2997}
2998
2999PyDoc_STRVAR(inet_ntop_doc,
3000"inet_ntop(af, packed_ip) -> string formatted IP address\n\
3001\n\
3002Convert a packed IP address of the given family to string format.");
3003
3004static PyObject *
3005socket_inet_ntop(PyObject *self, PyObject *args)
3006{
3007 int af;
3008 char* packed;
3009 int len;
3010 const char* retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003011#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003012 char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003013#else
3014 char ip[INET_ADDRSTRLEN + 1];
3015#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003016
3017 /* Guarantee NUL-termination for PyString_FromString() below */
3018 memset((void *) &ip[0], '\0', sizeof(ip) + 1);
3019
3020 if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) {
3021 return NULL;
3022 }
3023
3024 if (af == AF_INET) {
3025 if (len != sizeof(struct in_addr)) {
3026 PyErr_SetString(PyExc_ValueError,
3027 "invalid length of packed IP address string");
3028 return NULL;
3029 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003030#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003031 } else if (af == AF_INET6) {
3032 if (len != sizeof(struct in6_addr)) {
3033 PyErr_SetString(PyExc_ValueError,
3034 "invalid length of packed IP address string");
3035 return NULL;
3036 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003037#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003038 } else {
3039 PyErr_Format(PyExc_ValueError,
3040 "unknown address family %d", af);
3041 return NULL;
3042 }
3043
3044 retval = inet_ntop(af, packed, ip, sizeof(ip));
3045 if (!retval) {
3046 PyErr_SetFromErrno(socket_error);
3047 return NULL;
3048 } else {
3049 return PyString_FromString(retval);
3050 }
3051
3052 /* NOTREACHED */
3053 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
3054 return NULL;
3055}
3056
3057#endif /* HAVE_INET_PTON */
3058
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003059/* Python interface to getaddrinfo(host, port). */
3060
3061/*ARGSUSED*/
3062static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003063socket_getaddrinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003064{
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003065 struct addrinfo hints, *res;
3066 struct addrinfo *res0 = NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003067 PyObject *hobj = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003068 PyObject *pobj = (PyObject *)NULL;
Martin v. Löwisf65b1a12001-08-12 09:28:40 +00003069 char pbuf[30];
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003070 char *hptr, *pptr;
3071 int family, socktype, protocol, flags;
3072 int error;
3073 PyObject *all = (PyObject *)NULL;
3074 PyObject *single = (PyObject *)NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003075 PyObject *idna = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003076
3077 family = socktype = protocol = flags = 0;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003078 family = AF_UNSPEC;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003079 if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo",
3080 &hobj, &pobj, &family, &socktype,
3081 &protocol, &flags)) {
3082 return NULL;
3083 }
3084 if (hobj == Py_None) {
3085 hptr = NULL;
3086 } else if (PyUnicode_Check(hobj)) {
3087 idna = PyObject_CallMethod(hobj, "encode", "s", "idna");
3088 if (!idna)
3089 return NULL;
3090 hptr = PyString_AsString(idna);
3091 } else if (PyString_Check(hobj)) {
3092 hptr = PyString_AsString(hobj);
3093 } else {
3094 PyErr_SetString(PyExc_TypeError,
3095 "getaddrinfo() argument 1 must be string or None");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003096 return NULL;
3097 }
3098 if (PyInt_Check(pobj)) {
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003099 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003100 pptr = pbuf;
3101 } else if (PyString_Check(pobj)) {
3102 pptr = PyString_AsString(pobj);
3103 } else if (pobj == Py_None) {
3104 pptr = (char *)NULL;
3105 } else {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003106 PyErr_SetString(socket_error, "Int or String expected");
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003107 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003108 }
3109 memset(&hints, 0, sizeof(hints));
3110 hints.ai_family = family;
3111 hints.ai_socktype = socktype;
3112 hints.ai_protocol = protocol;
3113 hints.ai_flags = flags;
Just van Rossum1040d2c2003-05-09 07:53:18 +00003114 Py_BEGIN_ALLOW_THREADS
3115 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003116 error = getaddrinfo(hptr, pptr, &hints, &res0);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003117 Py_END_ALLOW_THREADS
3118 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003119 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003120 set_gaierror(error);
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003121 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003122 }
3123
3124 if ((all = PyList_New(0)) == NULL)
3125 goto err;
3126 for (res = res0; res; res = res->ai_next) {
Guido van Rossum716aac02001-10-12 18:59:27 +00003127 PyObject *addr =
3128 makesockaddr(-1, res->ai_addr, res->ai_addrlen);
3129 if (addr == NULL)
3130 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003131 single = Py_BuildValue("iiisO", res->ai_family,
3132 res->ai_socktype, res->ai_protocol,
3133 res->ai_canonname ? res->ai_canonname : "",
Guido van Rossum716aac02001-10-12 18:59:27 +00003134 addr);
3135 Py_DECREF(addr);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003136 if (single == NULL)
3137 goto err;
3138
3139 if (PyList_Append(all, single))
3140 goto err;
3141 Py_XDECREF(single);
3142 }
Martin v. Löwis2548c732003-04-18 10:39:54 +00003143 Py_XDECREF(idna);
Neal Norwitz90128ba2002-08-09 03:37:42 +00003144 if (res0)
3145 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003146 return all;
3147 err:
3148 Py_XDECREF(single);
3149 Py_XDECREF(all);
Martin v. Löwis2548c732003-04-18 10:39:54 +00003150 Py_XDECREF(idna);
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003151 if (res0)
3152 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003153 return (PyObject *)NULL;
3154}
3155
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003156PyDoc_STRVAR(getaddrinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003157"getaddrinfo(host, port [, family, socktype, proto, flags])\n\
3158 -> list of (family, socktype, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003159\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003160Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003161
3162/* Python interface to getnameinfo(sa, flags). */
3163
3164/*ARGSUSED*/
3165static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003166socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003167{
3168 PyObject *sa = (PyObject *)NULL;
3169 int flags;
3170 char *hostp;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003171 int port, flowinfo, scope_id;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003172 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
3173 struct addrinfo hints, *res = NULL;
3174 int error;
3175 PyObject *ret = (PyObject *)NULL;
3176
3177 flags = flowinfo = scope_id = 0;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003178 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003179 return NULL;
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003180 if (!PyArg_ParseTuple(sa, "si|ii",
3181 &hostp, &port, &flowinfo, &scope_id))
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003182 return NULL;
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003183 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003184 memset(&hints, 0, sizeof(hints));
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003185 hints.ai_family = AF_UNSPEC;
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003186 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Just van Rossum1040d2c2003-05-09 07:53:18 +00003187 Py_BEGIN_ALLOW_THREADS
3188 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003189 error = getaddrinfo(hostp, pbuf, &hints, &res);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003190 Py_END_ALLOW_THREADS
3191 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003192 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003193 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003194 goto fail;
3195 }
3196 if (res->ai_next) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003197 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003198 "sockaddr resolved to multiple addresses");
3199 goto fail;
3200 }
3201 switch (res->ai_family) {
3202 case AF_INET:
3203 {
3204 char *t1;
3205 int t2;
3206 if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003207 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003208 "IPv4 sockaddr must be 2 tuple");
3209 goto fail;
3210 }
3211 break;
3212 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003213#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003214 case AF_INET6:
3215 {
3216 struct sockaddr_in6 *sin6;
3217 sin6 = (struct sockaddr_in6 *)res->ai_addr;
3218 sin6->sin6_flowinfo = flowinfo;
3219 sin6->sin6_scope_id = scope_id;
3220 break;
3221 }
3222#endif
3223 }
3224 error = getnameinfo(res->ai_addr, res->ai_addrlen,
3225 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
3226 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003227 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003228 goto fail;
3229 }
3230 ret = Py_BuildValue("ss", hbuf, pbuf);
3231
3232fail:
3233 if (res)
3234 freeaddrinfo(res);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003235 return ret;
3236}
3237
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003238PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003239"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003240\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003241Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003242
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003243
3244/* Python API to getting and setting the default timeout value. */
3245
3246static PyObject *
3247socket_getdefaulttimeout(PyObject *self)
3248{
3249 if (defaulttimeout < 0.0) {
3250 Py_INCREF(Py_None);
3251 return Py_None;
3252 }
3253 else
3254 return PyFloat_FromDouble(defaulttimeout);
3255}
3256
3257PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003258"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003259\n\
3260Returns the default timeout in floating seconds for new socket objects.\n\
3261A value of None indicates that new socket objects have no timeout.\n\
3262When the socket module is first imported, the default is None.");
3263
3264static PyObject *
3265socket_setdefaulttimeout(PyObject *self, PyObject *arg)
3266{
3267 double timeout;
3268
3269 if (arg == Py_None)
3270 timeout = -1.0;
3271 else {
3272 timeout = PyFloat_AsDouble(arg);
3273 if (timeout < 0.0) {
3274 if (!PyErr_Occurred())
3275 PyErr_SetString(PyExc_ValueError,
3276 "Timeout value out of range");
3277 return NULL;
3278 }
3279 }
3280
3281 defaulttimeout = timeout;
3282
3283 Py_INCREF(Py_None);
3284 return Py_None;
3285}
3286
3287PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003288"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003289\n\
3290Set the default timeout in floating seconds for new socket objects.\n\
3291A value of None indicates that new socket objects have no timeout.\n\
3292When the socket module is first imported, the default is None.");
3293
3294
Guido van Rossum30a685f1991-06-27 15:51:29 +00003295/* List of functions exported by this module. */
3296
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003297static PyMethodDef socket_methods[] = {
3298 {"gethostbyname", socket_gethostbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003299 METH_VARARGS, gethostbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003300 {"gethostbyname_ex", socket_gethostbyname_ex,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003301 METH_VARARGS, ghbn_ex_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003302 {"gethostbyaddr", socket_gethostbyaddr,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003303 METH_VARARGS, gethostbyaddr_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003304 {"gethostname", socket_gethostname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003305 METH_VARARGS, gethostname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003306 {"getservbyname", socket_getservbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003307 METH_VARARGS, getservbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003308 {"getprotobyname", socket_getprotobyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003309 METH_VARARGS,getprotobyname_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003310#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003311 {"fromfd", socket_fromfd,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003312 METH_VARARGS, fromfd_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003313#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003314 {"ntohs", socket_ntohs,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003315 METH_VARARGS, ntohs_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003316 {"ntohl", socket_ntohl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003317 METH_O, ntohl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003318 {"htons", socket_htons,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003319 METH_VARARGS, htons_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003320 {"htonl", socket_htonl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003321 METH_O, htonl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003322 {"inet_aton", socket_inet_aton,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003323 METH_VARARGS, inet_aton_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003324 {"inet_ntoa", socket_inet_ntoa,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003325 METH_VARARGS, inet_ntoa_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003326#ifdef HAVE_INET_PTON
3327 {"inet_pton", socket_inet_pton,
3328 METH_VARARGS, inet_pton_doc},
3329 {"inet_ntop", socket_inet_ntop,
3330 METH_VARARGS, inet_ntop_doc},
3331#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003332 {"getaddrinfo", socket_getaddrinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003333 METH_VARARGS, getaddrinfo_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003334 {"getnameinfo", socket_getnameinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003335 METH_VARARGS, getnameinfo_doc},
Guido van Rossum1693ba82002-07-18 21:11:26 +00003336 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003337 METH_NOARGS, getdefaulttimeout_doc},
3338 {"setdefaulttimeout", socket_setdefaulttimeout,
3339 METH_O, setdefaulttimeout_doc},
Guido van Rossum30a685f1991-06-27 15:51:29 +00003340 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003341};
3342
Guido van Rossum30a685f1991-06-27 15:51:29 +00003343
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003344#ifdef RISCOS
3345#define OS_INIT_DEFINED
Guido van Rossumbe32c891996-06-20 16:25:29 +00003346
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003347static int
3348os_init(void)
3349{
3350 _kernel_swi_regs r;
3351
3352 r.r[0] = 0;
3353 _kernel_swi(0x43380, &r, &r);
3354 taskwindow = r.r[0];
3355
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003356 return 1;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003357}
3358
3359#endif /* RISCOS */
3360
3361
3362#ifdef MS_WINDOWS
3363#define OS_INIT_DEFINED
3364
3365/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003366
3367static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003368os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00003369{
3370 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00003371}
3372
3373static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003374os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00003375{
3376 WSADATA WSAData;
3377 int ret;
3378 char buf[100];
3379 ret = WSAStartup(0x0101, &WSAData);
3380 switch (ret) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003381 case 0: /* No error */
Mark Hammond14350ab2003-07-09 04:57:46 +00003382 Py_AtExit(os_cleanup);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003383 return 1; /* Success */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003384 case WSASYSNOTREADY:
3385 PyErr_SetString(PyExc_ImportError,
3386 "WSAStartup failed: network not ready");
3387 break;
3388 case WSAVERNOTSUPPORTED:
3389 case WSAEINVAL:
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003390 PyErr_SetString(
3391 PyExc_ImportError,
3392 "WSAStartup failed: requested version not supported");
Guido van Rossumbe32c891996-06-20 16:25:29 +00003393 break;
3394 default:
Tim Peters885d4572001-11-28 20:27:42 +00003395 PyOS_snprintf(buf, sizeof(buf),
3396 "WSAStartup failed: error code %d", ret);
Guido van Rossumbe32c891996-06-20 16:25:29 +00003397 PyErr_SetString(PyExc_ImportError, buf);
3398 break;
3399 }
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003400 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003401}
3402
Guido van Rossum8d665e61996-06-26 18:22:49 +00003403#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003404
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003405
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003406#ifdef PYOS_OS2
3407#define OS_INIT_DEFINED
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003408
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003409/* Additional initialization for OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003410
3411static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003412os_init(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003413{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003414#ifndef PYCC_GCC
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003415 char reason[64];
3416 int rc = sock_init();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003417
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003418 if (rc == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003419 return 1; /* Success */
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003420 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003421
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003422 PyOS_snprintf(reason, sizeof(reason),
3423 "OS/2 TCP/IP Error# %d", sock_errno());
3424 PyErr_SetString(PyExc_ImportError, reason);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003425
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003426 return 0; /* Failure */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00003427#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003428 /* No need to initialise sockets with GCC/EMX */
3429 return 1; /* Success */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00003430#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003431}
3432
3433#endif /* PYOS_OS2 */
3434
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003435
3436#ifndef OS_INIT_DEFINED
3437static int
3438os_init(void)
3439{
3440 return 1; /* Success */
3441}
3442#endif
3443
3444
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003445/* C API table - always add new things to the end for binary
3446 compatibility. */
3447static
3448PySocketModule_APIObject PySocketModuleAPI =
3449{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003450 &sock_type,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003451};
3452
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003453
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003454/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003455
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003456 This module is actually called "_socket", and there's a wrapper
3457 "socket.py" which implements some additional functionality. On some
3458 platforms (e.g. Windows and OS/2), socket.py also implements a
3459 wrapper for the socket type that provides missing functionality such
3460 as makefile(), dup() and fromfd(). The import of "_socket" may fail
3461 with an ImportError exception if os-specific initialization fails.
3462 On Windows, this does WINSOCK initialization. When WINSOCK is
3463 initialized succesfully, a call to WSACleanup() is scheduled to be
3464 made at exit time.
3465*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003466
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003467PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003468"Implementation module for socket operations.\n\
3469\n\
3470See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003471
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003472PyMODINIT_FUNC
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003473init_socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003474{
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003475 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00003476
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003477 if (!os_init())
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003478 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003479
3480 sock_type.ob_type = &PyType_Type;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003481 m = Py_InitModule3(PySocket_MODULE_NAME,
3482 socket_methods,
3483 socket_doc);
3484
3485 socket_error = PyErr_NewException("socket.error", NULL, NULL);
3486 if (socket_error == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003487 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003488 Py_INCREF(socket_error);
3489 PyModule_AddObject(m, "error", socket_error);
3490 socket_herror = PyErr_NewException("socket.herror",
3491 socket_error, NULL);
3492 if (socket_herror == NULL)
3493 return;
3494 Py_INCREF(socket_herror);
3495 PyModule_AddObject(m, "herror", socket_herror);
3496 socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003497 NULL);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003498 if (socket_gaierror == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003499 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003500 Py_INCREF(socket_gaierror);
3501 PyModule_AddObject(m, "gaierror", socket_gaierror);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00003502 socket_timeout = PyErr_NewException("socket.timeout",
3503 socket_error, NULL);
3504 if (socket_timeout == NULL)
3505 return;
3506 Py_INCREF(socket_timeout);
3507 PyModule_AddObject(m, "timeout", socket_timeout);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003508 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00003509 if (PyModule_AddObject(m, "SocketType",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003510 (PyObject *)&sock_type) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003511 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003512 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00003513 if (PyModule_AddObject(m, "socket",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003514 (PyObject *)&sock_type) != 0)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003515 return;
Guido van Rossum09be4091999-08-09 14:40:40 +00003516
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003517#ifdef ENABLE_IPV6
3518 has_ipv6 = Py_True;
3519#else
3520 has_ipv6 = Py_False;
3521#endif
3522 Py_INCREF(has_ipv6);
3523 PyModule_AddObject(m, "has_ipv6", has_ipv6);
3524
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003525 /* Export C API */
Fred Drake4baedc12002-04-01 14:53:37 +00003526 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003527 PyCObject_FromVoidPtr((void *)&PySocketModuleAPI, NULL)
3528 ) != 0)
3529 return;
3530
Guido van Rossum09be4091999-08-09 14:40:40 +00003531 /* Address families (we only support AF_INET and AF_UNIX) */
3532#ifdef AF_UNSPEC
Fred Drake4baedc12002-04-01 14:53:37 +00003533 PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00003534#endif
Fred Drake4baedc12002-04-01 14:53:37 +00003535 PyModule_AddIntConstant(m, "AF_INET", AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003536#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00003537 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003538#endif /* AF_INET6 */
Andrew MacIntyre34d3e2d2003-01-02 12:45:34 +00003539#if defined(AF_UNIX) && !defined(PYOS_OS2)
Fred Drake4baedc12002-04-01 14:53:37 +00003540 PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003541#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00003542#ifdef AF_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00003543 /* Amateur Radio AX.25 */
3544 PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003545#endif
3546#ifdef AF_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00003547 PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00003548#endif
3549#ifdef AF_APPLETALK
Fred Drake4baedc12002-04-01 14:53:37 +00003550 /* Appletalk DDP */
3551 PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00003552#endif
3553#ifdef AF_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00003554 /* Amateur radio NetROM */
3555 PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00003556#endif
3557#ifdef AF_BRIDGE
Fred Drake4baedc12002-04-01 14:53:37 +00003558 /* Multiprotocol bridge */
3559 PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003560#endif
3561#ifdef AF_AAL5
Fred Drake4baedc12002-04-01 14:53:37 +00003562 /* Reserved for Werner's ATM */
3563 PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00003564#endif
3565#ifdef AF_X25
Fred Drake4baedc12002-04-01 14:53:37 +00003566 /* Reserved for X.25 project */
3567 PyModule_AddIntConstant(m, "AF_X25", AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003568#endif
3569#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00003570 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00003571#endif
3572#ifdef AF_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00003573 /* Amateur Radio X.25 PLP */
3574 PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003575#endif
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00003576#ifdef HAVE_NETPACKET_PACKET_H
Fred Drake4baedc12002-04-01 14:53:37 +00003577 PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET);
3578 PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET);
3579 PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST);
3580 PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST);
3581 PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST);
3582 PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST);
3583 PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING);
3584 PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK);
3585 PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00003586#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003587
3588 /* Socket types */
Fred Drake4baedc12002-04-01 14:53:37 +00003589 PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
3590 PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00003591#ifndef __BEOS__
3592/* We have incomplete socket support. */
Fred Drake4baedc12002-04-01 14:53:37 +00003593 PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
3594 PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003595#if defined(SOCK_RDM)
Fred Drake4baedc12002-04-01 14:53:37 +00003596 PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00003597#endif
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003598#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003599
3600#ifdef SO_DEBUG
Fred Drake4baedc12002-04-01 14:53:37 +00003601 PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003602#endif
3603#ifdef SO_ACCEPTCONN
Fred Drake4baedc12002-04-01 14:53:37 +00003604 PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003605#endif
3606#ifdef SO_REUSEADDR
Fred Drake4baedc12002-04-01 14:53:37 +00003607 PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003608#endif
3609#ifdef SO_KEEPALIVE
Fred Drake4baedc12002-04-01 14:53:37 +00003610 PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003611#endif
3612#ifdef SO_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00003613 PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003614#endif
3615#ifdef SO_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00003616 PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003617#endif
3618#ifdef SO_USELOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00003619 PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003620#endif
3621#ifdef SO_LINGER
Fred Drake4baedc12002-04-01 14:53:37 +00003622 PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003623#endif
3624#ifdef SO_OOBINLINE
Fred Drake4baedc12002-04-01 14:53:37 +00003625 PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003626#endif
3627#ifdef SO_REUSEPORT
Fred Drake4baedc12002-04-01 14:53:37 +00003628 PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003629#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003630#ifdef SO_SNDBUF
Fred Drake4baedc12002-04-01 14:53:37 +00003631 PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003632#endif
3633#ifdef SO_RCVBUF
Fred Drake4baedc12002-04-01 14:53:37 +00003634 PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003635#endif
3636#ifdef SO_SNDLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00003637 PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003638#endif
3639#ifdef SO_RCVLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00003640 PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003641#endif
3642#ifdef SO_SNDTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00003643 PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003644#endif
3645#ifdef SO_RCVTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00003646 PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003647#endif
3648#ifdef SO_ERROR
Fred Drake4baedc12002-04-01 14:53:37 +00003649 PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003650#endif
3651#ifdef SO_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00003652 PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003653#endif
3654
3655 /* Maximum number of connections for "listen" */
3656#ifdef SOMAXCONN
Fred Drake4baedc12002-04-01 14:53:37 +00003657 PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003658#else
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003659 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003660#endif
3661
3662 /* Flags for send, recv */
3663#ifdef MSG_OOB
Fred Drake4baedc12002-04-01 14:53:37 +00003664 PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003665#endif
3666#ifdef MSG_PEEK
Fred Drake4baedc12002-04-01 14:53:37 +00003667 PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003668#endif
3669#ifdef MSG_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00003670 PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003671#endif
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00003672#ifdef MSG_DONTWAIT
Fred Drake4baedc12002-04-01 14:53:37 +00003673 PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00003674#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003675#ifdef MSG_EOR
Fred Drake4baedc12002-04-01 14:53:37 +00003676 PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003677#endif
3678#ifdef MSG_TRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00003679 PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003680#endif
3681#ifdef MSG_CTRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00003682 PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003683#endif
3684#ifdef MSG_WAITALL
Fred Drake4baedc12002-04-01 14:53:37 +00003685 PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003686#endif
3687#ifdef MSG_BTAG
Fred Drake4baedc12002-04-01 14:53:37 +00003688 PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003689#endif
3690#ifdef MSG_ETAG
Fred Drake4baedc12002-04-01 14:53:37 +00003691 PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003692#endif
3693
3694 /* Protocol level and numbers, usable for [gs]etsockopt */
3695#ifdef SOL_SOCKET
Fred Drake4baedc12002-04-01 14:53:37 +00003696 PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003697#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003698#ifdef SOL_IP
Fred Drake4baedc12002-04-01 14:53:37 +00003699 PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003700#else
Fred Drake4baedc12002-04-01 14:53:37 +00003701 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00003702#endif
3703#ifdef SOL_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00003704 PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00003705#endif
3706#ifdef SOL_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00003707 PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003708#endif
3709#ifdef SOL_ATALK
Fred Drake4baedc12002-04-01 14:53:37 +00003710 PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00003711#endif
3712#ifdef SOL_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00003713 PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00003714#endif
3715#ifdef SOL_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00003716 PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003717#endif
3718#ifdef SOL_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00003719 PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003720#else
Fred Drake4baedc12002-04-01 14:53:37 +00003721 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00003722#endif
3723#ifdef SOL_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00003724 PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003725#else
Fred Drake4baedc12002-04-01 14:53:37 +00003726 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00003727#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003728#ifdef IPPROTO_IP
Fred Drake4baedc12002-04-01 14:53:37 +00003729 PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00003730#else
Fred Drake4baedc12002-04-01 14:53:37 +00003731 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003732#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003733#ifdef IPPROTO_HOPOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003734 PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003735#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003736#ifdef IPPROTO_ICMP
Fred Drake4baedc12002-04-01 14:53:37 +00003737 PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00003738#else
Fred Drake4baedc12002-04-01 14:53:37 +00003739 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003740#endif
3741#ifdef IPPROTO_IGMP
Fred Drake4baedc12002-04-01 14:53:37 +00003742 PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003743#endif
3744#ifdef IPPROTO_GGP
Fred Drake4baedc12002-04-01 14:53:37 +00003745 PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003746#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003747#ifdef IPPROTO_IPV4
Fred Drake4baedc12002-04-01 14:53:37 +00003748 PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003749#endif
Martin v. Löwisa0f17342003-10-03 13:56:20 +00003750#ifdef IPPROTO_IPV6
3751 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
3752#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003753#ifdef IPPROTO_IPIP
Fred Drake4baedc12002-04-01 14:53:37 +00003754 PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003755#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003756#ifdef IPPROTO_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00003757 PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00003758#else
Fred Drake4baedc12002-04-01 14:53:37 +00003759 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003760#endif
3761#ifdef IPPROTO_EGP
Fred Drake4baedc12002-04-01 14:53:37 +00003762 PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003763#endif
3764#ifdef IPPROTO_PUP
Fred Drake4baedc12002-04-01 14:53:37 +00003765 PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003766#endif
3767#ifdef IPPROTO_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00003768 PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00003769#else
Fred Drake4baedc12002-04-01 14:53:37 +00003770 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003771#endif
3772#ifdef IPPROTO_IDP
Fred Drake4baedc12002-04-01 14:53:37 +00003773 PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003774#endif
3775#ifdef IPPROTO_HELLO
Fred Drake4baedc12002-04-01 14:53:37 +00003776 PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003777#endif
3778#ifdef IPPROTO_ND
Fred Drake4baedc12002-04-01 14:53:37 +00003779 PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003780#endif
3781#ifdef IPPROTO_TP
Fred Drake4baedc12002-04-01 14:53:37 +00003782 PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003783#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003784#ifdef IPPROTO_IPV6
Fred Drake4baedc12002-04-01 14:53:37 +00003785 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003786#endif
3787#ifdef IPPROTO_ROUTING
Fred Drake4baedc12002-04-01 14:53:37 +00003788 PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003789#endif
3790#ifdef IPPROTO_FRAGMENT
Fred Drake4baedc12002-04-01 14:53:37 +00003791 PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003792#endif
3793#ifdef IPPROTO_RSVP
Fred Drake4baedc12002-04-01 14:53:37 +00003794 PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003795#endif
3796#ifdef IPPROTO_GRE
Fred Drake4baedc12002-04-01 14:53:37 +00003797 PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003798#endif
3799#ifdef IPPROTO_ESP
Fred Drake4baedc12002-04-01 14:53:37 +00003800 PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003801#endif
3802#ifdef IPPROTO_AH
Fred Drake4baedc12002-04-01 14:53:37 +00003803 PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003804#endif
3805#ifdef IPPROTO_MOBILE
Fred Drake4baedc12002-04-01 14:53:37 +00003806 PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003807#endif
3808#ifdef IPPROTO_ICMPV6
Fred Drake4baedc12002-04-01 14:53:37 +00003809 PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003810#endif
3811#ifdef IPPROTO_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00003812 PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003813#endif
3814#ifdef IPPROTO_DSTOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003815 PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003816#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003817#ifdef IPPROTO_XTP
Fred Drake4baedc12002-04-01 14:53:37 +00003818 PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003819#endif
3820#ifdef IPPROTO_EON
Fred Drake4baedc12002-04-01 14:53:37 +00003821 PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003822#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003823#ifdef IPPROTO_PIM
Fred Drake4baedc12002-04-01 14:53:37 +00003824 PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003825#endif
3826#ifdef IPPROTO_IPCOMP
Fred Drake4baedc12002-04-01 14:53:37 +00003827 PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003828#endif
3829#ifdef IPPROTO_VRRP
Fred Drake4baedc12002-04-01 14:53:37 +00003830 PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003831#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003832#ifdef IPPROTO_BIP
Fred Drake4baedc12002-04-01 14:53:37 +00003833 PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003834#endif
3835/**/
3836#ifdef IPPROTO_RAW
Fred Drake4baedc12002-04-01 14:53:37 +00003837 PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00003838#else
Fred Drake4baedc12002-04-01 14:53:37 +00003839 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003840#endif
3841#ifdef IPPROTO_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00003842 PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003843#endif
3844
3845 /* Some port configuration */
3846#ifdef IPPORT_RESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00003847 PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003848#else
Fred Drake4baedc12002-04-01 14:53:37 +00003849 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003850#endif
3851#ifdef IPPORT_USERRESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00003852 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003853#else
Fred Drake4baedc12002-04-01 14:53:37 +00003854 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003855#endif
3856
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003857 /* Some reserved IP v.4 addresses */
3858#ifdef INADDR_ANY
Fred Drake4baedc12002-04-01 14:53:37 +00003859 PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003860#else
Fred Drake4baedc12002-04-01 14:53:37 +00003861 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003862#endif
3863#ifdef INADDR_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00003864 PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003865#else
Fred Drake4baedc12002-04-01 14:53:37 +00003866 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003867#endif
3868#ifdef INADDR_LOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00003869 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003870#else
Fred Drake4baedc12002-04-01 14:53:37 +00003871 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003872#endif
3873#ifdef INADDR_UNSPEC_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003874 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003875#else
Fred Drake4baedc12002-04-01 14:53:37 +00003876 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003877#endif
3878#ifdef INADDR_ALLHOSTS_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003879 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
3880 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003881#else
Fred Drake4baedc12002-04-01 14:53:37 +00003882 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003883#endif
3884#ifdef INADDR_MAX_LOCAL_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003885 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
3886 INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003887#else
Fred Drake4baedc12002-04-01 14:53:37 +00003888 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003889#endif
3890#ifdef INADDR_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00003891 PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003892#else
Fred Drake4baedc12002-04-01 14:53:37 +00003893 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003894#endif
3895
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003896 /* IPv4 [gs]etsockopt options */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003897#ifdef IP_OPTIONS
Fred Drake4baedc12002-04-01 14:53:37 +00003898 PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003899#endif
3900#ifdef IP_HDRINCL
Fred Drake4baedc12002-04-01 14:53:37 +00003901 PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003902#endif
3903#ifdef IP_TOS
Fred Drake4baedc12002-04-01 14:53:37 +00003904 PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003905#endif
3906#ifdef IP_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00003907 PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003908#endif
3909#ifdef IP_RECVOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003910 PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003911#endif
3912#ifdef IP_RECVRETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003913 PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003914#endif
3915#ifdef IP_RECVDSTADDR
Fred Drake4baedc12002-04-01 14:53:37 +00003916 PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003917#endif
3918#ifdef IP_RETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003919 PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003920#endif
3921#ifdef IP_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00003922 PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003923#endif
3924#ifdef IP_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00003925 PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003926#endif
3927#ifdef IP_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00003928 PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003929#endif
3930#ifdef IP_ADD_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00003931 PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003932#endif
3933#ifdef IP_DROP_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00003934 PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003935#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003936#ifdef IP_DEFAULT_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00003937 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
3938 IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00003939#endif
3940#ifdef IP_DEFAULT_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00003941 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
3942 IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003943#endif
3944#ifdef IP_MAX_MEMBERSHIPS
Fred Drake4baedc12002-04-01 14:53:37 +00003945 PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00003946#endif
3947
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003948 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
3949#ifdef IPV6_JOIN_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003950 PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003951#endif
3952#ifdef IPV6_LEAVE_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003953 PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003954#endif
3955#ifdef IPV6_MULTICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00003956 PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003957#endif
3958#ifdef IPV6_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00003959 PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003960#endif
3961#ifdef IPV6_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00003962 PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003963#endif
3964#ifdef IPV6_UNICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00003965 PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003966#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00003967 /* Additional IPV6 socket options, defined in RFC 3493 */
3968#ifdef IPV6_V6ONLY
3969 PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY);
3970#endif
3971 /* Advanced IPV6 socket options, from RFC 3542 */
3972#ifdef IPV6_CHECKSUM
3973 PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM);
3974#endif
3975#ifdef IPV6_DONTFRAG
3976 PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG);
3977#endif
3978#ifdef IPV6_DSTOPTS
3979 PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS);
3980#endif
3981#ifdef IPV6_HOPLIMIT
3982 PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT);
3983#endif
3984#ifdef IPV6_HOPOPTS
3985 PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS);
3986#endif
3987#ifdef IPV6_NEXTHOP
3988 PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP);
3989#endif
3990#ifdef IPV6_PATHMTU
3991 PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU);
3992#endif
3993#ifdef IPV6_PKTINFO
3994 PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO);
3995#endif
3996#ifdef IPV6_RECVDSTOPTS
3997 PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS);
3998#endif
3999#ifdef IPV6_RECVHOPLIMIT
4000 PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT);
4001#endif
4002#ifdef IPV6_RECVHOPOPTS
4003 PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS);
4004#endif
4005#ifdef IPV6_RECVPKTINFO
4006 PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO);
4007#endif
4008#ifdef IPV6_RECVRTHDR
4009 PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR);
4010#endif
4011#ifdef IPV6_RECVTCLASS
4012 PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS);
4013#endif
4014#ifdef IPV6_RTHDR
4015 PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR);
4016#endif
4017#ifdef IPV6_RTHDRDSTOPTS
4018 PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS);
4019#endif
4020#ifdef IPV6_RTHDR_TYPE_0
4021 PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0);
4022#endif
4023#ifdef IPV6_RECVPATHMTU
4024 PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU);
4025#endif
4026#ifdef IPV6_TCLASS
4027 PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS);
4028#endif
4029#ifdef IPV6_USE_MIN_MTU
4030 PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU);
4031#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004032
Guido van Rossum09be4091999-08-09 14:40:40 +00004033 /* TCP options */
4034#ifdef TCP_NODELAY
Fred Drake4baedc12002-04-01 14:53:37 +00004035 PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00004036#endif
4037#ifdef TCP_MAXSEG
Fred Drake4baedc12002-04-01 14:53:37 +00004038 PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00004039#endif
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004040#ifdef TCP_CORK
Fred Drake4baedc12002-04-01 14:53:37 +00004041 PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004042#endif
4043#ifdef TCP_KEEPIDLE
Fred Drake4baedc12002-04-01 14:53:37 +00004044 PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004045#endif
4046#ifdef TCP_KEEPINTVL
Fred Drake4baedc12002-04-01 14:53:37 +00004047 PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004048#endif
4049#ifdef TCP_KEEPCNT
Fred Drake4baedc12002-04-01 14:53:37 +00004050 PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004051#endif
4052#ifdef TCP_SYNCNT
Fred Drake4baedc12002-04-01 14:53:37 +00004053 PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004054#endif
4055#ifdef TCP_LINGER2
Fred Drake4baedc12002-04-01 14:53:37 +00004056 PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004057#endif
4058#ifdef TCP_DEFER_ACCEPT
Fred Drake4baedc12002-04-01 14:53:37 +00004059 PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004060#endif
4061#ifdef TCP_WINDOW_CLAMP
Fred Drake4baedc12002-04-01 14:53:37 +00004062 PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004063#endif
4064#ifdef TCP_INFO
Fred Drake4baedc12002-04-01 14:53:37 +00004065 PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004066#endif
4067#ifdef TCP_QUICKACK
Fred Drake4baedc12002-04-01 14:53:37 +00004068 PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004069#endif
4070
Guido van Rossum09be4091999-08-09 14:40:40 +00004071
4072 /* IPX options */
4073#ifdef IPX_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004074 PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004075#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004076
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004077 /* get{addr,name}info parameters */
4078#ifdef EAI_ADDRFAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004079 PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004080#endif
4081#ifdef EAI_AGAIN
Fred Drake4baedc12002-04-01 14:53:37 +00004082 PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004083#endif
4084#ifdef EAI_BADFLAGS
Fred Drake4baedc12002-04-01 14:53:37 +00004085 PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004086#endif
4087#ifdef EAI_FAIL
Fred Drake4baedc12002-04-01 14:53:37 +00004088 PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004089#endif
4090#ifdef EAI_FAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004091 PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004092#endif
4093#ifdef EAI_MEMORY
Fred Drake4baedc12002-04-01 14:53:37 +00004094 PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004095#endif
4096#ifdef EAI_NODATA
Fred Drake4baedc12002-04-01 14:53:37 +00004097 PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004098#endif
4099#ifdef EAI_NONAME
Fred Drake4baedc12002-04-01 14:53:37 +00004100 PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004101#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004102#ifdef EAI_OVERFLOW
4103 PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW);
4104#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004105#ifdef EAI_SERVICE
Fred Drake4baedc12002-04-01 14:53:37 +00004106 PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004107#endif
4108#ifdef EAI_SOCKTYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004109 PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004110#endif
4111#ifdef EAI_SYSTEM
Fred Drake4baedc12002-04-01 14:53:37 +00004112 PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004113#endif
4114#ifdef EAI_BADHINTS
Fred Drake4baedc12002-04-01 14:53:37 +00004115 PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004116#endif
4117#ifdef EAI_PROTOCOL
Fred Drake4baedc12002-04-01 14:53:37 +00004118 PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004119#endif
4120#ifdef EAI_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00004121 PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004122#endif
4123#ifdef AI_PASSIVE
Fred Drake4baedc12002-04-01 14:53:37 +00004124 PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004125#endif
4126#ifdef AI_CANONNAME
Fred Drake4baedc12002-04-01 14:53:37 +00004127 PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004128#endif
4129#ifdef AI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004130 PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004131#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004132#ifdef AI_NUMERICSERV
4133 PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV);
4134#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004135#ifdef AI_MASK
Fred Drake4baedc12002-04-01 14:53:37 +00004136 PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004137#endif
4138#ifdef AI_ALL
Fred Drake4baedc12002-04-01 14:53:37 +00004139 PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004140#endif
4141#ifdef AI_V4MAPPED_CFG
Fred Drake4baedc12002-04-01 14:53:37 +00004142 PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004143#endif
4144#ifdef AI_ADDRCONFIG
Fred Drake4baedc12002-04-01 14:53:37 +00004145 PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004146#endif
4147#ifdef AI_V4MAPPED
Fred Drake4baedc12002-04-01 14:53:37 +00004148 PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004149#endif
4150#ifdef AI_DEFAULT
Fred Drake4baedc12002-04-01 14:53:37 +00004151 PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004152#endif
4153#ifdef NI_MAXHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004154 PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004155#endif
4156#ifdef NI_MAXSERV
Fred Drake4baedc12002-04-01 14:53:37 +00004157 PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004158#endif
4159#ifdef NI_NOFQDN
Fred Drake4baedc12002-04-01 14:53:37 +00004160 PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004161#endif
4162#ifdef NI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004163 PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004164#endif
4165#ifdef NI_NAMEREQD
Fred Drake4baedc12002-04-01 14:53:37 +00004166 PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004167#endif
4168#ifdef NI_NUMERICSERV
Fred Drake4baedc12002-04-01 14:53:37 +00004169 PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004170#endif
4171#ifdef NI_DGRAM
Fred Drake4baedc12002-04-01 14:53:37 +00004172 PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004173#endif
4174
Martin v. Löwis94681fc2003-11-27 19:40:22 +00004175 /* shutdown() parameters */
4176#ifdef SHUT_RD
4177 PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD);
4178#elif defined(SD_RECEIVE)
4179 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
4180#else
4181 PyModule_AddIntConstant(m, "SHUT_RD", 0);
4182#endif
4183#ifdef SHUT_WR
4184 PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR);
4185#elif defined(SD_SEND)
4186 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
4187#else
4188 PyModule_AddIntConstant(m, "SHUT_WR", 1);
4189#endif
4190#ifdef SHUT_RDWR
4191 PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR);
4192#elif defined(SD_BOTH)
4193 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
4194#else
4195 PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
4196#endif
4197
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004198 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00004199#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
4200 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004201#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004202}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004203
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004204
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004205#ifndef HAVE_INET_PTON
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004206
4207/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004208/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004209
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004210int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004211inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004212{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004213 if (af == AF_INET) {
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004214 long packed_addr;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004215 packed_addr = inet_addr(src);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004216 if (packed_addr == INADDR_NONE)
4217 return 0;
4218 memcpy(dst, &packed_addr, 4);
4219 return 1;
4220 }
4221 /* Should set errno to EAFNOSUPPORT */
4222 return -1;
4223}
4224
Martin v. Löwisc925b1532001-07-21 09:42:15 +00004225const char *
4226inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004227{
4228 if (af == AF_INET) {
4229 struct in_addr packed_addr;
4230 if (size < 16)
4231 /* Should set errno to ENOSPC. */
4232 return NULL;
4233 memcpy(&packed_addr, src, sizeof(packed_addr));
4234 return strncpy(dst, inet_ntoa(packed_addr), size);
4235 }
4236 /* Should set errno to EAFNOSUPPORT */
4237 return NULL;
4238}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004239
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004240#endif