blob: 8fd59636002e808c1249d8efa773d6d0343c2e47 [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
197#ifndef ENABLE_IPV6
198#define INET_ADDRSTRLEN 16
199#endif
200
201#define HAVE_INET_PTON
202#include <netdb.h>
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000203#endif
204
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000205/* Generic includes */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206#include <sys/types.h>
Guido van Rossum81194471991-07-27 21:42:02 +0000207#include <signal.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000208
Marc-André Lemburg976ade62002-02-16 18:47:07 +0000209/* Generic socket object definitions and includes */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000210#define PySocket_BUILDING_SOCKET
Marc-André Lemburgbb8b78b2002-02-16 18:44:52 +0000211#include "socketmodule.h"
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000212
213/* Addressing includes */
214
Guido van Rossum6f489d91996-06-28 20:15:15 +0000215#ifndef MS_WINDOWS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000216
217/* Non-MS WINDOWS includes */
218# include <netdb.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000219
Guido van Rossum9376b741999-09-15 22:01:40 +0000220/* Headers needed for inet_ntoa() and inet_addr() */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000221# ifdef __BEOS__
222# include <net/netdb.h>
223# elif defined(PYOS_OS2) && defined(PYCC_VACPP)
224# include <netdb.h>
Tim Peters603c6832001-11-05 02:45:59 +0000225typedef size_t socklen_t;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000226# else
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000227# include <arpa/inet.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000228# endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000229
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000230# ifndef RISCOS
231# include <fcntl.h>
232# else
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000233# include <sys/ioctl.h>
234# include <socklib.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000235# define NO_DUP
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000236int h_errno; /* not used */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000237# define INET_ADDRSTRLEN 16
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000238# endif
239
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000240#else
Guido van Rossum48a680c2001-03-02 06:34:14 +0000241
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000242/* MS_WINDOWS includes */
243# include <fcntl.h>
Guido van Rossum48a680c2001-03-02 06:34:14 +0000244
Jeremy Hylton22308652001-02-02 03:23:09 +0000245#endif
246
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000247#ifdef HAVE_STDDEF_H
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000248# include <stddef.h>
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000249#endif
250
251#ifndef offsetof
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000252# define offsetof(type, member) ((size_t)(&((type *)0)->member))
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000253#endif
254
Neal Norwitz39d22e52002-11-02 19:55:21 +0000255#ifndef O_NONBLOCK
256# define O_NONBLOCK O_NDELAY
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000257#endif
258
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000259#include "addrinfo.h"
260
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000261#ifndef HAVE_INET_PTON
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000262int inet_pton(int af, const char *src, void *dst);
Martin v. Löwisc925b1532001-07-21 09:42:15 +0000263const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000264#endif
265
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000266#ifdef __APPLE__
267/* On OS X, getaddrinfo returns no error indication of lookup
268 failure, so we must use the emulation instead of the libinfo
269 implementation. Unfortunately, performing an autoconf test
270 for this bug would require DNS access for the machine performing
271 the configuration, which is not acceptable. Therefore, we
272 determine the bug just by checking for __APPLE__. If this bug
273 gets ever fixed, perhaps checking for sys/version.h would be
274 appropriate, which is 10/0 on the system with the bug. */
Jack Jansen84262fb2002-07-02 14:40:42 +0000275#ifndef HAVE_GETNAMEINFO
276/* This bug seems to be fixed in Jaguar. Ths easiest way I could
277 Find to check for Jaguar is that it has getnameinfo(), which
278 older releases don't have */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000279#undef HAVE_GETADDRINFO
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000280#endif
Jack Jansen84262fb2002-07-02 14:40:42 +0000281#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000282
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000283/* I know this is a bad practice, but it is the easiest... */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000284#if !defined(HAVE_GETADDRINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000285/* avoid clashes with the C library definition of the symbol. */
286#define getaddrinfo fake_getaddrinfo
287#define gai_strerror fake_gai_strerror
288#define freeaddrinfo fake_freeaddrinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000289#include "getaddrinfo.c"
290#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000291#if !defined(HAVE_GETNAMEINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000292#define getnameinfo fake_getnameinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000293#include "getnameinfo.c"
294#endif
295
Guido van Rossumbcc20741998-08-04 22:53:56 +0000296#if defined(MS_WINDOWS) || defined(__BEOS__)
297/* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000298/* seem to be a few differences in the API */
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000299#define SOCKETCLOSE closesocket
Guido van Rossumbe32c891996-06-20 16:25:29 +0000300#define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000301#endif
302
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000303#ifdef MS_WIN32
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000304#define EAFNOSUPPORT WSAEAFNOSUPPORT
305#define snprintf _snprintf
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000306#endif
Fred Drakea04eaad2000-06-30 02:46:07 +0000307
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000308#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000309#define SOCKETCLOSE soclose
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000310#define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000311#endif
312
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000313#ifndef SOCKETCLOSE
314#define SOCKETCLOSE close
315#endif
316
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000317#ifdef __VMS
318/* TCP/IP Services for VMS uses a maximum send/revc buffer length of 65535 */
319#define SEGMENT_SIZE 65535
320#endif
321
Martin v. Löwise9416172003-05-03 10:12:45 +0000322/*
323 * Constants for getnameinfo()
324 */
325#if !defined(NI_MAXHOST)
326#define NI_MAXHOST 1025
327#endif
328#if !defined(NI_MAXSERV)
329#define NI_MAXSERV 32
330#endif
331
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000332/* XXX There's a problem here: *static* functions are not supposed to have
333 a Py prefix (or use CapitalizedWords). Later... */
334
Guido van Rossum30a685f1991-06-27 15:51:29 +0000335/* Global variable holding the exception type for errors detected
336 by this module (but not argument type or memory errors, etc.). */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000337static PyObject *socket_error;
338static PyObject *socket_herror;
339static PyObject *socket_gaierror;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000340static PyObject *socket_timeout;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000341
Guido van Rossum48a680c2001-03-02 06:34:14 +0000342#ifdef RISCOS
343/* Global variable which is !=0 if Python is running in a RISC OS taskwindow */
344static int taskwindow;
345#endif
346
Tim Peters643a7fc2002-02-17 04:13:21 +0000347/* A forward reference to the socket type object.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000348 The sock_type variable contains pointers to various functions,
349 some of which call new_sockobject(), which uses sock_type, so
Tim Peters643a7fc2002-02-17 04:13:21 +0000350 there has to be a circular reference. */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000351static PyTypeObject sock_type;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000352
Guido van Rossum30a685f1991-06-27 15:51:29 +0000353/* Convenience function to raise an error according to errno
354 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000355
Guido van Rossum73624e91994-10-10 17:59:00 +0000356static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000357set_error(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000358{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000359#ifdef MS_WINDOWS
Mark Hammond46a733d2000-07-24 01:45:11 +0000360 int err_no = WSAGetLastError();
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000361 static struct {
362 int no;
363 const char *msg;
364 } *msgp, msgs[] = {
365 {WSAEINTR, "Interrupted system call"},
366 {WSAEBADF, "Bad file descriptor"},
367 {WSAEACCES, "Permission denied"},
368 {WSAEFAULT, "Bad address"},
369 {WSAEINVAL, "Invalid argument"},
370 {WSAEMFILE, "Too many open files"},
371 {WSAEWOULDBLOCK,
372 "The socket operation could not complete "
373 "without blocking"},
374 {WSAEINPROGRESS, "Operation now in progress"},
375 {WSAEALREADY, "Operation already in progress"},
376 {WSAENOTSOCK, "Socket operation on non-socket"},
377 {WSAEDESTADDRREQ, "Destination address required"},
378 {WSAEMSGSIZE, "Message too long"},
379 {WSAEPROTOTYPE, "Protocol wrong type for socket"},
380 {WSAENOPROTOOPT, "Protocol not available"},
381 {WSAEPROTONOSUPPORT, "Protocol not supported"},
382 {WSAESOCKTNOSUPPORT, "Socket type not supported"},
383 {WSAEOPNOTSUPP, "Operation not supported"},
384 {WSAEPFNOSUPPORT, "Protocol family not supported"},
385 {WSAEAFNOSUPPORT, "Address family not supported"},
386 {WSAEADDRINUSE, "Address already in use"},
387 {WSAEADDRNOTAVAIL, "Can't assign requested address"},
388 {WSAENETDOWN, "Network is down"},
389 {WSAENETUNREACH, "Network is unreachable"},
390 {WSAENETRESET, "Network dropped connection on reset"},
391 {WSAECONNABORTED, "Software caused connection abort"},
392 {WSAECONNRESET, "Connection reset by peer"},
393 {WSAENOBUFS, "No buffer space available"},
394 {WSAEISCONN, "Socket is already connected"},
395 {WSAENOTCONN, "Socket is not connected"},
396 {WSAESHUTDOWN, "Can't send after socket shutdown"},
397 {WSAETOOMANYREFS, "Too many references: can't splice"},
398 {WSAETIMEDOUT, "Operation timed out"},
399 {WSAECONNREFUSED, "Connection refused"},
400 {WSAELOOP, "Too many levels of symbolic links"},
401 {WSAENAMETOOLONG, "File name too long"},
402 {WSAEHOSTDOWN, "Host is down"},
403 {WSAEHOSTUNREACH, "No route to host"},
404 {WSAENOTEMPTY, "Directory not empty"},
405 {WSAEPROCLIM, "Too many processes"},
406 {WSAEUSERS, "Too many users"},
407 {WSAEDQUOT, "Disc quota exceeded"},
408 {WSAESTALE, "Stale NFS file handle"},
409 {WSAEREMOTE, "Too many levels of remote in path"},
410 {WSASYSNOTREADY, "Network subsystem is unvailable"},
411 {WSAVERNOTSUPPORTED, "WinSock version is not supported"},
412 {WSANOTINITIALISED,
413 "Successful WSAStartup() not yet performed"},
414 {WSAEDISCON, "Graceful shutdown in progress"},
415 /* Resolver errors */
416 {WSAHOST_NOT_FOUND, "No such host is known"},
417 {WSATRY_AGAIN, "Host not found, or server failed"},
418 {WSANO_RECOVERY, "Unexpected server error encountered"},
419 {WSANO_DATA, "Valid name without requested data"},
420 {WSANO_ADDRESS, "No address, look for MX record"},
421 {0, NULL}
422 };
Mark Hammond46a733d2000-07-24 01:45:11 +0000423 if (err_no) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000424 PyObject *v;
Mark Hammond46a733d2000-07-24 01:45:11 +0000425 const char *msg = "winsock error";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000426
Mark Hammond46a733d2000-07-24 01:45:11 +0000427 for (msgp = msgs; msgp->msg; msgp++) {
428 if (err_no == msgp->no) {
429 msg = msgp->msg;
430 break;
431 }
432 }
433
434 v = Py_BuildValue("(is)", err_no, msg);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000435 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000436 PyErr_SetObject(socket_error, v);
Guido van Rossum73624e91994-10-10 17:59:00 +0000437 Py_DECREF(v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000438 }
439 return NULL;
440 }
441 else
442#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000443
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000444#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000445 if (sock_errno() != NO_ERROR) {
446 APIRET rc;
447 ULONG msglen;
448 char outbuf[100];
449 int myerrorcode = sock_errno();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000450
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000451 /* Retrieve socket-related error message from MPTN.MSG file */
452 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
453 myerrorcode - SOCBASEERR + 26,
454 "mptn.msg",
455 &msglen);
456 if (rc == NO_ERROR) {
457 PyObject *v;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000458
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000459 /* OS/2 doesn't guarantee a terminator */
460 outbuf[msglen] = '\0';
461 if (strlen(outbuf) > 0) {
462 /* If non-empty msg, trim CRLF */
463 char *lastc = &outbuf[ strlen(outbuf)-1 ];
464 while (lastc > outbuf && isspace(*lastc)) {
465 /* Trim trailing whitespace (CRLF) */
466 *lastc-- = '\0';
467 }
468 }
469 v = Py_BuildValue("(is)", myerrorcode, outbuf);
470 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000471 PyErr_SetObject(socket_error, v);
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000472 Py_DECREF(v);
473 }
474 return NULL;
475 }
476 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000477#endif
478
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000479#if defined(RISCOS)
480 if (_inet_error.errnum != NULL) {
481 PyObject *v;
482 v = Py_BuildValue("(is)", errno, _inet_err());
483 if (v != NULL) {
484 PyErr_SetObject(socket_error, v);
485 Py_DECREF(v);
486 }
487 return NULL;
488 }
489#endif
490
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000491 return PyErr_SetFromErrno(socket_error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000492}
493
Guido van Rossum30a685f1991-06-27 15:51:29 +0000494
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000495static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000496set_herror(int h_error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000497{
498 PyObject *v;
499
500#ifdef HAVE_HSTRERROR
501 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
502#else
503 v = Py_BuildValue("(is)", h_error, "host not found");
504#endif
505 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000506 PyErr_SetObject(socket_herror, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000507 Py_DECREF(v);
508 }
509
510 return NULL;
511}
512
513
514static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000515set_gaierror(int error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000516{
517 PyObject *v;
518
Martin v. Löwis272cb402002-03-01 08:31:07 +0000519#ifdef EAI_SYSTEM
520 /* EAI_SYSTEM is not available on Windows XP. */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000521 if (error == EAI_SYSTEM)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000522 return set_error();
Martin v. Löwis272cb402002-03-01 08:31:07 +0000523#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000524
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000525#ifdef HAVE_GAI_STRERROR
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000526 v = Py_BuildValue("(is)", error, gai_strerror(error));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000527#else
528 v = Py_BuildValue("(is)", error, "getaddrinfo failed");
529#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000530 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000531 PyErr_SetObject(socket_gaierror, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000532 Py_DECREF(v);
533 }
534
535 return NULL;
536}
537
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000538/* Function to perform the setting of socket blocking mode
539 internally. block = (1 | 0). */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000540static int
541internal_setblocking(PySocketSockObject *s, int block)
542{
543#ifndef RISCOS
544#ifndef MS_WINDOWS
545 int delay_flag;
546#endif
547#endif
548
549 Py_BEGIN_ALLOW_THREADS
550#ifdef __BEOS__
551 block = !block;
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000552 setsockopt(s->sock_fd, SOL_SOCKET, SO_NONBLOCK,
553 (void *)(&block), sizeof(int));
Guido van Rossum67f7a382002-06-06 21:08:16 +0000554#else
555#ifndef RISCOS
556#ifndef MS_WINDOWS
557#if defined(PYOS_OS2) && !defined(PYCC_GCC)
558 block = !block;
559 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000560#elif defined(__VMS)
561 block = !block;
562 ioctl(s->sock_fd, FIONBIO, (char *)&block);
563#else /* !PYOS_OS2 && !_VMS */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000564 delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
565 if (block)
Neal Norwitz39d22e52002-11-02 19:55:21 +0000566 delay_flag &= (~O_NONBLOCK);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000567 else
Neal Norwitz39d22e52002-11-02 19:55:21 +0000568 delay_flag |= O_NONBLOCK;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000569 fcntl(s->sock_fd, F_SETFL, delay_flag);
570#endif /* !PYOS_OS2 */
571#else /* MS_WINDOWS */
572 block = !block;
573 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
574#endif /* MS_WINDOWS */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000575#else /* RISCOS */
576 block = !block;
577 socketioctl(s->sock_fd, FIONBIO, (u_long*)&block);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000578#endif /* RISCOS */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000579#endif /* __BEOS__ */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000580 Py_END_ALLOW_THREADS
581
582 /* Since these don't return anything */
583 return 1;
584}
585
Guido van Rossum11ba0942002-06-13 15:07:44 +0000586/* Do a select() on the socket, if necessary (sock_timeout > 0).
587 The argument writing indicates the direction.
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000588 This does not raise an exception; we'll let our caller do that
589 after they've reacquired the interpreter lock.
590 Returns 1 on timeout, 0 otherwise. */
591static int
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000592internal_select(PySocketSockObject *s, int writing)
Guido van Rossum67f7a382002-06-06 21:08:16 +0000593{
594 fd_set fds;
595 struct timeval tv;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000596 int n;
Guido van Rossum11ba0942002-06-13 15:07:44 +0000597
Guido van Rossumad654902002-07-19 12:44:59 +0000598 /* Nothing to do unless we're in timeout mode (not non-blocking) */
Guido van Rossum11ba0942002-06-13 15:07:44 +0000599 if (s->sock_timeout <= 0.0)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000600 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000601
Guido van Rossumad654902002-07-19 12:44:59 +0000602 /* Guard against closed socket */
603 if (s->sock_fd < 0)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000604 return 0;
Guido van Rossumad654902002-07-19 12:44:59 +0000605
Guido van Rossum67f7a382002-06-06 21:08:16 +0000606 /* Construct the arguments to select */
607 tv.tv_sec = (int)s->sock_timeout;
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000608 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000609 FD_ZERO(&fds);
610 FD_SET(s->sock_fd, &fds);
611
612 /* See if the socket is ready */
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000613 if (writing)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000614 n = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000615 else
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000616 n = select(s->sock_fd+1, &fds, NULL, NULL, &tv);
617 if (n == 0)
618 return 1;
619 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000620}
621
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000622/* Initialize a new socket object. */
623
Tim Petersa12b4cf2002-07-18 22:38:44 +0000624static double defaulttimeout = -1.0; /* Default timeout for new sockets */
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000625
Mark Hammond62b1ab12002-07-23 06:31:15 +0000626PyMODINIT_FUNC
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000627init_sockobject(PySocketSockObject *s,
628 SOCKET_T fd, int family, int type, int proto)
629{
630#ifdef RISCOS
631 int block = 1;
632#endif
633 s->sock_fd = fd;
634 s->sock_family = family;
635 s->sock_type = type;
636 s->sock_proto = proto;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000637 s->sock_timeout = defaulttimeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000638
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000639 s->errorhandler = &set_error;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000640
641 if (defaulttimeout >= 0.0)
642 internal_setblocking(s, 0);
643
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000644#ifdef RISCOS
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000645 if (taskwindow)
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000646 socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000647#endif
648}
649
650
Guido van Rossum30a685f1991-06-27 15:51:29 +0000651/* Create a new socket object.
652 This just creates the object and initializes it.
653 If the creation fails, return NULL and set an exception (implicit
654 in NEWOBJ()). */
655
Guido van Rossum73624e91994-10-10 17:59:00 +0000656static PySocketSockObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000657new_sockobject(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000658{
Guido van Rossum73624e91994-10-10 17:59:00 +0000659 PySocketSockObject *s;
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000660 s = (PySocketSockObject *)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000661 PyType_GenericNew(&sock_type, NULL, NULL);
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000662 if (s != NULL)
663 init_sockobject(s, fd, family, type, proto);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000664 return s;
665}
666
Guido van Rossum30a685f1991-06-27 15:51:29 +0000667
Guido van Rossum48a680c2001-03-02 06:34:14 +0000668/* Lock to allow python interpreter to continue, but only allow one
Just van Rossum1040d2c2003-05-09 07:53:18 +0000669 thread to be in gethostbyname or getaddrinfo */
670#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
671PyThread_type_lock netdb_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000672#endif
673
674
Guido van Rossum30a685f1991-06-27 15:51:29 +0000675/* Convert a string specifying a host name or one of a few symbolic
676 names to a numeric IP address. This usually calls gethostbyname()
677 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000678 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000679 an error occurred; then an exception is raised. */
680
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000681static int
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000682setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000683{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000684 struct addrinfo hints, *res;
685 int error;
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000686 int d1, d2, d3, d4;
687 char ch;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000688
Guido van Rossuma376cc51996-12-05 23:43:35 +0000689 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
Guido van Rossum30a685f1991-06-27 15:51:29 +0000690 if (name[0] == '\0') {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000691 int siz;
692 memset(&hints, 0, sizeof(hints));
693 hints.ai_family = af;
694 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
695 hints.ai_flags = AI_PASSIVE;
Just van Rossum1040d2c2003-05-09 07:53:18 +0000696 Py_BEGIN_ALLOW_THREADS
697 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000698 error = getaddrinfo(NULL, "0", &hints, &res);
Just van Rossum1040d2c2003-05-09 07:53:18 +0000699 Py_END_ALLOW_THREADS
700 /* We assume that those thread-unsafe getaddrinfo() versions
701 *are* safe regarding their return value, ie. that a
702 subsequent call to getaddrinfo() does not destroy the
703 outcome of the first call. */
704 RELEASE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000705 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000706 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000707 return -1;
708 }
709 switch (res->ai_family) {
710 case AF_INET:
711 siz = 4;
712 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000713#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000714 case AF_INET6:
715 siz = 16;
716 break;
717#endif
718 default:
719 freeaddrinfo(res);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000720 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000721 "unsupported address family");
722 return -1;
723 }
724 if (res->ai_next) {
Martin v. Löwisf0b11d22001-11-07 08:31:03 +0000725 freeaddrinfo(res);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000726 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000727 "wildcard resolved to multiple address");
728 return -1;
729 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000730 if (res->ai_addrlen < addr_ret_size)
731 addr_ret_size = res->ai_addrlen;
732 memcpy(addr_ret, res->ai_addr, addr_ret_size);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000733 freeaddrinfo(res);
734 return siz;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000735 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000736 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000737 struct sockaddr_in *sin;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +0000738 if (af != AF_INET && af != AF_UNSPEC) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000739 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000740 "address family mismatched");
741 return -1;
742 }
743 sin = (struct sockaddr_in *)addr_ret;
744 memset((void *) sin, '\0', sizeof(*sin));
745 sin->sin_family = AF_INET;
746#ifdef HAVE_SOCKADDR_SA_LEN
747 sin->sin_len = sizeof(*sin);
748#endif
749 sin->sin_addr.s_addr = INADDR_BROADCAST;
750 return sizeof(sin->sin_addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000751 }
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000752 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
753 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
754 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
755 struct sockaddr_in *sin;
756 sin = (struct sockaddr_in *)addr_ret;
757 sin->sin_addr.s_addr = htonl(
758 ((long) d1 << 24) | ((long) d2 << 16) |
759 ((long) d3 << 8) | ((long) d4 << 0));
760 sin->sin_family = AF_INET;
761#ifdef HAVE_SOCKADDR_SA_LEN
762 sin->sin_len = sizeof(*sin);
763#endif
764 return 4;
765 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000766 memset(&hints, 0, sizeof(hints));
767 hints.ai_family = af;
Just van Rossum1040d2c2003-05-09 07:53:18 +0000768 Py_BEGIN_ALLOW_THREADS
769 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000770 error = getaddrinfo(name, NULL, &hints, &res);
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000771#if defined(__digital__) && defined(__unix__)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000772 if (error == EAI_NONAME && af == AF_UNSPEC) {
773 /* On Tru64 V5.1, numeric-to-addr conversion fails
774 if no address family is given. Assume IPv4 for now.*/
775 hints.ai_family = AF_INET;
776 error = getaddrinfo(name, NULL, &hints, &res);
777 }
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000778#endif
Just van Rossum1040d2c2003-05-09 07:53:18 +0000779 Py_END_ALLOW_THREADS
780 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000781 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000782 set_gaierror(error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000783 return -1;
784 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000785 if (res->ai_addrlen < addr_ret_size)
786 addr_ret_size = res->ai_addrlen;
787 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000788 freeaddrinfo(res);
789 switch (addr_ret->sa_family) {
790 case AF_INET:
791 return 4;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000792#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000793 case AF_INET6:
794 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +0000795#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000796 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000797 PyErr_SetString(socket_error, "unknown address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000798 return -1;
799 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000800}
801
Guido van Rossum30a685f1991-06-27 15:51:29 +0000802
Guido van Rossum30a685f1991-06-27 15:51:29 +0000803/* Create a string object representing an IP address.
804 This is always a string of the form 'dd.dd.dd.dd' (with variable
805 size numbers). */
806
Guido van Rossum73624e91994-10-10 17:59:00 +0000807static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000808makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000809{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000810 char buf[NI_MAXHOST];
811 int error;
812
813 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
814 NI_NUMERICHOST);
815 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000816 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000817 return NULL;
818 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000819 return PyString_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000820}
821
822
823/* Create an object representing the given socket address,
824 suitable for passing it back to bind(), connect() etc.
825 The family field of the sockaddr structure is inspected
826 to determine what kind of address it really is. */
827
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000828/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000829static PyObject *
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000830makesockaddr(int sockfd, struct sockaddr *addr, int addrlen)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000831{
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000832 if (addrlen == 0) {
833 /* No address -- may be recvfrom() from known socket */
Guido van Rossum73624e91994-10-10 17:59:00 +0000834 Py_INCREF(Py_None);
835 return Py_None;
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000836 }
837
Guido van Rossumbcc20741998-08-04 22:53:56 +0000838#ifdef __BEOS__
Guido van Rossum2c8bcb82000-04-25 21:34:53 +0000839 /* XXX: BeOS version of accept() doesn't set family correctly */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000840 addr->sa_family = AF_INET;
841#endif
842
Guido van Rossum30a685f1991-06-27 15:51:29 +0000843 switch (addr->sa_family) {
844
845 case AF_INET:
846 {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000847 struct sockaddr_in *a;
848 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
Barry Warsaw752300b1997-01-03 17:18:10 +0000849 PyObject *ret = NULL;
850 if (addrobj) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000851 a = (struct sockaddr_in *)addr;
Barry Warsaw752300b1997-01-03 17:18:10 +0000852 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
853 Py_DECREF(addrobj);
854 }
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000855 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000856 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000857
Andrew MacIntyre34d3e2d2003-01-02 12:45:34 +0000858#if defined(AF_UNIX) && !defined(PYOS_OS2)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000859 case AF_UNIX:
860 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000861 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000862 return PyString_FromString(a->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000863 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000864#endif /* AF_UNIX */
865
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000866#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000867 case AF_INET6:
868 {
869 struct sockaddr_in6 *a;
870 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
871 PyObject *ret = NULL;
872 if (addrobj) {
873 a = (struct sockaddr_in6 *)addr;
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000874 ret = Py_BuildValue("Oiii",
875 addrobj,
876 ntohs(a->sin6_port),
877 a->sin6_flowinfo,
878 a->sin6_scope_id);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000879 Py_DECREF(addrobj);
880 }
881 return ret;
882 }
Jeremy Hylton22308652001-02-02 03:23:09 +0000883#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +0000884
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +0000885#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +0000886 case AF_PACKET:
887 {
888 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
889 char *ifname = "";
890 struct ifreq ifr;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000891 /* need to look up interface name give index */
892 if (a->sll_ifindex) {
Jeremy Hylton22308652001-02-02 03:23:09 +0000893 ifr.ifr_ifindex = a->sll_ifindex;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000894 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
Jeremy Hylton22308652001-02-02 03:23:09 +0000895 ifname = ifr.ifr_name;
Jeremy Hylton22308652001-02-02 03:23:09 +0000896 }
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000897 return Py_BuildValue("shbhs#",
898 ifname,
899 ntohs(a->sll_protocol),
900 a->sll_pkttype,
901 a->sll_hatype,
902 a->sll_addr,
903 a->sll_halen);
Jeremy Hylton22308652001-02-02 03:23:09 +0000904 }
905#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000906
Guido van Rossum30a685f1991-06-27 15:51:29 +0000907 /* More cases here... */
908
909 default:
Guido van Rossumaa948df1997-05-07 17:41:48 +0000910 /* If we don't know the address family, don't raise an
911 exception -- return it as a tuple. */
912 return Py_BuildValue("is#",
913 addr->sa_family,
914 addr->sa_data,
915 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000916
Guido van Rossum30a685f1991-06-27 15:51:29 +0000917 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000918}
919
Guido van Rossum30a685f1991-06-27 15:51:29 +0000920
921/* Parse a socket address argument according to the socket object's
922 address family. Return 1 if the address was in the proper format,
923 0 of not. The address is returned through addr_ret, its length
924 through len_ret. */
925
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000926static int
Guido van Rossum48a680c2001-03-02 06:34:14 +0000927getsockaddrarg(PySocketSockObject *s, PyObject *args,
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000928 struct sockaddr **addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000929{
Guido van Rossum30a685f1991-06-27 15:51:29 +0000930 switch (s->sock_family) {
931
Andrew MacIntyre34d3e2d2003-01-02 12:45:34 +0000932#if defined(AF_UNIX) && !defined(PYOS_OS2)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000933 case AF_UNIX:
934 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000935 struct sockaddr_un* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000936 char *path;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000937 int len;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000938 addr = (struct sockaddr_un*)&(s->sock_addr).un;
Guido van Rossum7e488981998-10-08 02:25:24 +0000939 if (!PyArg_Parse(args, "t#", &path, &len))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000940 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000941 if (len > sizeof addr->sun_path) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000942 PyErr_SetString(socket_error,
Barry Warsaw752300b1997-01-03 17:18:10 +0000943 "AF_UNIX path too long");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000944 return 0;
945 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000946 addr->sun_family = s->sock_family;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000947 memcpy(addr->sun_path, path, len);
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000948 addr->sun_path[len] = 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000949 *addr_ret = (struct sockaddr *) addr;
Guido van Rossum65af28a1996-06-11 18:36:33 +0000950 *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000951 return 1;
952 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000953#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000954
Guido van Rossum30a685f1991-06-27 15:51:29 +0000955 case AF_INET:
956 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000957 struct sockaddr_in* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000958 char *host;
Martin v. Löwis5db099a2003-08-07 11:55:15 +0000959 int port, result;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000960 addr=(struct sockaddr_in*)&(s->sock_addr).in;
Guido van Rossume4dad902000-12-01 13:13:11 +0000961 if (!PyTuple_Check(args)) {
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000962 PyErr_Format(
963 PyExc_TypeError,
964 "getsockaddrarg: "
965 "AF_INET address must be tuple, not %.500s",
966 args->ob_type->tp_name);
Guido van Rossume4dad902000-12-01 13:13:11 +0000967 return 0;
968 }
Martin v. Löwis2548c732003-04-18 10:39:54 +0000969 if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
970 "idna", &host, &port))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000971 return 0;
Martin v. Löwis5db099a2003-08-07 11:55:15 +0000972 result = setipaddr(host, (struct sockaddr *)addr,
973 sizeof(*addr), AF_INET);
974 PyMem_Free(host);
975 if (result < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000976 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000977 addr->sin_family = AF_INET;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000978 addr->sin_port = htons((short)port);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000979 *addr_ret = (struct sockaddr *) addr;
980 *len_ret = sizeof *addr;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000981 return 1;
982 }
983
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000984#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000985 case AF_INET6:
986 {
987 struct sockaddr_in6* addr;
988 char *host;
Martin v. Löwis5db099a2003-08-07 11:55:15 +0000989 int port, flowinfo, scope_id, result;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000990 addr = (struct sockaddr_in6*)&(s->sock_addr).in6;
991 flowinfo = scope_id = 0;
Martin v. Löwis2548c732003-04-18 10:39:54 +0000992 if (!PyArg_ParseTuple(args, "eti|ii",
993 "idna", &host, &port, &flowinfo,
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000994 &scope_id)) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000995 return 0;
996 }
Martin v. Löwis5db099a2003-08-07 11:55:15 +0000997 result = setipaddr(host, (struct sockaddr *)addr,
998 sizeof(*addr), AF_INET6);
999 PyMem_Free(host);
1000 if (result < 0)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001001 return 0;
1002 addr->sin6_family = s->sock_family;
1003 addr->sin6_port = htons((short)port);
1004 addr->sin6_flowinfo = flowinfo;
1005 addr->sin6_scope_id = scope_id;
1006 *addr_ret = (struct sockaddr *) addr;
1007 *len_ret = sizeof *addr;
1008 return 1;
1009 }
1010#endif
1011
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001012#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001013 case AF_PACKET:
1014 {
1015 struct sockaddr_ll* addr;
1016 struct ifreq ifr;
1017 char *interfaceName;
1018 int protoNumber;
1019 int hatype = 0;
1020 int pkttype = 0;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001021 char *haddr;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001022
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001023 if (!PyArg_ParseTuple(args, "si|iis", &interfaceName,
1024 &protoNumber, &pkttype, &hatype, &haddr))
Jeremy Hylton22308652001-02-02 03:23:09 +00001025 return 0;
1026 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
1027 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001028 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001029 s->errorhandler();
Jeremy Hylton22308652001-02-02 03:23:09 +00001030 return 0;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001031 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001032 addr = &(s->sock_addr.ll);
1033 addr->sll_family = AF_PACKET;
1034 addr->sll_protocol = htons((short)protoNumber);
1035 addr->sll_ifindex = ifr.ifr_ifindex;
1036 addr->sll_pkttype = pkttype;
1037 addr->sll_hatype = hatype;
1038 *addr_ret = (struct sockaddr *) addr;
1039 *len_ret = sizeof *addr;
1040 return 1;
1041 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001042#endif
1043
Guido van Rossum30a685f1991-06-27 15:51:29 +00001044 /* More cases here... */
1045
1046 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001047 PyErr_SetString(socket_error, "getsockaddrarg: bad family");
Guido van Rossum30a685f1991-06-27 15:51:29 +00001048 return 0;
1049
1050 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001051}
1052
Guido van Rossum30a685f1991-06-27 15:51:29 +00001053
Guido van Rossum48a680c2001-03-02 06:34:14 +00001054/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +00001055 Return 1 if the family is known, 0 otherwise. The length is returned
1056 through len_ret. */
1057
1058static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +00001059getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001060{
1061 switch (s->sock_family) {
1062
Andrew MacIntyre34d3e2d2003-01-02 12:45:34 +00001063#if defined(AF_UNIX) && !defined(PYOS_OS2)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001064 case AF_UNIX:
1065 {
1066 *len_ret = sizeof (struct sockaddr_un);
1067 return 1;
1068 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001069#endif /* AF_UNIX */
Guido van Rossum710e1df1992-06-12 10:39:36 +00001070
1071 case AF_INET:
1072 {
1073 *len_ret = sizeof (struct sockaddr_in);
1074 return 1;
1075 }
1076
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001077#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001078 case AF_INET6:
1079 {
1080 *len_ret = sizeof (struct sockaddr_in6);
1081 return 1;
1082 }
1083#endif
1084
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001085#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001086 case AF_PACKET:
1087 {
1088 *len_ret = sizeof (struct sockaddr_ll);
1089 return 1;
1090 }
1091#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001092
Guido van Rossum710e1df1992-06-12 10:39:36 +00001093 /* More cases here... */
1094
1095 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001096 PyErr_SetString(socket_error, "getsockaddrlen: bad family");
Guido van Rossum710e1df1992-06-12 10:39:36 +00001097 return 0;
1098
1099 }
1100}
1101
1102
Guido van Rossum30a685f1991-06-27 15:51:29 +00001103/* s.accept() method */
1104
Guido van Rossum73624e91994-10-10 17:59:00 +00001105static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001106sock_accept(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001107{
1108 char addrbuf[256];
Fred Drakea04eaad2000-06-30 02:46:07 +00001109 SOCKET_T newfd;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001110 socklen_t addrlen;
Barry Warsaw752300b1997-01-03 17:18:10 +00001111 PyObject *sock = NULL;
1112 PyObject *addr = NULL;
1113 PyObject *res = NULL;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001114 int timeout;
Barry Warsaw752300b1997-01-03 17:18:10 +00001115
Guido van Rossum710e1df1992-06-12 10:39:36 +00001116 if (!getsockaddrlen(s, &addrlen))
1117 return NULL;
Fred Drakefd168342001-05-09 19:11:33 +00001118 memset(addrbuf, 0, addrlen);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001119
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001120#ifdef MS_WINDOWS
1121 newfd = INVALID_SOCKET;
1122#else
1123 newfd = -1;
1124#endif
1125
Guido van Rossum73624e91994-10-10 17:59:00 +00001126 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001127 timeout = internal_select(s, 0);
1128 if (!timeout)
1129 newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf,
1130 &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001131 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001132
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001133 if (timeout) {
1134 PyErr_SetString(socket_timeout, "timed out");
1135 return NULL;
1136 }
1137
Fred Drakea04eaad2000-06-30 02:46:07 +00001138#ifdef MS_WINDOWS
1139 if (newfd == INVALID_SOCKET)
1140#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001141 if (newfd < 0)
Fred Drakea04eaad2000-06-30 02:46:07 +00001142#endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001143 return s->errorhandler();
Barry Warsaw752300b1997-01-03 17:18:10 +00001144
Guido van Rossum30a685f1991-06-27 15:51:29 +00001145 /* Create the new object with unspecified family,
1146 to avoid calls to bind() etc. on it. */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001147 sock = (PyObject *) new_sockobject(newfd,
1148 s->sock_family,
1149 s->sock_type,
1150 s->sock_proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001151
Barry Warsaw752300b1997-01-03 17:18:10 +00001152 if (sock == NULL) {
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001153 SOCKETCLOSE(newfd);
Barry Warsaw752300b1997-01-03 17:18:10 +00001154 goto finally;
1155 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001156 addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001157 addrlen);
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001158 if (addr == NULL)
Barry Warsaw752300b1997-01-03 17:18:10 +00001159 goto finally;
1160
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001161 res = Py_BuildValue("OO", sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00001162
Guido van Rossum67f7a382002-06-06 21:08:16 +00001163finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001164 Py_XDECREF(sock);
1165 Py_XDECREF(addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001166 return res;
1167}
1168
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001169PyDoc_STRVAR(accept_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001170"accept() -> (socket object, address info)\n\
1171\n\
1172Wait for an incoming connection. Return a new socket representing the\n\
1173connection, and the address of the client. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001174info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001175
Guido van Rossum11ba0942002-06-13 15:07:44 +00001176/* s.setblocking(flag) method. Argument:
1177 False -- non-blocking mode; same as settimeout(0)
1178 True -- blocking mode; same as settimeout(None)
1179*/
Guido van Rossume4485b01994-09-07 14:32:49 +00001180
Guido van Rossum73624e91994-10-10 17:59:00 +00001181static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001182sock_setblocking(PySocketSockObject *s, PyObject *arg)
Guido van Rossume4485b01994-09-07 14:32:49 +00001183{
1184 int block;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001185
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001186 block = PyInt_AsLong(arg);
1187 if (block == -1 && PyErr_Occurred())
Guido van Rossume4485b01994-09-07 14:32:49 +00001188 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001189
Guido van Rossum11ba0942002-06-13 15:07:44 +00001190 s->sock_timeout = block ? -1.0 : 0.0;
Guido van Rossume8008f02002-06-07 03:36:20 +00001191 internal_setblocking(s, block);
Guido van Rossume4485b01994-09-07 14:32:49 +00001192
Guido van Rossum73624e91994-10-10 17:59:00 +00001193 Py_INCREF(Py_None);
1194 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +00001195}
Guido van Rossume4485b01994-09-07 14:32:49 +00001196
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001197PyDoc_STRVAR(setblocking_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001198"setblocking(flag)\n\
1199\n\
1200Set the socket to blocking (flag is true) or non-blocking (false).\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001201setblocking(True) is equivalent to settimeout(None);\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001202setblocking(False) is equivalent to settimeout(0.0).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001203
Guido van Rossum11ba0942002-06-13 15:07:44 +00001204/* s.settimeout(timeout) method. Argument:
1205 None -- no timeout, blocking mode; same as setblocking(True)
1206 0.0 -- non-blocking mode; same as setblocking(False)
1207 > 0 -- timeout mode; operations time out after timeout seconds
1208 < 0 -- illegal; raises an exception
1209*/
Guido van Rossum67f7a382002-06-06 21:08:16 +00001210static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001211sock_settimeout(PySocketSockObject *s, PyObject *arg)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001212{
Guido van Rossum11ba0942002-06-13 15:07:44 +00001213 double timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001214
1215 if (arg == Py_None)
Guido van Rossum11ba0942002-06-13 15:07:44 +00001216 timeout = -1.0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001217 else {
Guido van Rossum11ba0942002-06-13 15:07:44 +00001218 timeout = PyFloat_AsDouble(arg);
1219 if (timeout < 0.0) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00001220 if (!PyErr_Occurred())
1221 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00001222 "Timeout value out of range");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001223 return NULL;
1224 }
1225 }
1226
Guido van Rossum11ba0942002-06-13 15:07:44 +00001227 s->sock_timeout = timeout;
1228 internal_setblocking(s, timeout < 0.0);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001229
1230 Py_INCREF(Py_None);
1231 return Py_None;
1232}
1233
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001234PyDoc_STRVAR(settimeout_doc,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001235"settimeout(timeout)\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001236\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001237Set a timeout on socket operations. 'timeout' can be a float,\n\
1238giving in seconds, or None. Setting a timeout of None disables\n\
1239the timeout feature and is equivalent to setblocking(1).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001240Setting a timeout of zero is the same as setblocking(0).");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001241
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001242/* s.gettimeout() method.
1243 Returns the timeout associated with a socket. */
Guido van Rossum67f7a382002-06-06 21:08:16 +00001244static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001245sock_gettimeout(PySocketSockObject *s)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001246{
1247 if (s->sock_timeout < 0.0) {
1248 Py_INCREF(Py_None);
1249 return Py_None;
1250 }
1251 else
1252 return PyFloat_FromDouble(s->sock_timeout);
1253}
1254
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001255PyDoc_STRVAR(gettimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001256"gettimeout() -> timeout\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001257\n\
1258Returns the timeout in floating seconds associated with socket \n\
1259operations. A timeout of None indicates that timeouts on socket \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001260operations are disabled.");
Guido van Rossume4485b01994-09-07 14:32:49 +00001261
Guido van Rossum48a680c2001-03-02 06:34:14 +00001262#ifdef RISCOS
1263/* s.sleeptaskw(1 | 0) method */
1264
1265static PyObject *
Martin v. Löwisa94568a2003-05-10 07:36:56 +00001266sock_sleeptaskw(PySocketSockObject *s,PyObject *arg)
Guido van Rossum48a680c2001-03-02 06:34:14 +00001267{
Guido van Rossum67f7a382002-06-06 21:08:16 +00001268 int block;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00001269 block = PyInt_AsLong(arg);
1270 if (block == -1 && PyErr_Occurred())
Guido van Rossum67f7a382002-06-06 21:08:16 +00001271 return NULL;
1272 Py_BEGIN_ALLOW_THREADS
1273 socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
1274 Py_END_ALLOW_THREADS
Guido van Rossum48a680c2001-03-02 06:34:14 +00001275
Guido van Rossum67f7a382002-06-06 21:08:16 +00001276 Py_INCREF(Py_None);
1277 return Py_None;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001278}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001279PyDoc_STRVAR(sleeptaskw_doc,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001280"sleeptaskw(flag)\n\
1281\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001282Allow sleeps in taskwindows.");
Guido van Rossum48a680c2001-03-02 06:34:14 +00001283#endif
1284
1285
Guido van Rossumaee08791992-09-08 09:05:33 +00001286/* s.setsockopt() method.
1287 With an integer third argument, sets an integer option.
1288 With a string third argument, sets an option from a buffer;
1289 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001290
Guido van Rossum73624e91994-10-10 17:59:00 +00001291static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001292sock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001293{
1294 int level;
1295 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001296 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +00001297 char *buf;
1298 int buflen;
1299 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001300
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001301 if (PyArg_ParseTuple(args, "iii:setsockopt",
1302 &level, &optname, &flag)) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001303 buf = (char *) &flag;
1304 buflen = sizeof flag;
1305 }
1306 else {
Guido van Rossum73624e91994-10-10 17:59:00 +00001307 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001308 if (!PyArg_ParseTuple(args, "iis#:setsockopt",
1309 &level, &optname, &buf, &buflen))
Guido van Rossumaee08791992-09-08 09:05:33 +00001310 return NULL;
1311 }
Thomas Wouters334fb892000-07-25 12:56:38 +00001312 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001313 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001314 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001315 Py_INCREF(Py_None);
1316 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001317}
1318
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001319PyDoc_STRVAR(setsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001320"setsockopt(level, option, value)\n\
1321\n\
1322Set a socket option. See the Unix manual for level and option.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001323The value argument can either be an integer or a string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001324
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001325
Guido van Rossumaee08791992-09-08 09:05:33 +00001326/* s.getsockopt() method.
1327 With two arguments, retrieves an integer option.
1328 With a third integer argument, retrieves a string buffer of that size;
1329 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001330
Guido van Rossum73624e91994-10-10 17:59:00 +00001331static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001332sock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001333{
1334 int level;
1335 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001336 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +00001337 PyObject *buf;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001338 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001339
Guido van Rossumbcc20741998-08-04 22:53:56 +00001340#ifdef __BEOS__
Fred Drake564a6cc2001-05-11 20:12:26 +00001341 /* We have incomplete socket support. */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001342 PyErr_SetString(socket_error, "getsockopt not supported");
Guido van Rossumbcc20741998-08-04 22:53:56 +00001343 return NULL;
1344#else
1345
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001346 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
1347 &level, &optname, &buflen))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001348 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001349
Guido van Rossumbe32c891996-06-20 16:25:29 +00001350 if (buflen == 0) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001351 int flag = 0;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001352 socklen_t flagsize = sizeof flag;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001353 res = getsockopt(s->sock_fd, level, optname,
Thomas Wouters334fb892000-07-25 12:56:38 +00001354 (void *)&flag, &flagsize);
Guido van Rossumaee08791992-09-08 09:05:33 +00001355 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001356 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001357 return PyInt_FromLong(flag);
Guido van Rossumaee08791992-09-08 09:05:33 +00001358 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001359#ifdef __VMS
1360 if (buflen > 1024) {
1361#else
Guido van Rossumaee08791992-09-08 09:05:33 +00001362 if (buflen <= 0 || buflen > 1024) {
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001363#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001364 PyErr_SetString(socket_error,
Barry Warsaw752300b1997-01-03 17:18:10 +00001365 "getsockopt buflen out of range");
Guido van Rossumaee08791992-09-08 09:05:33 +00001366 return NULL;
1367 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001368 buf = PyString_FromStringAndSize((char *)NULL, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001369 if (buf == NULL)
1370 return NULL;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001371 res = getsockopt(s->sock_fd, level, optname,
Fred Drake564a6cc2001-05-11 20:12:26 +00001372 (void *)PyString_AS_STRING(buf), &buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001373 if (res < 0) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001374 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001375 return s->errorhandler();
Guido van Rossumaee08791992-09-08 09:05:33 +00001376 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001377 _PyString_Resize(&buf, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001378 return buf;
Guido van Rossumbcc20741998-08-04 22:53:56 +00001379#endif /* __BEOS__ */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001380}
1381
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001382PyDoc_STRVAR(getsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001383"getsockopt(level, option[, buffersize]) -> value\n\
1384\n\
1385Get a socket option. See the Unix manual for level and option.\n\
1386If a nonzero buffersize argument is given, the return value is a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001387string of that length; otherwise it is an integer.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001388
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001389
Fred Drake728819a2000-07-01 03:40:12 +00001390/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001391
Guido van Rossum73624e91994-10-10 17:59:00 +00001392static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001393sock_bind(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001394{
1395 struct sockaddr *addr;
1396 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001397 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001398
Fred Drake728819a2000-07-01 03:40:12 +00001399 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001400 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001401 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001402 res = bind(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001403 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001404 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001405 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001406 Py_INCREF(Py_None);
1407 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001408}
1409
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001410PyDoc_STRVAR(bind_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001411"bind(address)\n\
1412\n\
1413Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00001414pair (host, port); the host must refer to the local host. For raw packet\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001415sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001416
Guido van Rossum30a685f1991-06-27 15:51:29 +00001417
1418/* s.close() method.
1419 Set the file descriptor to -1 so operations tried subsequently
1420 will surely fail. */
1421
Guido van Rossum73624e91994-10-10 17:59:00 +00001422static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001423sock_close(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001424{
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001425 SOCKET_T fd;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001426
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001427 if ((fd = s->sock_fd) != -1) {
1428 s->sock_fd = -1;
Guido van Rossum08481461996-10-12 14:07:22 +00001429 Py_BEGIN_ALLOW_THREADS
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001430 (void) SOCKETCLOSE(fd);
Guido van Rossum08481461996-10-12 14:07:22 +00001431 Py_END_ALLOW_THREADS
1432 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001433 Py_INCREF(Py_None);
1434 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001435}
1436
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001437PyDoc_STRVAR(close_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001438"close()\n\
1439\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001440Close the socket. It cannot be used after this call.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001441
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001442static int
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001443internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
1444 int *timeoutp)
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001445{
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001446 int res, timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001447
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001448 timeout = 0;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001449 res = connect(s->sock_fd, addr, addrlen);
1450
1451#ifdef MS_WINDOWS
1452
1453 if (s->sock_timeout > 0.0) {
1454 if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK) {
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001455 /* This is a mess. Best solution: trust select */
1456 fd_set fds;
1457 struct timeval tv;
1458 tv.tv_sec = (int)s->sock_timeout;
1459 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
1460 FD_ZERO(&fds);
1461 FD_SET(s->sock_fd, &fds);
1462 res = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001463 if (res == 0) {
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001464 res = WSAEWOULDBLOCK;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001465 timeout = 1;
1466 } else if (res > 0)
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001467 res = 0;
1468 /* else if (res < 0) an error occurred */
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001469 }
1470 }
1471
1472 if (res < 0)
1473 res = WSAGetLastError();
1474
1475#else
1476
1477 if (s->sock_timeout > 0.0) {
1478 if (res < 0 && errno == EINPROGRESS) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001479 timeout = internal_select(s, 1);
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001480 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum12e3c712002-08-08 20:39:30 +00001481 if (res < 0 && errno == EISCONN)
1482 res = 0;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001483 }
1484 }
1485
1486 if (res < 0)
1487 res = errno;
1488
1489#endif
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001490 *timeoutp = timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001491
1492 return res;
1493}
Guido van Rossum30a685f1991-06-27 15:51:29 +00001494
Fred Drake728819a2000-07-01 03:40:12 +00001495/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001496
Guido van Rossum73624e91994-10-10 17:59:00 +00001497static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001498sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001499{
1500 struct sockaddr *addr;
1501 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001502 int res;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001503 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001504
Fred Drake728819a2000-07-01 03:40:12 +00001505 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001506 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001507
Guido van Rossum73624e91994-10-10 17:59:00 +00001508 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001509 res = internal_connect(s, addr, addrlen, &timeout);
Guido van Rossum11ba0942002-06-13 15:07:44 +00001510 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001511
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001512 if (timeout) {
1513 PyErr_SetString(socket_timeout, "timed out");
1514 return NULL;
1515 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001516 if (res != 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001517 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001518 Py_INCREF(Py_None);
1519 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001520}
1521
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001522PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001523"connect(address)\n\
1524\n\
1525Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001526is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001527
Guido van Rossum30a685f1991-06-27 15:51:29 +00001528
Fred Drake728819a2000-07-01 03:40:12 +00001529/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001530
1531static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001532sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001533{
1534 struct sockaddr *addr;
1535 int addrlen;
1536 int res;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001537 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001538
Fred Drake728819a2000-07-01 03:40:12 +00001539 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001540 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001541
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001542 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001543 res = internal_connect(s, addr, addrlen, &timeout);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001544 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001545
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001546 return PyInt_FromLong((long) res);
1547}
1548
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001549PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001550"connect_ex(address) -> errno\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001551\n\
1552This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001553instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001554
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001555
Guido van Rossumed233a51992-06-23 09:07:03 +00001556/* s.fileno() method */
1557
Guido van Rossum73624e91994-10-10 17:59:00 +00001558static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001559sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00001560{
Fred Drakea04eaad2000-06-30 02:46:07 +00001561#if SIZEOF_SOCKET_T <= SIZEOF_LONG
Guido van Rossum73624e91994-10-10 17:59:00 +00001562 return PyInt_FromLong((long) s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +00001563#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001564 return PyLong_FromLongLong((PY_LONG_LONG)s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +00001565#endif
Guido van Rossumed233a51992-06-23 09:07:03 +00001566}
1567
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001568PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001569"fileno() -> integer\n\
1570\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001571Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001572
Guido van Rossumed233a51992-06-23 09:07:03 +00001573
Guido van Rossumbe32c891996-06-20 16:25:29 +00001574#ifndef NO_DUP
1575/* s.dup() method */
1576
1577static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001578sock_dup(PySocketSockObject *s)
Guido van Rossumbe32c891996-06-20 16:25:29 +00001579{
Fred Drakea04eaad2000-06-30 02:46:07 +00001580 SOCKET_T newfd;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001581 PyObject *sock;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001582
Guido van Rossumbe32c891996-06-20 16:25:29 +00001583 newfd = dup(s->sock_fd);
1584 if (newfd < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001585 return s->errorhandler();
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001586 sock = (PyObject *) new_sockobject(newfd,
1587 s->sock_family,
1588 s->sock_type,
1589 s->sock_proto);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001590 if (sock == NULL)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001591 SOCKETCLOSE(newfd);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001592 return sock;
1593}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001594
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001595PyDoc_STRVAR(dup_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001596"dup() -> socket object\n\
1597\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001598Return a new socket object connected to the same system resource.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001599
Guido van Rossumbe32c891996-06-20 16:25:29 +00001600#endif
1601
1602
Guido van Rossumc89705d1992-11-26 08:54:07 +00001603/* s.getsockname() method */
1604
Guido van Rossum73624e91994-10-10 17:59:00 +00001605static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001606sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001607{
1608 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001609 int res;
1610 socklen_t addrlen;
1611
Guido van Rossumc89705d1992-11-26 08:54:07 +00001612 if (!getsockaddrlen(s, &addrlen))
1613 return NULL;
Guido van Rossumca42b161996-01-12 01:36:05 +00001614 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001615 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001616 res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001617 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001618 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001619 return s->errorhandler();
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001620 return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001621}
1622
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001623PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001624"getsockname() -> address info\n\
1625\n\
1626Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001627info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001628
Guido van Rossumc89705d1992-11-26 08:54:07 +00001629
Guido van Rossumb6775db1994-08-01 11:34:53 +00001630#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001631/* s.getpeername() method */
1632
Guido van Rossum73624e91994-10-10 17:59:00 +00001633static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001634sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001635{
1636 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001637 int res;
1638 socklen_t addrlen;
1639
Guido van Rossumc89705d1992-11-26 08:54:07 +00001640 if (!getsockaddrlen(s, &addrlen))
1641 return NULL;
Fred Drakefd168342001-05-09 19:11:33 +00001642 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001643 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001644 res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001645 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001646 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001647 return s->errorhandler();
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001648 return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001649}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001650
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001651PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001652"getpeername() -> address info\n\
1653\n\
1654Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001655info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001656
Guido van Rossumb6775db1994-08-01 11:34:53 +00001657#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001658
1659
Guido van Rossum30a685f1991-06-27 15:51:29 +00001660/* s.listen(n) method */
1661
Guido van Rossum73624e91994-10-10 17:59:00 +00001662static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001663sock_listen(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001664{
1665 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001666 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001667
1668 backlog = PyInt_AsLong(arg);
1669 if (backlog == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001670 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001671 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +00001672 if (backlog < 1)
1673 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001674 res = listen(s->sock_fd, backlog);
Guido van Rossum73624e91994-10-10 17:59:00 +00001675 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001676 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001677 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001678 Py_INCREF(Py_None);
1679 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001680}
1681
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001682PyDoc_STRVAR(listen_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001683"listen(backlog)\n\
1684\n\
1685Enable a server to accept connections. The backlog argument must be at\n\
1686least 1; it specifies the number of unaccepted connection that the system\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001687will allow before refusing new connections.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001688
1689
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001690#ifndef NO_DUP
Guido van Rossum30a685f1991-06-27 15:51:29 +00001691/* s.makefile(mode) method.
1692 Create a new open file object referring to a dupped version of
1693 the socket's file descriptor. (The dup() call is necessary so
1694 that the open file and socket objects may be closed independent
1695 of each other.)
1696 The mode argument specifies 'r' or 'w' passed to fdopen(). */
1697
Guido van Rossum73624e91994-10-10 17:59:00 +00001698static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001699sock_makefile(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001700{
Tim Petersdbd9ba62000-07-09 03:09:57 +00001701 extern int fclose(FILE *);
Guido van Rossum6b144911995-03-14 15:05:13 +00001702 char *mode = "r";
1703 int bufsize = -1;
Fred Drakea04eaad2000-06-30 02:46:07 +00001704#ifdef MS_WIN32
Tim Peters79248aa2001-08-29 21:37:10 +00001705 Py_intptr_t fd;
Fred Drakea04eaad2000-06-30 02:46:07 +00001706#else
Guido van Rossum30a685f1991-06-27 15:51:29 +00001707 int fd;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001708#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001709 FILE *fp;
Guido van Rossum6b144911995-03-14 15:05:13 +00001710 PyObject *f;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001711#ifdef __VMS
1712 char *mode_r = "r";
1713 char *mode_w = "w";
1714#endif
Guido van Rossum6b144911995-03-14 15:05:13 +00001715
Guido van Rossum43713e52000-02-29 13:59:29 +00001716 if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001717 return NULL;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001718#ifdef __VMS
1719 if (strcmp(mode,"rb") == 0) {
1720 mode = mode_r;
1721 }
1722 else {
1723 if (strcmp(mode,"wb") == 0) {
1724 mode = mode_w;
1725 }
1726 }
1727#endif
Guido van Rossum8d665e61996-06-26 18:22:49 +00001728#ifdef MS_WIN32
Guido van Rossum82a5c661998-07-07 20:45:43 +00001729 if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) ||
1730 ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL))
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001731#else
Guido van Rossum82a5c661998-07-07 20:45:43 +00001732 if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL)
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001733#endif
Guido van Rossum82a5c661998-07-07 20:45:43 +00001734 {
Guido van Rossum6b144911995-03-14 15:05:13 +00001735 if (fd >= 0)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001736 SOCKETCLOSE(fd);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001737 return s->errorhandler();
Guido van Rossum6b144911995-03-14 15:05:13 +00001738 }
Jack Jansen6d8898b2002-03-25 15:32:01 +00001739#ifdef USE_GUSI2
1740 /* Workaround for bug in Metrowerks MSL vs. GUSI I/O library */
Guido van Rossum67f7a382002-06-06 21:08:16 +00001741 if (strchr(mode, 'b') != NULL)
Jack Jansen6d8898b2002-03-25 15:32:01 +00001742 bufsize = 0;
1743#endif
Guido van Rossum6b144911995-03-14 15:05:13 +00001744 f = PyFile_FromFile(fp, "<socket>", mode, fclose);
1745 if (f != NULL)
1746 PyFile_SetBufSize(f, bufsize);
1747 return f;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001748}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001749
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001750PyDoc_STRVAR(makefile_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001751"makefile([mode[, buffersize]]) -> file object\n\
1752\n\
1753Return a regular file object corresponding to the socket.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001754The mode and buffersize arguments are as for the built-in open() function.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001755
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001756#endif /* NO_DUP */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001757
Guido van Rossum48a680c2001-03-02 06:34:14 +00001758
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001759/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001760
Guido van Rossum73624e91994-10-10 17:59:00 +00001761static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001762sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001763{
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001764 int len, n = 0, flags = 0, timeout;
Guido van Rossum73624e91994-10-10 17:59:00 +00001765 PyObject *buf;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001766#ifdef __VMS
1767 int read_length;
1768 char *read_buf;
1769#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00001770
Guido van Rossum43713e52000-02-29 13:59:29 +00001771 if (!PyArg_ParseTuple(args, "i|i:recv", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001772 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001773
1774 if (len < 0) {
Martin v. Löwisfba64e12001-11-19 10:41:26 +00001775 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00001776 "negative buffersize in recv");
Martin v. Löwisfba64e12001-11-19 10:41:26 +00001777 return NULL;
1778 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00001779
Guido van Rossum73624e91994-10-10 17:59:00 +00001780 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001781 if (buf == NULL)
1782 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001783
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001784#ifndef __VMS
Guido van Rossum73624e91994-10-10 17:59:00 +00001785 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001786 timeout = internal_select(s, 0);
1787 if (!timeout)
1788 n = recv(s->sock_fd, PyString_AS_STRING(buf), len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001789 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001790
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001791 if (timeout) {
1792 Py_DECREF(buf);
1793 PyErr_SetString(socket_timeout, "timed out");
1794 return NULL;
1795 }
Guido van Rossum7c53b771995-09-13 18:39:47 +00001796 if (n < 0) {
1797 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001798 return s->errorhandler();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001799 }
Tim Peters5de98422002-04-27 18:44:32 +00001800 if (n != len)
1801 _PyString_Resize(&buf, n);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001802#else
1803 read_buf = PyString_AsString(buf);
1804 read_length = len;
1805 while (read_length != 0) {
1806 unsigned int segment;
1807
1808 segment = read_length /SEGMENT_SIZE;
1809 if (segment != 0) {
1810 segment = SEGMENT_SIZE;
1811 }
1812 else {
1813 segment = read_length;
1814 }
1815
1816 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001817 timeout = internal_select(s, 0);
1818 if (!timeout)
1819 n = recv(s->sock_fd, read_buf, segment, flags);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001820 Py_END_ALLOW_THREADS
1821
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001822 if (timeout) {
1823 Py_DECREF(buf);
1824 PyErr_SetString(socket_timeout, "timed out");
1825 return NULL;
1826 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001827 if (n < 0) {
1828 Py_DECREF(buf);
1829 return s->errorhandler();
1830 }
1831 if (n != read_length) {
1832 read_buf += n;
1833 break;
1834 }
1835
1836 read_length -= segment;
1837 read_buf += segment;
1838 }
1839 if (_PyString_Resize(&buf, (read_buf - PyString_AsString(buf))) < 0)
1840 {
1841 return NULL;
1842 }
1843#endif /* !__VMS */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001844 return buf;
1845}
1846
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001847PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001848"recv(buffersize[, flags]) -> data\n\
1849\n\
1850Receive up to buffersize bytes from the socket. For the optional flags\n\
1851argument, see the Unix manual. When no data is available, block until\n\
1852at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001853the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001854
Guido van Rossum30a685f1991-06-27 15:51:29 +00001855
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001856/* s.recvfrom(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001857
Guido van Rossum73624e91994-10-10 17:59:00 +00001858static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001859sock_recvfrom(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001860{
1861 char addrbuf[256];
Barry Warsaw752300b1997-01-03 17:18:10 +00001862 PyObject *buf = NULL;
1863 PyObject *addr = NULL;
1864 PyObject *ret = NULL;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001865 int len, n = 0, flags = 0, timeout;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001866 socklen_t addrlen;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001867
Guido van Rossum43713e52000-02-29 13:59:29 +00001868 if (!PyArg_ParseTuple(args, "i|i:recvfrom", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001869 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001870
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00001871 if (!getsockaddrlen(s, &addrlen))
1872 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001873 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00001874 if (buf == NULL)
1875 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001876
Guido van Rossum73624e91994-10-10 17:59:00 +00001877 Py_BEGIN_ALLOW_THREADS
Fred Drakefd168342001-05-09 19:11:33 +00001878 memset(addrbuf, 0, addrlen);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001879 timeout = internal_select(s, 0);
1880 if (!timeout)
1881 n = recvfrom(s->sock_fd, PyString_AS_STRING(buf), len, flags,
Guido van Rossum8d665e61996-06-26 18:22:49 +00001882#ifndef MS_WINDOWS
Andrew MacIntyreba43e872002-03-03 03:03:52 +00001883#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001884 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00001885#else
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001886 (void *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00001887#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001888#else
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001889 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001890#endif
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001891 );
Guido van Rossum73624e91994-10-10 17:59:00 +00001892 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001893
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001894 if (timeout) {
1895 Py_DECREF(buf);
1896 PyErr_SetString(socket_timeout, "timed out");
1897 return NULL;
1898 }
Guido van Rossum7c53b771995-09-13 18:39:47 +00001899 if (n < 0) {
1900 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001901 return s->errorhandler();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001902 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00001903
Guido van Rossum53a9bf81996-06-11 18:35:24 +00001904 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001905 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001906
Guido van Rossum67f7a382002-06-06 21:08:16 +00001907 if (!(addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001908 addrlen)))
Barry Warsaw752300b1997-01-03 17:18:10 +00001909 goto finally;
1910
Guido van Rossum73624e91994-10-10 17:59:00 +00001911 ret = Py_BuildValue("OO", buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001912
1913finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001914 Py_XDECREF(addr);
1915 Py_XDECREF(buf);
Guido van Rossum6f5afc91993-02-05 09:46:15 +00001916 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001917}
1918
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001919PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001920"recvfrom(buffersize[, flags]) -> (data, address info)\n\
1921\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001922Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001923
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001924/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001925
Guido van Rossum73624e91994-10-10 17:59:00 +00001926static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001927sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001928{
Guido van Rossumff4949e1992-08-05 19:58:53 +00001929 char *buf;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001930 int len, n = 0, flags = 0, timeout;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001931#ifdef __VMS
1932 int send_length;
1933#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00001934
Guido van Rossum43713e52000-02-29 13:59:29 +00001935 if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001936 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001937
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001938#ifndef __VMS
Guido van Rossum73624e91994-10-10 17:59:00 +00001939 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001940 timeout = internal_select(s, 1);
1941 if (!timeout)
1942 n = send(s->sock_fd, buf, len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001943 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001944
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001945 if (timeout) {
1946 PyErr_SetString(socket_timeout, "timed out");
1947 return NULL;
1948 }
Guido van Rossum30a685f1991-06-27 15:51:29 +00001949 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001950 return s->errorhandler();
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001951#else
1952 /* Divide packet into smaller segments for */
1953 /* TCP/IP Services for OpenVMS */
1954 send_length = len;
1955 while (send_length != 0) {
1956 unsigned int segment;
1957
1958 segment = send_length / SEGMENT_SIZE;
1959 if (segment != 0) {
1960 segment = SEGMENT_SIZE;
1961 }
1962 else {
1963 segment = send_length;
1964 }
1965 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001966 timeout = internal_select(s, 1);
1967 if (!timeout)
1968 n = send(s->sock_fd, buf, segment, flags);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001969 Py_END_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001970 if (timeout) {
1971 PyErr_SetString(socket_timeout, "timed out");
1972 return NULL;
1973 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001974 if (n < 0) {
1975 return s->errorhandler();
1976 }
1977 send_length -= segment;
1978 buf += segment;
1979 } /* end while */
1980#endif /* !__VMS */
Guido van Rossum73624e91994-10-10 17:59:00 +00001981 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00001982}
1983
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001984PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001985"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001986\n\
1987Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001988argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001989sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001990
1991
1992/* s.sendall(data [,flags]) method */
1993
1994static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001995sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001996{
1997 char *buf;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001998 int len, n = 0, flags = 0, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001999
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002000 if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags))
2001 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002002
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002003 Py_BEGIN_ALLOW_THREADS
2004 do {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002005 timeout = internal_select(s, 1);
2006 if (timeout)
2007 break;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002008 n = send(s->sock_fd, buf, len, flags);
2009 if (n < 0)
2010 break;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002011 buf += n;
2012 len -= n;
2013 } while (len > 0);
2014 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002015
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002016 if (timeout) {
2017 PyErr_SetString(socket_timeout, "timed out");
2018 return NULL;
2019 }
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002020 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002021 return s->errorhandler();
Guido van Rossum67f7a382002-06-06 21:08:16 +00002022
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002023 Py_INCREF(Py_None);
2024 return Py_None;
2025}
2026
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002027PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002028"sendall(data[, flags])\n\
2029\n\
2030Send a data string to the socket. For the optional flags\n\
2031argument, see the Unix manual. This calls send() repeatedly\n\
2032until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002033to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002034
Guido van Rossum30a685f1991-06-27 15:51:29 +00002035
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002036/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002037
Guido van Rossum73624e91994-10-10 17:59:00 +00002038static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002039sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002040{
Guido van Rossum73624e91994-10-10 17:59:00 +00002041 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002042 char *buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002043 struct sockaddr *addr;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002044 int addrlen, len, n = 0, flags, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002045
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002046 flags = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002047 if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) {
Guido van Rossum73624e91994-10-10 17:59:00 +00002048 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002049 if (!PyArg_ParseTuple(args, "s#iO:sendto",
2050 &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002051 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002052 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002053
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002054 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002055 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002056
Guido van Rossum73624e91994-10-10 17:59:00 +00002057 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002058 timeout = internal_select(s, 1);
2059 if (!timeout)
2060 n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002061 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002062
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002063 if (timeout) {
2064 PyErr_SetString(socket_timeout, "timed out");
2065 return NULL;
2066 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002067 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002068 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002069 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002070}
2071
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002072PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002073"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002074\n\
2075Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002076For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002077
Guido van Rossum30a685f1991-06-27 15:51:29 +00002078
2079/* s.shutdown(how) method */
2080
Guido van Rossum73624e91994-10-10 17:59:00 +00002081static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002082sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002083{
2084 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002085 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002086
2087 how = PyInt_AsLong(arg);
2088 if (how == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002089 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002090 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002091 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +00002092 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002093 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002094 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002095 Py_INCREF(Py_None);
2096 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002097}
2098
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002099PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002100"shutdown(flag)\n\
2101\n\
2102Shut down the reading side of the socket (flag == 0), the writing side\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002103of the socket (flag == 1), or both ends (flag == 2).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002104
Guido van Rossum30a685f1991-06-27 15:51:29 +00002105
2106/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002107
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002108static PyMethodDef sock_methods[] = {
2109 {"accept", (PyCFunction)sock_accept, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002110 accept_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002111 {"bind", (PyCFunction)sock_bind, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002112 bind_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002113 {"close", (PyCFunction)sock_close, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002114 close_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002115 {"connect", (PyCFunction)sock_connect, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002116 connect_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002117 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002118 connect_ex_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00002119#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002120 {"dup", (PyCFunction)sock_dup, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002121 dup_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00002122#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002123 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002124 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002125#ifdef HAVE_GETPEERNAME
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002126 {"getpeername", (PyCFunction)sock_getpeername,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002127 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00002128#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002129 {"getsockname", (PyCFunction)sock_getsockname,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002130 METH_NOARGS, getsockname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002131 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002132 getsockopt_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002133 {"listen", (PyCFunction)sock_listen, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002134 listen_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002135#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002136 {"makefile", (PyCFunction)sock_makefile, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002137 makefile_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002138#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002139 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002140 recv_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002141 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002142 recvfrom_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002143 {"send", (PyCFunction)sock_send, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002144 send_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002145 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002146 sendall_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002147 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002148 sendto_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002149 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002150 setblocking_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002151 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
Guido van Rossum67f7a382002-06-06 21:08:16 +00002152 settimeout_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002153 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
Guido van Rossum67f7a382002-06-06 21:08:16 +00002154 gettimeout_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002155 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002156 setsockopt_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002157 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002158 shutdown_doc},
2159#ifdef RISCOS
Martin v. Löwisa94568a2003-05-10 07:36:56 +00002160 {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002161 sleeptaskw_doc},
2162#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00002163 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002164};
2165
Guido van Rossum30a685f1991-06-27 15:51:29 +00002166
Guido van Rossum73624e91994-10-10 17:59:00 +00002167/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00002168 First close the file description. */
2169
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002170static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002171sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002172{
Guido van Rossumfa972c92000-04-10 12:45:45 +00002173 if (s->sock_fd != -1)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00002174 (void) SOCKETCLOSE(s->sock_fd);
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002175 s->ob_type->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002176}
2177
Guido van Rossum30a685f1991-06-27 15:51:29 +00002178
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002179static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002180sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002181{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002182 char buf[512];
Fred Drakea04eaad2000-06-30 02:46:07 +00002183#if SIZEOF_SOCKET_T > SIZEOF_LONG
2184 if (s->sock_fd > LONG_MAX) {
2185 /* this can occur on Win64, and actually there is a special
2186 ugly printf formatter for decimal pointer length integer
2187 printing, only bother if necessary*/
2188 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002189 "no printf formatter to display "
2190 "the socket descriptor in decimal");
Fred Drakea04eaad2000-06-30 02:46:07 +00002191 return NULL;
2192 }
2193#endif
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002194 PyOS_snprintf(
2195 buf, sizeof(buf),
2196 "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>",
2197 (long)s->sock_fd, s->sock_family,
2198 s->sock_type,
2199 s->sock_proto);
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00002200 return PyString_FromString(buf);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002201}
2202
2203
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002204/* Create a new, uninitialized socket object. */
2205
2206static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002207sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002208{
2209 PyObject *new;
2210
2211 new = type->tp_alloc(type, 0);
Guido van Rossume1c478f2002-06-06 20:08:25 +00002212 if (new != NULL) {
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002213 ((PySocketSockObject *)new)->sock_fd = -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002214 ((PySocketSockObject *)new)->sock_timeout = -1.0;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002215 ((PySocketSockObject *)new)->errorhandler = &set_error;
Guido van Rossume1c478f2002-06-06 20:08:25 +00002216 }
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002217 return new;
2218}
2219
2220
2221/* Initialize a new socket object. */
2222
2223/*ARGSUSED*/
2224static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002225sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002226{
2227 PySocketSockObject *s = (PySocketSockObject *)self;
2228 SOCKET_T fd;
2229 int family = AF_INET, type = SOCK_STREAM, proto = 0;
2230 static char *keywords[] = {"family", "type", "proto", 0};
2231
2232 if (!PyArg_ParseTupleAndKeywords(args, kwds,
2233 "|iii:socket", keywords,
2234 &family, &type, &proto))
2235 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002236
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002237 Py_BEGIN_ALLOW_THREADS
2238 fd = socket(family, type, proto);
2239 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002240
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002241#ifdef MS_WINDOWS
2242 if (fd == INVALID_SOCKET)
2243#else
2244 if (fd < 0)
2245#endif
2246 {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002247 set_error();
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002248 return -1;
2249 }
2250 init_sockobject(s, fd, family, type, proto);
2251 /* From now on, ignore SIGPIPE and let the error checking
2252 do the work. */
2253#ifdef SIGPIPE
2254 (void) signal(SIGPIPE, SIG_IGN);
2255#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002256
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002257 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002258
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002259}
2260
2261
Guido van Rossumb6775db1994-08-01 11:34:53 +00002262/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002263
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002264static PyTypeObject sock_type = {
Guido van Rossum55558541996-05-23 22:54:50 +00002265 PyObject_HEAD_INIT(0) /* Must fill in type value later */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002266 0, /* ob_size */
Guido van Rossumca6dfa52001-10-28 12:31:33 +00002267 "_socket.socket", /* tp_name */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002268 sizeof(PySocketSockObject), /* tp_basicsize */
2269 0, /* tp_itemsize */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002270 (destructor)sock_dealloc, /* tp_dealloc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002271 0, /* tp_print */
2272 0, /* tp_getattr */
2273 0, /* tp_setattr */
2274 0, /* tp_compare */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002275 (reprfunc)sock_repr, /* tp_repr */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002276 0, /* tp_as_number */
2277 0, /* tp_as_sequence */
2278 0, /* tp_as_mapping */
2279 0, /* tp_hash */
2280 0, /* tp_call */
2281 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002282 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002283 0, /* tp_setattro */
2284 0, /* tp_as_buffer */
2285 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002286 sock_doc, /* tp_doc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002287 0, /* tp_traverse */
2288 0, /* tp_clear */
2289 0, /* tp_richcompare */
2290 0, /* tp_weaklistoffset */
2291 0, /* tp_iter */
2292 0, /* tp_iternext */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002293 sock_methods, /* tp_methods */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002294 0, /* tp_members */
2295 0, /* tp_getset */
2296 0, /* tp_base */
2297 0, /* tp_dict */
2298 0, /* tp_descr_get */
2299 0, /* tp_descr_set */
2300 0, /* tp_dictoffset */
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002301 sock_initobj, /* tp_init */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002302 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002303 sock_new, /* tp_new */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002304 PyObject_Del, /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002305};
2306
Guido van Rossum30a685f1991-06-27 15:51:29 +00002307
Guido van Rossum81194471991-07-27 21:42:02 +00002308/* Python interface to gethostname(). */
2309
2310/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002311static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002312socket_gethostname(PyObject *self, PyObject *args)
Guido van Rossum81194471991-07-27 21:42:02 +00002313{
2314 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00002315 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002316 if (!PyArg_ParseTuple(args, ":gethostname"))
Guido van Rossum81194471991-07-27 21:42:02 +00002317 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002318 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002319 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00002320 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002321 if (res < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002322 return set_error();
Guido van Rossum81194471991-07-27 21:42:02 +00002323 buf[sizeof buf - 1] = '\0';
Guido van Rossum73624e91994-10-10 17:59:00 +00002324 return PyString_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00002325}
Guido van Rossumff4949e1992-08-05 19:58:53 +00002326
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002327PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002328"gethostname() -> string\n\
2329\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002330Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002331
Guido van Rossumff4949e1992-08-05 19:58:53 +00002332
Guido van Rossum30a685f1991-06-27 15:51:29 +00002333/* Python interface to gethostbyname(name). */
2334
2335/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002336static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002337socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002338{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002339 char *name;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00002340#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002341 struct sockaddr_storage addrbuf;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00002342#else
2343 struct sockaddr_in addrbuf;
2344#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002345
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002346 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002347 return NULL;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002348 if (setipaddr(name, (struct sockaddr *)&addrbuf, sizeof(addrbuf), AF_INET) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002349 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002350 return makeipaddr((struct sockaddr *)&addrbuf,
2351 sizeof(struct sockaddr_in));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002352}
2353
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002354PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002355"gethostbyname(host) -> address\n\
2356\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002357Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002358
2359
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002360/* Convenience function common to gethostbyname_ex and gethostbyaddr */
2361
2362static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002363gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002364{
2365 char **pch;
2366 PyObject *rtn_tuple = (PyObject *)NULL;
2367 PyObject *name_list = (PyObject *)NULL;
2368 PyObject *addr_list = (PyObject *)NULL;
2369 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002370
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002371 if (h == NULL) {
Guido van Rossum48a680c2001-03-02 06:34:14 +00002372 /* Let's get real error message to return */
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002373#ifndef RISCOS
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002374 set_herror(h_errno);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002375#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002376 PyErr_SetString(socket_error, "host not found");
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002377#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002378 return NULL;
2379 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002380
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002381 if (h->h_addrtype != af) {
2382#ifdef HAVE_STRERROR
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002383 /* Let's get real error message to return */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002384 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002385 (char *)strerror(EAFNOSUPPORT));
2386#else
2387 PyErr_SetString(
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002388 socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002389 "Address family not supported by protocol family");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002390#endif
2391 return NULL;
2392 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002393
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002394 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002395
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002396 case AF_INET:
2397 if (alen < sizeof(struct sockaddr_in))
2398 return NULL;
2399 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002400
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002401#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002402 case AF_INET6:
2403 if (alen < sizeof(struct sockaddr_in6))
2404 return NULL;
2405 break;
2406#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002407
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002408 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002409
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002410 if ((name_list = PyList_New(0)) == NULL)
2411 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002412
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002413 if ((addr_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 for (pch = h->h_aliases; *pch != NULL; pch++) {
2417 int status;
2418 tmp = PyString_FromString(*pch);
2419 if (tmp == NULL)
2420 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002421
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002422 status = PyList_Append(name_list, tmp);
2423 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002424
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002425 if (status)
2426 goto err;
2427 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002428
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002429 for (pch = h->h_addr_list; *pch != NULL; pch++) {
2430 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002431
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002432 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002433
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002434 case AF_INET:
2435 {
2436 struct sockaddr_in sin;
2437 memset(&sin, 0, sizeof(sin));
2438 sin.sin_family = af;
2439#ifdef HAVE_SOCKADDR_SA_LEN
2440 sin.sin_len = sizeof(sin);
2441#endif
2442 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
2443 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002444
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002445 if (pch == h->h_addr_list && alen >= sizeof(sin))
2446 memcpy((char *) addr, &sin, sizeof(sin));
2447 break;
2448 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002449
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002450#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002451 case AF_INET6:
2452 {
2453 struct sockaddr_in6 sin6;
2454 memset(&sin6, 0, sizeof(sin6));
2455 sin6.sin6_family = af;
2456#ifdef HAVE_SOCKADDR_SA_LEN
2457 sin6.sin6_len = sizeof(sin6);
2458#endif
2459 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
2460 tmp = makeipaddr((struct sockaddr *)&sin6,
2461 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002462
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002463 if (pch == h->h_addr_list && alen >= sizeof(sin6))
2464 memcpy((char *) addr, &sin6, sizeof(sin6));
2465 break;
2466 }
2467#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002468
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002469 default: /* can't happen */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002470 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002471 "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002472 return NULL;
2473 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002474
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002475 if (tmp == NULL)
2476 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002477
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002478 status = PyList_Append(addr_list, tmp);
2479 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002480
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002481 if (status)
2482 goto err;
2483 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002484
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002485 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002486
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002487 err:
2488 Py_XDECREF(name_list);
2489 Py_XDECREF(addr_list);
2490 return rtn_tuple;
2491}
2492
2493
2494/* Python interface to gethostbyname_ex(name). */
2495
2496/*ARGSUSED*/
2497static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002498socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002499{
2500 char *name;
2501 struct hostent *h;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00002502#ifdef ENABLE_IPV6
2503 struct sockaddr_storage addr;
2504#else
2505 struct sockaddr_in addr;
2506#endif
Martin v. Löwis9db2f572001-07-23 01:30:10 +00002507 struct sockaddr *sa;
Guido van Rossum955becc1999-03-22 20:14:53 +00002508 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002509#ifdef HAVE_GETHOSTBYNAME_R
2510 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002511#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
2512 struct hostent_data data;
2513#else
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002514 char buf[16384];
2515 int buf_len = (sizeof buf) - 1;
2516 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002517#endif
2518#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002519 int result;
2520#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002521#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002522
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002523 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002524 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00002525 if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002526 return NULL;
2527 Py_BEGIN_ALLOW_THREADS
2528#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002529#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002530 result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
2531 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002532#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002533 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002534#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00002535 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002536 result = gethostbyname_r(name, &hp_allocated, &data);
2537 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002538#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002539#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002540#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002541 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002542#endif
2543 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002544#endif /* HAVE_GETHOSTBYNAME_R */
2545 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002546 /* Some C libraries would require addr.__ss_family instead of
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002547 addr.ss_family.
2548 Therefore, we cast the sockaddr_storage into sockaddr to
2549 access sa_family. */
Martin v. Löwis9db2f572001-07-23 01:30:10 +00002550 sa = (struct sockaddr*)&addr;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002551 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002552 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002553#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002554 PyThread_release_lock(netdb_lock);
Guido van Rossum955becc1999-03-22 20:14:53 +00002555#endif
2556 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002557}
2558
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002559PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002560"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
2561\n\
2562Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002563for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002564
2565
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002566/* Python interface to gethostbyaddr(IP). */
2567
2568/*ARGSUSED*/
2569static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002570socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002571{
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002572#ifdef ENABLE_IPV6
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002573 struct sockaddr_storage addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002574#else
Guido van Rossum48a680c2001-03-02 06:34:14 +00002575 struct sockaddr_in addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002576#endif
2577 struct sockaddr *sa = (struct sockaddr *)&addr;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002578 char *ip_num;
2579 struct hostent *h;
Guido van Rossum3baaa131999-03-22 21:44:51 +00002580 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002581#ifdef HAVE_GETHOSTBYNAME_R
2582 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002583#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
2584 struct hostent_data data;
2585#else
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002586 char buf[16384];
2587 int buf_len = (sizeof buf) - 1;
2588 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002589#endif
2590#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002591 int result;
2592#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002593#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002594 char *ap;
2595 int al;
2596 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002597
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002598 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002599 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00002600 af = AF_UNSPEC;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002601 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002602 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002603 af = sa->sa_family;
2604 ap = NULL;
2605 al = 0;
2606 switch (af) {
2607 case AF_INET:
2608 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
2609 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
2610 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002611#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002612 case AF_INET6:
2613 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
2614 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
2615 break;
2616#endif
2617 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002618 PyErr_SetString(socket_error, "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002619 return NULL;
2620 }
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002621 Py_BEGIN_ALLOW_THREADS
2622#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002623#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002624 result = gethostbyaddr_r(ap, al, af,
2625 &hp_allocated, buf, buf_len,
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002626 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002627#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002628 h = gethostbyaddr_r(ap, al, af,
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002629 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002630#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00002631 memset((void *) &data, '\0', sizeof(data));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002632 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002633 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002634#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002635#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002636#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002637 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002638#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002639 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002640#endif /* HAVE_GETHOSTBYNAME_R */
2641 Py_END_ALLOW_THREADS
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002642 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002643#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002644 PyThread_release_lock(netdb_lock);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002645#endif
2646 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002647}
2648
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002649PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002650"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
2651\n\
2652Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002653for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002654
Guido van Rossum30a685f1991-06-27 15:51:29 +00002655
2656/* Python interface to getservbyname(name).
2657 This only returns the port number, since the other info is already
2658 known or not useful (like the list of aliases). */
2659
2660/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002661static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002662socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002663{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002664 char *name, *proto;
Guido van Rossum30a685f1991-06-27 15:51:29 +00002665 struct servent *sp;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002666 if (!PyArg_ParseTuple(args, "ss:getservbyname", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002667 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002668 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002669 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00002670 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00002671 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002672 PyErr_SetString(socket_error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00002673 return NULL;
2674 }
Guido van Rossum73624e91994-10-10 17:59:00 +00002675 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002676}
2677
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002678PyDoc_STRVAR(getservbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002679"getservbyname(servicename, protocolname) -> integer\n\
2680\n\
2681Return a port number from a service name and protocol name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002682The protocol name should be 'tcp' or 'udp'.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002683
Guido van Rossum30a685f1991-06-27 15:51:29 +00002684
Guido van Rossum3901d851996-12-19 16:35:04 +00002685/* Python interface to getprotobyname(name).
2686 This only returns the protocol number, since the other info is
2687 already known or not useful (like the list of aliases). */
2688
2689/*ARGSUSED*/
2690static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002691socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00002692{
2693 char *name;
2694 struct protoent *sp;
Guido van Rossumbcc20741998-08-04 22:53:56 +00002695#ifdef __BEOS__
2696/* Not available in BeOS yet. - [cjh] */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002697 PyErr_SetString(socket_error, "getprotobyname not supported");
Guido van Rossumbcc20741998-08-04 22:53:56 +00002698 return NULL;
2699#else
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002700 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
Guido van Rossum3901d851996-12-19 16:35:04 +00002701 return NULL;
2702 Py_BEGIN_ALLOW_THREADS
2703 sp = getprotobyname(name);
2704 Py_END_ALLOW_THREADS
2705 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002706 PyErr_SetString(socket_error, "protocol not found");
Guido van Rossum3901d851996-12-19 16:35:04 +00002707 return NULL;
2708 }
2709 return PyInt_FromLong((long) sp->p_proto);
Guido van Rossumbcc20741998-08-04 22:53:56 +00002710#endif
Guido van Rossum3901d851996-12-19 16:35:04 +00002711}
2712
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002713PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002714"getprotobyname(name) -> integer\n\
2715\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002716Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002717
Guido van Rossum3901d851996-12-19 16:35:04 +00002718
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002719#ifndef NO_DUP
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002720/* Create a socket object from a numeric file description.
2721 Useful e.g. if stdin is a socket.
2722 Additional arguments as for socket(). */
2723
2724/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002725static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002726socket_fromfd(PyObject *self, PyObject *args)
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002727{
Guido van Rossum73624e91994-10-10 17:59:00 +00002728 PySocketSockObject *s;
Fred Drakea04eaad2000-06-30 02:46:07 +00002729 SOCKET_T fd;
2730 int family, type, proto = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002731 if (!PyArg_ParseTuple(args, "iii|i:fromfd",
2732 &fd, &family, &type, &proto))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002733 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +00002734 /* Dup the fd so it and the socket can be closed independently */
2735 fd = dup(fd);
2736 if (fd < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002737 return set_error();
2738 s = new_sockobject(fd, family, type, proto);
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002739 /* From now on, ignore SIGPIPE and let the error checking
2740 do the work. */
Guido van Rossum48a680c2001-03-02 06:34:14 +00002741#ifdef SIGPIPE
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002742 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum48a680c2001-03-02 06:34:14 +00002743#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00002744 return (PyObject *) s;
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002745}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002746
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002747PyDoc_STRVAR(fromfd_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002748"fromfd(fd, family, type[, proto]) -> socket object\n\
2749\n\
2750Create a socket object from the given file descriptor.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002751The remaining arguments are the same as for socket().");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002752
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002753#endif /* NO_DUP */
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002754
Guido van Rossum82a5c661998-07-07 20:45:43 +00002755
Guido van Rossum006bf911996-06-12 04:04:55 +00002756static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002757socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00002758{
2759 int x1, x2;
2760
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002761 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00002762 return NULL;
2763 }
2764 x2 = (int)ntohs((short)x1);
2765 return PyInt_FromLong(x2);
2766}
2767
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002768PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002769"ntohs(integer) -> integer\n\
2770\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002771Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002772
2773
Guido van Rossum006bf911996-06-12 04:04:55 +00002774static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002775socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00002776{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002777 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00002778
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002779 if (PyInt_Check(arg)) {
2780 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002781 if (x == (unsigned long) -1 && PyErr_Occurred())
2782 return NULL;
Guido van Rossum006bf911996-06-12 04:04:55 +00002783 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002784 else if (PyLong_Check(arg)) {
2785 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002786 if (x == (unsigned long) -1 && PyErr_Occurred())
2787 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002788#if SIZEOF_LONG > 4
2789 {
2790 unsigned long y;
2791 /* only want the trailing 32 bits */
2792 y = x & 0xFFFFFFFFUL;
2793 if (y ^ x)
2794 return PyErr_Format(PyExc_OverflowError,
2795 "long int larger than 32 bits");
2796 x = y;
Tim Peters58141872002-08-06 22:25:02 +00002797 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002798#endif
2799 }
2800 else
Tim Peters58141872002-08-06 22:25:02 +00002801 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002802 "expected int/long, %s found",
2803 arg->ob_type->tp_name);
2804 if (x == (unsigned long) -1 && PyErr_Occurred())
2805 return NULL;
2806 return PyInt_FromLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00002807}
2808
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002809PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002810"ntohl(integer) -> integer\n\
2811\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002812Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002813
2814
Guido van Rossum006bf911996-06-12 04:04:55 +00002815static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002816socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00002817{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002818 unsigned long x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00002819
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002820 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00002821 return NULL;
2822 }
2823 x2 = (int)htons((short)x1);
2824 return PyInt_FromLong(x2);
2825}
2826
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002827PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002828"htons(integer) -> integer\n\
2829\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002830Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002831
2832
Guido van Rossum006bf911996-06-12 04:04:55 +00002833static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002834socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00002835{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002836 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00002837
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002838 if (PyInt_Check(arg)) {
2839 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002840 if (x == (unsigned long) -1 && PyErr_Occurred())
2841 return NULL;
Guido van Rossum006bf911996-06-12 04:04:55 +00002842 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002843 else if (PyLong_Check(arg)) {
2844 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002845 if (x == (unsigned long) -1 && PyErr_Occurred())
2846 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002847#if SIZEOF_LONG > 4
2848 {
2849 unsigned long y;
2850 /* only want the trailing 32 bits */
2851 y = x & 0xFFFFFFFFUL;
2852 if (y ^ x)
2853 return PyErr_Format(PyExc_OverflowError,
2854 "long int larger than 32 bits");
2855 x = y;
Tim Peters58141872002-08-06 22:25:02 +00002856 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002857#endif
2858 }
2859 else
Tim Peters58141872002-08-06 22:25:02 +00002860 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002861 "expected int/long, %s found",
2862 arg->ob_type->tp_name);
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002863 return PyInt_FromLong(htonl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00002864}
2865
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002866PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002867"htonl(integer) -> integer\n\
2868\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002869Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002870
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002871/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002872
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002873PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002874"inet_aton(string) -> packed 32-bit IP representation\n\
2875\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002876Convert 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 +00002877binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002878
2879static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002880socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002881{
Guido van Rossuma2e48551999-09-09 15:42:59 +00002882#ifndef INADDR_NONE
2883#define INADDR_NONE (-1)
2884#endif
Neal Norwitz88f115b2003-02-13 02:15:42 +00002885#ifdef HAVE_INET_ATON
Guido van Rossumad05cdf2003-02-12 23:08:22 +00002886 struct in_addr buf;
Tim Peters1df9fdd2003-02-13 03:13:40 +00002887#else
2888 /* Have to use inet_addr() instead */
2889 unsigned long packed_addr;
2890#endif
2891 char *ip_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002892
Tim Peters1df9fdd2003-02-13 03:13:40 +00002893 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002894 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00002895
Tim Peters1df9fdd2003-02-13 03:13:40 +00002896
2897#ifdef HAVE_INET_ATON
Guido van Rossumad05cdf2003-02-12 23:08:22 +00002898 if (inet_aton(ip_addr, &buf))
2899 return PyString_FromStringAndSize((char *)(&buf),
2900 sizeof(buf));
2901
2902 PyErr_SetString(socket_error,
2903 "illegal IP address string passed to inet_aton");
2904 return NULL;
2905
Tim Peters1df9fdd2003-02-13 03:13:40 +00002906#else /* ! HAVE_INET_ATON */
Guido van Rossumad05cdf2003-02-12 23:08:22 +00002907 /* XXX Problem here: inet_aton('255.255.255.255') raises
2908 an exception while it should be a valid address. */
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002909 packed_addr = inet_addr(ip_addr);
2910
2911 if (packed_addr == INADDR_NONE) { /* invalid address */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002912 PyErr_SetString(socket_error,
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002913 "illegal IP address string passed to inet_aton");
2914 return NULL;
2915 }
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002916 return PyString_FromStringAndSize((char *) &packed_addr,
2917 sizeof(packed_addr));
Guido van Rossumad05cdf2003-02-12 23:08:22 +00002918#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002919}
2920
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002921PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00002922"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002923\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002924Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002925
2926static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002927socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002928{
2929 char *packed_str;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002930 int addr_len;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002931 struct in_addr packed_addr;
2932
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002933 if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) {
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002934 return NULL;
2935 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00002936
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002937 if (addr_len != sizeof(packed_addr)) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002938 PyErr_SetString(socket_error,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002939 "packed IP wrong length for inet_ntoa");
2940 return NULL;
2941 }
2942
2943 memcpy(&packed_addr, packed_str, addr_len);
2944
2945 return PyString_FromString(inet_ntoa(packed_addr));
2946}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002947
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00002948#ifdef HAVE_INET_PTON
2949
2950PyDoc_STRVAR(inet_pton_doc,
2951"inet_pton(af, ip) -> packed IP address string\n\
2952\n\
2953Convert an IP address from string format to a packed string suitable\n\
2954for use with low-level network functions.");
2955
2956static PyObject *
2957socket_inet_pton(PyObject *self, PyObject *args)
2958{
2959 int af;
2960 char* ip;
2961 int retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00002962#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00002963 char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00002964#else
2965 char packed[sizeof(struct in_addr)];
2966#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00002967 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
2968 return NULL;
2969 }
2970
Martin v. Löwis10649092003-08-05 06:25:06 +00002971#ifndef ENABLE_IPV6
2972 if(af == AF_INET6) {
2973 PyErr_SetString(socket_error,
2974 "can't use AF_INET6, IPv6 is disabled");
2975 return NULL;
2976 }
2977#endif
2978
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00002979 retval = inet_pton(af, ip, packed);
2980 if (retval < 0) {
2981 PyErr_SetFromErrno(socket_error);
2982 return NULL;
2983 } else if (retval == 0) {
2984 PyErr_SetString(socket_error,
2985 "illegal IP address string passed to inet_pton");
2986 return NULL;
2987 } else if (af == AF_INET) {
2988 return PyString_FromStringAndSize(packed,
2989 sizeof(struct in_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00002990#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00002991 } else if (af == AF_INET6) {
2992 return PyString_FromStringAndSize(packed,
2993 sizeof(struct in6_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00002994#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00002995 } else {
2996 PyErr_SetString(socket_error, "unknown address family");
2997 return NULL;
2998 }
2999}
3000
3001PyDoc_STRVAR(inet_ntop_doc,
3002"inet_ntop(af, packed_ip) -> string formatted IP address\n\
3003\n\
3004Convert a packed IP address of the given family to string format.");
3005
3006static PyObject *
3007socket_inet_ntop(PyObject *self, PyObject *args)
3008{
3009 int af;
3010 char* packed;
3011 int len;
3012 const char* retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003013#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003014 char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003015#else
3016 char ip[INET_ADDRSTRLEN + 1];
3017#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003018
3019 /* Guarantee NUL-termination for PyString_FromString() below */
3020 memset((void *) &ip[0], '\0', sizeof(ip) + 1);
3021
3022 if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) {
3023 return NULL;
3024 }
3025
3026 if (af == AF_INET) {
3027 if (len != sizeof(struct in_addr)) {
3028 PyErr_SetString(PyExc_ValueError,
3029 "invalid length of packed IP address string");
3030 return NULL;
3031 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003032#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003033 } else if (af == AF_INET6) {
3034 if (len != sizeof(struct in6_addr)) {
3035 PyErr_SetString(PyExc_ValueError,
3036 "invalid length of packed IP address string");
3037 return NULL;
3038 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003039#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003040 } else {
3041 PyErr_Format(PyExc_ValueError,
3042 "unknown address family %d", af);
3043 return NULL;
3044 }
3045
3046 retval = inet_ntop(af, packed, ip, sizeof(ip));
3047 if (!retval) {
3048 PyErr_SetFromErrno(socket_error);
3049 return NULL;
3050 } else {
3051 return PyString_FromString(retval);
3052 }
3053
3054 /* NOTREACHED */
3055 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
3056 return NULL;
3057}
3058
3059#endif /* HAVE_INET_PTON */
3060
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003061/* Python interface to getaddrinfo(host, port). */
3062
3063/*ARGSUSED*/
3064static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003065socket_getaddrinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003066{
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003067 struct addrinfo hints, *res;
3068 struct addrinfo *res0 = NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003069 PyObject *hobj = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003070 PyObject *pobj = (PyObject *)NULL;
Martin v. Löwisf65b1a12001-08-12 09:28:40 +00003071 char pbuf[30];
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003072 char *hptr, *pptr;
3073 int family, socktype, protocol, flags;
3074 int error;
3075 PyObject *all = (PyObject *)NULL;
3076 PyObject *single = (PyObject *)NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003077 PyObject *idna = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003078
3079 family = socktype = protocol = flags = 0;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003080 family = AF_UNSPEC;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003081 if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo",
3082 &hobj, &pobj, &family, &socktype,
3083 &protocol, &flags)) {
3084 return NULL;
3085 }
3086 if (hobj == Py_None) {
3087 hptr = NULL;
3088 } else if (PyUnicode_Check(hobj)) {
3089 idna = PyObject_CallMethod(hobj, "encode", "s", "idna");
3090 if (!idna)
3091 return NULL;
3092 hptr = PyString_AsString(idna);
3093 } else if (PyString_Check(hobj)) {
3094 hptr = PyString_AsString(hobj);
3095 } else {
3096 PyErr_SetString(PyExc_TypeError,
3097 "getaddrinfo() argument 1 must be string or None");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003098 return NULL;
3099 }
3100 if (PyInt_Check(pobj)) {
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003101 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003102 pptr = pbuf;
3103 } else if (PyString_Check(pobj)) {
3104 pptr = PyString_AsString(pobj);
3105 } else if (pobj == Py_None) {
3106 pptr = (char *)NULL;
3107 } else {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003108 PyErr_SetString(socket_error, "Int or String expected");
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003109 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003110 }
3111 memset(&hints, 0, sizeof(hints));
3112 hints.ai_family = family;
3113 hints.ai_socktype = socktype;
3114 hints.ai_protocol = protocol;
3115 hints.ai_flags = flags;
Just van Rossum1040d2c2003-05-09 07:53:18 +00003116 Py_BEGIN_ALLOW_THREADS
3117 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003118 error = getaddrinfo(hptr, pptr, &hints, &res0);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003119 Py_END_ALLOW_THREADS
3120 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003121 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003122 set_gaierror(error);
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003123 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003124 }
3125
3126 if ((all = PyList_New(0)) == NULL)
3127 goto err;
3128 for (res = res0; res; res = res->ai_next) {
Guido van Rossum716aac02001-10-12 18:59:27 +00003129 PyObject *addr =
3130 makesockaddr(-1, res->ai_addr, res->ai_addrlen);
3131 if (addr == NULL)
3132 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003133 single = Py_BuildValue("iiisO", res->ai_family,
3134 res->ai_socktype, res->ai_protocol,
3135 res->ai_canonname ? res->ai_canonname : "",
Guido van Rossum716aac02001-10-12 18:59:27 +00003136 addr);
3137 Py_DECREF(addr);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003138 if (single == NULL)
3139 goto err;
3140
3141 if (PyList_Append(all, single))
3142 goto err;
3143 Py_XDECREF(single);
3144 }
Martin v. Löwis2548c732003-04-18 10:39:54 +00003145 Py_XDECREF(idna);
Neal Norwitz90128ba2002-08-09 03:37:42 +00003146 if (res0)
3147 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003148 return all;
3149 err:
3150 Py_XDECREF(single);
3151 Py_XDECREF(all);
Martin v. Löwis2548c732003-04-18 10:39:54 +00003152 Py_XDECREF(idna);
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003153 if (res0)
3154 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003155 return (PyObject *)NULL;
3156}
3157
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003158PyDoc_STRVAR(getaddrinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003159"getaddrinfo(host, port [, family, socktype, proto, flags])\n\
3160 -> list of (family, socktype, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003161\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003162Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003163
3164/* Python interface to getnameinfo(sa, flags). */
3165
3166/*ARGSUSED*/
3167static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003168socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003169{
3170 PyObject *sa = (PyObject *)NULL;
3171 int flags;
3172 char *hostp;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003173 int port, flowinfo, scope_id;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003174 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
3175 struct addrinfo hints, *res = NULL;
3176 int error;
3177 PyObject *ret = (PyObject *)NULL;
3178
3179 flags = flowinfo = scope_id = 0;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003180 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003181 return NULL;
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003182 if (!PyArg_ParseTuple(sa, "si|ii",
3183 &hostp, &port, &flowinfo, &scope_id))
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003184 return NULL;
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003185 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003186 memset(&hints, 0, sizeof(hints));
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003187 hints.ai_family = AF_UNSPEC;
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003188 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Just van Rossum1040d2c2003-05-09 07:53:18 +00003189 Py_BEGIN_ALLOW_THREADS
3190 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003191 error = getaddrinfo(hostp, pbuf, &hints, &res);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003192 Py_END_ALLOW_THREADS
3193 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003194 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003195 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003196 goto fail;
3197 }
3198 if (res->ai_next) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003199 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003200 "sockaddr resolved to multiple addresses");
3201 goto fail;
3202 }
3203 switch (res->ai_family) {
3204 case AF_INET:
3205 {
3206 char *t1;
3207 int t2;
3208 if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003209 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003210 "IPv4 sockaddr must be 2 tuple");
3211 goto fail;
3212 }
3213 break;
3214 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003215#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003216 case AF_INET6:
3217 {
3218 struct sockaddr_in6 *sin6;
3219 sin6 = (struct sockaddr_in6 *)res->ai_addr;
3220 sin6->sin6_flowinfo = flowinfo;
3221 sin6->sin6_scope_id = scope_id;
3222 break;
3223 }
3224#endif
3225 }
3226 error = getnameinfo(res->ai_addr, res->ai_addrlen,
3227 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
3228 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003229 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003230 goto fail;
3231 }
3232 ret = Py_BuildValue("ss", hbuf, pbuf);
3233
3234fail:
3235 if (res)
3236 freeaddrinfo(res);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003237 return ret;
3238}
3239
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003240PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003241"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003242\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003243Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003244
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003245
3246/* Python API to getting and setting the default timeout value. */
3247
3248static PyObject *
3249socket_getdefaulttimeout(PyObject *self)
3250{
3251 if (defaulttimeout < 0.0) {
3252 Py_INCREF(Py_None);
3253 return Py_None;
3254 }
3255 else
3256 return PyFloat_FromDouble(defaulttimeout);
3257}
3258
3259PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003260"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003261\n\
3262Returns the default timeout in floating seconds for new socket objects.\n\
3263A value of None indicates that new socket objects have no timeout.\n\
3264When the socket module is first imported, the default is None.");
3265
3266static PyObject *
3267socket_setdefaulttimeout(PyObject *self, PyObject *arg)
3268{
3269 double timeout;
3270
3271 if (arg == Py_None)
3272 timeout = -1.0;
3273 else {
3274 timeout = PyFloat_AsDouble(arg);
3275 if (timeout < 0.0) {
3276 if (!PyErr_Occurred())
3277 PyErr_SetString(PyExc_ValueError,
3278 "Timeout value out of range");
3279 return NULL;
3280 }
3281 }
3282
3283 defaulttimeout = timeout;
3284
3285 Py_INCREF(Py_None);
3286 return Py_None;
3287}
3288
3289PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003290"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003291\n\
3292Set the default timeout in floating seconds for new socket objects.\n\
3293A value of None indicates that new socket objects have no timeout.\n\
3294When the socket module is first imported, the default is None.");
3295
3296
Guido van Rossum30a685f1991-06-27 15:51:29 +00003297/* List of functions exported by this module. */
3298
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003299static PyMethodDef socket_methods[] = {
3300 {"gethostbyname", socket_gethostbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003301 METH_VARARGS, gethostbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003302 {"gethostbyname_ex", socket_gethostbyname_ex,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003303 METH_VARARGS, ghbn_ex_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003304 {"gethostbyaddr", socket_gethostbyaddr,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003305 METH_VARARGS, gethostbyaddr_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003306 {"gethostname", socket_gethostname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003307 METH_VARARGS, gethostname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003308 {"getservbyname", socket_getservbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003309 METH_VARARGS, getservbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003310 {"getprotobyname", socket_getprotobyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003311 METH_VARARGS,getprotobyname_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003312#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003313 {"fromfd", socket_fromfd,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003314 METH_VARARGS, fromfd_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003315#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003316 {"ntohs", socket_ntohs,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003317 METH_VARARGS, ntohs_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003318 {"ntohl", socket_ntohl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003319 METH_O, ntohl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003320 {"htons", socket_htons,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003321 METH_VARARGS, htons_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003322 {"htonl", socket_htonl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003323 METH_O, htonl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003324 {"inet_aton", socket_inet_aton,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003325 METH_VARARGS, inet_aton_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003326 {"inet_ntoa", socket_inet_ntoa,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003327 METH_VARARGS, inet_ntoa_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003328#ifdef HAVE_INET_PTON
3329 {"inet_pton", socket_inet_pton,
3330 METH_VARARGS, inet_pton_doc},
3331 {"inet_ntop", socket_inet_ntop,
3332 METH_VARARGS, inet_ntop_doc},
3333#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003334 {"getaddrinfo", socket_getaddrinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003335 METH_VARARGS, getaddrinfo_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003336 {"getnameinfo", socket_getnameinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003337 METH_VARARGS, getnameinfo_doc},
Guido van Rossum1693ba82002-07-18 21:11:26 +00003338 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003339 METH_NOARGS, getdefaulttimeout_doc},
3340 {"setdefaulttimeout", socket_setdefaulttimeout,
3341 METH_O, setdefaulttimeout_doc},
Guido van Rossum30a685f1991-06-27 15:51:29 +00003342 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003343};
3344
Guido van Rossum30a685f1991-06-27 15:51:29 +00003345
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003346#ifdef RISCOS
3347#define OS_INIT_DEFINED
Guido van Rossumbe32c891996-06-20 16:25:29 +00003348
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003349static int
3350os_init(void)
3351{
3352 _kernel_swi_regs r;
3353
3354 r.r[0] = 0;
3355 _kernel_swi(0x43380, &r, &r);
3356 taskwindow = r.r[0];
3357
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003358 return 1;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003359}
3360
3361#endif /* RISCOS */
3362
3363
3364#ifdef MS_WINDOWS
3365#define OS_INIT_DEFINED
3366
3367/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003368
3369static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003370os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00003371{
3372 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00003373}
3374
3375static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003376os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00003377{
3378 WSADATA WSAData;
3379 int ret;
3380 char buf[100];
3381 ret = WSAStartup(0x0101, &WSAData);
3382 switch (ret) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003383 case 0: /* No error */
Mark Hammond14350ab2003-07-09 04:57:46 +00003384 Py_AtExit(os_cleanup);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003385 return 1; /* Success */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003386 case WSASYSNOTREADY:
3387 PyErr_SetString(PyExc_ImportError,
3388 "WSAStartup failed: network not ready");
3389 break;
3390 case WSAVERNOTSUPPORTED:
3391 case WSAEINVAL:
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003392 PyErr_SetString(
3393 PyExc_ImportError,
3394 "WSAStartup failed: requested version not supported");
Guido van Rossumbe32c891996-06-20 16:25:29 +00003395 break;
3396 default:
Tim Peters885d4572001-11-28 20:27:42 +00003397 PyOS_snprintf(buf, sizeof(buf),
3398 "WSAStartup failed: error code %d", ret);
Guido van Rossumbe32c891996-06-20 16:25:29 +00003399 PyErr_SetString(PyExc_ImportError, buf);
3400 break;
3401 }
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003402 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003403}
3404
Guido van Rossum8d665e61996-06-26 18:22:49 +00003405#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003406
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003407
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003408#ifdef PYOS_OS2
3409#define OS_INIT_DEFINED
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003410
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003411/* Additional initialization for OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003412
3413static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003414os_init(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003415{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003416#ifndef PYCC_GCC
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003417 char reason[64];
3418 int rc = sock_init();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003419
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003420 if (rc == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003421 return 1; /* Success */
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003422 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003423
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003424 PyOS_snprintf(reason, sizeof(reason),
3425 "OS/2 TCP/IP Error# %d", sock_errno());
3426 PyErr_SetString(PyExc_ImportError, reason);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003427
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003428 return 0; /* Failure */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00003429#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003430 /* No need to initialise sockets with GCC/EMX */
3431 return 1; /* Success */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00003432#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003433}
3434
3435#endif /* PYOS_OS2 */
3436
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003437
3438#ifndef OS_INIT_DEFINED
3439static int
3440os_init(void)
3441{
3442 return 1; /* Success */
3443}
3444#endif
3445
3446
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003447/* C API table - always add new things to the end for binary
3448 compatibility. */
3449static
3450PySocketModule_APIObject PySocketModuleAPI =
3451{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003452 &sock_type,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003453};
3454
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003455
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003456/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003457
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003458 This module is actually called "_socket", and there's a wrapper
3459 "socket.py" which implements some additional functionality. On some
3460 platforms (e.g. Windows and OS/2), socket.py also implements a
3461 wrapper for the socket type that provides missing functionality such
3462 as makefile(), dup() and fromfd(). The import of "_socket" may fail
3463 with an ImportError exception if os-specific initialization fails.
3464 On Windows, this does WINSOCK initialization. When WINSOCK is
3465 initialized succesfully, a call to WSACleanup() is scheduled to be
3466 made at exit time.
3467*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003468
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003469PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003470"Implementation module for socket operations.\n\
3471\n\
3472See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003473
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003474PyMODINIT_FUNC
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003475init_socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003476{
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003477 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00003478
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003479 if (!os_init())
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003480 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003481
3482 sock_type.ob_type = &PyType_Type;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003483 m = Py_InitModule3(PySocket_MODULE_NAME,
3484 socket_methods,
3485 socket_doc);
3486
3487 socket_error = PyErr_NewException("socket.error", NULL, NULL);
3488 if (socket_error == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003489 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003490 Py_INCREF(socket_error);
3491 PyModule_AddObject(m, "error", socket_error);
3492 socket_herror = PyErr_NewException("socket.herror",
3493 socket_error, NULL);
3494 if (socket_herror == NULL)
3495 return;
3496 Py_INCREF(socket_herror);
3497 PyModule_AddObject(m, "herror", socket_herror);
3498 socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003499 NULL);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003500 if (socket_gaierror == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003501 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003502 Py_INCREF(socket_gaierror);
3503 PyModule_AddObject(m, "gaierror", socket_gaierror);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00003504 socket_timeout = PyErr_NewException("socket.timeout",
3505 socket_error, NULL);
3506 if (socket_timeout == NULL)
3507 return;
3508 Py_INCREF(socket_timeout);
3509 PyModule_AddObject(m, "timeout", socket_timeout);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003510 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00003511 if (PyModule_AddObject(m, "SocketType",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003512 (PyObject *)&sock_type) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003513 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003514 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00003515 if (PyModule_AddObject(m, "socket",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003516 (PyObject *)&sock_type) != 0)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003517 return;
Guido van Rossum09be4091999-08-09 14:40:40 +00003518
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003519#ifdef ENABLE_IPV6
3520 has_ipv6 = Py_True;
3521#else
3522 has_ipv6 = Py_False;
3523#endif
3524 Py_INCREF(has_ipv6);
3525 PyModule_AddObject(m, "has_ipv6", has_ipv6);
3526
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003527 /* Export C API */
Fred Drake4baedc12002-04-01 14:53:37 +00003528 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003529 PyCObject_FromVoidPtr((void *)&PySocketModuleAPI, NULL)
3530 ) != 0)
3531 return;
3532
Guido van Rossum09be4091999-08-09 14:40:40 +00003533 /* Address families (we only support AF_INET and AF_UNIX) */
3534#ifdef AF_UNSPEC
Fred Drake4baedc12002-04-01 14:53:37 +00003535 PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00003536#endif
Fred Drake4baedc12002-04-01 14:53:37 +00003537 PyModule_AddIntConstant(m, "AF_INET", AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003538#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00003539 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003540#endif /* AF_INET6 */
Andrew MacIntyre34d3e2d2003-01-02 12:45:34 +00003541#if defined(AF_UNIX) && !defined(PYOS_OS2)
Fred Drake4baedc12002-04-01 14:53:37 +00003542 PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003543#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00003544#ifdef AF_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00003545 /* Amateur Radio AX.25 */
3546 PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003547#endif
3548#ifdef AF_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00003549 PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00003550#endif
3551#ifdef AF_APPLETALK
Fred Drake4baedc12002-04-01 14:53:37 +00003552 /* Appletalk DDP */
3553 PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00003554#endif
3555#ifdef AF_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00003556 /* Amateur radio NetROM */
3557 PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00003558#endif
3559#ifdef AF_BRIDGE
Fred Drake4baedc12002-04-01 14:53:37 +00003560 /* Multiprotocol bridge */
3561 PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003562#endif
3563#ifdef AF_AAL5
Fred Drake4baedc12002-04-01 14:53:37 +00003564 /* Reserved for Werner's ATM */
3565 PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00003566#endif
3567#ifdef AF_X25
Fred Drake4baedc12002-04-01 14:53:37 +00003568 /* Reserved for X.25 project */
3569 PyModule_AddIntConstant(m, "AF_X25", AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003570#endif
3571#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00003572 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00003573#endif
3574#ifdef AF_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00003575 /* Amateur Radio X.25 PLP */
3576 PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003577#endif
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00003578#ifdef HAVE_NETPACKET_PACKET_H
Fred Drake4baedc12002-04-01 14:53:37 +00003579 PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET);
3580 PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET);
3581 PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST);
3582 PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST);
3583 PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST);
3584 PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST);
3585 PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING);
3586 PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK);
3587 PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00003588#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003589
3590 /* Socket types */
Fred Drake4baedc12002-04-01 14:53:37 +00003591 PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
3592 PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00003593#ifndef __BEOS__
3594/* We have incomplete socket support. */
Fred Drake4baedc12002-04-01 14:53:37 +00003595 PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
3596 PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003597#if defined(SOCK_RDM)
Fred Drake4baedc12002-04-01 14:53:37 +00003598 PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00003599#endif
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003600#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003601
3602#ifdef SO_DEBUG
Fred Drake4baedc12002-04-01 14:53:37 +00003603 PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003604#endif
3605#ifdef SO_ACCEPTCONN
Fred Drake4baedc12002-04-01 14:53:37 +00003606 PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003607#endif
3608#ifdef SO_REUSEADDR
Fred Drake4baedc12002-04-01 14:53:37 +00003609 PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003610#endif
3611#ifdef SO_KEEPALIVE
Fred Drake4baedc12002-04-01 14:53:37 +00003612 PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003613#endif
3614#ifdef SO_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00003615 PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003616#endif
3617#ifdef SO_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00003618 PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003619#endif
3620#ifdef SO_USELOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00003621 PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003622#endif
3623#ifdef SO_LINGER
Fred Drake4baedc12002-04-01 14:53:37 +00003624 PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003625#endif
3626#ifdef SO_OOBINLINE
Fred Drake4baedc12002-04-01 14:53:37 +00003627 PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003628#endif
3629#ifdef SO_REUSEPORT
Fred Drake4baedc12002-04-01 14:53:37 +00003630 PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003631#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003632#ifdef SO_SNDBUF
Fred Drake4baedc12002-04-01 14:53:37 +00003633 PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003634#endif
3635#ifdef SO_RCVBUF
Fred Drake4baedc12002-04-01 14:53:37 +00003636 PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003637#endif
3638#ifdef SO_SNDLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00003639 PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003640#endif
3641#ifdef SO_RCVLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00003642 PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003643#endif
3644#ifdef SO_SNDTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00003645 PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003646#endif
3647#ifdef SO_RCVTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00003648 PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003649#endif
3650#ifdef SO_ERROR
Fred Drake4baedc12002-04-01 14:53:37 +00003651 PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003652#endif
3653#ifdef SO_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00003654 PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003655#endif
3656
3657 /* Maximum number of connections for "listen" */
3658#ifdef SOMAXCONN
Fred Drake4baedc12002-04-01 14:53:37 +00003659 PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003660#else
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003661 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003662#endif
3663
3664 /* Flags for send, recv */
3665#ifdef MSG_OOB
Fred Drake4baedc12002-04-01 14:53:37 +00003666 PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003667#endif
3668#ifdef MSG_PEEK
Fred Drake4baedc12002-04-01 14:53:37 +00003669 PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003670#endif
3671#ifdef MSG_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00003672 PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003673#endif
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00003674#ifdef MSG_DONTWAIT
Fred Drake4baedc12002-04-01 14:53:37 +00003675 PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00003676#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003677#ifdef MSG_EOR
Fred Drake4baedc12002-04-01 14:53:37 +00003678 PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003679#endif
3680#ifdef MSG_TRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00003681 PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003682#endif
3683#ifdef MSG_CTRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00003684 PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003685#endif
3686#ifdef MSG_WAITALL
Fred Drake4baedc12002-04-01 14:53:37 +00003687 PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003688#endif
3689#ifdef MSG_BTAG
Fred Drake4baedc12002-04-01 14:53:37 +00003690 PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003691#endif
3692#ifdef MSG_ETAG
Fred Drake4baedc12002-04-01 14:53:37 +00003693 PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003694#endif
3695
3696 /* Protocol level and numbers, usable for [gs]etsockopt */
3697#ifdef SOL_SOCKET
Fred Drake4baedc12002-04-01 14:53:37 +00003698 PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003699#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003700#ifdef SOL_IP
Fred Drake4baedc12002-04-01 14:53:37 +00003701 PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003702#else
Fred Drake4baedc12002-04-01 14:53:37 +00003703 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00003704#endif
3705#ifdef SOL_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00003706 PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00003707#endif
3708#ifdef SOL_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00003709 PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003710#endif
3711#ifdef SOL_ATALK
Fred Drake4baedc12002-04-01 14:53:37 +00003712 PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00003713#endif
3714#ifdef SOL_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00003715 PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00003716#endif
3717#ifdef SOL_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00003718 PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003719#endif
3720#ifdef SOL_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00003721 PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003722#else
Fred Drake4baedc12002-04-01 14:53:37 +00003723 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00003724#endif
3725#ifdef SOL_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00003726 PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003727#else
Fred Drake4baedc12002-04-01 14:53:37 +00003728 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00003729#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003730#ifdef IPPROTO_IP
Fred Drake4baedc12002-04-01 14:53:37 +00003731 PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00003732#else
Fred Drake4baedc12002-04-01 14:53:37 +00003733 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003734#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003735#ifdef IPPROTO_HOPOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003736 PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003737#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003738#ifdef IPPROTO_ICMP
Fred Drake4baedc12002-04-01 14:53:37 +00003739 PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00003740#else
Fred Drake4baedc12002-04-01 14:53:37 +00003741 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003742#endif
3743#ifdef IPPROTO_IGMP
Fred Drake4baedc12002-04-01 14:53:37 +00003744 PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003745#endif
3746#ifdef IPPROTO_GGP
Fred Drake4baedc12002-04-01 14:53:37 +00003747 PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003748#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003749#ifdef IPPROTO_IPV4
Fred Drake4baedc12002-04-01 14:53:37 +00003750 PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003751#endif
Martin v. Löwisa0f17342003-10-03 13:56:20 +00003752#ifdef IPPROTO_IPV6
3753 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
3754#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003755#ifdef IPPROTO_IPIP
Fred Drake4baedc12002-04-01 14:53:37 +00003756 PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003757#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003758#ifdef IPPROTO_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00003759 PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00003760#else
Fred Drake4baedc12002-04-01 14:53:37 +00003761 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003762#endif
3763#ifdef IPPROTO_EGP
Fred Drake4baedc12002-04-01 14:53:37 +00003764 PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003765#endif
3766#ifdef IPPROTO_PUP
Fred Drake4baedc12002-04-01 14:53:37 +00003767 PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003768#endif
3769#ifdef IPPROTO_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00003770 PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00003771#else
Fred Drake4baedc12002-04-01 14:53:37 +00003772 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003773#endif
3774#ifdef IPPROTO_IDP
Fred Drake4baedc12002-04-01 14:53:37 +00003775 PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003776#endif
3777#ifdef IPPROTO_HELLO
Fred Drake4baedc12002-04-01 14:53:37 +00003778 PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003779#endif
3780#ifdef IPPROTO_ND
Fred Drake4baedc12002-04-01 14:53:37 +00003781 PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003782#endif
3783#ifdef IPPROTO_TP
Fred Drake4baedc12002-04-01 14:53:37 +00003784 PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003785#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003786#ifdef IPPROTO_IPV6
Fred Drake4baedc12002-04-01 14:53:37 +00003787 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003788#endif
3789#ifdef IPPROTO_ROUTING
Fred Drake4baedc12002-04-01 14:53:37 +00003790 PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003791#endif
3792#ifdef IPPROTO_FRAGMENT
Fred Drake4baedc12002-04-01 14:53:37 +00003793 PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003794#endif
3795#ifdef IPPROTO_RSVP
Fred Drake4baedc12002-04-01 14:53:37 +00003796 PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003797#endif
3798#ifdef IPPROTO_GRE
Fred Drake4baedc12002-04-01 14:53:37 +00003799 PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003800#endif
3801#ifdef IPPROTO_ESP
Fred Drake4baedc12002-04-01 14:53:37 +00003802 PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003803#endif
3804#ifdef IPPROTO_AH
Fred Drake4baedc12002-04-01 14:53:37 +00003805 PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003806#endif
3807#ifdef IPPROTO_MOBILE
Fred Drake4baedc12002-04-01 14:53:37 +00003808 PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003809#endif
3810#ifdef IPPROTO_ICMPV6
Fred Drake4baedc12002-04-01 14:53:37 +00003811 PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003812#endif
3813#ifdef IPPROTO_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00003814 PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003815#endif
3816#ifdef IPPROTO_DSTOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003817 PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003818#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003819#ifdef IPPROTO_XTP
Fred Drake4baedc12002-04-01 14:53:37 +00003820 PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003821#endif
3822#ifdef IPPROTO_EON
Fred Drake4baedc12002-04-01 14:53:37 +00003823 PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003824#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003825#ifdef IPPROTO_PIM
Fred Drake4baedc12002-04-01 14:53:37 +00003826 PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003827#endif
3828#ifdef IPPROTO_IPCOMP
Fred Drake4baedc12002-04-01 14:53:37 +00003829 PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003830#endif
3831#ifdef IPPROTO_VRRP
Fred Drake4baedc12002-04-01 14:53:37 +00003832 PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003833#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003834#ifdef IPPROTO_BIP
Fred Drake4baedc12002-04-01 14:53:37 +00003835 PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003836#endif
3837/**/
3838#ifdef IPPROTO_RAW
Fred Drake4baedc12002-04-01 14:53:37 +00003839 PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00003840#else
Fred Drake4baedc12002-04-01 14:53:37 +00003841 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003842#endif
3843#ifdef IPPROTO_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00003844 PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003845#endif
3846
3847 /* Some port configuration */
3848#ifdef IPPORT_RESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00003849 PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003850#else
Fred Drake4baedc12002-04-01 14:53:37 +00003851 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003852#endif
3853#ifdef IPPORT_USERRESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00003854 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003855#else
Fred Drake4baedc12002-04-01 14:53:37 +00003856 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003857#endif
3858
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003859 /* Some reserved IP v.4 addresses */
3860#ifdef INADDR_ANY
Fred Drake4baedc12002-04-01 14:53:37 +00003861 PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003862#else
Fred Drake4baedc12002-04-01 14:53:37 +00003863 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003864#endif
3865#ifdef INADDR_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00003866 PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003867#else
Fred Drake4baedc12002-04-01 14:53:37 +00003868 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003869#endif
3870#ifdef INADDR_LOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00003871 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003872#else
Fred Drake4baedc12002-04-01 14:53:37 +00003873 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003874#endif
3875#ifdef INADDR_UNSPEC_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003876 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003877#else
Fred Drake4baedc12002-04-01 14:53:37 +00003878 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003879#endif
3880#ifdef INADDR_ALLHOSTS_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003881 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
3882 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003883#else
Fred Drake4baedc12002-04-01 14:53:37 +00003884 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003885#endif
3886#ifdef INADDR_MAX_LOCAL_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003887 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
3888 INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003889#else
Fred Drake4baedc12002-04-01 14:53:37 +00003890 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003891#endif
3892#ifdef INADDR_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00003893 PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003894#else
Fred Drake4baedc12002-04-01 14:53:37 +00003895 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003896#endif
3897
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003898 /* IPv4 [gs]etsockopt options */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003899#ifdef IP_OPTIONS
Fred Drake4baedc12002-04-01 14:53:37 +00003900 PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003901#endif
3902#ifdef IP_HDRINCL
Fred Drake4baedc12002-04-01 14:53:37 +00003903 PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003904#endif
3905#ifdef IP_TOS
Fred Drake4baedc12002-04-01 14:53:37 +00003906 PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003907#endif
3908#ifdef IP_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00003909 PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003910#endif
3911#ifdef IP_RECVOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003912 PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003913#endif
3914#ifdef IP_RECVRETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003915 PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003916#endif
3917#ifdef IP_RECVDSTADDR
Fred Drake4baedc12002-04-01 14:53:37 +00003918 PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003919#endif
3920#ifdef IP_RETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003921 PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003922#endif
3923#ifdef IP_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00003924 PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003925#endif
3926#ifdef IP_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00003927 PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003928#endif
3929#ifdef IP_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00003930 PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003931#endif
3932#ifdef IP_ADD_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00003933 PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003934#endif
3935#ifdef IP_DROP_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00003936 PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003937#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003938#ifdef IP_DEFAULT_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00003939 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
3940 IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00003941#endif
3942#ifdef IP_DEFAULT_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00003943 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
3944 IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003945#endif
3946#ifdef IP_MAX_MEMBERSHIPS
Fred Drake4baedc12002-04-01 14:53:37 +00003947 PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00003948#endif
3949
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003950 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
3951#ifdef IPV6_JOIN_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003952 PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003953#endif
3954#ifdef IPV6_LEAVE_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003955 PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003956#endif
3957#ifdef IPV6_MULTICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00003958 PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003959#endif
3960#ifdef IPV6_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00003961 PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003962#endif
3963#ifdef IPV6_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00003964 PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003965#endif
3966#ifdef IPV6_UNICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00003967 PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003968#endif
3969
Guido van Rossum09be4091999-08-09 14:40:40 +00003970 /* TCP options */
3971#ifdef TCP_NODELAY
Fred Drake4baedc12002-04-01 14:53:37 +00003972 PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00003973#endif
3974#ifdef TCP_MAXSEG
Fred Drake4baedc12002-04-01 14:53:37 +00003975 PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00003976#endif
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003977#ifdef TCP_CORK
Fred Drake4baedc12002-04-01 14:53:37 +00003978 PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003979#endif
3980#ifdef TCP_KEEPIDLE
Fred Drake4baedc12002-04-01 14:53:37 +00003981 PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003982#endif
3983#ifdef TCP_KEEPINTVL
Fred Drake4baedc12002-04-01 14:53:37 +00003984 PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003985#endif
3986#ifdef TCP_KEEPCNT
Fred Drake4baedc12002-04-01 14:53:37 +00003987 PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003988#endif
3989#ifdef TCP_SYNCNT
Fred Drake4baedc12002-04-01 14:53:37 +00003990 PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003991#endif
3992#ifdef TCP_LINGER2
Fred Drake4baedc12002-04-01 14:53:37 +00003993 PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003994#endif
3995#ifdef TCP_DEFER_ACCEPT
Fred Drake4baedc12002-04-01 14:53:37 +00003996 PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003997#endif
3998#ifdef TCP_WINDOW_CLAMP
Fred Drake4baedc12002-04-01 14:53:37 +00003999 PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004000#endif
4001#ifdef TCP_INFO
Fred Drake4baedc12002-04-01 14:53:37 +00004002 PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004003#endif
4004#ifdef TCP_QUICKACK
Fred Drake4baedc12002-04-01 14:53:37 +00004005 PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004006#endif
4007
Guido van Rossum09be4091999-08-09 14:40:40 +00004008
4009 /* IPX options */
4010#ifdef IPX_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004011 PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004012#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004013
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004014 /* get{addr,name}info parameters */
4015#ifdef EAI_ADDRFAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004016 PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004017#endif
4018#ifdef EAI_AGAIN
Fred Drake4baedc12002-04-01 14:53:37 +00004019 PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004020#endif
4021#ifdef EAI_BADFLAGS
Fred Drake4baedc12002-04-01 14:53:37 +00004022 PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004023#endif
4024#ifdef EAI_FAIL
Fred Drake4baedc12002-04-01 14:53:37 +00004025 PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004026#endif
4027#ifdef EAI_FAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004028 PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004029#endif
4030#ifdef EAI_MEMORY
Fred Drake4baedc12002-04-01 14:53:37 +00004031 PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004032#endif
4033#ifdef EAI_NODATA
Fred Drake4baedc12002-04-01 14:53:37 +00004034 PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004035#endif
4036#ifdef EAI_NONAME
Fred Drake4baedc12002-04-01 14:53:37 +00004037 PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004038#endif
4039#ifdef EAI_SERVICE
Fred Drake4baedc12002-04-01 14:53:37 +00004040 PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004041#endif
4042#ifdef EAI_SOCKTYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004043 PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004044#endif
4045#ifdef EAI_SYSTEM
Fred Drake4baedc12002-04-01 14:53:37 +00004046 PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004047#endif
4048#ifdef EAI_BADHINTS
Fred Drake4baedc12002-04-01 14:53:37 +00004049 PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004050#endif
4051#ifdef EAI_PROTOCOL
Fred Drake4baedc12002-04-01 14:53:37 +00004052 PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004053#endif
4054#ifdef EAI_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00004055 PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004056#endif
4057#ifdef AI_PASSIVE
Fred Drake4baedc12002-04-01 14:53:37 +00004058 PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004059#endif
4060#ifdef AI_CANONNAME
Fred Drake4baedc12002-04-01 14:53:37 +00004061 PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004062#endif
4063#ifdef AI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004064 PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004065#endif
4066#ifdef AI_MASK
Fred Drake4baedc12002-04-01 14:53:37 +00004067 PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004068#endif
4069#ifdef AI_ALL
Fred Drake4baedc12002-04-01 14:53:37 +00004070 PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004071#endif
4072#ifdef AI_V4MAPPED_CFG
Fred Drake4baedc12002-04-01 14:53:37 +00004073 PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004074#endif
4075#ifdef AI_ADDRCONFIG
Fred Drake4baedc12002-04-01 14:53:37 +00004076 PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004077#endif
4078#ifdef AI_V4MAPPED
Fred Drake4baedc12002-04-01 14:53:37 +00004079 PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004080#endif
4081#ifdef AI_DEFAULT
Fred Drake4baedc12002-04-01 14:53:37 +00004082 PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004083#endif
4084#ifdef NI_MAXHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004085 PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004086#endif
4087#ifdef NI_MAXSERV
Fred Drake4baedc12002-04-01 14:53:37 +00004088 PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004089#endif
4090#ifdef NI_NOFQDN
Fred Drake4baedc12002-04-01 14:53:37 +00004091 PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004092#endif
4093#ifdef NI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004094 PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004095#endif
4096#ifdef NI_NAMEREQD
Fred Drake4baedc12002-04-01 14:53:37 +00004097 PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004098#endif
4099#ifdef NI_NUMERICSERV
Fred Drake4baedc12002-04-01 14:53:37 +00004100 PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004101#endif
4102#ifdef NI_DGRAM
Fred Drake4baedc12002-04-01 14:53:37 +00004103 PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004104#endif
4105
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004106 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00004107#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
4108 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004109#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004110}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004111
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004112
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004113#ifndef HAVE_INET_PTON
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004114
4115/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004116/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004117
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004118int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004119inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004120{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004121 if (af == AF_INET) {
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004122 long packed_addr;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004123 packed_addr = inet_addr(src);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004124 if (packed_addr == INADDR_NONE)
4125 return 0;
4126 memcpy(dst, &packed_addr, 4);
4127 return 1;
4128 }
4129 /* Should set errno to EAFNOSUPPORT */
4130 return -1;
4131}
4132
Martin v. Löwisc925b1532001-07-21 09:42:15 +00004133const char *
4134inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004135{
4136 if (af == AF_INET) {
4137 struct in_addr packed_addr;
4138 if (size < 16)
4139 /* Should set errno to ENOSPC. */
4140 return NULL;
4141 memcpy(&packed_addr, src, sizeof(packed_addr));
4142 return strncpy(dst, inet_ntoa(packed_addr), size);
4143 }
4144 /* Should set errno to EAFNOSUPPORT */
4145 return NULL;
4146}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004147
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004148#endif