blob: 4a2fb583e8c3047f22bc280a814b8754de1405fc [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
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000108/* XXX This is a terrible mess of of platform-dependent preprocessor hacks.
109 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 }
Jack Jansen6d8898b2002-03-25 15:32:01 +00001742#ifdef USE_GUSI2
1743 /* Workaround for bug in Metrowerks MSL vs. GUSI I/O library */
Guido van Rossum67f7a382002-06-06 21:08:16 +00001744 if (strchr(mode, 'b') != NULL)
Jack Jansen6d8898b2002-03-25 15:32:01 +00001745 bufsize = 0;
1746#endif
Guido van Rossum6b144911995-03-14 15:05:13 +00001747 f = PyFile_FromFile(fp, "<socket>", mode, fclose);
1748 if (f != NULL)
1749 PyFile_SetBufSize(f, bufsize);
1750 return f;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001751}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001752
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001753PyDoc_STRVAR(makefile_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001754"makefile([mode[, buffersize]]) -> file object\n\
1755\n\
1756Return a regular file object corresponding to the socket.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001757The mode and buffersize arguments are as for the built-in open() function.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001758
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001759#endif /* NO_DUP */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001760
Guido van Rossum48a680c2001-03-02 06:34:14 +00001761
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001762/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001763
Guido van Rossum73624e91994-10-10 17:59:00 +00001764static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001765sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001766{
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001767 int len, n = 0, flags = 0, timeout;
Guido van Rossum73624e91994-10-10 17:59:00 +00001768 PyObject *buf;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001769#ifdef __VMS
1770 int read_length;
1771 char *read_buf;
1772#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00001773
Guido van Rossum43713e52000-02-29 13:59:29 +00001774 if (!PyArg_ParseTuple(args, "i|i:recv", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001775 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001776
1777 if (len < 0) {
Martin v. Löwisfba64e12001-11-19 10:41:26 +00001778 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00001779 "negative buffersize in recv");
Martin v. Löwisfba64e12001-11-19 10:41:26 +00001780 return NULL;
1781 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00001782
Guido van Rossum73624e91994-10-10 17:59:00 +00001783 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001784 if (buf == NULL)
1785 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001786
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001787#ifndef __VMS
Guido van Rossum73624e91994-10-10 17:59:00 +00001788 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001789 timeout = internal_select(s, 0);
1790 if (!timeout)
1791 n = recv(s->sock_fd, PyString_AS_STRING(buf), len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001792 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001793
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001794 if (timeout) {
1795 Py_DECREF(buf);
1796 PyErr_SetString(socket_timeout, "timed out");
1797 return NULL;
1798 }
Guido van Rossum7c53b771995-09-13 18:39:47 +00001799 if (n < 0) {
1800 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001801 return s->errorhandler();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001802 }
Tim Peters5de98422002-04-27 18:44:32 +00001803 if (n != len)
1804 _PyString_Resize(&buf, n);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001805#else
1806 read_buf = PyString_AsString(buf);
1807 read_length = len;
1808 while (read_length != 0) {
1809 unsigned int segment;
1810
1811 segment = read_length /SEGMENT_SIZE;
1812 if (segment != 0) {
1813 segment = SEGMENT_SIZE;
1814 }
1815 else {
1816 segment = read_length;
1817 }
1818
1819 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001820 timeout = internal_select(s, 0);
1821 if (!timeout)
1822 n = recv(s->sock_fd, read_buf, segment, flags);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001823 Py_END_ALLOW_THREADS
1824
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001825 if (timeout) {
1826 Py_DECREF(buf);
1827 PyErr_SetString(socket_timeout, "timed out");
1828 return NULL;
1829 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001830 if (n < 0) {
1831 Py_DECREF(buf);
1832 return s->errorhandler();
1833 }
1834 if (n != read_length) {
1835 read_buf += n;
1836 break;
1837 }
1838
1839 read_length -= segment;
1840 read_buf += segment;
1841 }
1842 if (_PyString_Resize(&buf, (read_buf - PyString_AsString(buf))) < 0)
1843 {
1844 return NULL;
1845 }
1846#endif /* !__VMS */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001847 return buf;
1848}
1849
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001850PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001851"recv(buffersize[, flags]) -> data\n\
1852\n\
1853Receive up to buffersize bytes from the socket. For the optional flags\n\
1854argument, see the Unix manual. When no data is available, block until\n\
1855at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001856the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001857
Guido van Rossum30a685f1991-06-27 15:51:29 +00001858
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001859/* s.recvfrom(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001860
Guido van Rossum73624e91994-10-10 17:59:00 +00001861static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001862sock_recvfrom(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001863{
1864 char addrbuf[256];
Barry Warsaw752300b1997-01-03 17:18:10 +00001865 PyObject *buf = NULL;
1866 PyObject *addr = NULL;
1867 PyObject *ret = NULL;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001868 int len, n = 0, flags = 0, timeout;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001869 socklen_t addrlen;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001870
Guido van Rossum43713e52000-02-29 13:59:29 +00001871 if (!PyArg_ParseTuple(args, "i|i:recvfrom", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001872 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001873
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00001874 if (!getsockaddrlen(s, &addrlen))
1875 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001876 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00001877 if (buf == NULL)
1878 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001879
Guido van Rossum73624e91994-10-10 17:59:00 +00001880 Py_BEGIN_ALLOW_THREADS
Fred Drakefd168342001-05-09 19:11:33 +00001881 memset(addrbuf, 0, addrlen);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001882 timeout = internal_select(s, 0);
1883 if (!timeout)
1884 n = recvfrom(s->sock_fd, PyString_AS_STRING(buf), len, flags,
Guido van Rossum8d665e61996-06-26 18:22:49 +00001885#ifndef MS_WINDOWS
Andrew MacIntyreba43e872002-03-03 03:03:52 +00001886#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001887 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00001888#else
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001889 (void *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00001890#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001891#else
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001892 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001893#endif
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001894 );
Guido van Rossum73624e91994-10-10 17:59:00 +00001895 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001896
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001897 if (timeout) {
1898 Py_DECREF(buf);
1899 PyErr_SetString(socket_timeout, "timed out");
1900 return NULL;
1901 }
Guido van Rossum7c53b771995-09-13 18:39:47 +00001902 if (n < 0) {
1903 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001904 return s->errorhandler();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001905 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00001906
Guido van Rossum53a9bf81996-06-11 18:35:24 +00001907 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001908 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001909
Guido van Rossum67f7a382002-06-06 21:08:16 +00001910 if (!(addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001911 addrlen)))
Barry Warsaw752300b1997-01-03 17:18:10 +00001912 goto finally;
1913
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001914 ret = PyTuple_Pack(2, buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001915
1916finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001917 Py_XDECREF(addr);
1918 Py_XDECREF(buf);
Guido van Rossum6f5afc91993-02-05 09:46:15 +00001919 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001920}
1921
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001922PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001923"recvfrom(buffersize[, flags]) -> (data, address info)\n\
1924\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001925Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001926
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001927/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001928
Guido van Rossum73624e91994-10-10 17:59:00 +00001929static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001930sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001931{
Guido van Rossumff4949e1992-08-05 19:58:53 +00001932 char *buf;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001933 int len, n = 0, flags = 0, timeout;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001934#ifdef __VMS
1935 int send_length;
1936#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00001937
Guido van Rossum43713e52000-02-29 13:59:29 +00001938 if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001939 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001940
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001941#ifndef __VMS
Guido van Rossum73624e91994-10-10 17:59:00 +00001942 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001943 timeout = internal_select(s, 1);
1944 if (!timeout)
1945 n = send(s->sock_fd, buf, len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001946 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001947
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001948 if (timeout) {
1949 PyErr_SetString(socket_timeout, "timed out");
1950 return NULL;
1951 }
Guido van Rossum30a685f1991-06-27 15:51:29 +00001952 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001953 return s->errorhandler();
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001954#else
1955 /* Divide packet into smaller segments for */
1956 /* TCP/IP Services for OpenVMS */
1957 send_length = len;
1958 while (send_length != 0) {
1959 unsigned int segment;
1960
1961 segment = send_length / SEGMENT_SIZE;
1962 if (segment != 0) {
1963 segment = SEGMENT_SIZE;
1964 }
1965 else {
1966 segment = send_length;
1967 }
1968 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001969 timeout = internal_select(s, 1);
1970 if (!timeout)
1971 n = send(s->sock_fd, buf, segment, flags);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001972 Py_END_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001973 if (timeout) {
1974 PyErr_SetString(socket_timeout, "timed out");
1975 return NULL;
1976 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001977 if (n < 0) {
1978 return s->errorhandler();
1979 }
1980 send_length -= segment;
1981 buf += segment;
1982 } /* end while */
1983#endif /* !__VMS */
Guido van Rossum73624e91994-10-10 17:59:00 +00001984 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00001985}
1986
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001987PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001988"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001989\n\
1990Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001991argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001992sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001993
1994
1995/* s.sendall(data [,flags]) method */
1996
1997static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001998sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001999{
2000 char *buf;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002001 int len, n = 0, flags = 0, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002002
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002003 if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags))
2004 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002005
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002006 Py_BEGIN_ALLOW_THREADS
2007 do {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002008 timeout = internal_select(s, 1);
2009 if (timeout)
2010 break;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002011 n = send(s->sock_fd, buf, len, flags);
2012 if (n < 0)
2013 break;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002014 buf += n;
2015 len -= n;
2016 } while (len > 0);
2017 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002018
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002019 if (timeout) {
2020 PyErr_SetString(socket_timeout, "timed out");
2021 return NULL;
2022 }
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002023 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002024 return s->errorhandler();
Guido van Rossum67f7a382002-06-06 21:08:16 +00002025
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002026 Py_INCREF(Py_None);
2027 return Py_None;
2028}
2029
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002030PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002031"sendall(data[, flags])\n\
2032\n\
2033Send a data string to the socket. For the optional flags\n\
2034argument, see the Unix manual. This calls send() repeatedly\n\
2035until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002036to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002037
Guido van Rossum30a685f1991-06-27 15:51:29 +00002038
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002039/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002040
Guido van Rossum73624e91994-10-10 17:59:00 +00002041static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002042sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002043{
Guido van Rossum73624e91994-10-10 17:59:00 +00002044 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002045 char *buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002046 struct sockaddr *addr;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002047 int addrlen, len, n = 0, flags, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002048
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002049 flags = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002050 if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) {
Guido van Rossum73624e91994-10-10 17:59:00 +00002051 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002052 if (!PyArg_ParseTuple(args, "s#iO:sendto",
2053 &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002054 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002055 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002056
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002057 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002058 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002059
Guido van Rossum73624e91994-10-10 17:59:00 +00002060 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002061 timeout = internal_select(s, 1);
2062 if (!timeout)
2063 n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002064 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002065
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002066 if (timeout) {
2067 PyErr_SetString(socket_timeout, "timed out");
2068 return NULL;
2069 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002070 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002071 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002072 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002073}
2074
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002075PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002076"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002077\n\
2078Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002079For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002080
Guido van Rossum30a685f1991-06-27 15:51:29 +00002081
2082/* s.shutdown(how) method */
2083
Guido van Rossum73624e91994-10-10 17:59:00 +00002084static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002085sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002086{
2087 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002088 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002089
2090 how = PyInt_AsLong(arg);
2091 if (how == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002092 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002093 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002094 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +00002095 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002096 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002097 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002098 Py_INCREF(Py_None);
2099 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002100}
2101
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002102PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002103"shutdown(flag)\n\
2104\n\
2105Shut down the reading side of the socket (flag == 0), the writing side\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002106of the socket (flag == 1), or both ends (flag == 2).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002107
Guido van Rossum30a685f1991-06-27 15:51:29 +00002108
2109/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002110
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002111static PyMethodDef sock_methods[] = {
2112 {"accept", (PyCFunction)sock_accept, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002113 accept_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002114 {"bind", (PyCFunction)sock_bind, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002115 bind_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002116 {"close", (PyCFunction)sock_close, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002117 close_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002118 {"connect", (PyCFunction)sock_connect, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002119 connect_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002120 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002121 connect_ex_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00002122#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002123 {"dup", (PyCFunction)sock_dup, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002124 dup_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00002125#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002126 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002127 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002128#ifdef HAVE_GETPEERNAME
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002129 {"getpeername", (PyCFunction)sock_getpeername,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002130 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00002131#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002132 {"getsockname", (PyCFunction)sock_getsockname,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002133 METH_NOARGS, getsockname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002134 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002135 getsockopt_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002136 {"listen", (PyCFunction)sock_listen, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002137 listen_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002138#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002139 {"makefile", (PyCFunction)sock_makefile, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002140 makefile_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002141#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002142 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002143 recv_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002144 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002145 recvfrom_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002146 {"send", (PyCFunction)sock_send, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002147 send_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002148 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002149 sendall_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002150 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002151 sendto_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002152 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002153 setblocking_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002154 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
Guido van Rossum67f7a382002-06-06 21:08:16 +00002155 settimeout_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002156 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
Guido van Rossum67f7a382002-06-06 21:08:16 +00002157 gettimeout_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002158 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002159 setsockopt_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002160 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002161 shutdown_doc},
2162#ifdef RISCOS
Martin v. Löwisa94568a2003-05-10 07:36:56 +00002163 {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002164 sleeptaskw_doc},
2165#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00002166 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002167};
2168
Guido van Rossum30a685f1991-06-27 15:51:29 +00002169
Guido van Rossum73624e91994-10-10 17:59:00 +00002170/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00002171 First close the file description. */
2172
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002173static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002174sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002175{
Guido van Rossumfa972c92000-04-10 12:45:45 +00002176 if (s->sock_fd != -1)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00002177 (void) SOCKETCLOSE(s->sock_fd);
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002178 s->ob_type->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002179}
2180
Guido van Rossum30a685f1991-06-27 15:51:29 +00002181
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002182static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002183sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002184{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002185 char buf[512];
Fred Drakea04eaad2000-06-30 02:46:07 +00002186#if SIZEOF_SOCKET_T > SIZEOF_LONG
2187 if (s->sock_fd > LONG_MAX) {
2188 /* this can occur on Win64, and actually there is a special
2189 ugly printf formatter for decimal pointer length integer
2190 printing, only bother if necessary*/
2191 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002192 "no printf formatter to display "
2193 "the socket descriptor in decimal");
Fred Drakea04eaad2000-06-30 02:46:07 +00002194 return NULL;
2195 }
2196#endif
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002197 PyOS_snprintf(
2198 buf, sizeof(buf),
2199 "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>",
2200 (long)s->sock_fd, s->sock_family,
2201 s->sock_type,
2202 s->sock_proto);
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00002203 return PyString_FromString(buf);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002204}
2205
2206
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002207/* Create a new, uninitialized socket object. */
2208
2209static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002210sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002211{
2212 PyObject *new;
2213
2214 new = type->tp_alloc(type, 0);
Guido van Rossume1c478f2002-06-06 20:08:25 +00002215 if (new != NULL) {
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002216 ((PySocketSockObject *)new)->sock_fd = -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002217 ((PySocketSockObject *)new)->sock_timeout = -1.0;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002218 ((PySocketSockObject *)new)->errorhandler = &set_error;
Guido van Rossume1c478f2002-06-06 20:08:25 +00002219 }
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002220 return new;
2221}
2222
2223
2224/* Initialize a new socket object. */
2225
2226/*ARGSUSED*/
2227static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002228sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002229{
2230 PySocketSockObject *s = (PySocketSockObject *)self;
2231 SOCKET_T fd;
2232 int family = AF_INET, type = SOCK_STREAM, proto = 0;
2233 static char *keywords[] = {"family", "type", "proto", 0};
2234
2235 if (!PyArg_ParseTupleAndKeywords(args, kwds,
2236 "|iii:socket", keywords,
2237 &family, &type, &proto))
2238 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002239
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002240 Py_BEGIN_ALLOW_THREADS
2241 fd = socket(family, type, proto);
2242 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002243
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002244#ifdef MS_WINDOWS
2245 if (fd == INVALID_SOCKET)
2246#else
2247 if (fd < 0)
2248#endif
2249 {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002250 set_error();
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002251 return -1;
2252 }
2253 init_sockobject(s, fd, family, type, proto);
2254 /* From now on, ignore SIGPIPE and let the error checking
2255 do the work. */
2256#ifdef SIGPIPE
2257 (void) signal(SIGPIPE, SIG_IGN);
2258#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002259
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002260 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002261
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002262}
2263
2264
Guido van Rossumb6775db1994-08-01 11:34:53 +00002265/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002266
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002267static PyTypeObject sock_type = {
Guido van Rossum55558541996-05-23 22:54:50 +00002268 PyObject_HEAD_INIT(0) /* Must fill in type value later */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002269 0, /* ob_size */
Guido van Rossumca6dfa52001-10-28 12:31:33 +00002270 "_socket.socket", /* tp_name */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002271 sizeof(PySocketSockObject), /* tp_basicsize */
2272 0, /* tp_itemsize */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002273 (destructor)sock_dealloc, /* tp_dealloc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002274 0, /* tp_print */
2275 0, /* tp_getattr */
2276 0, /* tp_setattr */
2277 0, /* tp_compare */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002278 (reprfunc)sock_repr, /* tp_repr */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002279 0, /* tp_as_number */
2280 0, /* tp_as_sequence */
2281 0, /* tp_as_mapping */
2282 0, /* tp_hash */
2283 0, /* tp_call */
2284 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002285 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002286 0, /* tp_setattro */
2287 0, /* tp_as_buffer */
2288 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002289 sock_doc, /* tp_doc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002290 0, /* tp_traverse */
2291 0, /* tp_clear */
2292 0, /* tp_richcompare */
2293 0, /* tp_weaklistoffset */
2294 0, /* tp_iter */
2295 0, /* tp_iternext */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002296 sock_methods, /* tp_methods */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002297 0, /* tp_members */
2298 0, /* tp_getset */
2299 0, /* tp_base */
2300 0, /* tp_dict */
2301 0, /* tp_descr_get */
2302 0, /* tp_descr_set */
2303 0, /* tp_dictoffset */
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002304 sock_initobj, /* tp_init */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002305 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002306 sock_new, /* tp_new */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002307 PyObject_Del, /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002308};
2309
Guido van Rossum30a685f1991-06-27 15:51:29 +00002310
Guido van Rossum81194471991-07-27 21:42:02 +00002311/* Python interface to gethostname(). */
2312
2313/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002314static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002315socket_gethostname(PyObject *self, PyObject *args)
Guido van Rossum81194471991-07-27 21:42:02 +00002316{
2317 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00002318 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002319 if (!PyArg_ParseTuple(args, ":gethostname"))
Guido van Rossum81194471991-07-27 21:42:02 +00002320 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002321 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002322 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00002323 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002324 if (res < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002325 return set_error();
Guido van Rossum81194471991-07-27 21:42:02 +00002326 buf[sizeof buf - 1] = '\0';
Guido van Rossum73624e91994-10-10 17:59:00 +00002327 return PyString_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00002328}
Guido van Rossumff4949e1992-08-05 19:58:53 +00002329
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002330PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002331"gethostname() -> string\n\
2332\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002333Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002334
Guido van Rossumff4949e1992-08-05 19:58:53 +00002335
Guido van Rossum30a685f1991-06-27 15:51:29 +00002336/* Python interface to gethostbyname(name). */
2337
2338/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002339static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002340socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002341{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002342 char *name;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00002343#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002344 struct sockaddr_storage addrbuf;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00002345#else
2346 struct sockaddr_in addrbuf;
2347#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002348
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002349 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002350 return NULL;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002351 if (setipaddr(name, (struct sockaddr *)&addrbuf, sizeof(addrbuf), AF_INET) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002352 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002353 return makeipaddr((struct sockaddr *)&addrbuf,
2354 sizeof(struct sockaddr_in));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002355}
2356
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002357PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002358"gethostbyname(host) -> address\n\
2359\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002360Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002361
2362
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002363/* Convenience function common to gethostbyname_ex and gethostbyaddr */
2364
2365static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002366gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002367{
2368 char **pch;
2369 PyObject *rtn_tuple = (PyObject *)NULL;
2370 PyObject *name_list = (PyObject *)NULL;
2371 PyObject *addr_list = (PyObject *)NULL;
2372 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002373
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002374 if (h == NULL) {
Guido van Rossum48a680c2001-03-02 06:34:14 +00002375 /* Let's get real error message to return */
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002376#ifndef RISCOS
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002377 set_herror(h_errno);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002378#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002379 PyErr_SetString(socket_error, "host not found");
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002380#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002381 return NULL;
2382 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002383
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002384 if (h->h_addrtype != af) {
2385#ifdef HAVE_STRERROR
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002386 /* Let's get real error message to return */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002387 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002388 (char *)strerror(EAFNOSUPPORT));
2389#else
2390 PyErr_SetString(
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002391 socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002392 "Address family not supported by protocol family");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002393#endif
2394 return NULL;
2395 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002396
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002397 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002398
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002399 case AF_INET:
2400 if (alen < sizeof(struct sockaddr_in))
2401 return NULL;
2402 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002403
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002404#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002405 case AF_INET6:
2406 if (alen < sizeof(struct sockaddr_in6))
2407 return NULL;
2408 break;
2409#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002410
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002411 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002412
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002413 if ((name_list = PyList_New(0)) == NULL)
2414 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002415
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002416 if ((addr_list = PyList_New(0)) == NULL)
2417 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002418
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002419 for (pch = h->h_aliases; *pch != NULL; pch++) {
2420 int status;
2421 tmp = PyString_FromString(*pch);
2422 if (tmp == NULL)
2423 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002424
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002425 status = PyList_Append(name_list, tmp);
2426 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002427
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002428 if (status)
2429 goto err;
2430 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002431
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002432 for (pch = h->h_addr_list; *pch != NULL; pch++) {
2433 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002434
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002435 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002436
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002437 case AF_INET:
2438 {
2439 struct sockaddr_in sin;
2440 memset(&sin, 0, sizeof(sin));
2441 sin.sin_family = af;
2442#ifdef HAVE_SOCKADDR_SA_LEN
2443 sin.sin_len = sizeof(sin);
2444#endif
2445 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
2446 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002447
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002448 if (pch == h->h_addr_list && alen >= sizeof(sin))
2449 memcpy((char *) addr, &sin, sizeof(sin));
2450 break;
2451 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002452
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002453#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002454 case AF_INET6:
2455 {
2456 struct sockaddr_in6 sin6;
2457 memset(&sin6, 0, sizeof(sin6));
2458 sin6.sin6_family = af;
2459#ifdef HAVE_SOCKADDR_SA_LEN
2460 sin6.sin6_len = sizeof(sin6);
2461#endif
2462 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
2463 tmp = makeipaddr((struct sockaddr *)&sin6,
2464 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002465
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002466 if (pch == h->h_addr_list && alen >= sizeof(sin6))
2467 memcpy((char *) addr, &sin6, sizeof(sin6));
2468 break;
2469 }
2470#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002471
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002472 default: /* can't happen */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002473 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002474 "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002475 return NULL;
2476 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002477
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002478 if (tmp == NULL)
2479 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002480
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002481 status = PyList_Append(addr_list, tmp);
2482 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002483
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002484 if (status)
2485 goto err;
2486 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002487
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002488 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002489
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002490 err:
2491 Py_XDECREF(name_list);
2492 Py_XDECREF(addr_list);
2493 return rtn_tuple;
2494}
2495
2496
2497/* Python interface to gethostbyname_ex(name). */
2498
2499/*ARGSUSED*/
2500static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002501socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002502{
2503 char *name;
2504 struct hostent *h;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00002505#ifdef ENABLE_IPV6
2506 struct sockaddr_storage addr;
2507#else
2508 struct sockaddr_in addr;
2509#endif
Martin v. Löwis9db2f572001-07-23 01:30:10 +00002510 struct sockaddr *sa;
Guido van Rossum955becc1999-03-22 20:14:53 +00002511 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002512#ifdef HAVE_GETHOSTBYNAME_R
2513 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002514#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
2515 struct hostent_data data;
2516#else
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002517 char buf[16384];
2518 int buf_len = (sizeof buf) - 1;
2519 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002520#endif
2521#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002522 int result;
2523#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002524#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002525
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002526 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002527 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00002528 if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002529 return NULL;
2530 Py_BEGIN_ALLOW_THREADS
2531#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002532#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002533 result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
2534 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002535#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002536 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002537#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00002538 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002539 result = gethostbyname_r(name, &hp_allocated, &data);
2540 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002541#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002542#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002543#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002544 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002545#endif
2546 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002547#endif /* HAVE_GETHOSTBYNAME_R */
2548 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002549 /* Some C libraries would require addr.__ss_family instead of
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002550 addr.ss_family.
2551 Therefore, we cast the sockaddr_storage into sockaddr to
2552 access sa_family. */
Martin v. Löwis9db2f572001-07-23 01:30:10 +00002553 sa = (struct sockaddr*)&addr;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002554 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002555 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002556#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002557 PyThread_release_lock(netdb_lock);
Guido van Rossum955becc1999-03-22 20:14:53 +00002558#endif
2559 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002560}
2561
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002562PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002563"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
2564\n\
2565Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002566for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002567
2568
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002569/* Python interface to gethostbyaddr(IP). */
2570
2571/*ARGSUSED*/
2572static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002573socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002574{
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002575#ifdef ENABLE_IPV6
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002576 struct sockaddr_storage addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002577#else
Guido van Rossum48a680c2001-03-02 06:34:14 +00002578 struct sockaddr_in addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002579#endif
2580 struct sockaddr *sa = (struct sockaddr *)&addr;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002581 char *ip_num;
2582 struct hostent *h;
Guido van Rossum3baaa131999-03-22 21:44:51 +00002583 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002584#ifdef HAVE_GETHOSTBYNAME_R
2585 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002586#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
2587 struct hostent_data data;
2588#else
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002589 char buf[16384];
2590 int buf_len = (sizeof buf) - 1;
2591 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002592#endif
2593#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002594 int result;
2595#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002596#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002597 char *ap;
2598 int al;
2599 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002600
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002601 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002602 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00002603 af = AF_UNSPEC;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002604 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002605 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002606 af = sa->sa_family;
2607 ap = NULL;
2608 al = 0;
2609 switch (af) {
2610 case AF_INET:
2611 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
2612 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
2613 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002614#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002615 case AF_INET6:
2616 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
2617 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
2618 break;
2619#endif
2620 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002621 PyErr_SetString(socket_error, "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002622 return NULL;
2623 }
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002624 Py_BEGIN_ALLOW_THREADS
2625#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002626#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002627 result = gethostbyaddr_r(ap, al, af,
2628 &hp_allocated, buf, buf_len,
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002629 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002630#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002631 h = gethostbyaddr_r(ap, al, af,
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002632 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002633#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00002634 memset((void *) &data, '\0', sizeof(data));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002635 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002636 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002637#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002638#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002639#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002640 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002641#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002642 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002643#endif /* HAVE_GETHOSTBYNAME_R */
2644 Py_END_ALLOW_THREADS
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002645 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002646#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002647 PyThread_release_lock(netdb_lock);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002648#endif
2649 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002650}
2651
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002652PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002653"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
2654\n\
2655Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002656for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002657
Guido van Rossum30a685f1991-06-27 15:51:29 +00002658
2659/* Python interface to getservbyname(name).
2660 This only returns the port number, since the other info is already
2661 known or not useful (like the list of aliases). */
2662
2663/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002664static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002665socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002666{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002667 char *name, *proto;
Guido van Rossum30a685f1991-06-27 15:51:29 +00002668 struct servent *sp;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002669 if (!PyArg_ParseTuple(args, "ss:getservbyname", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002670 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002671 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002672 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00002673 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00002674 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002675 PyErr_SetString(socket_error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00002676 return NULL;
2677 }
Guido van Rossum73624e91994-10-10 17:59:00 +00002678 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002679}
2680
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002681PyDoc_STRVAR(getservbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002682"getservbyname(servicename, protocolname) -> integer\n\
2683\n\
2684Return a port number from a service name and protocol name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002685The protocol name should be 'tcp' or 'udp'.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002686
Guido van Rossum30a685f1991-06-27 15:51:29 +00002687
Guido van Rossum3901d851996-12-19 16:35:04 +00002688/* Python interface to getprotobyname(name).
2689 This only returns the protocol number, since the other info is
2690 already known or not useful (like the list of aliases). */
2691
2692/*ARGSUSED*/
2693static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002694socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00002695{
2696 char *name;
2697 struct protoent *sp;
Guido van Rossumbcc20741998-08-04 22:53:56 +00002698#ifdef __BEOS__
2699/* Not available in BeOS yet. - [cjh] */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002700 PyErr_SetString(socket_error, "getprotobyname not supported");
Guido van Rossumbcc20741998-08-04 22:53:56 +00002701 return NULL;
2702#else
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002703 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
Guido van Rossum3901d851996-12-19 16:35:04 +00002704 return NULL;
2705 Py_BEGIN_ALLOW_THREADS
2706 sp = getprotobyname(name);
2707 Py_END_ALLOW_THREADS
2708 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002709 PyErr_SetString(socket_error, "protocol not found");
Guido van Rossum3901d851996-12-19 16:35:04 +00002710 return NULL;
2711 }
2712 return PyInt_FromLong((long) sp->p_proto);
Guido van Rossumbcc20741998-08-04 22:53:56 +00002713#endif
Guido van Rossum3901d851996-12-19 16:35:04 +00002714}
2715
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002716PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002717"getprotobyname(name) -> integer\n\
2718\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002719Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002720
Guido van Rossum3901d851996-12-19 16:35:04 +00002721
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002722#ifndef NO_DUP
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002723/* Create a socket object from a numeric file description.
2724 Useful e.g. if stdin is a socket.
2725 Additional arguments as for socket(). */
2726
2727/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002728static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002729socket_fromfd(PyObject *self, PyObject *args)
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002730{
Guido van Rossum73624e91994-10-10 17:59:00 +00002731 PySocketSockObject *s;
Fred Drakea04eaad2000-06-30 02:46:07 +00002732 SOCKET_T fd;
2733 int family, type, proto = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002734 if (!PyArg_ParseTuple(args, "iii|i:fromfd",
2735 &fd, &family, &type, &proto))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002736 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +00002737 /* Dup the fd so it and the socket can be closed independently */
2738 fd = dup(fd);
2739 if (fd < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002740 return set_error();
2741 s = new_sockobject(fd, family, type, proto);
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002742 /* From now on, ignore SIGPIPE and let the error checking
2743 do the work. */
Guido van Rossum48a680c2001-03-02 06:34:14 +00002744#ifdef SIGPIPE
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002745 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum48a680c2001-03-02 06:34:14 +00002746#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00002747 return (PyObject *) s;
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002748}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002749
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002750PyDoc_STRVAR(fromfd_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002751"fromfd(fd, family, type[, proto]) -> socket object\n\
2752\n\
2753Create a socket object from the given file descriptor.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002754The remaining arguments are the same as for socket().");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002755
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002756#endif /* NO_DUP */
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002757
Guido van Rossum82a5c661998-07-07 20:45:43 +00002758
Guido van Rossum006bf911996-06-12 04:04:55 +00002759static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002760socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00002761{
2762 int x1, x2;
2763
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002764 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00002765 return NULL;
2766 }
2767 x2 = (int)ntohs((short)x1);
2768 return PyInt_FromLong(x2);
2769}
2770
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002771PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002772"ntohs(integer) -> integer\n\
2773\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002774Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002775
2776
Guido van Rossum006bf911996-06-12 04:04:55 +00002777static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002778socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00002779{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002780 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00002781
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002782 if (PyInt_Check(arg)) {
2783 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002784 if (x == (unsigned long) -1 && PyErr_Occurred())
2785 return NULL;
Guido van Rossum006bf911996-06-12 04:04:55 +00002786 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002787 else if (PyLong_Check(arg)) {
2788 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002789 if (x == (unsigned long) -1 && PyErr_Occurred())
2790 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002791#if SIZEOF_LONG > 4
2792 {
2793 unsigned long y;
2794 /* only want the trailing 32 bits */
2795 y = x & 0xFFFFFFFFUL;
2796 if (y ^ x)
2797 return PyErr_Format(PyExc_OverflowError,
2798 "long int larger than 32 bits");
2799 x = y;
Tim Peters58141872002-08-06 22:25:02 +00002800 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002801#endif
2802 }
2803 else
Tim Peters58141872002-08-06 22:25:02 +00002804 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002805 "expected int/long, %s found",
2806 arg->ob_type->tp_name);
2807 if (x == (unsigned long) -1 && PyErr_Occurred())
2808 return NULL;
2809 return PyInt_FromLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00002810}
2811
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002812PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002813"ntohl(integer) -> integer\n\
2814\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002815Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002816
2817
Guido van Rossum006bf911996-06-12 04:04:55 +00002818static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002819socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00002820{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002821 unsigned long x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00002822
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002823 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00002824 return NULL;
2825 }
2826 x2 = (int)htons((short)x1);
2827 return PyInt_FromLong(x2);
2828}
2829
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002830PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002831"htons(integer) -> integer\n\
2832\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002833Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002834
2835
Guido van Rossum006bf911996-06-12 04:04:55 +00002836static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002837socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00002838{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002839 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00002840
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002841 if (PyInt_Check(arg)) {
2842 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002843 if (x == (unsigned long) -1 && PyErr_Occurred())
2844 return NULL;
Guido van Rossum006bf911996-06-12 04:04:55 +00002845 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002846 else if (PyLong_Check(arg)) {
2847 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002848 if (x == (unsigned long) -1 && PyErr_Occurred())
2849 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002850#if SIZEOF_LONG > 4
2851 {
2852 unsigned long y;
2853 /* only want the trailing 32 bits */
2854 y = x & 0xFFFFFFFFUL;
2855 if (y ^ x)
2856 return PyErr_Format(PyExc_OverflowError,
2857 "long int larger than 32 bits");
2858 x = y;
Tim Peters58141872002-08-06 22:25:02 +00002859 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002860#endif
2861 }
2862 else
Tim Peters58141872002-08-06 22:25:02 +00002863 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002864 "expected int/long, %s found",
2865 arg->ob_type->tp_name);
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002866 return PyInt_FromLong(htonl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00002867}
2868
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002869PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002870"htonl(integer) -> integer\n\
2871\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002872Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002873
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002874/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002875
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002876PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002877"inet_aton(string) -> packed 32-bit IP representation\n\
2878\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002879Convert 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 +00002880binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002881
2882static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002883socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002884{
Guido van Rossuma2e48551999-09-09 15:42:59 +00002885#ifndef INADDR_NONE
2886#define INADDR_NONE (-1)
2887#endif
Neal Norwitz88f115b2003-02-13 02:15:42 +00002888#ifdef HAVE_INET_ATON
Guido van Rossumad05cdf2003-02-12 23:08:22 +00002889 struct in_addr buf;
Tim Peters1df9fdd2003-02-13 03:13:40 +00002890#else
2891 /* Have to use inet_addr() instead */
2892 unsigned long packed_addr;
2893#endif
2894 char *ip_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002895
Tim Peters1df9fdd2003-02-13 03:13:40 +00002896 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002897 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00002898
Tim Peters1df9fdd2003-02-13 03:13:40 +00002899
2900#ifdef HAVE_INET_ATON
Guido van Rossumad05cdf2003-02-12 23:08:22 +00002901 if (inet_aton(ip_addr, &buf))
2902 return PyString_FromStringAndSize((char *)(&buf),
2903 sizeof(buf));
2904
2905 PyErr_SetString(socket_error,
2906 "illegal IP address string passed to inet_aton");
2907 return NULL;
2908
Tim Peters1df9fdd2003-02-13 03:13:40 +00002909#else /* ! HAVE_INET_ATON */
Guido van Rossumad05cdf2003-02-12 23:08:22 +00002910 /* XXX Problem here: inet_aton('255.255.255.255') raises
2911 an exception while it should be a valid address. */
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002912 packed_addr = inet_addr(ip_addr);
2913
2914 if (packed_addr == INADDR_NONE) { /* invalid address */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002915 PyErr_SetString(socket_error,
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002916 "illegal IP address string passed to inet_aton");
2917 return NULL;
2918 }
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002919 return PyString_FromStringAndSize((char *) &packed_addr,
2920 sizeof(packed_addr));
Guido van Rossumad05cdf2003-02-12 23:08:22 +00002921#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002922}
2923
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002924PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00002925"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002926\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002927Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002928
2929static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002930socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002931{
2932 char *packed_str;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002933 int addr_len;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002934 struct in_addr packed_addr;
2935
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002936 if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) {
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002937 return NULL;
2938 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00002939
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002940 if (addr_len != sizeof(packed_addr)) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002941 PyErr_SetString(socket_error,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002942 "packed IP wrong length for inet_ntoa");
2943 return NULL;
2944 }
2945
2946 memcpy(&packed_addr, packed_str, addr_len);
2947
2948 return PyString_FromString(inet_ntoa(packed_addr));
2949}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002950
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00002951#ifdef HAVE_INET_PTON
2952
2953PyDoc_STRVAR(inet_pton_doc,
2954"inet_pton(af, ip) -> packed IP address string\n\
2955\n\
2956Convert an IP address from string format to a packed string suitable\n\
2957for use with low-level network functions.");
2958
2959static PyObject *
2960socket_inet_pton(PyObject *self, PyObject *args)
2961{
2962 int af;
2963 char* ip;
2964 int retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00002965#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00002966 char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00002967#else
2968 char packed[sizeof(struct in_addr)];
2969#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00002970 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
2971 return NULL;
2972 }
2973
Martin v. Löwis10649092003-08-05 06:25:06 +00002974#ifndef ENABLE_IPV6
2975 if(af == AF_INET6) {
2976 PyErr_SetString(socket_error,
2977 "can't use AF_INET6, IPv6 is disabled");
2978 return NULL;
2979 }
2980#endif
2981
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00002982 retval = inet_pton(af, ip, packed);
2983 if (retval < 0) {
2984 PyErr_SetFromErrno(socket_error);
2985 return NULL;
2986 } else if (retval == 0) {
2987 PyErr_SetString(socket_error,
2988 "illegal IP address string passed to inet_pton");
2989 return NULL;
2990 } else if (af == AF_INET) {
2991 return PyString_FromStringAndSize(packed,
2992 sizeof(struct in_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00002993#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00002994 } else if (af == AF_INET6) {
2995 return PyString_FromStringAndSize(packed,
2996 sizeof(struct in6_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00002997#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00002998 } else {
2999 PyErr_SetString(socket_error, "unknown address family");
3000 return NULL;
3001 }
3002}
3003
3004PyDoc_STRVAR(inet_ntop_doc,
3005"inet_ntop(af, packed_ip) -> string formatted IP address\n\
3006\n\
3007Convert a packed IP address of the given family to string format.");
3008
3009static PyObject *
3010socket_inet_ntop(PyObject *self, PyObject *args)
3011{
3012 int af;
3013 char* packed;
3014 int len;
3015 const char* retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003016#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003017 char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003018#else
3019 char ip[INET_ADDRSTRLEN + 1];
3020#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003021
3022 /* Guarantee NUL-termination for PyString_FromString() below */
3023 memset((void *) &ip[0], '\0', sizeof(ip) + 1);
3024
3025 if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) {
3026 return NULL;
3027 }
3028
3029 if (af == AF_INET) {
3030 if (len != sizeof(struct in_addr)) {
3031 PyErr_SetString(PyExc_ValueError,
3032 "invalid length of packed IP address string");
3033 return NULL;
3034 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003035#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003036 } else if (af == AF_INET6) {
3037 if (len != sizeof(struct in6_addr)) {
3038 PyErr_SetString(PyExc_ValueError,
3039 "invalid length of packed IP address string");
3040 return NULL;
3041 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003042#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003043 } else {
3044 PyErr_Format(PyExc_ValueError,
3045 "unknown address family %d", af);
3046 return NULL;
3047 }
3048
3049 retval = inet_ntop(af, packed, ip, sizeof(ip));
3050 if (!retval) {
3051 PyErr_SetFromErrno(socket_error);
3052 return NULL;
3053 } else {
3054 return PyString_FromString(retval);
3055 }
3056
3057 /* NOTREACHED */
3058 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
3059 return NULL;
3060}
3061
3062#endif /* HAVE_INET_PTON */
3063
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003064/* Python interface to getaddrinfo(host, port). */
3065
3066/*ARGSUSED*/
3067static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003068socket_getaddrinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003069{
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003070 struct addrinfo hints, *res;
3071 struct addrinfo *res0 = NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003072 PyObject *hobj = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003073 PyObject *pobj = (PyObject *)NULL;
Martin v. Löwisf65b1a12001-08-12 09:28:40 +00003074 char pbuf[30];
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003075 char *hptr, *pptr;
3076 int family, socktype, protocol, flags;
3077 int error;
3078 PyObject *all = (PyObject *)NULL;
3079 PyObject *single = (PyObject *)NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003080 PyObject *idna = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003081
3082 family = socktype = protocol = flags = 0;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003083 family = AF_UNSPEC;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003084 if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo",
3085 &hobj, &pobj, &family, &socktype,
3086 &protocol, &flags)) {
3087 return NULL;
3088 }
3089 if (hobj == Py_None) {
3090 hptr = NULL;
3091 } else if (PyUnicode_Check(hobj)) {
3092 idna = PyObject_CallMethod(hobj, "encode", "s", "idna");
3093 if (!idna)
3094 return NULL;
3095 hptr = PyString_AsString(idna);
3096 } else if (PyString_Check(hobj)) {
3097 hptr = PyString_AsString(hobj);
3098 } else {
3099 PyErr_SetString(PyExc_TypeError,
3100 "getaddrinfo() argument 1 must be string or None");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003101 return NULL;
3102 }
3103 if (PyInt_Check(pobj)) {
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003104 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003105 pptr = pbuf;
3106 } else if (PyString_Check(pobj)) {
3107 pptr = PyString_AsString(pobj);
3108 } else if (pobj == Py_None) {
3109 pptr = (char *)NULL;
3110 } else {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003111 PyErr_SetString(socket_error, "Int or String expected");
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003112 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003113 }
3114 memset(&hints, 0, sizeof(hints));
3115 hints.ai_family = family;
3116 hints.ai_socktype = socktype;
3117 hints.ai_protocol = protocol;
3118 hints.ai_flags = flags;
Just van Rossum1040d2c2003-05-09 07:53:18 +00003119 Py_BEGIN_ALLOW_THREADS
3120 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003121 error = getaddrinfo(hptr, pptr, &hints, &res0);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003122 Py_END_ALLOW_THREADS
3123 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003124 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003125 set_gaierror(error);
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003126 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003127 }
3128
3129 if ((all = PyList_New(0)) == NULL)
3130 goto err;
3131 for (res = res0; res; res = res->ai_next) {
Guido van Rossum716aac02001-10-12 18:59:27 +00003132 PyObject *addr =
3133 makesockaddr(-1, res->ai_addr, res->ai_addrlen);
3134 if (addr == NULL)
3135 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003136 single = Py_BuildValue("iiisO", res->ai_family,
3137 res->ai_socktype, res->ai_protocol,
3138 res->ai_canonname ? res->ai_canonname : "",
Guido van Rossum716aac02001-10-12 18:59:27 +00003139 addr);
3140 Py_DECREF(addr);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003141 if (single == NULL)
3142 goto err;
3143
3144 if (PyList_Append(all, single))
3145 goto err;
3146 Py_XDECREF(single);
3147 }
Martin v. Löwis2548c732003-04-18 10:39:54 +00003148 Py_XDECREF(idna);
Neal Norwitz90128ba2002-08-09 03:37:42 +00003149 if (res0)
3150 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003151 return all;
3152 err:
3153 Py_XDECREF(single);
3154 Py_XDECREF(all);
Martin v. Löwis2548c732003-04-18 10:39:54 +00003155 Py_XDECREF(idna);
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003156 if (res0)
3157 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003158 return (PyObject *)NULL;
3159}
3160
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003161PyDoc_STRVAR(getaddrinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003162"getaddrinfo(host, port [, family, socktype, proto, flags])\n\
3163 -> list of (family, socktype, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003164\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003165Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003166
3167/* Python interface to getnameinfo(sa, flags). */
3168
3169/*ARGSUSED*/
3170static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003171socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003172{
3173 PyObject *sa = (PyObject *)NULL;
3174 int flags;
3175 char *hostp;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003176 int port, flowinfo, scope_id;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003177 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
3178 struct addrinfo hints, *res = NULL;
3179 int error;
3180 PyObject *ret = (PyObject *)NULL;
3181
3182 flags = flowinfo = scope_id = 0;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003183 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003184 return NULL;
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003185 if (!PyArg_ParseTuple(sa, "si|ii",
3186 &hostp, &port, &flowinfo, &scope_id))
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003187 return NULL;
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003188 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003189 memset(&hints, 0, sizeof(hints));
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003190 hints.ai_family = AF_UNSPEC;
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003191 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Just van Rossum1040d2c2003-05-09 07:53:18 +00003192 Py_BEGIN_ALLOW_THREADS
3193 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003194 error = getaddrinfo(hostp, pbuf, &hints, &res);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003195 Py_END_ALLOW_THREADS
3196 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003197 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003198 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003199 goto fail;
3200 }
3201 if (res->ai_next) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003202 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003203 "sockaddr resolved to multiple addresses");
3204 goto fail;
3205 }
3206 switch (res->ai_family) {
3207 case AF_INET:
3208 {
3209 char *t1;
3210 int t2;
3211 if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003212 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003213 "IPv4 sockaddr must be 2 tuple");
3214 goto fail;
3215 }
3216 break;
3217 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003218#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003219 case AF_INET6:
3220 {
3221 struct sockaddr_in6 *sin6;
3222 sin6 = (struct sockaddr_in6 *)res->ai_addr;
3223 sin6->sin6_flowinfo = flowinfo;
3224 sin6->sin6_scope_id = scope_id;
3225 break;
3226 }
3227#endif
3228 }
3229 error = getnameinfo(res->ai_addr, res->ai_addrlen,
3230 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
3231 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003232 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003233 goto fail;
3234 }
3235 ret = Py_BuildValue("ss", hbuf, pbuf);
3236
3237fail:
3238 if (res)
3239 freeaddrinfo(res);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003240 return ret;
3241}
3242
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003243PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003244"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003245\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003246Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003247
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003248
3249/* Python API to getting and setting the default timeout value. */
3250
3251static PyObject *
3252socket_getdefaulttimeout(PyObject *self)
3253{
3254 if (defaulttimeout < 0.0) {
3255 Py_INCREF(Py_None);
3256 return Py_None;
3257 }
3258 else
3259 return PyFloat_FromDouble(defaulttimeout);
3260}
3261
3262PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003263"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003264\n\
3265Returns the default timeout in floating seconds for new socket objects.\n\
3266A value of None indicates that new socket objects have no timeout.\n\
3267When the socket module is first imported, the default is None.");
3268
3269static PyObject *
3270socket_setdefaulttimeout(PyObject *self, PyObject *arg)
3271{
3272 double timeout;
3273
3274 if (arg == Py_None)
3275 timeout = -1.0;
3276 else {
3277 timeout = PyFloat_AsDouble(arg);
3278 if (timeout < 0.0) {
3279 if (!PyErr_Occurred())
3280 PyErr_SetString(PyExc_ValueError,
3281 "Timeout value out of range");
3282 return NULL;
3283 }
3284 }
3285
3286 defaulttimeout = timeout;
3287
3288 Py_INCREF(Py_None);
3289 return Py_None;
3290}
3291
3292PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003293"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003294\n\
3295Set the default timeout in floating seconds for new socket objects.\n\
3296A value of None indicates that new socket objects have no timeout.\n\
3297When the socket module is first imported, the default is None.");
3298
3299
Guido van Rossum30a685f1991-06-27 15:51:29 +00003300/* List of functions exported by this module. */
3301
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003302static PyMethodDef socket_methods[] = {
3303 {"gethostbyname", socket_gethostbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003304 METH_VARARGS, gethostbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003305 {"gethostbyname_ex", socket_gethostbyname_ex,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003306 METH_VARARGS, ghbn_ex_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003307 {"gethostbyaddr", socket_gethostbyaddr,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003308 METH_VARARGS, gethostbyaddr_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003309 {"gethostname", socket_gethostname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003310 METH_VARARGS, gethostname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003311 {"getservbyname", socket_getservbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003312 METH_VARARGS, getservbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003313 {"getprotobyname", socket_getprotobyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003314 METH_VARARGS,getprotobyname_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003315#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003316 {"fromfd", socket_fromfd,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003317 METH_VARARGS, fromfd_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003318#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003319 {"ntohs", socket_ntohs,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003320 METH_VARARGS, ntohs_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003321 {"ntohl", socket_ntohl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003322 METH_O, ntohl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003323 {"htons", socket_htons,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003324 METH_VARARGS, htons_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003325 {"htonl", socket_htonl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003326 METH_O, htonl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003327 {"inet_aton", socket_inet_aton,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003328 METH_VARARGS, inet_aton_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003329 {"inet_ntoa", socket_inet_ntoa,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003330 METH_VARARGS, inet_ntoa_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003331#ifdef HAVE_INET_PTON
3332 {"inet_pton", socket_inet_pton,
3333 METH_VARARGS, inet_pton_doc},
3334 {"inet_ntop", socket_inet_ntop,
3335 METH_VARARGS, inet_ntop_doc},
3336#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003337 {"getaddrinfo", socket_getaddrinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003338 METH_VARARGS, getaddrinfo_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003339 {"getnameinfo", socket_getnameinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003340 METH_VARARGS, getnameinfo_doc},
Guido van Rossum1693ba82002-07-18 21:11:26 +00003341 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003342 METH_NOARGS, getdefaulttimeout_doc},
3343 {"setdefaulttimeout", socket_setdefaulttimeout,
3344 METH_O, setdefaulttimeout_doc},
Guido van Rossum30a685f1991-06-27 15:51:29 +00003345 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003346};
3347
Guido van Rossum30a685f1991-06-27 15:51:29 +00003348
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003349#ifdef RISCOS
3350#define OS_INIT_DEFINED
Guido van Rossumbe32c891996-06-20 16:25:29 +00003351
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003352static int
3353os_init(void)
3354{
3355 _kernel_swi_regs r;
3356
3357 r.r[0] = 0;
3358 _kernel_swi(0x43380, &r, &r);
3359 taskwindow = r.r[0];
3360
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003361 return 1;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003362}
3363
3364#endif /* RISCOS */
3365
3366
3367#ifdef MS_WINDOWS
3368#define OS_INIT_DEFINED
3369
3370/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003371
3372static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003373os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00003374{
3375 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00003376}
3377
3378static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003379os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00003380{
3381 WSADATA WSAData;
3382 int ret;
3383 char buf[100];
3384 ret = WSAStartup(0x0101, &WSAData);
3385 switch (ret) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003386 case 0: /* No error */
Mark Hammond14350ab2003-07-09 04:57:46 +00003387 Py_AtExit(os_cleanup);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003388 return 1; /* Success */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003389 case WSASYSNOTREADY:
3390 PyErr_SetString(PyExc_ImportError,
3391 "WSAStartup failed: network not ready");
3392 break;
3393 case WSAVERNOTSUPPORTED:
3394 case WSAEINVAL:
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003395 PyErr_SetString(
3396 PyExc_ImportError,
3397 "WSAStartup failed: requested version not supported");
Guido van Rossumbe32c891996-06-20 16:25:29 +00003398 break;
3399 default:
Tim Peters885d4572001-11-28 20:27:42 +00003400 PyOS_snprintf(buf, sizeof(buf),
3401 "WSAStartup failed: error code %d", ret);
Guido van Rossumbe32c891996-06-20 16:25:29 +00003402 PyErr_SetString(PyExc_ImportError, buf);
3403 break;
3404 }
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003405 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003406}
3407
Guido van Rossum8d665e61996-06-26 18:22:49 +00003408#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003409
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003410
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003411#ifdef PYOS_OS2
3412#define OS_INIT_DEFINED
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003413
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003414/* Additional initialization for OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003415
3416static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003417os_init(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003418{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003419#ifndef PYCC_GCC
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003420 char reason[64];
3421 int rc = sock_init();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003422
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003423 if (rc == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003424 return 1; /* Success */
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003425 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003426
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003427 PyOS_snprintf(reason, sizeof(reason),
3428 "OS/2 TCP/IP Error# %d", sock_errno());
3429 PyErr_SetString(PyExc_ImportError, reason);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003430
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003431 return 0; /* Failure */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00003432#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003433 /* No need to initialise sockets with GCC/EMX */
3434 return 1; /* Success */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00003435#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003436}
3437
3438#endif /* PYOS_OS2 */
3439
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003440
3441#ifndef OS_INIT_DEFINED
3442static int
3443os_init(void)
3444{
3445 return 1; /* Success */
3446}
3447#endif
3448
3449
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003450/* C API table - always add new things to the end for binary
3451 compatibility. */
3452static
3453PySocketModule_APIObject PySocketModuleAPI =
3454{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003455 &sock_type,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003456};
3457
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003458
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003459/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003460
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003461 This module is actually called "_socket", and there's a wrapper
3462 "socket.py" which implements some additional functionality. On some
3463 platforms (e.g. Windows and OS/2), socket.py also implements a
3464 wrapper for the socket type that provides missing functionality such
3465 as makefile(), dup() and fromfd(). The import of "_socket" may fail
3466 with an ImportError exception if os-specific initialization fails.
3467 On Windows, this does WINSOCK initialization. When WINSOCK is
3468 initialized succesfully, a call to WSACleanup() is scheduled to be
3469 made at exit time.
3470*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003471
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003472PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003473"Implementation module for socket operations.\n\
3474\n\
3475See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003476
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003477PyMODINIT_FUNC
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003478init_socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003479{
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003480 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00003481
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003482 if (!os_init())
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003483 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003484
3485 sock_type.ob_type = &PyType_Type;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003486 m = Py_InitModule3(PySocket_MODULE_NAME,
3487 socket_methods,
3488 socket_doc);
3489
3490 socket_error = PyErr_NewException("socket.error", NULL, NULL);
3491 if (socket_error == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003492 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003493 Py_INCREF(socket_error);
3494 PyModule_AddObject(m, "error", socket_error);
3495 socket_herror = PyErr_NewException("socket.herror",
3496 socket_error, NULL);
3497 if (socket_herror == NULL)
3498 return;
3499 Py_INCREF(socket_herror);
3500 PyModule_AddObject(m, "herror", socket_herror);
3501 socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003502 NULL);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003503 if (socket_gaierror == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003504 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003505 Py_INCREF(socket_gaierror);
3506 PyModule_AddObject(m, "gaierror", socket_gaierror);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00003507 socket_timeout = PyErr_NewException("socket.timeout",
3508 socket_error, NULL);
3509 if (socket_timeout == NULL)
3510 return;
3511 Py_INCREF(socket_timeout);
3512 PyModule_AddObject(m, "timeout", socket_timeout);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003513 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00003514 if (PyModule_AddObject(m, "SocketType",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003515 (PyObject *)&sock_type) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003516 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003517 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00003518 if (PyModule_AddObject(m, "socket",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003519 (PyObject *)&sock_type) != 0)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003520 return;
Guido van Rossum09be4091999-08-09 14:40:40 +00003521
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003522#ifdef ENABLE_IPV6
3523 has_ipv6 = Py_True;
3524#else
3525 has_ipv6 = Py_False;
3526#endif
3527 Py_INCREF(has_ipv6);
3528 PyModule_AddObject(m, "has_ipv6", has_ipv6);
3529
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003530 /* Export C API */
Fred Drake4baedc12002-04-01 14:53:37 +00003531 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003532 PyCObject_FromVoidPtr((void *)&PySocketModuleAPI, NULL)
3533 ) != 0)
3534 return;
3535
Guido van Rossum09be4091999-08-09 14:40:40 +00003536 /* Address families (we only support AF_INET and AF_UNIX) */
3537#ifdef AF_UNSPEC
Fred Drake4baedc12002-04-01 14:53:37 +00003538 PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00003539#endif
Fred Drake4baedc12002-04-01 14:53:37 +00003540 PyModule_AddIntConstant(m, "AF_INET", AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003541#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00003542 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003543#endif /* AF_INET6 */
Andrew MacIntyre34d3e2d2003-01-02 12:45:34 +00003544#if defined(AF_UNIX) && !defined(PYOS_OS2)
Fred Drake4baedc12002-04-01 14:53:37 +00003545 PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003546#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00003547#ifdef AF_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00003548 /* Amateur Radio AX.25 */
3549 PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003550#endif
3551#ifdef AF_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00003552 PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00003553#endif
3554#ifdef AF_APPLETALK
Fred Drake4baedc12002-04-01 14:53:37 +00003555 /* Appletalk DDP */
3556 PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00003557#endif
3558#ifdef AF_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00003559 /* Amateur radio NetROM */
3560 PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00003561#endif
3562#ifdef AF_BRIDGE
Fred Drake4baedc12002-04-01 14:53:37 +00003563 /* Multiprotocol bridge */
3564 PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003565#endif
3566#ifdef AF_AAL5
Fred Drake4baedc12002-04-01 14:53:37 +00003567 /* Reserved for Werner's ATM */
3568 PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00003569#endif
3570#ifdef AF_X25
Fred Drake4baedc12002-04-01 14:53:37 +00003571 /* Reserved for X.25 project */
3572 PyModule_AddIntConstant(m, "AF_X25", AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003573#endif
3574#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00003575 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00003576#endif
3577#ifdef AF_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00003578 /* Amateur Radio X.25 PLP */
3579 PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003580#endif
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00003581#ifdef HAVE_NETPACKET_PACKET_H
Fred Drake4baedc12002-04-01 14:53:37 +00003582 PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET);
3583 PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET);
3584 PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST);
3585 PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST);
3586 PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST);
3587 PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST);
3588 PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING);
3589 PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK);
3590 PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00003591#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003592
3593 /* Socket types */
Fred Drake4baedc12002-04-01 14:53:37 +00003594 PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
3595 PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00003596#ifndef __BEOS__
3597/* We have incomplete socket support. */
Fred Drake4baedc12002-04-01 14:53:37 +00003598 PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
3599 PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003600#if defined(SOCK_RDM)
Fred Drake4baedc12002-04-01 14:53:37 +00003601 PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00003602#endif
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003603#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003604
3605#ifdef SO_DEBUG
Fred Drake4baedc12002-04-01 14:53:37 +00003606 PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003607#endif
3608#ifdef SO_ACCEPTCONN
Fred Drake4baedc12002-04-01 14:53:37 +00003609 PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003610#endif
3611#ifdef SO_REUSEADDR
Fred Drake4baedc12002-04-01 14:53:37 +00003612 PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003613#endif
3614#ifdef SO_KEEPALIVE
Fred Drake4baedc12002-04-01 14:53:37 +00003615 PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003616#endif
3617#ifdef SO_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00003618 PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003619#endif
3620#ifdef SO_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00003621 PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003622#endif
3623#ifdef SO_USELOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00003624 PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003625#endif
3626#ifdef SO_LINGER
Fred Drake4baedc12002-04-01 14:53:37 +00003627 PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003628#endif
3629#ifdef SO_OOBINLINE
Fred Drake4baedc12002-04-01 14:53:37 +00003630 PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003631#endif
3632#ifdef SO_REUSEPORT
Fred Drake4baedc12002-04-01 14:53:37 +00003633 PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003634#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003635#ifdef SO_SNDBUF
Fred Drake4baedc12002-04-01 14:53:37 +00003636 PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003637#endif
3638#ifdef SO_RCVBUF
Fred Drake4baedc12002-04-01 14:53:37 +00003639 PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003640#endif
3641#ifdef SO_SNDLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00003642 PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003643#endif
3644#ifdef SO_RCVLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00003645 PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003646#endif
3647#ifdef SO_SNDTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00003648 PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003649#endif
3650#ifdef SO_RCVTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00003651 PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003652#endif
3653#ifdef SO_ERROR
Fred Drake4baedc12002-04-01 14:53:37 +00003654 PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003655#endif
3656#ifdef SO_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00003657 PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003658#endif
3659
3660 /* Maximum number of connections for "listen" */
3661#ifdef SOMAXCONN
Fred Drake4baedc12002-04-01 14:53:37 +00003662 PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003663#else
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003664 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003665#endif
3666
3667 /* Flags for send, recv */
3668#ifdef MSG_OOB
Fred Drake4baedc12002-04-01 14:53:37 +00003669 PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003670#endif
3671#ifdef MSG_PEEK
Fred Drake4baedc12002-04-01 14:53:37 +00003672 PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003673#endif
3674#ifdef MSG_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00003675 PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003676#endif
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00003677#ifdef MSG_DONTWAIT
Fred Drake4baedc12002-04-01 14:53:37 +00003678 PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00003679#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003680#ifdef MSG_EOR
Fred Drake4baedc12002-04-01 14:53:37 +00003681 PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003682#endif
3683#ifdef MSG_TRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00003684 PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003685#endif
3686#ifdef MSG_CTRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00003687 PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003688#endif
3689#ifdef MSG_WAITALL
Fred Drake4baedc12002-04-01 14:53:37 +00003690 PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003691#endif
3692#ifdef MSG_BTAG
Fred Drake4baedc12002-04-01 14:53:37 +00003693 PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003694#endif
3695#ifdef MSG_ETAG
Fred Drake4baedc12002-04-01 14:53:37 +00003696 PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003697#endif
3698
3699 /* Protocol level and numbers, usable for [gs]etsockopt */
3700#ifdef SOL_SOCKET
Fred Drake4baedc12002-04-01 14:53:37 +00003701 PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003702#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003703#ifdef SOL_IP
Fred Drake4baedc12002-04-01 14:53:37 +00003704 PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003705#else
Fred Drake4baedc12002-04-01 14:53:37 +00003706 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00003707#endif
3708#ifdef SOL_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00003709 PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00003710#endif
3711#ifdef SOL_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00003712 PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003713#endif
3714#ifdef SOL_ATALK
Fred Drake4baedc12002-04-01 14:53:37 +00003715 PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00003716#endif
3717#ifdef SOL_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00003718 PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00003719#endif
3720#ifdef SOL_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00003721 PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003722#endif
3723#ifdef SOL_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00003724 PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003725#else
Fred Drake4baedc12002-04-01 14:53:37 +00003726 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00003727#endif
3728#ifdef SOL_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00003729 PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003730#else
Fred Drake4baedc12002-04-01 14:53:37 +00003731 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00003732#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003733#ifdef IPPROTO_IP
Fred Drake4baedc12002-04-01 14:53:37 +00003734 PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00003735#else
Fred Drake4baedc12002-04-01 14:53:37 +00003736 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003737#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003738#ifdef IPPROTO_HOPOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003739 PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003740#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003741#ifdef IPPROTO_ICMP
Fred Drake4baedc12002-04-01 14:53:37 +00003742 PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00003743#else
Fred Drake4baedc12002-04-01 14:53:37 +00003744 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003745#endif
3746#ifdef IPPROTO_IGMP
Fred Drake4baedc12002-04-01 14:53:37 +00003747 PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003748#endif
3749#ifdef IPPROTO_GGP
Fred Drake4baedc12002-04-01 14:53:37 +00003750 PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003751#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003752#ifdef IPPROTO_IPV4
Fred Drake4baedc12002-04-01 14:53:37 +00003753 PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003754#endif
Martin v. Löwisa0f17342003-10-03 13:56:20 +00003755#ifdef IPPROTO_IPV6
3756 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
3757#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003758#ifdef IPPROTO_IPIP
Fred Drake4baedc12002-04-01 14:53:37 +00003759 PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003760#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003761#ifdef IPPROTO_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00003762 PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00003763#else
Fred Drake4baedc12002-04-01 14:53:37 +00003764 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003765#endif
3766#ifdef IPPROTO_EGP
Fred Drake4baedc12002-04-01 14:53:37 +00003767 PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003768#endif
3769#ifdef IPPROTO_PUP
Fred Drake4baedc12002-04-01 14:53:37 +00003770 PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003771#endif
3772#ifdef IPPROTO_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00003773 PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00003774#else
Fred Drake4baedc12002-04-01 14:53:37 +00003775 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003776#endif
3777#ifdef IPPROTO_IDP
Fred Drake4baedc12002-04-01 14:53:37 +00003778 PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003779#endif
3780#ifdef IPPROTO_HELLO
Fred Drake4baedc12002-04-01 14:53:37 +00003781 PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003782#endif
3783#ifdef IPPROTO_ND
Fred Drake4baedc12002-04-01 14:53:37 +00003784 PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003785#endif
3786#ifdef IPPROTO_TP
Fred Drake4baedc12002-04-01 14:53:37 +00003787 PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003788#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003789#ifdef IPPROTO_IPV6
Fred Drake4baedc12002-04-01 14:53:37 +00003790 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003791#endif
3792#ifdef IPPROTO_ROUTING
Fred Drake4baedc12002-04-01 14:53:37 +00003793 PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003794#endif
3795#ifdef IPPROTO_FRAGMENT
Fred Drake4baedc12002-04-01 14:53:37 +00003796 PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003797#endif
3798#ifdef IPPROTO_RSVP
Fred Drake4baedc12002-04-01 14:53:37 +00003799 PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003800#endif
3801#ifdef IPPROTO_GRE
Fred Drake4baedc12002-04-01 14:53:37 +00003802 PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003803#endif
3804#ifdef IPPROTO_ESP
Fred Drake4baedc12002-04-01 14:53:37 +00003805 PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003806#endif
3807#ifdef IPPROTO_AH
Fred Drake4baedc12002-04-01 14:53:37 +00003808 PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003809#endif
3810#ifdef IPPROTO_MOBILE
Fred Drake4baedc12002-04-01 14:53:37 +00003811 PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003812#endif
3813#ifdef IPPROTO_ICMPV6
Fred Drake4baedc12002-04-01 14:53:37 +00003814 PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003815#endif
3816#ifdef IPPROTO_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00003817 PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003818#endif
3819#ifdef IPPROTO_DSTOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003820 PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003821#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003822#ifdef IPPROTO_XTP
Fred Drake4baedc12002-04-01 14:53:37 +00003823 PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003824#endif
3825#ifdef IPPROTO_EON
Fred Drake4baedc12002-04-01 14:53:37 +00003826 PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003827#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003828#ifdef IPPROTO_PIM
Fred Drake4baedc12002-04-01 14:53:37 +00003829 PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003830#endif
3831#ifdef IPPROTO_IPCOMP
Fred Drake4baedc12002-04-01 14:53:37 +00003832 PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003833#endif
3834#ifdef IPPROTO_VRRP
Fred Drake4baedc12002-04-01 14:53:37 +00003835 PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003836#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003837#ifdef IPPROTO_BIP
Fred Drake4baedc12002-04-01 14:53:37 +00003838 PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003839#endif
3840/**/
3841#ifdef IPPROTO_RAW
Fred Drake4baedc12002-04-01 14:53:37 +00003842 PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00003843#else
Fred Drake4baedc12002-04-01 14:53:37 +00003844 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003845#endif
3846#ifdef IPPROTO_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00003847 PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003848#endif
3849
3850 /* Some port configuration */
3851#ifdef IPPORT_RESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00003852 PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003853#else
Fred Drake4baedc12002-04-01 14:53:37 +00003854 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003855#endif
3856#ifdef IPPORT_USERRESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00003857 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003858#else
Fred Drake4baedc12002-04-01 14:53:37 +00003859 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003860#endif
3861
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003862 /* Some reserved IP v.4 addresses */
3863#ifdef INADDR_ANY
Fred Drake4baedc12002-04-01 14:53:37 +00003864 PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003865#else
Fred Drake4baedc12002-04-01 14:53:37 +00003866 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003867#endif
3868#ifdef INADDR_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00003869 PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003870#else
Fred Drake4baedc12002-04-01 14:53:37 +00003871 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003872#endif
3873#ifdef INADDR_LOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00003874 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003875#else
Fred Drake4baedc12002-04-01 14:53:37 +00003876 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003877#endif
3878#ifdef INADDR_UNSPEC_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003879 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003880#else
Fred Drake4baedc12002-04-01 14:53:37 +00003881 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003882#endif
3883#ifdef INADDR_ALLHOSTS_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003884 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
3885 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003886#else
Fred Drake4baedc12002-04-01 14:53:37 +00003887 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003888#endif
3889#ifdef INADDR_MAX_LOCAL_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003890 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
3891 INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003892#else
Fred Drake4baedc12002-04-01 14:53:37 +00003893 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003894#endif
3895#ifdef INADDR_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00003896 PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003897#else
Fred Drake4baedc12002-04-01 14:53:37 +00003898 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003899#endif
3900
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003901 /* IPv4 [gs]etsockopt options */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003902#ifdef IP_OPTIONS
Fred Drake4baedc12002-04-01 14:53:37 +00003903 PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003904#endif
3905#ifdef IP_HDRINCL
Fred Drake4baedc12002-04-01 14:53:37 +00003906 PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003907#endif
3908#ifdef IP_TOS
Fred Drake4baedc12002-04-01 14:53:37 +00003909 PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003910#endif
3911#ifdef IP_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00003912 PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003913#endif
3914#ifdef IP_RECVOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003915 PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003916#endif
3917#ifdef IP_RECVRETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003918 PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003919#endif
3920#ifdef IP_RECVDSTADDR
Fred Drake4baedc12002-04-01 14:53:37 +00003921 PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003922#endif
3923#ifdef IP_RETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003924 PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003925#endif
3926#ifdef IP_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00003927 PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003928#endif
3929#ifdef IP_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00003930 PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003931#endif
3932#ifdef IP_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00003933 PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003934#endif
3935#ifdef IP_ADD_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00003936 PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003937#endif
3938#ifdef IP_DROP_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00003939 PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003940#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003941#ifdef IP_DEFAULT_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00003942 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
3943 IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00003944#endif
3945#ifdef IP_DEFAULT_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00003946 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
3947 IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003948#endif
3949#ifdef IP_MAX_MEMBERSHIPS
Fred Drake4baedc12002-04-01 14:53:37 +00003950 PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00003951#endif
3952
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003953 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
3954#ifdef IPV6_JOIN_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003955 PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003956#endif
3957#ifdef IPV6_LEAVE_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003958 PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003959#endif
3960#ifdef IPV6_MULTICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00003961 PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003962#endif
3963#ifdef IPV6_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00003964 PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003965#endif
3966#ifdef IPV6_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00003967 PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003968#endif
3969#ifdef IPV6_UNICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00003970 PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003971#endif
3972
Guido van Rossum09be4091999-08-09 14:40:40 +00003973 /* TCP options */
3974#ifdef TCP_NODELAY
Fred Drake4baedc12002-04-01 14:53:37 +00003975 PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00003976#endif
3977#ifdef TCP_MAXSEG
Fred Drake4baedc12002-04-01 14:53:37 +00003978 PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00003979#endif
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003980#ifdef TCP_CORK
Fred Drake4baedc12002-04-01 14:53:37 +00003981 PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003982#endif
3983#ifdef TCP_KEEPIDLE
Fred Drake4baedc12002-04-01 14:53:37 +00003984 PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003985#endif
3986#ifdef TCP_KEEPINTVL
Fred Drake4baedc12002-04-01 14:53:37 +00003987 PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003988#endif
3989#ifdef TCP_KEEPCNT
Fred Drake4baedc12002-04-01 14:53:37 +00003990 PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003991#endif
3992#ifdef TCP_SYNCNT
Fred Drake4baedc12002-04-01 14:53:37 +00003993 PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003994#endif
3995#ifdef TCP_LINGER2
Fred Drake4baedc12002-04-01 14:53:37 +00003996 PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003997#endif
3998#ifdef TCP_DEFER_ACCEPT
Fred Drake4baedc12002-04-01 14:53:37 +00003999 PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004000#endif
4001#ifdef TCP_WINDOW_CLAMP
Fred Drake4baedc12002-04-01 14:53:37 +00004002 PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004003#endif
4004#ifdef TCP_INFO
Fred Drake4baedc12002-04-01 14:53:37 +00004005 PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004006#endif
4007#ifdef TCP_QUICKACK
Fred Drake4baedc12002-04-01 14:53:37 +00004008 PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004009#endif
4010
Guido van Rossum09be4091999-08-09 14:40:40 +00004011
4012 /* IPX options */
4013#ifdef IPX_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004014 PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004015#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004016
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004017 /* get{addr,name}info parameters */
4018#ifdef EAI_ADDRFAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004019 PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004020#endif
4021#ifdef EAI_AGAIN
Fred Drake4baedc12002-04-01 14:53:37 +00004022 PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004023#endif
4024#ifdef EAI_BADFLAGS
Fred Drake4baedc12002-04-01 14:53:37 +00004025 PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004026#endif
4027#ifdef EAI_FAIL
Fred Drake4baedc12002-04-01 14:53:37 +00004028 PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004029#endif
4030#ifdef EAI_FAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004031 PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004032#endif
4033#ifdef EAI_MEMORY
Fred Drake4baedc12002-04-01 14:53:37 +00004034 PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004035#endif
4036#ifdef EAI_NODATA
Fred Drake4baedc12002-04-01 14:53:37 +00004037 PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004038#endif
4039#ifdef EAI_NONAME
Fred Drake4baedc12002-04-01 14:53:37 +00004040 PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004041#endif
4042#ifdef EAI_SERVICE
Fred Drake4baedc12002-04-01 14:53:37 +00004043 PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004044#endif
4045#ifdef EAI_SOCKTYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004046 PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004047#endif
4048#ifdef EAI_SYSTEM
Fred Drake4baedc12002-04-01 14:53:37 +00004049 PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004050#endif
4051#ifdef EAI_BADHINTS
Fred Drake4baedc12002-04-01 14:53:37 +00004052 PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004053#endif
4054#ifdef EAI_PROTOCOL
Fred Drake4baedc12002-04-01 14:53:37 +00004055 PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004056#endif
4057#ifdef EAI_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00004058 PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004059#endif
4060#ifdef AI_PASSIVE
Fred Drake4baedc12002-04-01 14:53:37 +00004061 PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004062#endif
4063#ifdef AI_CANONNAME
Fred Drake4baedc12002-04-01 14:53:37 +00004064 PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004065#endif
4066#ifdef AI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004067 PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004068#endif
4069#ifdef AI_MASK
Fred Drake4baedc12002-04-01 14:53:37 +00004070 PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004071#endif
4072#ifdef AI_ALL
Fred Drake4baedc12002-04-01 14:53:37 +00004073 PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004074#endif
4075#ifdef AI_V4MAPPED_CFG
Fred Drake4baedc12002-04-01 14:53:37 +00004076 PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004077#endif
4078#ifdef AI_ADDRCONFIG
Fred Drake4baedc12002-04-01 14:53:37 +00004079 PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004080#endif
4081#ifdef AI_V4MAPPED
Fred Drake4baedc12002-04-01 14:53:37 +00004082 PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004083#endif
4084#ifdef AI_DEFAULT
Fred Drake4baedc12002-04-01 14:53:37 +00004085 PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004086#endif
4087#ifdef NI_MAXHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004088 PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004089#endif
4090#ifdef NI_MAXSERV
Fred Drake4baedc12002-04-01 14:53:37 +00004091 PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004092#endif
4093#ifdef NI_NOFQDN
Fred Drake4baedc12002-04-01 14:53:37 +00004094 PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004095#endif
4096#ifdef NI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004097 PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004098#endif
4099#ifdef NI_NAMEREQD
Fred Drake4baedc12002-04-01 14:53:37 +00004100 PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004101#endif
4102#ifdef NI_NUMERICSERV
Fred Drake4baedc12002-04-01 14:53:37 +00004103 PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004104#endif
4105#ifdef NI_DGRAM
Fred Drake4baedc12002-04-01 14:53:37 +00004106 PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004107#endif
4108
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004109 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00004110#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
4111 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004112#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004113}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004114
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004115
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004116#ifndef HAVE_INET_PTON
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004117
4118/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004119/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004120
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004121int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004122inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004123{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004124 if (af == AF_INET) {
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004125 long packed_addr;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004126 packed_addr = inet_addr(src);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004127 if (packed_addr == INADDR_NONE)
4128 return 0;
4129 memcpy(dst, &packed_addr, 4);
4130 return 1;
4131 }
4132 /* Should set errno to EAFNOSUPPORT */
4133 return -1;
4134}
4135
Martin v. Löwisc925b1532001-07-21 09:42:15 +00004136const char *
4137inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004138{
4139 if (af == AF_INET) {
4140 struct in_addr packed_addr;
4141 if (size < 16)
4142 /* Should set errno to ENOSPC. */
4143 return NULL;
4144 memcpy(&packed_addr, src, sizeof(packed_addr));
4145 return strncpy(dst, inet_ntoa(packed_addr), size);
4146 }
4147 /* Should set errno to EAFNOSUPPORT */
4148 return NULL;
4149}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004150
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004151#endif