blob: 7efb890ff5d4927cf749c3a7712f7b659cb29166 [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
Michael W. Hudson76b8cc82004-05-26 17:06:31 +000064#if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI
65#define _BSD_TYPES
66#endif
67
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000068#include "Python.h"
69
Guido van Rossum47dfa4a2003-04-25 05:48:32 +000070#undef MAX
71#define MAX(x, y) ((x) < (y) ? (y) : (x))
72
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000073/* Socket object documentation */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000074PyDoc_STRVAR(sock_doc,
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000075"socket([family[, type[, proto]]]) -> socket object\n\
76\n\
77Open a socket of the given type. The family argument specifies the\n\
78address family; it defaults to AF_INET. The type argument specifies\n\
79whether this is a stream (SOCK_STREAM, this is the default)\n\
80or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\
81specifying the default protocol. Keyword arguments are accepted.\n\
82\n\
83A socket object represents one endpoint of a network connection.\n\
84\n\
85Methods of socket objects (keyword arguments not allowed):\n\
86\n\
87accept() -- accept a connection, returning new socket and client address\n\
88bind(addr) -- bind the socket to a local address\n\
89close() -- close the socket\n\
90connect(addr) -- connect the socket to a remote address\n\
91connect_ex(addr) -- connect, return an error code instead of an exception\n\
92dup() -- return a new socket object identical to the current one [*]\n\
93fileno() -- return underlying file descriptor\n\
94getpeername() -- return remote address [*]\n\
95getsockname() -- return local address\n\
96getsockopt(level, optname[, buflen]) -- get socket options\n\
97gettimeout() -- return timeout or None\n\
98listen(n) -- start listening for incoming connections\n\
99makefile([mode, [bufsize]]) -- return a file object for the socket [*]\n\
100recv(buflen[, flags]) -- receive data\n\
101recvfrom(buflen[, flags]) -- receive data and sender's address\n\
102sendall(data[, flags]) -- send all data\n\
103send(data[, flags]) -- send data, may not send all of it\n\
104sendto(data[, flags], addr) -- send data to a given address\n\
105setblocking(0 | 1) -- set or clear the blocking I/O flag\n\
106setsockopt(level, optname, value) -- set socket options\n\
107settimeout(None | float) -- set or clear the timeout\n\
108shutdown(how) -- shut down traffic in one or both directions\n\
109\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000110 [*] not available on all platforms!");
Guido van Rossum3baaa131999-03-22 21:44:51 +0000111
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000112/* XXX This is a terrible mess of platform-dependent preprocessor hacks.
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000113 I hope some day someone can clean this up please... */
114
Guido van Rossum9376b741999-09-15 22:01:40 +0000115/* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure
116 script doesn't get this right, so we hardcode some platform checks below.
117 On the other hand, not all Linux versions agree, so there the settings
118 computed by the configure script are needed! */
119
120#ifndef linux
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000121# undef HAVE_GETHOSTBYNAME_R_3_ARG
122# undef HAVE_GETHOSTBYNAME_R_5_ARG
123# undef HAVE_GETHOSTBYNAME_R_6_ARG
Guido van Rossum9376b741999-09-15 22:01:40 +0000124#endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000125
Guido van Rossum7a122991999-04-13 04:07:32 +0000126#ifndef WITH_THREAD
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000127# undef HAVE_GETHOSTBYNAME_R
Guido van Rossum7a122991999-04-13 04:07:32 +0000128#endif
129
Guido van Rossume7de2061999-03-24 17:24:33 +0000130#ifdef HAVE_GETHOSTBYNAME_R
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000131# if defined(_AIX) || defined(__osf__)
132# define HAVE_GETHOSTBYNAME_R_3_ARG
133# elif defined(__sun) || defined(__sgi)
134# define HAVE_GETHOSTBYNAME_R_5_ARG
135# elif defined(linux)
Guido van Rossum9376b741999-09-15 22:01:40 +0000136/* Rely on the configure script */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000137# else
138# undef HAVE_GETHOSTBYNAME_R
139# endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000140#endif
141
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000142#if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \
143 !defined(MS_WINDOWS)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000144# define USE_GETHOSTBYNAME_LOCK
Guido van Rossum3baaa131999-03-22 21:44:51 +0000145#endif
146
Just van Rossum1040d2c2003-05-09 07:53:18 +0000147/* On systems on which getaddrinfo() is believed to not be thread-safe,
Just van Rossum09aecd72003-05-09 08:03:44 +0000148 (this includes the getaddrinfo emulation) protect access with a lock. */
Just van Rossum16e426b2003-05-09 08:12:00 +0000149#if defined(WITH_THREAD) && (defined(__APPLE__) || defined(__FreeBSD__) || \
150 defined(__OpenBSD__) || defined(__NetBSD__) || !defined(HAVE_GETADDRINFO))
Just van Rossum1040d2c2003-05-09 07:53:18 +0000151#define USE_GETADDRINFO_LOCK
152#endif
153
154#ifdef USE_GETADDRINFO_LOCK
155#define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1);
156#define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock);
157#else
158#define ACQUIRE_GETADDRINFO_LOCK
159#define RELEASE_GETADDRINFO_LOCK
160#endif
161
162#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000163# include "pythread.h"
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000164#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000165
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000166#if defined(PYCC_VACPP)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000167# include <types.h>
168# include <io.h>
169# include <sys/ioctl.h>
170# include <utils.h>
171# include <ctype.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000172#endif
173
Martin v. Löwis9e437302002-12-06 12:57:26 +0000174#if defined(__VMS)
175#if ! defined(_SOCKADDR_LEN)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000176# ifdef getaddrinfo
177# undef getaddrinfo
178# endif
179# include "TCPIP_IOCTL_ROUTINE"
180#else
181# include <ioctl.h>
182#endif
Martin v. Löwis9e437302002-12-06 12:57:26 +0000183#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000184
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000185#if defined(PYOS_OS2)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000186# define INCL_DOS
187# define INCL_DOSERRORS
188# define INCL_NOPMAPI
189# include <os2.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000190#endif
191
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000192#if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000193/* make sure that the reentrant (gethostbyaddr_r etc)
194 functions are declared correctly if compiling with
195 MIPSPro 7.x in ANSI C mode (default) */
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000196
197/* XXX Using _SGIAPI is the wrong thing,
198 but I don't know what the right thing is. */
Michael W. Hudson76b8cc82004-05-26 17:06:31 +0000199#undef _SGIAPI /* to avoid warning */
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000200#define _SGIAPI 1
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000201
Michael W. Hudson76b8cc82004-05-26 17:06:31 +0000202#undef _XOPEN_SOURCE
203#include <sys/socket.h>
204#include <sys/types.h>
205#include <netinet/in.h>
206#ifdef _SS_ALIGNSIZE
207#define HAVE_GETADDRINFO 1
208#define HAVE_GETNAMEINFO 1
209#endif
210
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000211#define HAVE_INET_PTON
212#include <netdb.h>
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000213#endif
214
Anthony Baxterbab23cf2003-10-04 08:00:49 +0000215/* Irix 6.5 fails to define this variable at all. This is needed
216 for both GCC and SGI's compiler. I'd say that the SGI headers
217 are just busted. */
218#if defined(__sgi) && !defined(INET_ADDRSTRLEN)
219#define INET_ADDRSTRLEN 16
220#endif
221
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000222/* Generic includes */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000223#include <sys/types.h>
Guido van Rossum81194471991-07-27 21:42:02 +0000224#include <signal.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000225
Marc-André Lemburg976ade62002-02-16 18:47:07 +0000226/* Generic socket object definitions and includes */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000227#define PySocket_BUILDING_SOCKET
Marc-André Lemburgbb8b78b2002-02-16 18:44:52 +0000228#include "socketmodule.h"
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000229
230/* Addressing includes */
231
Guido van Rossum6f489d91996-06-28 20:15:15 +0000232#ifndef MS_WINDOWS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000233
234/* Non-MS WINDOWS includes */
235# include <netdb.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000236
Guido van Rossum9376b741999-09-15 22:01:40 +0000237/* Headers needed for inet_ntoa() and inet_addr() */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000238# ifdef __BEOS__
239# include <net/netdb.h>
240# elif defined(PYOS_OS2) && defined(PYCC_VACPP)
241# include <netdb.h>
Tim Peters603c6832001-11-05 02:45:59 +0000242typedef size_t socklen_t;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000243# else
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000244# include <arpa/inet.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000245# endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000246
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000247# ifndef RISCOS
248# include <fcntl.h>
249# else
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000250# include <sys/ioctl.h>
251# include <socklib.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000252# define NO_DUP
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000253int h_errno; /* not used */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000254# define INET_ADDRSTRLEN 16
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000255# endif
256
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000257#else
Guido van Rossum48a680c2001-03-02 06:34:14 +0000258
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000259/* MS_WINDOWS includes */
260# include <fcntl.h>
Guido van Rossum48a680c2001-03-02 06:34:14 +0000261
Jeremy Hylton22308652001-02-02 03:23:09 +0000262#endif
263
Skip Montanaro7befb992004-02-10 16:50:21 +0000264#include <stddef.h>
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000265
266#ifndef offsetof
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000267# define offsetof(type, member) ((size_t)(&((type *)0)->member))
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000268#endif
269
Neal Norwitz39d22e52002-11-02 19:55:21 +0000270#ifndef O_NONBLOCK
271# define O_NONBLOCK O_NDELAY
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000272#endif
273
Michael W. Hudson76b8cc82004-05-26 17:06:31 +0000274#if defined(__sgi) && _COMPILER_VERSION>700 \
275 && !defined(_SS_ALIGNSIZE) /* defined in sys/socket.h */
276 /* by some newer versions of IRIX */
277 /* (e.g. not by 6.5.10 but by 6.5.21) */
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000278#include "addrinfo.h"
Michael W. Hudson76b8cc82004-05-26 17:06:31 +0000279#endif
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000280
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000281#ifndef HAVE_INET_PTON
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000282int inet_pton(int af, const char *src, void *dst);
Martin v. Löwisc925b1532001-07-21 09:42:15 +0000283const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000284#endif
285
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000286#ifdef __APPLE__
287/* On OS X, getaddrinfo returns no error indication of lookup
288 failure, so we must use the emulation instead of the libinfo
289 implementation. Unfortunately, performing an autoconf test
290 for this bug would require DNS access for the machine performing
291 the configuration, which is not acceptable. Therefore, we
292 determine the bug just by checking for __APPLE__. If this bug
293 gets ever fixed, perhaps checking for sys/version.h would be
294 appropriate, which is 10/0 on the system with the bug. */
Jack Jansen84262fb2002-07-02 14:40:42 +0000295#ifndef HAVE_GETNAMEINFO
296/* This bug seems to be fixed in Jaguar. Ths easiest way I could
297 Find to check for Jaguar is that it has getnameinfo(), which
298 older releases don't have */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000299#undef HAVE_GETADDRINFO
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000300#endif
Jack Jansen84262fb2002-07-02 14:40:42 +0000301#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000302
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000303/* I know this is a bad practice, but it is the easiest... */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000304#if !defined(HAVE_GETADDRINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000305/* avoid clashes with the C library definition of the symbol. */
306#define getaddrinfo fake_getaddrinfo
307#define gai_strerror fake_gai_strerror
308#define freeaddrinfo fake_freeaddrinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000309#include "getaddrinfo.c"
310#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000311#if !defined(HAVE_GETNAMEINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000312#define getnameinfo fake_getnameinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000313#include "getnameinfo.c"
314#endif
315
Guido van Rossumbcc20741998-08-04 22:53:56 +0000316#if defined(MS_WINDOWS) || defined(__BEOS__)
317/* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000318/* seem to be a few differences in the API */
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000319#define SOCKETCLOSE closesocket
Guido van Rossumbe32c891996-06-20 16:25:29 +0000320#define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000321#endif
322
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000323#ifdef MS_WIN32
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000324#define EAFNOSUPPORT WSAEAFNOSUPPORT
325#define snprintf _snprintf
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000326#endif
Fred Drakea04eaad2000-06-30 02:46:07 +0000327
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000328#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000329#define SOCKETCLOSE soclose
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000330#define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000331#endif
332
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000333#ifndef SOCKETCLOSE
334#define SOCKETCLOSE close
335#endif
336
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000337#ifdef __VMS
338/* TCP/IP Services for VMS uses a maximum send/revc buffer length of 65535 */
339#define SEGMENT_SIZE 65535
340#endif
341
Hye-Shik Chang81268602004-02-02 06:05:24 +0000342#if defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H)
343#define USE_BLUETOOTH 1
344#if defined(__FreeBSD__)
345#define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP
346#define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM
347#define sockaddr_l2 sockaddr_l2cap
348#define sockaddr_rc sockaddr_rfcomm
349#define _BT_SOCKADDR_MEMB(s, proto) &((s)->sock_addr)
350#define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb)
351#define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb)
352#else
Anthony Baxter5d7c0672004-02-16 05:35:28 +0000353#define _BT_SOCKADDR_MEMB(s, proto) (&((s)->sock_addr).bt_##proto)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000354#define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb)
355#define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb)
356#define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb)
357#endif
358#endif
359
Martin v. Löwise9416172003-05-03 10:12:45 +0000360/*
361 * Constants for getnameinfo()
362 */
363#if !defined(NI_MAXHOST)
364#define NI_MAXHOST 1025
365#endif
366#if !defined(NI_MAXSERV)
367#define NI_MAXSERV 32
368#endif
369
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000370/* XXX There's a problem here: *static* functions are not supposed to have
371 a Py prefix (or use CapitalizedWords). Later... */
372
Guido van Rossum30a685f1991-06-27 15:51:29 +0000373/* Global variable holding the exception type for errors detected
374 by this module (but not argument type or memory errors, etc.). */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000375static PyObject *socket_error;
376static PyObject *socket_herror;
377static PyObject *socket_gaierror;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000378static PyObject *socket_timeout;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000379
Guido van Rossum48a680c2001-03-02 06:34:14 +0000380#ifdef RISCOS
381/* Global variable which is !=0 if Python is running in a RISC OS taskwindow */
382static int taskwindow;
383#endif
384
Tim Peters643a7fc2002-02-17 04:13:21 +0000385/* A forward reference to the socket type object.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000386 The sock_type variable contains pointers to various functions,
387 some of which call new_sockobject(), which uses sock_type, so
Tim Peters643a7fc2002-02-17 04:13:21 +0000388 there has to be a circular reference. */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000389static PyTypeObject sock_type;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000390
Guido van Rossum30a685f1991-06-27 15:51:29 +0000391/* Convenience function to raise an error according to errno
392 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000393
Guido van Rossum73624e91994-10-10 17:59:00 +0000394static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000395set_error(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000396{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000397#ifdef MS_WINDOWS
Mark Hammond46a733d2000-07-24 01:45:11 +0000398 int err_no = WSAGetLastError();
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000399 static struct {
400 int no;
401 const char *msg;
402 } *msgp, msgs[] = {
403 {WSAEINTR, "Interrupted system call"},
404 {WSAEBADF, "Bad file descriptor"},
405 {WSAEACCES, "Permission denied"},
406 {WSAEFAULT, "Bad address"},
407 {WSAEINVAL, "Invalid argument"},
408 {WSAEMFILE, "Too many open files"},
409 {WSAEWOULDBLOCK,
410 "The socket operation could not complete "
411 "without blocking"},
412 {WSAEINPROGRESS, "Operation now in progress"},
413 {WSAEALREADY, "Operation already in progress"},
414 {WSAENOTSOCK, "Socket operation on non-socket"},
415 {WSAEDESTADDRREQ, "Destination address required"},
416 {WSAEMSGSIZE, "Message too long"},
417 {WSAEPROTOTYPE, "Protocol wrong type for socket"},
418 {WSAENOPROTOOPT, "Protocol not available"},
419 {WSAEPROTONOSUPPORT, "Protocol not supported"},
420 {WSAESOCKTNOSUPPORT, "Socket type not supported"},
421 {WSAEOPNOTSUPP, "Operation not supported"},
422 {WSAEPFNOSUPPORT, "Protocol family not supported"},
423 {WSAEAFNOSUPPORT, "Address family not supported"},
424 {WSAEADDRINUSE, "Address already in use"},
425 {WSAEADDRNOTAVAIL, "Can't assign requested address"},
426 {WSAENETDOWN, "Network is down"},
427 {WSAENETUNREACH, "Network is unreachable"},
428 {WSAENETRESET, "Network dropped connection on reset"},
429 {WSAECONNABORTED, "Software caused connection abort"},
430 {WSAECONNRESET, "Connection reset by peer"},
431 {WSAENOBUFS, "No buffer space available"},
432 {WSAEISCONN, "Socket is already connected"},
433 {WSAENOTCONN, "Socket is not connected"},
434 {WSAESHUTDOWN, "Can't send after socket shutdown"},
435 {WSAETOOMANYREFS, "Too many references: can't splice"},
436 {WSAETIMEDOUT, "Operation timed out"},
437 {WSAECONNREFUSED, "Connection refused"},
438 {WSAELOOP, "Too many levels of symbolic links"},
439 {WSAENAMETOOLONG, "File name too long"},
440 {WSAEHOSTDOWN, "Host is down"},
441 {WSAEHOSTUNREACH, "No route to host"},
442 {WSAENOTEMPTY, "Directory not empty"},
443 {WSAEPROCLIM, "Too many processes"},
444 {WSAEUSERS, "Too many users"},
445 {WSAEDQUOT, "Disc quota exceeded"},
446 {WSAESTALE, "Stale NFS file handle"},
447 {WSAEREMOTE, "Too many levels of remote in path"},
448 {WSASYSNOTREADY, "Network subsystem is unvailable"},
449 {WSAVERNOTSUPPORTED, "WinSock version is not supported"},
450 {WSANOTINITIALISED,
451 "Successful WSAStartup() not yet performed"},
452 {WSAEDISCON, "Graceful shutdown in progress"},
453 /* Resolver errors */
454 {WSAHOST_NOT_FOUND, "No such host is known"},
455 {WSATRY_AGAIN, "Host not found, or server failed"},
456 {WSANO_RECOVERY, "Unexpected server error encountered"},
457 {WSANO_DATA, "Valid name without requested data"},
458 {WSANO_ADDRESS, "No address, look for MX record"},
459 {0, NULL}
460 };
Mark Hammond46a733d2000-07-24 01:45:11 +0000461 if (err_no) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000462 PyObject *v;
Mark Hammond46a733d2000-07-24 01:45:11 +0000463 const char *msg = "winsock error";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000464
Mark Hammond46a733d2000-07-24 01:45:11 +0000465 for (msgp = msgs; msgp->msg; msgp++) {
466 if (err_no == msgp->no) {
467 msg = msgp->msg;
468 break;
469 }
470 }
471
472 v = Py_BuildValue("(is)", err_no, msg);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000473 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000474 PyErr_SetObject(socket_error, v);
Guido van Rossum73624e91994-10-10 17:59:00 +0000475 Py_DECREF(v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000476 }
477 return NULL;
478 }
479 else
480#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000481
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000482#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000483 if (sock_errno() != NO_ERROR) {
484 APIRET rc;
485 ULONG msglen;
486 char outbuf[100];
487 int myerrorcode = sock_errno();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000488
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000489 /* Retrieve socket-related error message from MPTN.MSG file */
490 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
491 myerrorcode - SOCBASEERR + 26,
492 "mptn.msg",
493 &msglen);
494 if (rc == NO_ERROR) {
495 PyObject *v;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000496
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000497 /* OS/2 doesn't guarantee a terminator */
498 outbuf[msglen] = '\0';
499 if (strlen(outbuf) > 0) {
500 /* If non-empty msg, trim CRLF */
501 char *lastc = &outbuf[ strlen(outbuf)-1 ];
502 while (lastc > outbuf && isspace(*lastc)) {
503 /* Trim trailing whitespace (CRLF) */
504 *lastc-- = '\0';
505 }
506 }
507 v = Py_BuildValue("(is)", myerrorcode, outbuf);
508 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000509 PyErr_SetObject(socket_error, v);
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000510 Py_DECREF(v);
511 }
512 return NULL;
513 }
514 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000515#endif
516
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000517#if defined(RISCOS)
518 if (_inet_error.errnum != NULL) {
519 PyObject *v;
520 v = Py_BuildValue("(is)", errno, _inet_err());
521 if (v != NULL) {
522 PyErr_SetObject(socket_error, v);
523 Py_DECREF(v);
524 }
525 return NULL;
526 }
527#endif
528
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000529 return PyErr_SetFromErrno(socket_error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000530}
531
Guido van Rossum30a685f1991-06-27 15:51:29 +0000532
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000533static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000534set_herror(int h_error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000535{
536 PyObject *v;
537
538#ifdef HAVE_HSTRERROR
539 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
540#else
541 v = Py_BuildValue("(is)", h_error, "host not found");
542#endif
543 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000544 PyErr_SetObject(socket_herror, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000545 Py_DECREF(v);
546 }
547
548 return NULL;
549}
550
551
552static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000553set_gaierror(int error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000554{
555 PyObject *v;
556
Martin v. Löwis272cb402002-03-01 08:31:07 +0000557#ifdef EAI_SYSTEM
558 /* EAI_SYSTEM is not available on Windows XP. */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000559 if (error == EAI_SYSTEM)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000560 return set_error();
Martin v. Löwis272cb402002-03-01 08:31:07 +0000561#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000562
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000563#ifdef HAVE_GAI_STRERROR
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000564 v = Py_BuildValue("(is)", error, gai_strerror(error));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000565#else
566 v = Py_BuildValue("(is)", error, "getaddrinfo failed");
567#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000568 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000569 PyErr_SetObject(socket_gaierror, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000570 Py_DECREF(v);
571 }
572
573 return NULL;
574}
575
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000576/* Function to perform the setting of socket blocking mode
577 internally. block = (1 | 0). */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000578static int
579internal_setblocking(PySocketSockObject *s, int block)
580{
581#ifndef RISCOS
582#ifndef MS_WINDOWS
583 int delay_flag;
584#endif
585#endif
586
587 Py_BEGIN_ALLOW_THREADS
588#ifdef __BEOS__
589 block = !block;
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000590 setsockopt(s->sock_fd, SOL_SOCKET, SO_NONBLOCK,
591 (void *)(&block), sizeof(int));
Guido van Rossum67f7a382002-06-06 21:08:16 +0000592#else
593#ifndef RISCOS
594#ifndef MS_WINDOWS
595#if defined(PYOS_OS2) && !defined(PYCC_GCC)
596 block = !block;
597 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000598#elif defined(__VMS)
599 block = !block;
600 ioctl(s->sock_fd, FIONBIO, (char *)&block);
601#else /* !PYOS_OS2 && !_VMS */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000602 delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
603 if (block)
Neal Norwitz39d22e52002-11-02 19:55:21 +0000604 delay_flag &= (~O_NONBLOCK);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000605 else
Neal Norwitz39d22e52002-11-02 19:55:21 +0000606 delay_flag |= O_NONBLOCK;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000607 fcntl(s->sock_fd, F_SETFL, delay_flag);
608#endif /* !PYOS_OS2 */
609#else /* MS_WINDOWS */
610 block = !block;
611 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
612#endif /* MS_WINDOWS */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000613#else /* RISCOS */
614 block = !block;
615 socketioctl(s->sock_fd, FIONBIO, (u_long*)&block);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000616#endif /* RISCOS */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000617#endif /* __BEOS__ */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000618 Py_END_ALLOW_THREADS
619
620 /* Since these don't return anything */
621 return 1;
622}
623
Guido van Rossum11ba0942002-06-13 15:07:44 +0000624/* Do a select() on the socket, if necessary (sock_timeout > 0).
625 The argument writing indicates the direction.
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000626 This does not raise an exception; we'll let our caller do that
627 after they've reacquired the interpreter lock.
628 Returns 1 on timeout, 0 otherwise. */
629static int
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000630internal_select(PySocketSockObject *s, int writing)
Guido van Rossum67f7a382002-06-06 21:08:16 +0000631{
632 fd_set fds;
633 struct timeval tv;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000634 int n;
Guido van Rossum11ba0942002-06-13 15:07:44 +0000635
Guido van Rossumad654902002-07-19 12:44:59 +0000636 /* Nothing to do unless we're in timeout mode (not non-blocking) */
Guido van Rossum11ba0942002-06-13 15:07:44 +0000637 if (s->sock_timeout <= 0.0)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000638 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000639
Guido van Rossumad654902002-07-19 12:44:59 +0000640 /* Guard against closed socket */
641 if (s->sock_fd < 0)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000642 return 0;
Guido van Rossumad654902002-07-19 12:44:59 +0000643
Guido van Rossum67f7a382002-06-06 21:08:16 +0000644 /* Construct the arguments to select */
645 tv.tv_sec = (int)s->sock_timeout;
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000646 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000647 FD_ZERO(&fds);
648 FD_SET(s->sock_fd, &fds);
649
650 /* See if the socket is ready */
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000651 if (writing)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000652 n = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000653 else
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000654 n = select(s->sock_fd+1, &fds, NULL, NULL, &tv);
655 if (n == 0)
656 return 1;
657 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000658}
659
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000660/* Initialize a new socket object. */
661
Tim Petersa12b4cf2002-07-18 22:38:44 +0000662static double defaulttimeout = -1.0; /* Default timeout for new sockets */
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000663
Mark Hammond62b1ab12002-07-23 06:31:15 +0000664PyMODINIT_FUNC
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000665init_sockobject(PySocketSockObject *s,
666 SOCKET_T fd, int family, int type, int proto)
667{
668#ifdef RISCOS
669 int block = 1;
670#endif
671 s->sock_fd = fd;
672 s->sock_family = family;
673 s->sock_type = type;
674 s->sock_proto = proto;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000675 s->sock_timeout = defaulttimeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000676
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000677 s->errorhandler = &set_error;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000678
679 if (defaulttimeout >= 0.0)
680 internal_setblocking(s, 0);
681
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000682#ifdef RISCOS
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000683 if (taskwindow)
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000684 socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000685#endif
686}
687
688
Guido van Rossum30a685f1991-06-27 15:51:29 +0000689/* Create a new socket object.
690 This just creates the object and initializes it.
691 If the creation fails, return NULL and set an exception (implicit
692 in NEWOBJ()). */
693
Guido van Rossum73624e91994-10-10 17:59:00 +0000694static PySocketSockObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000695new_sockobject(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000696{
Guido van Rossum73624e91994-10-10 17:59:00 +0000697 PySocketSockObject *s;
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000698 s = (PySocketSockObject *)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000699 PyType_GenericNew(&sock_type, NULL, NULL);
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000700 if (s != NULL)
701 init_sockobject(s, fd, family, type, proto);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000702 return s;
703}
704
Guido van Rossum30a685f1991-06-27 15:51:29 +0000705
Guido van Rossum48a680c2001-03-02 06:34:14 +0000706/* Lock to allow python interpreter to continue, but only allow one
Just van Rossum1040d2c2003-05-09 07:53:18 +0000707 thread to be in gethostbyname or getaddrinfo */
708#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
709PyThread_type_lock netdb_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000710#endif
711
712
Guido van Rossum30a685f1991-06-27 15:51:29 +0000713/* Convert a string specifying a host name or one of a few symbolic
714 names to a numeric IP address. This usually calls gethostbyname()
715 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000716 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000717 an error occurred; then an exception is raised. */
718
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000719static int
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000720setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000721{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000722 struct addrinfo hints, *res;
723 int error;
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000724 int d1, d2, d3, d4;
725 char ch;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000726
Guido van Rossuma376cc51996-12-05 23:43:35 +0000727 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
Guido van Rossum30a685f1991-06-27 15:51:29 +0000728 if (name[0] == '\0') {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000729 int siz;
730 memset(&hints, 0, sizeof(hints));
731 hints.ai_family = af;
732 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
733 hints.ai_flags = AI_PASSIVE;
Just van Rossum1040d2c2003-05-09 07:53:18 +0000734 Py_BEGIN_ALLOW_THREADS
735 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000736 error = getaddrinfo(NULL, "0", &hints, &res);
Just van Rossum1040d2c2003-05-09 07:53:18 +0000737 Py_END_ALLOW_THREADS
738 /* We assume that those thread-unsafe getaddrinfo() versions
739 *are* safe regarding their return value, ie. that a
740 subsequent call to getaddrinfo() does not destroy the
741 outcome of the first call. */
742 RELEASE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000743 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000744 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000745 return -1;
746 }
747 switch (res->ai_family) {
748 case AF_INET:
749 siz = 4;
750 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000751#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000752 case AF_INET6:
753 siz = 16;
754 break;
755#endif
756 default:
757 freeaddrinfo(res);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000758 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000759 "unsupported address family");
760 return -1;
761 }
762 if (res->ai_next) {
Martin v. Löwisf0b11d22001-11-07 08:31:03 +0000763 freeaddrinfo(res);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000764 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000765 "wildcard resolved to multiple address");
766 return -1;
767 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000768 if (res->ai_addrlen < addr_ret_size)
769 addr_ret_size = res->ai_addrlen;
770 memcpy(addr_ret, res->ai_addr, addr_ret_size);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000771 freeaddrinfo(res);
772 return siz;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000773 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000774 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000775 struct sockaddr_in *sin;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +0000776 if (af != AF_INET && af != AF_UNSPEC) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000777 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000778 "address family mismatched");
779 return -1;
780 }
781 sin = (struct sockaddr_in *)addr_ret;
782 memset((void *) sin, '\0', sizeof(*sin));
783 sin->sin_family = AF_INET;
784#ifdef HAVE_SOCKADDR_SA_LEN
785 sin->sin_len = sizeof(*sin);
786#endif
787 sin->sin_addr.s_addr = INADDR_BROADCAST;
788 return sizeof(sin->sin_addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000789 }
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000790 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
791 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
792 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
793 struct sockaddr_in *sin;
794 sin = (struct sockaddr_in *)addr_ret;
795 sin->sin_addr.s_addr = htonl(
796 ((long) d1 << 24) | ((long) d2 << 16) |
797 ((long) d3 << 8) | ((long) d4 << 0));
798 sin->sin_family = AF_INET;
799#ifdef HAVE_SOCKADDR_SA_LEN
800 sin->sin_len = sizeof(*sin);
801#endif
802 return 4;
803 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000804 memset(&hints, 0, sizeof(hints));
805 hints.ai_family = af;
Just van Rossum1040d2c2003-05-09 07:53:18 +0000806 Py_BEGIN_ALLOW_THREADS
807 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000808 error = getaddrinfo(name, NULL, &hints, &res);
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000809#if defined(__digital__) && defined(__unix__)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000810 if (error == EAI_NONAME && af == AF_UNSPEC) {
811 /* On Tru64 V5.1, numeric-to-addr conversion fails
812 if no address family is given. Assume IPv4 for now.*/
813 hints.ai_family = AF_INET;
814 error = getaddrinfo(name, NULL, &hints, &res);
815 }
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000816#endif
Just van Rossum1040d2c2003-05-09 07:53:18 +0000817 Py_END_ALLOW_THREADS
818 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000819 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000820 set_gaierror(error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000821 return -1;
822 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000823 if (res->ai_addrlen < addr_ret_size)
824 addr_ret_size = res->ai_addrlen;
825 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000826 freeaddrinfo(res);
827 switch (addr_ret->sa_family) {
828 case AF_INET:
829 return 4;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000830#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000831 case AF_INET6:
832 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +0000833#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000834 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000835 PyErr_SetString(socket_error, "unknown address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000836 return -1;
837 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000838}
839
Guido van Rossum30a685f1991-06-27 15:51:29 +0000840
Guido van Rossum30a685f1991-06-27 15:51:29 +0000841/* Create a string object representing an IP address.
842 This is always a string of the form 'dd.dd.dd.dd' (with variable
843 size numbers). */
844
Guido van Rossum73624e91994-10-10 17:59:00 +0000845static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000846makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000847{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000848 char buf[NI_MAXHOST];
849 int error;
850
851 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
852 NI_NUMERICHOST);
853 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000854 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000855 return NULL;
856 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000857 return PyString_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000858}
859
860
861/* Create an object representing the given socket address,
862 suitable for passing it back to bind(), connect() etc.
863 The family field of the sockaddr structure is inspected
864 to determine what kind of address it really is. */
865
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000866/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000867static PyObject *
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000868makesockaddr(int sockfd, struct sockaddr *addr, int addrlen)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000869{
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000870 if (addrlen == 0) {
871 /* No address -- may be recvfrom() from known socket */
Guido van Rossum73624e91994-10-10 17:59:00 +0000872 Py_INCREF(Py_None);
873 return Py_None;
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000874 }
875
Guido van Rossumbcc20741998-08-04 22:53:56 +0000876#ifdef __BEOS__
Guido van Rossum2c8bcb82000-04-25 21:34:53 +0000877 /* XXX: BeOS version of accept() doesn't set family correctly */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000878 addr->sa_family = AF_INET;
879#endif
880
Guido van Rossum30a685f1991-06-27 15:51:29 +0000881 switch (addr->sa_family) {
882
883 case AF_INET:
884 {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000885 struct sockaddr_in *a;
886 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
Barry Warsaw752300b1997-01-03 17:18:10 +0000887 PyObject *ret = NULL;
888 if (addrobj) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000889 a = (struct sockaddr_in *)addr;
Barry Warsaw752300b1997-01-03 17:18:10 +0000890 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
891 Py_DECREF(addrobj);
892 }
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000893 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000894 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000895
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +0000896#if defined(AF_UNIX)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000897 case AF_UNIX:
898 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000899 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000900 return PyString_FromString(a->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000901 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000902#endif /* AF_UNIX */
903
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000904#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000905 case AF_INET6:
906 {
907 struct sockaddr_in6 *a;
908 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
909 PyObject *ret = NULL;
910 if (addrobj) {
911 a = (struct sockaddr_in6 *)addr;
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000912 ret = Py_BuildValue("Oiii",
913 addrobj,
914 ntohs(a->sin6_port),
915 a->sin6_flowinfo,
916 a->sin6_scope_id);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000917 Py_DECREF(addrobj);
918 }
919 return ret;
920 }
Jeremy Hylton22308652001-02-02 03:23:09 +0000921#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +0000922
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +0000923#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +0000924 case AF_PACKET:
925 {
926 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
927 char *ifname = "";
928 struct ifreq ifr;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000929 /* need to look up interface name give index */
930 if (a->sll_ifindex) {
Jeremy Hylton22308652001-02-02 03:23:09 +0000931 ifr.ifr_ifindex = a->sll_ifindex;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000932 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
Jeremy Hylton22308652001-02-02 03:23:09 +0000933 ifname = ifr.ifr_name;
Jeremy Hylton22308652001-02-02 03:23:09 +0000934 }
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000935 return Py_BuildValue("shbhs#",
936 ifname,
937 ntohs(a->sll_protocol),
938 a->sll_pkttype,
939 a->sll_hatype,
940 a->sll_addr,
941 a->sll_halen);
Jeremy Hylton22308652001-02-02 03:23:09 +0000942 }
943#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000944
Guido van Rossum30a685f1991-06-27 15:51:29 +0000945 /* More cases here... */
946
947 default:
Guido van Rossumaa948df1997-05-07 17:41:48 +0000948 /* If we don't know the address family, don't raise an
949 exception -- return it as a tuple. */
950 return Py_BuildValue("is#",
951 addr->sa_family,
952 addr->sa_data,
953 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000954
Guido van Rossum30a685f1991-06-27 15:51:29 +0000955 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000956}
957
Guido van Rossum30a685f1991-06-27 15:51:29 +0000958
959/* Parse a socket address argument according to the socket object's
960 address family. Return 1 if the address was in the proper format,
961 0 of not. The address is returned through addr_ret, its length
962 through len_ret. */
963
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000964static int
Guido van Rossum48a680c2001-03-02 06:34:14 +0000965getsockaddrarg(PySocketSockObject *s, PyObject *args,
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000966 struct sockaddr **addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000967{
Guido van Rossum30a685f1991-06-27 15:51:29 +0000968 switch (s->sock_family) {
969
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +0000970#if defined(AF_UNIX)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000971 case AF_UNIX:
972 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000973 struct sockaddr_un* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000974 char *path;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000975 int len;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000976 addr = (struct sockaddr_un*)&(s->sock_addr).un;
Guido van Rossum7e488981998-10-08 02:25:24 +0000977 if (!PyArg_Parse(args, "t#", &path, &len))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000978 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000979 if (len > sizeof addr->sun_path) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000980 PyErr_SetString(socket_error,
Barry Warsaw752300b1997-01-03 17:18:10 +0000981 "AF_UNIX path too long");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000982 return 0;
983 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000984 addr->sun_family = s->sock_family;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000985 memcpy(addr->sun_path, path, len);
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000986 addr->sun_path[len] = 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000987 *addr_ret = (struct sockaddr *) addr;
Andrew MacIntyredaedf212004-04-11 12:03:57 +0000988#if defined(PYOS_OS2)
989 *len_ret = sizeof(*addr);
990#else
Guido van Rossum65af28a1996-06-11 18:36:33 +0000991 *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path);
Andrew MacIntyredaedf212004-04-11 12:03:57 +0000992#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000993 return 1;
994 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000995#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000996
Guido van Rossum30a685f1991-06-27 15:51:29 +0000997 case AF_INET:
998 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000999 struct sockaddr_in* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001000 char *host;
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001001 int port, result;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001002 addr=(struct sockaddr_in*)&(s->sock_addr).in;
Guido van Rossume4dad902000-12-01 13:13:11 +00001003 if (!PyTuple_Check(args)) {
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001004 PyErr_Format(
1005 PyExc_TypeError,
1006 "getsockaddrarg: "
1007 "AF_INET address must be tuple, not %.500s",
1008 args->ob_type->tp_name);
Guido van Rossume4dad902000-12-01 13:13:11 +00001009 return 0;
1010 }
Martin v. Löwis2548c732003-04-18 10:39:54 +00001011 if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
1012 "idna", &host, &port))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001013 return 0;
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001014 result = setipaddr(host, (struct sockaddr *)addr,
1015 sizeof(*addr), AF_INET);
1016 PyMem_Free(host);
1017 if (result < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001018 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001019 addr->sin_family = AF_INET;
Guido van Rossum644a12b1997-04-09 19:24:53 +00001020 addr->sin_port = htons((short)port);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001021 *addr_ret = (struct sockaddr *) addr;
1022 *len_ret = sizeof *addr;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001023 return 1;
1024 }
1025
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001026#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001027 case AF_INET6:
1028 {
1029 struct sockaddr_in6* addr;
1030 char *host;
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001031 int port, flowinfo, scope_id, result;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001032 addr = (struct sockaddr_in6*)&(s->sock_addr).in6;
1033 flowinfo = scope_id = 0;
Martin v. Löwis2548c732003-04-18 10:39:54 +00001034 if (!PyArg_ParseTuple(args, "eti|ii",
1035 "idna", &host, &port, &flowinfo,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001036 &scope_id)) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001037 return 0;
1038 }
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001039 result = setipaddr(host, (struct sockaddr *)addr,
1040 sizeof(*addr), AF_INET6);
1041 PyMem_Free(host);
1042 if (result < 0)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001043 return 0;
1044 addr->sin6_family = s->sock_family;
1045 addr->sin6_port = htons((short)port);
1046 addr->sin6_flowinfo = flowinfo;
1047 addr->sin6_scope_id = scope_id;
1048 *addr_ret = (struct sockaddr *) addr;
1049 *len_ret = sizeof *addr;
1050 return 1;
1051 }
1052#endif
1053
Hye-Shik Chang81268602004-02-02 06:05:24 +00001054#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00001055 case AF_BLUETOOTH:
1056 {
1057 switch( s->sock_proto )
1058 {
1059 case BTPROTO_L2CAP:
1060 {
Hye-Shik Chang81268602004-02-02 06:05:24 +00001061 struct sockaddr_l2* addr = (struct sockaddr_l2*)_BT_SOCKADDR_MEMB(s, l2);
1062 bdaddr_t* bdaddr = &_BT_L2_MEMB(addr, bdaddr);
Martin v. Löwis12af0482004-01-31 12:34:17 +00001063
Hye-Shik Chang81268602004-02-02 06:05:24 +00001064 _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
1065 if( !PyArg_ParseTuple(args, "(iiiiii)i", &bdaddr->b[0], &bdaddr->b[1], &bdaddr->b[2], &bdaddr->b[3], &bdaddr->b[4], &bdaddr->b[5], &_BT_L2_MEMB(addr, psm)) )
Martin v. Löwis12af0482004-01-31 12:34:17 +00001066 {
1067 PyErr_SetString(socket_error, "getsockaddrarg: wrong format");
1068 return 0;
1069 }
1070
1071 *addr_ret = (struct sockaddr *) addr;
1072 *len_ret = sizeof *addr;
1073 return 1;
1074 }
1075 case BTPROTO_RFCOMM:
1076 {
Hye-Shik Chang81268602004-02-02 06:05:24 +00001077 struct sockaddr_rc* addr = (struct sockaddr_rc*)_BT_SOCKADDR_MEMB(s, rc);
1078 bdaddr_t* bdaddr = &_BT_RC_MEMB(addr, bdaddr);
Martin v. Löwis12af0482004-01-31 12:34:17 +00001079
Hye-Shik Chang81268602004-02-02 06:05:24 +00001080 _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
1081 if( !PyArg_ParseTuple(args, "(iiiiii)i", &bdaddr->b[0], &bdaddr->b[1], &bdaddr->b[2], &bdaddr->b[3], &bdaddr->b[4], &bdaddr->b[5], &_BT_RC_MEMB(addr, channel)) )
Martin v. Löwis12af0482004-01-31 12:34:17 +00001082 {
1083 PyErr_SetString(socket_error, "getsockaddrarg: wrong format");
1084 return 0;
1085 }
1086
1087 *addr_ret = (struct sockaddr *) addr;
1088 *len_ret = sizeof *addr;
1089 return 1;
1090 }
Hye-Shik Chang81268602004-02-02 06:05:24 +00001091#if !defined(__FreeBSD__)
Martin v. Löwis12af0482004-01-31 12:34:17 +00001092 case BTPROTO_SCO:
1093 {
Hye-Shik Chang81268602004-02-02 06:05:24 +00001094 struct sockaddr_sco* addr = (struct sockaddr_sco*)_BT_SOCKADDR_MEMB(s, sco);
1095 bdaddr_t* bdaddr = &_BT_SCO_MEMB(addr, bdaddr);
Martin v. Löwis12af0482004-01-31 12:34:17 +00001096
Hye-Shik Chang81268602004-02-02 06:05:24 +00001097 _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001098 if( !PyArg_ParseTuple(args, "iiiiii", &bdaddr->b[0], &bdaddr->b[1], &bdaddr->b[2], &bdaddr->b[3], &bdaddr->b[4], &bdaddr->b[5]) )
1099 {
1100 PyErr_SetString(socket_error, "getsockaddrarg: wrong format");
1101 return 0;
1102 }
1103
1104 *addr_ret = (struct sockaddr *) addr;
1105 *len_ret = sizeof *addr;
1106 return 1;
1107 }
Hye-Shik Chang81268602004-02-02 06:05:24 +00001108#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00001109 default:
1110 {
1111 PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol");
1112 return 0;
1113 }
1114 }
1115 }
1116#endif
1117
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001118#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001119 case AF_PACKET:
1120 {
1121 struct sockaddr_ll* addr;
1122 struct ifreq ifr;
1123 char *interfaceName;
1124 int protoNumber;
1125 int hatype = 0;
1126 int pkttype = 0;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001127 char *haddr;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001128
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001129 if (!PyArg_ParseTuple(args, "si|iis", &interfaceName,
1130 &protoNumber, &pkttype, &hatype, &haddr))
Jeremy Hylton22308652001-02-02 03:23:09 +00001131 return 0;
1132 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
1133 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001134 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001135 s->errorhandler();
Jeremy Hylton22308652001-02-02 03:23:09 +00001136 return 0;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001137 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001138 addr = &(s->sock_addr.ll);
1139 addr->sll_family = AF_PACKET;
1140 addr->sll_protocol = htons((short)protoNumber);
1141 addr->sll_ifindex = ifr.ifr_ifindex;
1142 addr->sll_pkttype = pkttype;
1143 addr->sll_hatype = hatype;
1144 *addr_ret = (struct sockaddr *) addr;
1145 *len_ret = sizeof *addr;
1146 return 1;
1147 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001148#endif
1149
Guido van Rossum30a685f1991-06-27 15:51:29 +00001150 /* More cases here... */
1151
1152 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001153 PyErr_SetString(socket_error, "getsockaddrarg: bad family");
Guido van Rossum30a685f1991-06-27 15:51:29 +00001154 return 0;
1155
1156 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001157}
1158
Guido van Rossum30a685f1991-06-27 15:51:29 +00001159
Guido van Rossum48a680c2001-03-02 06:34:14 +00001160/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +00001161 Return 1 if the family is known, 0 otherwise. The length is returned
1162 through len_ret. */
1163
1164static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +00001165getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001166{
1167 switch (s->sock_family) {
1168
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001169#if defined(AF_UNIX)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001170 case AF_UNIX:
1171 {
1172 *len_ret = sizeof (struct sockaddr_un);
1173 return 1;
1174 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001175#endif /* AF_UNIX */
Guido van Rossum710e1df1992-06-12 10:39:36 +00001176
1177 case AF_INET:
1178 {
1179 *len_ret = sizeof (struct sockaddr_in);
1180 return 1;
1181 }
1182
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001183#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001184 case AF_INET6:
1185 {
1186 *len_ret = sizeof (struct sockaddr_in6);
1187 return 1;
1188 }
1189#endif
1190
Hye-Shik Chang81268602004-02-02 06:05:24 +00001191#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00001192 case AF_BLUETOOTH:
1193 {
1194 switch(s->sock_proto)
1195 {
1196 case BTPROTO_L2CAP:
1197 {
1198 *len_ret = sizeof (struct sockaddr_l2);
1199 return 1;
1200 }
1201 case BTPROTO_RFCOMM:
1202 {
1203 *len_ret = sizeof (struct sockaddr_rc);
1204 return 1;
1205 }
Hye-Shik Chang81268602004-02-02 06:05:24 +00001206#if !defined(__FreeBSD__)
Martin v. Löwis12af0482004-01-31 12:34:17 +00001207 case BTPROTO_SCO:
1208 {
1209 *len_ret = sizeof (struct sockaddr_sco);
1210 return 1;
1211 }
Hye-Shik Chang81268602004-02-02 06:05:24 +00001212#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00001213 default:
1214 {
1215 PyErr_SetString(socket_error, "getsockaddrlen: unknown BT protocol");
1216 return 0;
1217 }
1218 }
1219 }
1220#endif
1221
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001222#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001223 case AF_PACKET:
1224 {
1225 *len_ret = sizeof (struct sockaddr_ll);
1226 return 1;
1227 }
1228#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001229
Guido van Rossum710e1df1992-06-12 10:39:36 +00001230 /* More cases here... */
1231
1232 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001233 PyErr_SetString(socket_error, "getsockaddrlen: bad family");
Guido van Rossum710e1df1992-06-12 10:39:36 +00001234 return 0;
1235
1236 }
1237}
1238
1239
Guido van Rossum30a685f1991-06-27 15:51:29 +00001240/* s.accept() method */
1241
Guido van Rossum73624e91994-10-10 17:59:00 +00001242static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001243sock_accept(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001244{
1245 char addrbuf[256];
Fred Drakea04eaad2000-06-30 02:46:07 +00001246 SOCKET_T newfd;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001247 socklen_t addrlen;
Barry Warsaw752300b1997-01-03 17:18:10 +00001248 PyObject *sock = NULL;
1249 PyObject *addr = NULL;
1250 PyObject *res = NULL;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001251 int timeout;
Barry Warsaw752300b1997-01-03 17:18:10 +00001252
Guido van Rossum710e1df1992-06-12 10:39:36 +00001253 if (!getsockaddrlen(s, &addrlen))
1254 return NULL;
Fred Drakefd168342001-05-09 19:11:33 +00001255 memset(addrbuf, 0, addrlen);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001256
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001257#ifdef MS_WINDOWS
1258 newfd = INVALID_SOCKET;
1259#else
1260 newfd = -1;
1261#endif
1262
Guido van Rossum73624e91994-10-10 17:59:00 +00001263 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001264 timeout = internal_select(s, 0);
1265 if (!timeout)
1266 newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf,
1267 &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001268 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001269
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001270 if (timeout) {
1271 PyErr_SetString(socket_timeout, "timed out");
1272 return NULL;
1273 }
1274
Fred Drakea04eaad2000-06-30 02:46:07 +00001275#ifdef MS_WINDOWS
1276 if (newfd == INVALID_SOCKET)
1277#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001278 if (newfd < 0)
Fred Drakea04eaad2000-06-30 02:46:07 +00001279#endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001280 return s->errorhandler();
Barry Warsaw752300b1997-01-03 17:18:10 +00001281
Guido van Rossum30a685f1991-06-27 15:51:29 +00001282 /* Create the new object with unspecified family,
1283 to avoid calls to bind() etc. on it. */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001284 sock = (PyObject *) new_sockobject(newfd,
1285 s->sock_family,
1286 s->sock_type,
1287 s->sock_proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001288
Barry Warsaw752300b1997-01-03 17:18:10 +00001289 if (sock == NULL) {
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001290 SOCKETCLOSE(newfd);
Barry Warsaw752300b1997-01-03 17:18:10 +00001291 goto finally;
1292 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001293 addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001294 addrlen);
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001295 if (addr == NULL)
Barry Warsaw752300b1997-01-03 17:18:10 +00001296 goto finally;
1297
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001298 res = PyTuple_Pack(2, sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00001299
Guido van Rossum67f7a382002-06-06 21:08:16 +00001300finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001301 Py_XDECREF(sock);
1302 Py_XDECREF(addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001303 return res;
1304}
1305
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001306PyDoc_STRVAR(accept_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001307"accept() -> (socket object, address info)\n\
1308\n\
1309Wait for an incoming connection. Return a new socket representing the\n\
1310connection, and the address of the client. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001311info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001312
Guido van Rossum11ba0942002-06-13 15:07:44 +00001313/* s.setblocking(flag) method. Argument:
1314 False -- non-blocking mode; same as settimeout(0)
1315 True -- blocking mode; same as settimeout(None)
1316*/
Guido van Rossume4485b01994-09-07 14:32:49 +00001317
Guido van Rossum73624e91994-10-10 17:59:00 +00001318static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001319sock_setblocking(PySocketSockObject *s, PyObject *arg)
Guido van Rossume4485b01994-09-07 14:32:49 +00001320{
1321 int block;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001322
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001323 block = PyInt_AsLong(arg);
1324 if (block == -1 && PyErr_Occurred())
Guido van Rossume4485b01994-09-07 14:32:49 +00001325 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001326
Guido van Rossum11ba0942002-06-13 15:07:44 +00001327 s->sock_timeout = block ? -1.0 : 0.0;
Guido van Rossume8008f02002-06-07 03:36:20 +00001328 internal_setblocking(s, block);
Guido van Rossume4485b01994-09-07 14:32:49 +00001329
Guido van Rossum73624e91994-10-10 17:59:00 +00001330 Py_INCREF(Py_None);
1331 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +00001332}
Guido van Rossume4485b01994-09-07 14:32:49 +00001333
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001334PyDoc_STRVAR(setblocking_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001335"setblocking(flag)\n\
1336\n\
1337Set the socket to blocking (flag is true) or non-blocking (false).\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001338setblocking(True) is equivalent to settimeout(None);\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001339setblocking(False) is equivalent to settimeout(0.0).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001340
Guido van Rossum11ba0942002-06-13 15:07:44 +00001341/* s.settimeout(timeout) method. Argument:
1342 None -- no timeout, blocking mode; same as setblocking(True)
1343 0.0 -- non-blocking mode; same as setblocking(False)
1344 > 0 -- timeout mode; operations time out after timeout seconds
1345 < 0 -- illegal; raises an exception
1346*/
Guido van Rossum67f7a382002-06-06 21:08:16 +00001347static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001348sock_settimeout(PySocketSockObject *s, PyObject *arg)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001349{
Guido van Rossum11ba0942002-06-13 15:07:44 +00001350 double timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001351
1352 if (arg == Py_None)
Guido van Rossum11ba0942002-06-13 15:07:44 +00001353 timeout = -1.0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001354 else {
Guido van Rossum11ba0942002-06-13 15:07:44 +00001355 timeout = PyFloat_AsDouble(arg);
1356 if (timeout < 0.0) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00001357 if (!PyErr_Occurred())
1358 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00001359 "Timeout value out of range");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001360 return NULL;
1361 }
1362 }
1363
Guido van Rossum11ba0942002-06-13 15:07:44 +00001364 s->sock_timeout = timeout;
1365 internal_setblocking(s, timeout < 0.0);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001366
1367 Py_INCREF(Py_None);
1368 return Py_None;
1369}
1370
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001371PyDoc_STRVAR(settimeout_doc,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001372"settimeout(timeout)\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001373\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001374Set a timeout on socket operations. 'timeout' can be a float,\n\
1375giving in seconds, or None. Setting a timeout of None disables\n\
1376the timeout feature and is equivalent to setblocking(1).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001377Setting a timeout of zero is the same as setblocking(0).");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001378
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001379/* s.gettimeout() method.
1380 Returns the timeout associated with a socket. */
Guido van Rossum67f7a382002-06-06 21:08:16 +00001381static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001382sock_gettimeout(PySocketSockObject *s)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001383{
1384 if (s->sock_timeout < 0.0) {
1385 Py_INCREF(Py_None);
1386 return Py_None;
1387 }
1388 else
1389 return PyFloat_FromDouble(s->sock_timeout);
1390}
1391
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001392PyDoc_STRVAR(gettimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001393"gettimeout() -> timeout\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001394\n\
1395Returns the timeout in floating seconds associated with socket \n\
1396operations. A timeout of None indicates that timeouts on socket \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001397operations are disabled.");
Guido van Rossume4485b01994-09-07 14:32:49 +00001398
Guido van Rossum48a680c2001-03-02 06:34:14 +00001399#ifdef RISCOS
1400/* s.sleeptaskw(1 | 0) method */
1401
1402static PyObject *
Martin v. Löwisa94568a2003-05-10 07:36:56 +00001403sock_sleeptaskw(PySocketSockObject *s,PyObject *arg)
Guido van Rossum48a680c2001-03-02 06:34:14 +00001404{
Guido van Rossum67f7a382002-06-06 21:08:16 +00001405 int block;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00001406 block = PyInt_AsLong(arg);
1407 if (block == -1 && PyErr_Occurred())
Guido van Rossum67f7a382002-06-06 21:08:16 +00001408 return NULL;
1409 Py_BEGIN_ALLOW_THREADS
1410 socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
1411 Py_END_ALLOW_THREADS
Guido van Rossum48a680c2001-03-02 06:34:14 +00001412
Guido van Rossum67f7a382002-06-06 21:08:16 +00001413 Py_INCREF(Py_None);
1414 return Py_None;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001415}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001416PyDoc_STRVAR(sleeptaskw_doc,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001417"sleeptaskw(flag)\n\
1418\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001419Allow sleeps in taskwindows.");
Guido van Rossum48a680c2001-03-02 06:34:14 +00001420#endif
1421
1422
Guido van Rossumaee08791992-09-08 09:05:33 +00001423/* s.setsockopt() method.
1424 With an integer third argument, sets an integer option.
1425 With a string third argument, sets an option from a buffer;
1426 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001427
Guido van Rossum73624e91994-10-10 17:59:00 +00001428static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001429sock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001430{
1431 int level;
1432 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001433 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +00001434 char *buf;
1435 int buflen;
1436 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001437
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001438 if (PyArg_ParseTuple(args, "iii:setsockopt",
1439 &level, &optname, &flag)) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001440 buf = (char *) &flag;
1441 buflen = sizeof flag;
1442 }
1443 else {
Guido van Rossum73624e91994-10-10 17:59:00 +00001444 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001445 if (!PyArg_ParseTuple(args, "iis#:setsockopt",
1446 &level, &optname, &buf, &buflen))
Guido van Rossumaee08791992-09-08 09:05:33 +00001447 return NULL;
1448 }
Thomas Wouters334fb892000-07-25 12:56:38 +00001449 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001450 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001451 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001452 Py_INCREF(Py_None);
1453 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001454}
1455
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001456PyDoc_STRVAR(setsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001457"setsockopt(level, option, value)\n\
1458\n\
1459Set a socket option. See the Unix manual for level and option.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001460The value argument can either be an integer or a string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001461
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001462
Guido van Rossumaee08791992-09-08 09:05:33 +00001463/* s.getsockopt() method.
1464 With two arguments, retrieves an integer option.
1465 With a third integer argument, retrieves a string buffer of that size;
1466 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001467
Guido van Rossum73624e91994-10-10 17:59:00 +00001468static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001469sock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001470{
1471 int level;
1472 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001473 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +00001474 PyObject *buf;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001475 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001476
Guido van Rossumbcc20741998-08-04 22:53:56 +00001477#ifdef __BEOS__
Fred Drake564a6cc2001-05-11 20:12:26 +00001478 /* We have incomplete socket support. */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001479 PyErr_SetString(socket_error, "getsockopt not supported");
Guido van Rossumbcc20741998-08-04 22:53:56 +00001480 return NULL;
1481#else
1482
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001483 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
1484 &level, &optname, &buflen))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001485 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001486
Guido van Rossumbe32c891996-06-20 16:25:29 +00001487 if (buflen == 0) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001488 int flag = 0;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001489 socklen_t flagsize = sizeof flag;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001490 res = getsockopt(s->sock_fd, level, optname,
Thomas Wouters334fb892000-07-25 12:56:38 +00001491 (void *)&flag, &flagsize);
Guido van Rossumaee08791992-09-08 09:05:33 +00001492 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001493 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001494 return PyInt_FromLong(flag);
Guido van Rossumaee08791992-09-08 09:05:33 +00001495 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001496#ifdef __VMS
1497 if (buflen > 1024) {
1498#else
Guido van Rossumaee08791992-09-08 09:05:33 +00001499 if (buflen <= 0 || buflen > 1024) {
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001500#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001501 PyErr_SetString(socket_error,
Barry Warsaw752300b1997-01-03 17:18:10 +00001502 "getsockopt buflen out of range");
Guido van Rossumaee08791992-09-08 09:05:33 +00001503 return NULL;
1504 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001505 buf = PyString_FromStringAndSize((char *)NULL, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001506 if (buf == NULL)
1507 return NULL;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001508 res = getsockopt(s->sock_fd, level, optname,
Fred Drake564a6cc2001-05-11 20:12:26 +00001509 (void *)PyString_AS_STRING(buf), &buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001510 if (res < 0) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001511 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001512 return s->errorhandler();
Guido van Rossumaee08791992-09-08 09:05:33 +00001513 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001514 _PyString_Resize(&buf, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001515 return buf;
Guido van Rossumbcc20741998-08-04 22:53:56 +00001516#endif /* __BEOS__ */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001517}
1518
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001519PyDoc_STRVAR(getsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001520"getsockopt(level, option[, buffersize]) -> value\n\
1521\n\
1522Get a socket option. See the Unix manual for level and option.\n\
1523If a nonzero buffersize argument is given, the return value is a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001524string of that length; otherwise it is an integer.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001525
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001526
Fred Drake728819a2000-07-01 03:40:12 +00001527/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001528
Guido van Rossum73624e91994-10-10 17:59:00 +00001529static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001530sock_bind(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001531{
1532 struct sockaddr *addr;
1533 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001534 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001535
Fred Drake728819a2000-07-01 03:40:12 +00001536 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001537 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001538 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001539 res = bind(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001540 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001541 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001542 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001543 Py_INCREF(Py_None);
1544 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001545}
1546
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001547PyDoc_STRVAR(bind_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001548"bind(address)\n\
1549\n\
1550Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00001551pair (host, port); the host must refer to the local host. For raw packet\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001552sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001553
Guido van Rossum30a685f1991-06-27 15:51:29 +00001554
1555/* s.close() method.
1556 Set the file descriptor to -1 so operations tried subsequently
1557 will surely fail. */
1558
Guido van Rossum73624e91994-10-10 17:59:00 +00001559static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001560sock_close(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001561{
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001562 SOCKET_T fd;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001563
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001564 if ((fd = s->sock_fd) != -1) {
1565 s->sock_fd = -1;
Guido van Rossum08481461996-10-12 14:07:22 +00001566 Py_BEGIN_ALLOW_THREADS
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001567 (void) SOCKETCLOSE(fd);
Guido van Rossum08481461996-10-12 14:07:22 +00001568 Py_END_ALLOW_THREADS
1569 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001570 Py_INCREF(Py_None);
1571 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001572}
1573
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001574PyDoc_STRVAR(close_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001575"close()\n\
1576\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001577Close the socket. It cannot be used after this call.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001578
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001579static int
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001580internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
1581 int *timeoutp)
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001582{
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001583 int res, timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001584
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001585 timeout = 0;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001586 res = connect(s->sock_fd, addr, addrlen);
1587
1588#ifdef MS_WINDOWS
1589
1590 if (s->sock_timeout > 0.0) {
1591 if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK) {
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001592 /* This is a mess. Best solution: trust select */
1593 fd_set fds;
1594 struct timeval tv;
1595 tv.tv_sec = (int)s->sock_timeout;
1596 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
1597 FD_ZERO(&fds);
1598 FD_SET(s->sock_fd, &fds);
1599 res = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001600 if (res == 0) {
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001601 res = WSAEWOULDBLOCK;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001602 timeout = 1;
1603 } else if (res > 0)
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001604 res = 0;
1605 /* else if (res < 0) an error occurred */
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001606 }
1607 }
1608
1609 if (res < 0)
1610 res = WSAGetLastError();
1611
1612#else
1613
1614 if (s->sock_timeout > 0.0) {
1615 if (res < 0 && errno == EINPROGRESS) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001616 timeout = internal_select(s, 1);
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001617 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum12e3c712002-08-08 20:39:30 +00001618 if (res < 0 && errno == EISCONN)
1619 res = 0;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001620 }
1621 }
1622
1623 if (res < 0)
1624 res = errno;
1625
1626#endif
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001627 *timeoutp = timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001628
1629 return res;
1630}
Guido van Rossum30a685f1991-06-27 15:51:29 +00001631
Fred Drake728819a2000-07-01 03:40:12 +00001632/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001633
Guido van Rossum73624e91994-10-10 17:59:00 +00001634static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001635sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001636{
1637 struct sockaddr *addr;
1638 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001639 int res;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001640 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001641
Fred Drake728819a2000-07-01 03:40:12 +00001642 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001643 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001644
Guido van Rossum73624e91994-10-10 17:59:00 +00001645 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001646 res = internal_connect(s, addr, addrlen, &timeout);
Guido van Rossum11ba0942002-06-13 15:07:44 +00001647 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001648
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001649 if (timeout) {
1650 PyErr_SetString(socket_timeout, "timed out");
1651 return NULL;
1652 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001653 if (res != 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001654 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001655 Py_INCREF(Py_None);
1656 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001657}
1658
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001659PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001660"connect(address)\n\
1661\n\
1662Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001663is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001664
Guido van Rossum30a685f1991-06-27 15:51:29 +00001665
Fred Drake728819a2000-07-01 03:40:12 +00001666/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001667
1668static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001669sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001670{
1671 struct sockaddr *addr;
1672 int addrlen;
1673 int res;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001674 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001675
Fred Drake728819a2000-07-01 03:40:12 +00001676 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001677 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001678
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001679 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001680 res = internal_connect(s, addr, addrlen, &timeout);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001681 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001682
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001683 return PyInt_FromLong((long) res);
1684}
1685
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001686PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001687"connect_ex(address) -> errno\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001688\n\
1689This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001690instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001691
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001692
Guido van Rossumed233a51992-06-23 09:07:03 +00001693/* s.fileno() method */
1694
Guido van Rossum73624e91994-10-10 17:59:00 +00001695static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001696sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00001697{
Fred Drakea04eaad2000-06-30 02:46:07 +00001698#if SIZEOF_SOCKET_T <= SIZEOF_LONG
Guido van Rossum73624e91994-10-10 17:59:00 +00001699 return PyInt_FromLong((long) s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +00001700#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001701 return PyLong_FromLongLong((PY_LONG_LONG)s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +00001702#endif
Guido van Rossumed233a51992-06-23 09:07:03 +00001703}
1704
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001705PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001706"fileno() -> integer\n\
1707\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001708Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001709
Guido van Rossumed233a51992-06-23 09:07:03 +00001710
Guido van Rossumbe32c891996-06-20 16:25:29 +00001711#ifndef NO_DUP
1712/* s.dup() method */
1713
1714static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001715sock_dup(PySocketSockObject *s)
Guido van Rossumbe32c891996-06-20 16:25:29 +00001716{
Fred Drakea04eaad2000-06-30 02:46:07 +00001717 SOCKET_T newfd;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001718 PyObject *sock;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001719
Guido van Rossumbe32c891996-06-20 16:25:29 +00001720 newfd = dup(s->sock_fd);
1721 if (newfd < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001722 return s->errorhandler();
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001723 sock = (PyObject *) new_sockobject(newfd,
1724 s->sock_family,
1725 s->sock_type,
1726 s->sock_proto);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001727 if (sock == NULL)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001728 SOCKETCLOSE(newfd);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001729 return sock;
1730}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001731
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001732PyDoc_STRVAR(dup_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001733"dup() -> socket object\n\
1734\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001735Return a new socket object connected to the same system resource.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001736
Guido van Rossumbe32c891996-06-20 16:25:29 +00001737#endif
1738
1739
Guido van Rossumc89705d1992-11-26 08:54:07 +00001740/* s.getsockname() method */
1741
Guido van Rossum73624e91994-10-10 17:59:00 +00001742static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001743sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001744{
1745 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001746 int res;
1747 socklen_t addrlen;
1748
Guido van Rossumc89705d1992-11-26 08:54:07 +00001749 if (!getsockaddrlen(s, &addrlen))
1750 return NULL;
Guido van Rossumca42b161996-01-12 01:36:05 +00001751 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001752 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001753 res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001754 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001755 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001756 return s->errorhandler();
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001757 return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001758}
1759
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001760PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001761"getsockname() -> address info\n\
1762\n\
1763Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001764info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001765
Guido van Rossumc89705d1992-11-26 08:54:07 +00001766
Guido van Rossumb6775db1994-08-01 11:34:53 +00001767#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001768/* s.getpeername() method */
1769
Guido van Rossum73624e91994-10-10 17:59:00 +00001770static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001771sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001772{
1773 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001774 int res;
1775 socklen_t addrlen;
1776
Guido van Rossumc89705d1992-11-26 08:54:07 +00001777 if (!getsockaddrlen(s, &addrlen))
1778 return NULL;
Fred Drakefd168342001-05-09 19:11:33 +00001779 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001780 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001781 res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001782 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001783 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001784 return s->errorhandler();
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001785 return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001786}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001787
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001788PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001789"getpeername() -> address info\n\
1790\n\
1791Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001792info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001793
Guido van Rossumb6775db1994-08-01 11:34:53 +00001794#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001795
1796
Guido van Rossum30a685f1991-06-27 15:51:29 +00001797/* s.listen(n) method */
1798
Guido van Rossum73624e91994-10-10 17:59:00 +00001799static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001800sock_listen(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001801{
1802 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001803 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001804
1805 backlog = PyInt_AsLong(arg);
1806 if (backlog == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001807 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001808 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +00001809 if (backlog < 1)
1810 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001811 res = listen(s->sock_fd, backlog);
Guido van Rossum73624e91994-10-10 17:59:00 +00001812 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001813 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001814 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001815 Py_INCREF(Py_None);
1816 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001817}
1818
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001819PyDoc_STRVAR(listen_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001820"listen(backlog)\n\
1821\n\
1822Enable a server to accept connections. The backlog argument must be at\n\
1823least 1; it specifies the number of unaccepted connection that the system\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001824will allow before refusing new connections.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001825
1826
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001827#ifndef NO_DUP
Guido van Rossum30a685f1991-06-27 15:51:29 +00001828/* s.makefile(mode) method.
1829 Create a new open file object referring to a dupped version of
1830 the socket's file descriptor. (The dup() call is necessary so
1831 that the open file and socket objects may be closed independent
1832 of each other.)
1833 The mode argument specifies 'r' or 'w' passed to fdopen(). */
1834
Guido van Rossum73624e91994-10-10 17:59:00 +00001835static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001836sock_makefile(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001837{
Tim Petersdbd9ba62000-07-09 03:09:57 +00001838 extern int fclose(FILE *);
Guido van Rossum6b144911995-03-14 15:05:13 +00001839 char *mode = "r";
1840 int bufsize = -1;
Fred Drakea04eaad2000-06-30 02:46:07 +00001841#ifdef MS_WIN32
Tim Peters79248aa2001-08-29 21:37:10 +00001842 Py_intptr_t fd;
Fred Drakea04eaad2000-06-30 02:46:07 +00001843#else
Guido van Rossum30a685f1991-06-27 15:51:29 +00001844 int fd;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001845#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001846 FILE *fp;
Guido van Rossum6b144911995-03-14 15:05:13 +00001847 PyObject *f;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001848#ifdef __VMS
1849 char *mode_r = "r";
1850 char *mode_w = "w";
1851#endif
Guido van Rossum6b144911995-03-14 15:05:13 +00001852
Guido van Rossum43713e52000-02-29 13:59:29 +00001853 if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001854 return NULL;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001855#ifdef __VMS
1856 if (strcmp(mode,"rb") == 0) {
1857 mode = mode_r;
1858 }
1859 else {
1860 if (strcmp(mode,"wb") == 0) {
1861 mode = mode_w;
1862 }
1863 }
1864#endif
Guido van Rossum8d665e61996-06-26 18:22:49 +00001865#ifdef MS_WIN32
Guido van Rossum82a5c661998-07-07 20:45:43 +00001866 if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) ||
1867 ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL))
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001868#else
Guido van Rossum82a5c661998-07-07 20:45:43 +00001869 if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL)
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001870#endif
Guido van Rossum82a5c661998-07-07 20:45:43 +00001871 {
Guido van Rossum6b144911995-03-14 15:05:13 +00001872 if (fd >= 0)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001873 SOCKETCLOSE(fd);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001874 return s->errorhandler();
Guido van Rossum6b144911995-03-14 15:05:13 +00001875 }
1876 f = PyFile_FromFile(fp, "<socket>", mode, fclose);
1877 if (f != NULL)
1878 PyFile_SetBufSize(f, bufsize);
1879 return f;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001880}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001881
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001882PyDoc_STRVAR(makefile_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001883"makefile([mode[, buffersize]]) -> file object\n\
1884\n\
1885Return a regular file object corresponding to the socket.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001886The mode and buffersize arguments are as for the built-in open() function.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001887
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001888#endif /* NO_DUP */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001889
Guido van Rossum48a680c2001-03-02 06:34:14 +00001890
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001891/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001892
Guido van Rossum73624e91994-10-10 17:59:00 +00001893static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001894sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001895{
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001896 int len, n = 0, flags = 0, timeout;
Guido van Rossum73624e91994-10-10 17:59:00 +00001897 PyObject *buf;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001898#ifdef __VMS
1899 int read_length;
1900 char *read_buf;
1901#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00001902
Guido van Rossum43713e52000-02-29 13:59:29 +00001903 if (!PyArg_ParseTuple(args, "i|i:recv", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001904 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001905
1906 if (len < 0) {
Martin v. Löwisfba64e12001-11-19 10:41:26 +00001907 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00001908 "negative buffersize in recv");
Martin v. Löwisfba64e12001-11-19 10:41:26 +00001909 return NULL;
1910 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00001911
Guido van Rossum73624e91994-10-10 17:59:00 +00001912 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001913 if (buf == NULL)
1914 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001915
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001916#ifndef __VMS
Guido van Rossum73624e91994-10-10 17:59:00 +00001917 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001918 timeout = internal_select(s, 0);
1919 if (!timeout)
1920 n = recv(s->sock_fd, PyString_AS_STRING(buf), len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001921 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001922
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001923 if (timeout) {
1924 Py_DECREF(buf);
1925 PyErr_SetString(socket_timeout, "timed out");
1926 return NULL;
1927 }
Guido van Rossum7c53b771995-09-13 18:39:47 +00001928 if (n < 0) {
1929 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001930 return s->errorhandler();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001931 }
Tim Peters5de98422002-04-27 18:44:32 +00001932 if (n != len)
1933 _PyString_Resize(&buf, n);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001934#else
1935 read_buf = PyString_AsString(buf);
1936 read_length = len;
1937 while (read_length != 0) {
1938 unsigned int segment;
1939
1940 segment = read_length /SEGMENT_SIZE;
1941 if (segment != 0) {
1942 segment = SEGMENT_SIZE;
1943 }
1944 else {
1945 segment = read_length;
1946 }
1947
1948 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001949 timeout = internal_select(s, 0);
1950 if (!timeout)
1951 n = recv(s->sock_fd, read_buf, segment, flags);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001952 Py_END_ALLOW_THREADS
1953
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001954 if (timeout) {
1955 Py_DECREF(buf);
1956 PyErr_SetString(socket_timeout, "timed out");
1957 return NULL;
1958 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001959 if (n < 0) {
1960 Py_DECREF(buf);
1961 return s->errorhandler();
1962 }
1963 if (n != read_length) {
1964 read_buf += n;
1965 break;
1966 }
1967
1968 read_length -= segment;
1969 read_buf += segment;
1970 }
1971 if (_PyString_Resize(&buf, (read_buf - PyString_AsString(buf))) < 0)
1972 {
1973 return NULL;
1974 }
1975#endif /* !__VMS */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001976 return buf;
1977}
1978
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001979PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001980"recv(buffersize[, flags]) -> data\n\
1981\n\
1982Receive up to buffersize bytes from the socket. For the optional flags\n\
1983argument, see the Unix manual. When no data is available, block until\n\
1984at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001985the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001986
Guido van Rossum30a685f1991-06-27 15:51:29 +00001987
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001988/* s.recvfrom(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001989
Guido van Rossum73624e91994-10-10 17:59:00 +00001990static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001991sock_recvfrom(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001992{
1993 char addrbuf[256];
Barry Warsaw752300b1997-01-03 17:18:10 +00001994 PyObject *buf = NULL;
1995 PyObject *addr = NULL;
1996 PyObject *ret = NULL;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001997 int len, n = 0, flags = 0, timeout;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001998 socklen_t addrlen;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001999
Guido van Rossum43713e52000-02-29 13:59:29 +00002000 if (!PyArg_ParseTuple(args, "i|i:recvfrom", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002001 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002002
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00002003 if (!getsockaddrlen(s, &addrlen))
2004 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002005 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00002006 if (buf == NULL)
2007 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002008
Guido van Rossum73624e91994-10-10 17:59:00 +00002009 Py_BEGIN_ALLOW_THREADS
Fred Drakefd168342001-05-09 19:11:33 +00002010 memset(addrbuf, 0, addrlen);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002011 timeout = internal_select(s, 0);
2012 if (!timeout)
2013 n = recvfrom(s->sock_fd, PyString_AS_STRING(buf), len, flags,
Guido van Rossum8d665e61996-06-26 18:22:49 +00002014#ifndef MS_WINDOWS
Andrew MacIntyreba43e872002-03-03 03:03:52 +00002015#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002016 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00002017#else
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002018 (void *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00002019#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002020#else
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002021 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002022#endif
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002023 );
Guido van Rossum73624e91994-10-10 17:59:00 +00002024 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002025
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002026 if (timeout) {
2027 Py_DECREF(buf);
2028 PyErr_SetString(socket_timeout, "timed out");
2029 return NULL;
2030 }
Guido van Rossum7c53b771995-09-13 18:39:47 +00002031 if (n < 0) {
2032 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002033 return s->errorhandler();
Guido van Rossum7c53b771995-09-13 18:39:47 +00002034 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002035
Guido van Rossum53a9bf81996-06-11 18:35:24 +00002036 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002037 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002038
Guido van Rossum67f7a382002-06-06 21:08:16 +00002039 if (!(addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002040 addrlen)))
Barry Warsaw752300b1997-01-03 17:18:10 +00002041 goto finally;
2042
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002043 ret = PyTuple_Pack(2, buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002044
2045finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00002046 Py_XDECREF(addr);
2047 Py_XDECREF(buf);
Guido van Rossum6f5afc91993-02-05 09:46:15 +00002048 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002049}
2050
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002051PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002052"recvfrom(buffersize[, flags]) -> (data, address info)\n\
2053\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002054Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002055
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002056/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002057
Guido van Rossum73624e91994-10-10 17:59:00 +00002058static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002059sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002060{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002061 char *buf;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002062 int len, n = 0, flags = 0, timeout;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002063#ifdef __VMS
2064 int send_length;
2065#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002066
Guido van Rossum43713e52000-02-29 13:59:29 +00002067 if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002068 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002069
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002070#ifndef __VMS
Guido van Rossum73624e91994-10-10 17:59:00 +00002071 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002072 timeout = internal_select(s, 1);
2073 if (!timeout)
2074 n = send(s->sock_fd, buf, len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00002075 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002076
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002077 if (timeout) {
2078 PyErr_SetString(socket_timeout, "timed out");
2079 return NULL;
2080 }
Guido van Rossum30a685f1991-06-27 15:51:29 +00002081 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002082 return s->errorhandler();
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002083#else
2084 /* Divide packet into smaller segments for */
2085 /* TCP/IP Services for OpenVMS */
2086 send_length = len;
2087 while (send_length != 0) {
2088 unsigned int segment;
2089
2090 segment = send_length / SEGMENT_SIZE;
2091 if (segment != 0) {
2092 segment = SEGMENT_SIZE;
2093 }
2094 else {
2095 segment = send_length;
2096 }
2097 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002098 timeout = internal_select(s, 1);
2099 if (!timeout)
2100 n = send(s->sock_fd, buf, segment, flags);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002101 Py_END_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002102 if (timeout) {
2103 PyErr_SetString(socket_timeout, "timed out");
2104 return NULL;
2105 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002106 if (n < 0) {
2107 return s->errorhandler();
2108 }
2109 send_length -= segment;
2110 buf += segment;
2111 } /* end while */
2112#endif /* !__VMS */
Guido van Rossum73624e91994-10-10 17:59:00 +00002113 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00002114}
2115
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002116PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002117"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002118\n\
2119Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002120argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002121sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002122
2123
2124/* s.sendall(data [,flags]) method */
2125
2126static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002127sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002128{
2129 char *buf;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002130 int len, n = 0, flags = 0, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002131
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002132 if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags))
2133 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002134
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002135 Py_BEGIN_ALLOW_THREADS
2136 do {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002137 timeout = internal_select(s, 1);
2138 if (timeout)
2139 break;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002140 n = send(s->sock_fd, buf, len, flags);
2141 if (n < 0)
2142 break;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002143 buf += n;
2144 len -= n;
2145 } while (len > 0);
2146 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002147
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002148 if (timeout) {
2149 PyErr_SetString(socket_timeout, "timed out");
2150 return NULL;
2151 }
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002152 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002153 return s->errorhandler();
Guido van Rossum67f7a382002-06-06 21:08:16 +00002154
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002155 Py_INCREF(Py_None);
2156 return Py_None;
2157}
2158
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002159PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002160"sendall(data[, flags])\n\
2161\n\
2162Send a data string to the socket. For the optional flags\n\
2163argument, see the Unix manual. This calls send() repeatedly\n\
2164until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002165to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002166
Guido van Rossum30a685f1991-06-27 15:51:29 +00002167
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002168/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002169
Guido van Rossum73624e91994-10-10 17:59:00 +00002170static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002171sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002172{
Guido van Rossum73624e91994-10-10 17:59:00 +00002173 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002174 char *buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002175 struct sockaddr *addr;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002176 int addrlen, len, n = 0, flags, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002177
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002178 flags = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002179 if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) {
Guido van Rossum73624e91994-10-10 17:59:00 +00002180 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002181 if (!PyArg_ParseTuple(args, "s#iO:sendto",
2182 &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002183 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002184 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002185
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002186 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002187 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002188
Guido van Rossum73624e91994-10-10 17:59:00 +00002189 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002190 timeout = internal_select(s, 1);
2191 if (!timeout)
2192 n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002193 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002194
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002195 if (timeout) {
2196 PyErr_SetString(socket_timeout, "timed out");
2197 return NULL;
2198 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002199 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002200 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002201 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002202}
2203
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002204PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002205"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002206\n\
2207Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002208For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002209
Guido van Rossum30a685f1991-06-27 15:51:29 +00002210
2211/* s.shutdown(how) method */
2212
Guido van Rossum73624e91994-10-10 17:59:00 +00002213static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002214sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002215{
2216 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002217 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002218
2219 how = PyInt_AsLong(arg);
2220 if (how == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002221 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002222 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002223 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +00002224 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002225 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002226 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002227 Py_INCREF(Py_None);
2228 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002229}
2230
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002231PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002232"shutdown(flag)\n\
2233\n\
Martin v. Löwis94681fc2003-11-27 19:40:22 +00002234Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\
2235of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002236
Guido van Rossum30a685f1991-06-27 15:51:29 +00002237
2238/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002239
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002240static PyMethodDef sock_methods[] = {
2241 {"accept", (PyCFunction)sock_accept, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002242 accept_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002243 {"bind", (PyCFunction)sock_bind, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002244 bind_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002245 {"close", (PyCFunction)sock_close, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002246 close_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002247 {"connect", (PyCFunction)sock_connect, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002248 connect_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002249 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002250 connect_ex_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00002251#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002252 {"dup", (PyCFunction)sock_dup, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002253 dup_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00002254#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002255 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002256 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002257#ifdef HAVE_GETPEERNAME
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002258 {"getpeername", (PyCFunction)sock_getpeername,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002259 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00002260#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002261 {"getsockname", (PyCFunction)sock_getsockname,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002262 METH_NOARGS, getsockname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002263 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002264 getsockopt_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002265 {"listen", (PyCFunction)sock_listen, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002266 listen_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002267#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002268 {"makefile", (PyCFunction)sock_makefile, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002269 makefile_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002270#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002271 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002272 recv_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002273 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002274 recvfrom_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002275 {"send", (PyCFunction)sock_send, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002276 send_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002277 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002278 sendall_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002279 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002280 sendto_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002281 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002282 setblocking_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002283 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
Guido van Rossum67f7a382002-06-06 21:08:16 +00002284 settimeout_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002285 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
Guido van Rossum67f7a382002-06-06 21:08:16 +00002286 gettimeout_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002287 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002288 setsockopt_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002289 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002290 shutdown_doc},
2291#ifdef RISCOS
Martin v. Löwisa94568a2003-05-10 07:36:56 +00002292 {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002293 sleeptaskw_doc},
2294#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00002295 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002296};
2297
Guido van Rossum30a685f1991-06-27 15:51:29 +00002298
Guido van Rossum73624e91994-10-10 17:59:00 +00002299/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00002300 First close the file description. */
2301
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002302static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002303sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002304{
Guido van Rossumfa972c92000-04-10 12:45:45 +00002305 if (s->sock_fd != -1)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00002306 (void) SOCKETCLOSE(s->sock_fd);
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002307 s->ob_type->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002308}
2309
Guido van Rossum30a685f1991-06-27 15:51:29 +00002310
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002311static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002312sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002313{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002314 char buf[512];
Fred Drakea04eaad2000-06-30 02:46:07 +00002315#if SIZEOF_SOCKET_T > SIZEOF_LONG
2316 if (s->sock_fd > LONG_MAX) {
2317 /* this can occur on Win64, and actually there is a special
2318 ugly printf formatter for decimal pointer length integer
2319 printing, only bother if necessary*/
2320 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002321 "no printf formatter to display "
2322 "the socket descriptor in decimal");
Fred Drakea04eaad2000-06-30 02:46:07 +00002323 return NULL;
2324 }
2325#endif
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002326 PyOS_snprintf(
2327 buf, sizeof(buf),
2328 "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>",
2329 (long)s->sock_fd, s->sock_family,
2330 s->sock_type,
2331 s->sock_proto);
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00002332 return PyString_FromString(buf);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002333}
2334
2335
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002336/* Create a new, uninitialized socket object. */
2337
2338static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002339sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002340{
2341 PyObject *new;
2342
2343 new = type->tp_alloc(type, 0);
Guido van Rossume1c478f2002-06-06 20:08:25 +00002344 if (new != NULL) {
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002345 ((PySocketSockObject *)new)->sock_fd = -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002346 ((PySocketSockObject *)new)->sock_timeout = -1.0;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002347 ((PySocketSockObject *)new)->errorhandler = &set_error;
Guido van Rossume1c478f2002-06-06 20:08:25 +00002348 }
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002349 return new;
2350}
2351
2352
2353/* Initialize a new socket object. */
2354
2355/*ARGSUSED*/
2356static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002357sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002358{
2359 PySocketSockObject *s = (PySocketSockObject *)self;
2360 SOCKET_T fd;
2361 int family = AF_INET, type = SOCK_STREAM, proto = 0;
2362 static char *keywords[] = {"family", "type", "proto", 0};
2363
2364 if (!PyArg_ParseTupleAndKeywords(args, kwds,
2365 "|iii:socket", keywords,
2366 &family, &type, &proto))
2367 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002368
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002369 Py_BEGIN_ALLOW_THREADS
2370 fd = socket(family, type, proto);
2371 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002372
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002373#ifdef MS_WINDOWS
2374 if (fd == INVALID_SOCKET)
2375#else
2376 if (fd < 0)
2377#endif
2378 {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002379 set_error();
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002380 return -1;
2381 }
2382 init_sockobject(s, fd, family, type, proto);
2383 /* From now on, ignore SIGPIPE and let the error checking
2384 do the work. */
2385#ifdef SIGPIPE
2386 (void) signal(SIGPIPE, SIG_IGN);
2387#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002388
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002389 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002390
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002391}
2392
2393
Guido van Rossumb6775db1994-08-01 11:34:53 +00002394/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002395
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002396static PyTypeObject sock_type = {
Guido van Rossum55558541996-05-23 22:54:50 +00002397 PyObject_HEAD_INIT(0) /* Must fill in type value later */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002398 0, /* ob_size */
Guido van Rossumca6dfa52001-10-28 12:31:33 +00002399 "_socket.socket", /* tp_name */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002400 sizeof(PySocketSockObject), /* tp_basicsize */
2401 0, /* tp_itemsize */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002402 (destructor)sock_dealloc, /* tp_dealloc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002403 0, /* tp_print */
2404 0, /* tp_getattr */
2405 0, /* tp_setattr */
2406 0, /* tp_compare */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002407 (reprfunc)sock_repr, /* tp_repr */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002408 0, /* tp_as_number */
2409 0, /* tp_as_sequence */
2410 0, /* tp_as_mapping */
2411 0, /* tp_hash */
2412 0, /* tp_call */
2413 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002414 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002415 0, /* tp_setattro */
2416 0, /* tp_as_buffer */
2417 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002418 sock_doc, /* tp_doc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002419 0, /* tp_traverse */
2420 0, /* tp_clear */
2421 0, /* tp_richcompare */
2422 0, /* tp_weaklistoffset */
2423 0, /* tp_iter */
2424 0, /* tp_iternext */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002425 sock_methods, /* tp_methods */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002426 0, /* tp_members */
2427 0, /* tp_getset */
2428 0, /* tp_base */
2429 0, /* tp_dict */
2430 0, /* tp_descr_get */
2431 0, /* tp_descr_set */
2432 0, /* tp_dictoffset */
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002433 sock_initobj, /* tp_init */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002434 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002435 sock_new, /* tp_new */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002436 PyObject_Del, /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002437};
2438
Guido van Rossum30a685f1991-06-27 15:51:29 +00002439
Guido van Rossum81194471991-07-27 21:42:02 +00002440/* Python interface to gethostname(). */
2441
2442/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002443static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002444socket_gethostname(PyObject *self, PyObject *args)
Guido van Rossum81194471991-07-27 21:42:02 +00002445{
2446 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00002447 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002448 if (!PyArg_ParseTuple(args, ":gethostname"))
Guido van Rossum81194471991-07-27 21:42:02 +00002449 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002450 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002451 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00002452 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002453 if (res < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002454 return set_error();
Guido van Rossum81194471991-07-27 21:42:02 +00002455 buf[sizeof buf - 1] = '\0';
Guido van Rossum73624e91994-10-10 17:59:00 +00002456 return PyString_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00002457}
Guido van Rossumff4949e1992-08-05 19:58:53 +00002458
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002459PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002460"gethostname() -> string\n\
2461\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002462Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002463
Guido van Rossumff4949e1992-08-05 19:58:53 +00002464
Guido van Rossum30a685f1991-06-27 15:51:29 +00002465/* Python interface to gethostbyname(name). */
2466
2467/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002468static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002469socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002470{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002471 char *name;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00002472#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002473 struct sockaddr_storage addrbuf;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00002474#else
2475 struct sockaddr_in addrbuf;
2476#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002477
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002478 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002479 return NULL;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002480 if (setipaddr(name, (struct sockaddr *)&addrbuf, sizeof(addrbuf), AF_INET) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002481 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002482 return makeipaddr((struct sockaddr *)&addrbuf,
2483 sizeof(struct sockaddr_in));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002484}
2485
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002486PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002487"gethostbyname(host) -> address\n\
2488\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002489Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002490
2491
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002492/* Convenience function common to gethostbyname_ex and gethostbyaddr */
2493
2494static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002495gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002496{
2497 char **pch;
2498 PyObject *rtn_tuple = (PyObject *)NULL;
2499 PyObject *name_list = (PyObject *)NULL;
2500 PyObject *addr_list = (PyObject *)NULL;
2501 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002502
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002503 if (h == NULL) {
Guido van Rossum48a680c2001-03-02 06:34:14 +00002504 /* Let's get real error message to return */
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002505#ifndef RISCOS
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002506 set_herror(h_errno);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002507#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002508 PyErr_SetString(socket_error, "host not found");
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002509#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002510 return NULL;
2511 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002512
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002513 if (h->h_addrtype != af) {
2514#ifdef HAVE_STRERROR
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002515 /* Let's get real error message to return */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002516 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002517 (char *)strerror(EAFNOSUPPORT));
2518#else
2519 PyErr_SetString(
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002520 socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002521 "Address family not supported by protocol family");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002522#endif
2523 return NULL;
2524 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002525
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002526 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002527
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002528 case AF_INET:
2529 if (alen < sizeof(struct sockaddr_in))
2530 return NULL;
2531 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002532
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002533#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002534 case AF_INET6:
2535 if (alen < sizeof(struct sockaddr_in6))
2536 return NULL;
2537 break;
2538#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002539
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002540 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002541
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002542 if ((name_list = PyList_New(0)) == NULL)
2543 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002544
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002545 if ((addr_list = PyList_New(0)) == NULL)
2546 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002547
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002548 for (pch = h->h_aliases; *pch != NULL; pch++) {
2549 int status;
2550 tmp = PyString_FromString(*pch);
2551 if (tmp == NULL)
2552 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002553
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002554 status = PyList_Append(name_list, tmp);
2555 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002556
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002557 if (status)
2558 goto err;
2559 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002560
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002561 for (pch = h->h_addr_list; *pch != NULL; pch++) {
2562 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002563
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002564 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002565
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002566 case AF_INET:
2567 {
2568 struct sockaddr_in sin;
2569 memset(&sin, 0, sizeof(sin));
2570 sin.sin_family = af;
2571#ifdef HAVE_SOCKADDR_SA_LEN
2572 sin.sin_len = sizeof(sin);
2573#endif
2574 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
2575 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002576
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002577 if (pch == h->h_addr_list && alen >= sizeof(sin))
2578 memcpy((char *) addr, &sin, sizeof(sin));
2579 break;
2580 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002581
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002582#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002583 case AF_INET6:
2584 {
2585 struct sockaddr_in6 sin6;
2586 memset(&sin6, 0, sizeof(sin6));
2587 sin6.sin6_family = af;
2588#ifdef HAVE_SOCKADDR_SA_LEN
2589 sin6.sin6_len = sizeof(sin6);
2590#endif
2591 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
2592 tmp = makeipaddr((struct sockaddr *)&sin6,
2593 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002594
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002595 if (pch == h->h_addr_list && alen >= sizeof(sin6))
2596 memcpy((char *) addr, &sin6, sizeof(sin6));
2597 break;
2598 }
2599#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002600
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002601 default: /* can't happen */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002602 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002603 "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002604 return NULL;
2605 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002606
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002607 if (tmp == NULL)
2608 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002609
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002610 status = PyList_Append(addr_list, tmp);
2611 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002612
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002613 if (status)
2614 goto err;
2615 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002616
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002617 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002618
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002619 err:
2620 Py_XDECREF(name_list);
2621 Py_XDECREF(addr_list);
2622 return rtn_tuple;
2623}
2624
2625
2626/* Python interface to gethostbyname_ex(name). */
2627
2628/*ARGSUSED*/
2629static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002630socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002631{
2632 char *name;
2633 struct hostent *h;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00002634#ifdef ENABLE_IPV6
2635 struct sockaddr_storage addr;
2636#else
2637 struct sockaddr_in addr;
2638#endif
Martin v. Löwis9db2f572001-07-23 01:30:10 +00002639 struct sockaddr *sa;
Guido van Rossum955becc1999-03-22 20:14:53 +00002640 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002641#ifdef HAVE_GETHOSTBYNAME_R
2642 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002643#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
2644 struct hostent_data data;
2645#else
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002646 char buf[16384];
2647 int buf_len = (sizeof buf) - 1;
2648 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002649#endif
2650#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002651 int result;
2652#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002653#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002654
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002655 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002656 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00002657 if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002658 return NULL;
2659 Py_BEGIN_ALLOW_THREADS
2660#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002661#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002662 result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
2663 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002664#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002665 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002666#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00002667 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002668 result = gethostbyname_r(name, &hp_allocated, &data);
2669 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002670#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002671#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002672#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002673 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002674#endif
2675 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002676#endif /* HAVE_GETHOSTBYNAME_R */
2677 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002678 /* Some C libraries would require addr.__ss_family instead of
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002679 addr.ss_family.
2680 Therefore, we cast the sockaddr_storage into sockaddr to
2681 access sa_family. */
Martin v. Löwis9db2f572001-07-23 01:30:10 +00002682 sa = (struct sockaddr*)&addr;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002683 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002684 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002685#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002686 PyThread_release_lock(netdb_lock);
Guido van Rossum955becc1999-03-22 20:14:53 +00002687#endif
2688 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002689}
2690
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002691PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002692"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
2693\n\
2694Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002695for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002696
2697
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002698/* Python interface to gethostbyaddr(IP). */
2699
2700/*ARGSUSED*/
2701static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002702socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002703{
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002704#ifdef ENABLE_IPV6
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002705 struct sockaddr_storage addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002706#else
Guido van Rossum48a680c2001-03-02 06:34:14 +00002707 struct sockaddr_in addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002708#endif
2709 struct sockaddr *sa = (struct sockaddr *)&addr;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002710 char *ip_num;
2711 struct hostent *h;
Guido van Rossum3baaa131999-03-22 21:44:51 +00002712 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002713#ifdef HAVE_GETHOSTBYNAME_R
2714 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002715#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
2716 struct hostent_data data;
2717#else
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002718 char buf[16384];
2719 int buf_len = (sizeof buf) - 1;
2720 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002721#endif
2722#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002723 int result;
2724#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002725#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002726 char *ap;
2727 int al;
2728 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002729
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002730 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002731 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00002732 af = AF_UNSPEC;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002733 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002734 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002735 af = sa->sa_family;
2736 ap = NULL;
2737 al = 0;
2738 switch (af) {
2739 case AF_INET:
2740 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
2741 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
2742 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002743#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002744 case AF_INET6:
2745 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
2746 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
2747 break;
2748#endif
2749 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002750 PyErr_SetString(socket_error, "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002751 return NULL;
2752 }
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002753 Py_BEGIN_ALLOW_THREADS
2754#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002755#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002756 result = gethostbyaddr_r(ap, al, af,
2757 &hp_allocated, buf, buf_len,
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002758 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002759#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002760 h = gethostbyaddr_r(ap, al, af,
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002761 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002762#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00002763 memset((void *) &data, '\0', sizeof(data));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002764 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002765 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002766#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002767#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002768#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002769 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002770#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002771 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002772#endif /* HAVE_GETHOSTBYNAME_R */
2773 Py_END_ALLOW_THREADS
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002774 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002775#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002776 PyThread_release_lock(netdb_lock);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002777#endif
2778 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002779}
2780
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002781PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002782"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
2783\n\
2784Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002785for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002786
Guido van Rossum30a685f1991-06-27 15:51:29 +00002787
2788/* Python interface to getservbyname(name).
2789 This only returns the port number, since the other info is already
2790 known or not useful (like the list of aliases). */
2791
2792/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002793static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002794socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002795{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002796 char *name, *proto;
Guido van Rossum30a685f1991-06-27 15:51:29 +00002797 struct servent *sp;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002798 if (!PyArg_ParseTuple(args, "ss:getservbyname", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002799 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002800 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002801 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00002802 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00002803 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002804 PyErr_SetString(socket_error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00002805 return NULL;
2806 }
Guido van Rossum73624e91994-10-10 17:59:00 +00002807 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002808}
2809
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002810PyDoc_STRVAR(getservbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002811"getservbyname(servicename, protocolname) -> integer\n\
2812\n\
2813Return a port number from a service name and protocol name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002814The protocol name should be 'tcp' or 'udp'.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002815
Guido van Rossum30a685f1991-06-27 15:51:29 +00002816
Guido van Rossum3901d851996-12-19 16:35:04 +00002817/* Python interface to getprotobyname(name).
2818 This only returns the protocol number, since the other info is
2819 already known or not useful (like the list of aliases). */
2820
2821/*ARGSUSED*/
2822static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002823socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00002824{
2825 char *name;
2826 struct protoent *sp;
Guido van Rossumbcc20741998-08-04 22:53:56 +00002827#ifdef __BEOS__
2828/* Not available in BeOS yet. - [cjh] */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002829 PyErr_SetString(socket_error, "getprotobyname not supported");
Guido van Rossumbcc20741998-08-04 22:53:56 +00002830 return NULL;
2831#else
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002832 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
Guido van Rossum3901d851996-12-19 16:35:04 +00002833 return NULL;
2834 Py_BEGIN_ALLOW_THREADS
2835 sp = getprotobyname(name);
2836 Py_END_ALLOW_THREADS
2837 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002838 PyErr_SetString(socket_error, "protocol not found");
Guido van Rossum3901d851996-12-19 16:35:04 +00002839 return NULL;
2840 }
2841 return PyInt_FromLong((long) sp->p_proto);
Guido van Rossumbcc20741998-08-04 22:53:56 +00002842#endif
Guido van Rossum3901d851996-12-19 16:35:04 +00002843}
2844
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002845PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002846"getprotobyname(name) -> integer\n\
2847\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002848Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002849
Guido van Rossum3901d851996-12-19 16:35:04 +00002850
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002851#ifndef NO_DUP
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002852/* Create a socket object from a numeric file description.
2853 Useful e.g. if stdin is a socket.
2854 Additional arguments as for socket(). */
2855
2856/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002857static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002858socket_fromfd(PyObject *self, PyObject *args)
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002859{
Guido van Rossum73624e91994-10-10 17:59:00 +00002860 PySocketSockObject *s;
Fred Drakea04eaad2000-06-30 02:46:07 +00002861 SOCKET_T fd;
2862 int family, type, proto = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002863 if (!PyArg_ParseTuple(args, "iii|i:fromfd",
2864 &fd, &family, &type, &proto))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002865 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +00002866 /* Dup the fd so it and the socket can be closed independently */
2867 fd = dup(fd);
2868 if (fd < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002869 return set_error();
2870 s = new_sockobject(fd, family, type, proto);
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002871 /* From now on, ignore SIGPIPE and let the error checking
2872 do the work. */
Guido van Rossum48a680c2001-03-02 06:34:14 +00002873#ifdef SIGPIPE
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002874 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum48a680c2001-03-02 06:34:14 +00002875#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00002876 return (PyObject *) s;
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002877}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002878
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002879PyDoc_STRVAR(fromfd_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002880"fromfd(fd, family, type[, proto]) -> socket object\n\
2881\n\
2882Create a socket object from the given file descriptor.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002883The remaining arguments are the same as for socket().");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002884
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002885#endif /* NO_DUP */
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002886
Guido van Rossum82a5c661998-07-07 20:45:43 +00002887
Guido van Rossum006bf911996-06-12 04:04:55 +00002888static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002889socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00002890{
2891 int x1, x2;
2892
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002893 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00002894 return NULL;
2895 }
2896 x2 = (int)ntohs((short)x1);
2897 return PyInt_FromLong(x2);
2898}
2899
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002900PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002901"ntohs(integer) -> integer\n\
2902\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002903Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002904
2905
Guido van Rossum006bf911996-06-12 04:04:55 +00002906static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002907socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00002908{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002909 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00002910
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002911 if (PyInt_Check(arg)) {
2912 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002913 if (x == (unsigned long) -1 && PyErr_Occurred())
2914 return NULL;
Guido van Rossum006bf911996-06-12 04:04:55 +00002915 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002916 else if (PyLong_Check(arg)) {
2917 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002918 if (x == (unsigned long) -1 && PyErr_Occurred())
2919 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002920#if SIZEOF_LONG > 4
2921 {
2922 unsigned long y;
2923 /* only want the trailing 32 bits */
2924 y = x & 0xFFFFFFFFUL;
2925 if (y ^ x)
2926 return PyErr_Format(PyExc_OverflowError,
2927 "long int larger than 32 bits");
2928 x = y;
Tim Peters58141872002-08-06 22:25:02 +00002929 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002930#endif
2931 }
2932 else
Tim Peters58141872002-08-06 22:25:02 +00002933 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002934 "expected int/long, %s found",
2935 arg->ob_type->tp_name);
2936 if (x == (unsigned long) -1 && PyErr_Occurred())
2937 return NULL;
2938 return PyInt_FromLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00002939}
2940
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002941PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002942"ntohl(integer) -> integer\n\
2943\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002944Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002945
2946
Guido van Rossum006bf911996-06-12 04:04:55 +00002947static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002948socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00002949{
Hye-Shik Changa7b673f2003-12-17 09:40:23 +00002950 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00002951
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002952 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00002953 return NULL;
2954 }
2955 x2 = (int)htons((short)x1);
2956 return PyInt_FromLong(x2);
2957}
2958
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002959PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002960"htons(integer) -> integer\n\
2961\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002962Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002963
2964
Guido van Rossum006bf911996-06-12 04:04:55 +00002965static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002966socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00002967{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002968 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00002969
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002970 if (PyInt_Check(arg)) {
2971 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002972 if (x == (unsigned long) -1 && PyErr_Occurred())
2973 return NULL;
Guido van Rossum006bf911996-06-12 04:04:55 +00002974 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002975 else if (PyLong_Check(arg)) {
2976 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002977 if (x == (unsigned long) -1 && PyErr_Occurred())
2978 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002979#if SIZEOF_LONG > 4
2980 {
2981 unsigned long y;
2982 /* only want the trailing 32 bits */
2983 y = x & 0xFFFFFFFFUL;
2984 if (y ^ x)
2985 return PyErr_Format(PyExc_OverflowError,
2986 "long int larger than 32 bits");
2987 x = y;
Tim Peters58141872002-08-06 22:25:02 +00002988 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002989#endif
2990 }
2991 else
Tim Peters58141872002-08-06 22:25:02 +00002992 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002993 "expected int/long, %s found",
2994 arg->ob_type->tp_name);
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002995 return PyInt_FromLong(htonl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00002996}
2997
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002998PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002999"htonl(integer) -> integer\n\
3000\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003001Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003002
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003003/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003004
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003005PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003006"inet_aton(string) -> packed 32-bit IP representation\n\
3007\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003008Convert 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 +00003009binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003010
3011static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003012socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003013{
Guido van Rossuma2e48551999-09-09 15:42:59 +00003014#ifndef INADDR_NONE
3015#define INADDR_NONE (-1)
3016#endif
Neal Norwitz88f115b2003-02-13 02:15:42 +00003017#ifdef HAVE_INET_ATON
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003018 struct in_addr buf;
Tim Peters1df9fdd2003-02-13 03:13:40 +00003019#else
3020 /* Have to use inet_addr() instead */
3021 unsigned long packed_addr;
3022#endif
3023 char *ip_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003024
Tim Peters1df9fdd2003-02-13 03:13:40 +00003025 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003026 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003027
Tim Peters1df9fdd2003-02-13 03:13:40 +00003028
3029#ifdef HAVE_INET_ATON
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003030 if (inet_aton(ip_addr, &buf))
3031 return PyString_FromStringAndSize((char *)(&buf),
3032 sizeof(buf));
3033
3034 PyErr_SetString(socket_error,
3035 "illegal IP address string passed to inet_aton");
3036 return NULL;
3037
Tim Peters1df9fdd2003-02-13 03:13:40 +00003038#else /* ! HAVE_INET_ATON */
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003039 /* XXX Problem here: inet_aton('255.255.255.255') raises
3040 an exception while it should be a valid address. */
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003041 packed_addr = inet_addr(ip_addr);
3042
3043 if (packed_addr == INADDR_NONE) { /* invalid address */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003044 PyErr_SetString(socket_error,
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003045 "illegal IP address string passed to inet_aton");
3046 return NULL;
3047 }
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003048 return PyString_FromStringAndSize((char *) &packed_addr,
3049 sizeof(packed_addr));
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003050#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003051}
3052
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003053PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00003054"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003055\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003056Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003057
3058static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003059socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003060{
3061 char *packed_str;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003062 int addr_len;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003063 struct in_addr packed_addr;
3064
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003065 if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) {
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003066 return NULL;
3067 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00003068
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003069 if (addr_len != sizeof(packed_addr)) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003070 PyErr_SetString(socket_error,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003071 "packed IP wrong length for inet_ntoa");
3072 return NULL;
3073 }
3074
3075 memcpy(&packed_addr, packed_str, addr_len);
3076
3077 return PyString_FromString(inet_ntoa(packed_addr));
3078}
Guido van Rossum82a5c661998-07-07 20:45:43 +00003079
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003080#ifdef HAVE_INET_PTON
3081
3082PyDoc_STRVAR(inet_pton_doc,
3083"inet_pton(af, ip) -> packed IP address string\n\
3084\n\
3085Convert an IP address from string format to a packed string suitable\n\
3086for use with low-level network functions.");
3087
3088static PyObject *
3089socket_inet_pton(PyObject *self, PyObject *args)
3090{
3091 int af;
3092 char* ip;
3093 int retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003094#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003095 char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003096#else
3097 char packed[sizeof(struct in_addr)];
3098#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003099 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
3100 return NULL;
3101 }
3102
Martin v. Löwis10649092003-08-05 06:25:06 +00003103#ifndef ENABLE_IPV6
3104 if(af == AF_INET6) {
3105 PyErr_SetString(socket_error,
3106 "can't use AF_INET6, IPv6 is disabled");
3107 return NULL;
3108 }
3109#endif
3110
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003111 retval = inet_pton(af, ip, packed);
3112 if (retval < 0) {
3113 PyErr_SetFromErrno(socket_error);
3114 return NULL;
3115 } else if (retval == 0) {
3116 PyErr_SetString(socket_error,
3117 "illegal IP address string passed to inet_pton");
3118 return NULL;
3119 } else if (af == AF_INET) {
3120 return PyString_FromStringAndSize(packed,
3121 sizeof(struct in_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003122#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003123 } else if (af == AF_INET6) {
3124 return PyString_FromStringAndSize(packed,
3125 sizeof(struct in6_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003126#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003127 } else {
3128 PyErr_SetString(socket_error, "unknown address family");
3129 return NULL;
3130 }
3131}
3132
3133PyDoc_STRVAR(inet_ntop_doc,
3134"inet_ntop(af, packed_ip) -> string formatted IP address\n\
3135\n\
3136Convert a packed IP address of the given family to string format.");
3137
3138static PyObject *
3139socket_inet_ntop(PyObject *self, PyObject *args)
3140{
3141 int af;
3142 char* packed;
3143 int len;
3144 const char* retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003145#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003146 char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003147#else
3148 char ip[INET_ADDRSTRLEN + 1];
3149#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003150
3151 /* Guarantee NUL-termination for PyString_FromString() below */
3152 memset((void *) &ip[0], '\0', sizeof(ip) + 1);
3153
3154 if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) {
3155 return NULL;
3156 }
3157
3158 if (af == AF_INET) {
3159 if (len != sizeof(struct in_addr)) {
3160 PyErr_SetString(PyExc_ValueError,
3161 "invalid length of packed IP address string");
3162 return NULL;
3163 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003164#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003165 } else if (af == AF_INET6) {
3166 if (len != sizeof(struct in6_addr)) {
3167 PyErr_SetString(PyExc_ValueError,
3168 "invalid length of packed IP address string");
3169 return NULL;
3170 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003171#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003172 } else {
3173 PyErr_Format(PyExc_ValueError,
3174 "unknown address family %d", af);
3175 return NULL;
3176 }
3177
3178 retval = inet_ntop(af, packed, ip, sizeof(ip));
3179 if (!retval) {
3180 PyErr_SetFromErrno(socket_error);
3181 return NULL;
3182 } else {
3183 return PyString_FromString(retval);
3184 }
3185
3186 /* NOTREACHED */
3187 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
3188 return NULL;
3189}
3190
3191#endif /* HAVE_INET_PTON */
3192
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003193/* Python interface to getaddrinfo(host, port). */
3194
3195/*ARGSUSED*/
3196static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003197socket_getaddrinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003198{
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003199 struct addrinfo hints, *res;
3200 struct addrinfo *res0 = NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003201 PyObject *hobj = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003202 PyObject *pobj = (PyObject *)NULL;
Martin v. Löwisf65b1a12001-08-12 09:28:40 +00003203 char pbuf[30];
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003204 char *hptr, *pptr;
3205 int family, socktype, protocol, flags;
3206 int error;
3207 PyObject *all = (PyObject *)NULL;
3208 PyObject *single = (PyObject *)NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003209 PyObject *idna = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003210
3211 family = socktype = protocol = flags = 0;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003212 family = AF_UNSPEC;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003213 if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo",
3214 &hobj, &pobj, &family, &socktype,
3215 &protocol, &flags)) {
3216 return NULL;
3217 }
3218 if (hobj == Py_None) {
3219 hptr = NULL;
3220 } else if (PyUnicode_Check(hobj)) {
3221 idna = PyObject_CallMethod(hobj, "encode", "s", "idna");
3222 if (!idna)
3223 return NULL;
3224 hptr = PyString_AsString(idna);
3225 } else if (PyString_Check(hobj)) {
3226 hptr = PyString_AsString(hobj);
3227 } else {
3228 PyErr_SetString(PyExc_TypeError,
3229 "getaddrinfo() argument 1 must be string or None");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003230 return NULL;
3231 }
3232 if (PyInt_Check(pobj)) {
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003233 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003234 pptr = pbuf;
3235 } else if (PyString_Check(pobj)) {
3236 pptr = PyString_AsString(pobj);
3237 } else if (pobj == Py_None) {
3238 pptr = (char *)NULL;
3239 } else {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003240 PyErr_SetString(socket_error, "Int or String expected");
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003241 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003242 }
3243 memset(&hints, 0, sizeof(hints));
3244 hints.ai_family = family;
3245 hints.ai_socktype = socktype;
3246 hints.ai_protocol = protocol;
3247 hints.ai_flags = flags;
Just van Rossum1040d2c2003-05-09 07:53:18 +00003248 Py_BEGIN_ALLOW_THREADS
3249 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003250 error = getaddrinfo(hptr, pptr, &hints, &res0);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003251 Py_END_ALLOW_THREADS
3252 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003253 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003254 set_gaierror(error);
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003255 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003256 }
3257
3258 if ((all = PyList_New(0)) == NULL)
3259 goto err;
3260 for (res = res0; res; res = res->ai_next) {
Guido van Rossum716aac02001-10-12 18:59:27 +00003261 PyObject *addr =
3262 makesockaddr(-1, res->ai_addr, res->ai_addrlen);
3263 if (addr == NULL)
3264 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003265 single = Py_BuildValue("iiisO", res->ai_family,
3266 res->ai_socktype, res->ai_protocol,
3267 res->ai_canonname ? res->ai_canonname : "",
Guido van Rossum716aac02001-10-12 18:59:27 +00003268 addr);
3269 Py_DECREF(addr);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003270 if (single == NULL)
3271 goto err;
3272
3273 if (PyList_Append(all, single))
3274 goto err;
3275 Py_XDECREF(single);
3276 }
Martin v. Löwis2548c732003-04-18 10:39:54 +00003277 Py_XDECREF(idna);
Neal Norwitz90128ba2002-08-09 03:37:42 +00003278 if (res0)
3279 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003280 return all;
3281 err:
3282 Py_XDECREF(single);
3283 Py_XDECREF(all);
Martin v. Löwis2548c732003-04-18 10:39:54 +00003284 Py_XDECREF(idna);
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003285 if (res0)
3286 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003287 return (PyObject *)NULL;
3288}
3289
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003290PyDoc_STRVAR(getaddrinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003291"getaddrinfo(host, port [, family, socktype, proto, flags])\n\
3292 -> list of (family, socktype, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003293\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003294Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003295
3296/* Python interface to getnameinfo(sa, flags). */
3297
3298/*ARGSUSED*/
3299static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003300socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003301{
3302 PyObject *sa = (PyObject *)NULL;
3303 int flags;
3304 char *hostp;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003305 int port, flowinfo, scope_id;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003306 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
3307 struct addrinfo hints, *res = NULL;
3308 int error;
3309 PyObject *ret = (PyObject *)NULL;
3310
3311 flags = flowinfo = scope_id = 0;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003312 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003313 return NULL;
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003314 if (!PyArg_ParseTuple(sa, "si|ii",
3315 &hostp, &port, &flowinfo, &scope_id))
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003316 return NULL;
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003317 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003318 memset(&hints, 0, sizeof(hints));
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003319 hints.ai_family = AF_UNSPEC;
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003320 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Just van Rossum1040d2c2003-05-09 07:53:18 +00003321 Py_BEGIN_ALLOW_THREADS
3322 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003323 error = getaddrinfo(hostp, pbuf, &hints, &res);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003324 Py_END_ALLOW_THREADS
3325 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003326 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003327 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003328 goto fail;
3329 }
3330 if (res->ai_next) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003331 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003332 "sockaddr resolved to multiple addresses");
3333 goto fail;
3334 }
3335 switch (res->ai_family) {
3336 case AF_INET:
3337 {
3338 char *t1;
3339 int t2;
3340 if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003341 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003342 "IPv4 sockaddr must be 2 tuple");
3343 goto fail;
3344 }
3345 break;
3346 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003347#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003348 case AF_INET6:
3349 {
3350 struct sockaddr_in6 *sin6;
3351 sin6 = (struct sockaddr_in6 *)res->ai_addr;
3352 sin6->sin6_flowinfo = flowinfo;
3353 sin6->sin6_scope_id = scope_id;
3354 break;
3355 }
3356#endif
3357 }
3358 error = getnameinfo(res->ai_addr, res->ai_addrlen,
3359 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
3360 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003361 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003362 goto fail;
3363 }
3364 ret = Py_BuildValue("ss", hbuf, pbuf);
3365
3366fail:
3367 if (res)
3368 freeaddrinfo(res);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003369 return ret;
3370}
3371
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003372PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003373"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003374\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003375Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003376
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003377
3378/* Python API to getting and setting the default timeout value. */
3379
3380static PyObject *
3381socket_getdefaulttimeout(PyObject *self)
3382{
3383 if (defaulttimeout < 0.0) {
3384 Py_INCREF(Py_None);
3385 return Py_None;
3386 }
3387 else
3388 return PyFloat_FromDouble(defaulttimeout);
3389}
3390
3391PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003392"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003393\n\
3394Returns the default timeout in floating seconds for new socket objects.\n\
3395A value of None indicates that new socket objects have no timeout.\n\
3396When the socket module is first imported, the default is None.");
3397
3398static PyObject *
3399socket_setdefaulttimeout(PyObject *self, PyObject *arg)
3400{
3401 double timeout;
3402
3403 if (arg == Py_None)
3404 timeout = -1.0;
3405 else {
3406 timeout = PyFloat_AsDouble(arg);
3407 if (timeout < 0.0) {
3408 if (!PyErr_Occurred())
3409 PyErr_SetString(PyExc_ValueError,
3410 "Timeout value out of range");
3411 return NULL;
3412 }
3413 }
3414
3415 defaulttimeout = timeout;
3416
3417 Py_INCREF(Py_None);
3418 return Py_None;
3419}
3420
3421PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003422"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003423\n\
3424Set the default timeout in floating seconds for new socket objects.\n\
3425A value of None indicates that new socket objects have no timeout.\n\
3426When the socket module is first imported, the default is None.");
3427
3428
Guido van Rossum30a685f1991-06-27 15:51:29 +00003429/* List of functions exported by this module. */
3430
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003431static PyMethodDef socket_methods[] = {
3432 {"gethostbyname", socket_gethostbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003433 METH_VARARGS, gethostbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003434 {"gethostbyname_ex", socket_gethostbyname_ex,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003435 METH_VARARGS, ghbn_ex_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003436 {"gethostbyaddr", socket_gethostbyaddr,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003437 METH_VARARGS, gethostbyaddr_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003438 {"gethostname", socket_gethostname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003439 METH_VARARGS, gethostname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003440 {"getservbyname", socket_getservbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003441 METH_VARARGS, getservbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003442 {"getprotobyname", socket_getprotobyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003443 METH_VARARGS,getprotobyname_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003444#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003445 {"fromfd", socket_fromfd,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003446 METH_VARARGS, fromfd_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003447#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003448 {"ntohs", socket_ntohs,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003449 METH_VARARGS, ntohs_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003450 {"ntohl", socket_ntohl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003451 METH_O, ntohl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003452 {"htons", socket_htons,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003453 METH_VARARGS, htons_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003454 {"htonl", socket_htonl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003455 METH_O, htonl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003456 {"inet_aton", socket_inet_aton,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003457 METH_VARARGS, inet_aton_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003458 {"inet_ntoa", socket_inet_ntoa,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003459 METH_VARARGS, inet_ntoa_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003460#ifdef HAVE_INET_PTON
3461 {"inet_pton", socket_inet_pton,
3462 METH_VARARGS, inet_pton_doc},
3463 {"inet_ntop", socket_inet_ntop,
3464 METH_VARARGS, inet_ntop_doc},
3465#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003466 {"getaddrinfo", socket_getaddrinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003467 METH_VARARGS, getaddrinfo_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003468 {"getnameinfo", socket_getnameinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003469 METH_VARARGS, getnameinfo_doc},
Guido van Rossum1693ba82002-07-18 21:11:26 +00003470 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003471 METH_NOARGS, getdefaulttimeout_doc},
3472 {"setdefaulttimeout", socket_setdefaulttimeout,
3473 METH_O, setdefaulttimeout_doc},
Guido van Rossum30a685f1991-06-27 15:51:29 +00003474 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003475};
3476
Guido van Rossum30a685f1991-06-27 15:51:29 +00003477
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003478#ifdef RISCOS
3479#define OS_INIT_DEFINED
Guido van Rossumbe32c891996-06-20 16:25:29 +00003480
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003481static int
3482os_init(void)
3483{
3484 _kernel_swi_regs r;
3485
3486 r.r[0] = 0;
3487 _kernel_swi(0x43380, &r, &r);
3488 taskwindow = r.r[0];
3489
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003490 return 1;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003491}
3492
3493#endif /* RISCOS */
3494
3495
3496#ifdef MS_WINDOWS
3497#define OS_INIT_DEFINED
3498
3499/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003500
3501static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003502os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00003503{
3504 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00003505}
3506
3507static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003508os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00003509{
3510 WSADATA WSAData;
3511 int ret;
3512 char buf[100];
3513 ret = WSAStartup(0x0101, &WSAData);
3514 switch (ret) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003515 case 0: /* No error */
Mark Hammond14350ab2003-07-09 04:57:46 +00003516 Py_AtExit(os_cleanup);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003517 return 1; /* Success */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003518 case WSASYSNOTREADY:
3519 PyErr_SetString(PyExc_ImportError,
3520 "WSAStartup failed: network not ready");
3521 break;
3522 case WSAVERNOTSUPPORTED:
3523 case WSAEINVAL:
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003524 PyErr_SetString(
3525 PyExc_ImportError,
3526 "WSAStartup failed: requested version not supported");
Guido van Rossumbe32c891996-06-20 16:25:29 +00003527 break;
3528 default:
Tim Peters885d4572001-11-28 20:27:42 +00003529 PyOS_snprintf(buf, sizeof(buf),
3530 "WSAStartup failed: error code %d", ret);
Guido van Rossumbe32c891996-06-20 16:25:29 +00003531 PyErr_SetString(PyExc_ImportError, buf);
3532 break;
3533 }
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003534 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003535}
3536
Guido van Rossum8d665e61996-06-26 18:22:49 +00003537#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003538
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003539
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003540#ifdef PYOS_OS2
3541#define OS_INIT_DEFINED
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003542
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003543/* Additional initialization for OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003544
3545static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003546os_init(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003547{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003548#ifndef PYCC_GCC
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003549 char reason[64];
3550 int rc = sock_init();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003551
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003552 if (rc == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003553 return 1; /* Success */
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003554 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003555
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003556 PyOS_snprintf(reason, sizeof(reason),
3557 "OS/2 TCP/IP Error# %d", sock_errno());
3558 PyErr_SetString(PyExc_ImportError, reason);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003559
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003560 return 0; /* Failure */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00003561#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003562 /* No need to initialise sockets with GCC/EMX */
3563 return 1; /* Success */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00003564#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003565}
3566
3567#endif /* PYOS_OS2 */
3568
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003569
3570#ifndef OS_INIT_DEFINED
3571static int
3572os_init(void)
3573{
3574 return 1; /* Success */
3575}
3576#endif
3577
3578
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003579/* C API table - always add new things to the end for binary
3580 compatibility. */
3581static
3582PySocketModule_APIObject PySocketModuleAPI =
3583{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003584 &sock_type,
Brett Cannon06c34792004-03-23 23:16:54 +00003585 NULL
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003586};
3587
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003588
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003589/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003590
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003591 This module is actually called "_socket", and there's a wrapper
3592 "socket.py" which implements some additional functionality. On some
3593 platforms (e.g. Windows and OS/2), socket.py also implements a
3594 wrapper for the socket type that provides missing functionality such
3595 as makefile(), dup() and fromfd(). The import of "_socket" may fail
3596 with an ImportError exception if os-specific initialization fails.
3597 On Windows, this does WINSOCK initialization. When WINSOCK is
3598 initialized succesfully, a call to WSACleanup() is scheduled to be
3599 made at exit time.
3600*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003601
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003602PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003603"Implementation module for socket operations.\n\
3604\n\
3605See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003606
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003607PyMODINIT_FUNC
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003608init_socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003609{
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003610 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00003611
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003612 if (!os_init())
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003613 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003614
3615 sock_type.ob_type = &PyType_Type;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003616 m = Py_InitModule3(PySocket_MODULE_NAME,
3617 socket_methods,
3618 socket_doc);
3619
3620 socket_error = PyErr_NewException("socket.error", NULL, NULL);
3621 if (socket_error == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003622 return;
Brett Cannon06c34792004-03-23 23:16:54 +00003623 PySocketModuleAPI.error = socket_error;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003624 Py_INCREF(socket_error);
3625 PyModule_AddObject(m, "error", socket_error);
3626 socket_herror = PyErr_NewException("socket.herror",
3627 socket_error, NULL);
3628 if (socket_herror == NULL)
3629 return;
3630 Py_INCREF(socket_herror);
3631 PyModule_AddObject(m, "herror", socket_herror);
3632 socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003633 NULL);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003634 if (socket_gaierror == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003635 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003636 Py_INCREF(socket_gaierror);
3637 PyModule_AddObject(m, "gaierror", socket_gaierror);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00003638 socket_timeout = PyErr_NewException("socket.timeout",
3639 socket_error, NULL);
3640 if (socket_timeout == NULL)
3641 return;
3642 Py_INCREF(socket_timeout);
3643 PyModule_AddObject(m, "timeout", socket_timeout);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003644 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00003645 if (PyModule_AddObject(m, "SocketType",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003646 (PyObject *)&sock_type) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003647 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003648 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00003649 if (PyModule_AddObject(m, "socket",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003650 (PyObject *)&sock_type) != 0)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003651 return;
Guido van Rossum09be4091999-08-09 14:40:40 +00003652
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003653#ifdef ENABLE_IPV6
3654 has_ipv6 = Py_True;
3655#else
3656 has_ipv6 = Py_False;
3657#endif
3658 Py_INCREF(has_ipv6);
3659 PyModule_AddObject(m, "has_ipv6", has_ipv6);
3660
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003661 /* Export C API */
Fred Drake4baedc12002-04-01 14:53:37 +00003662 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003663 PyCObject_FromVoidPtr((void *)&PySocketModuleAPI, NULL)
3664 ) != 0)
3665 return;
3666
Guido van Rossum09be4091999-08-09 14:40:40 +00003667 /* Address families (we only support AF_INET and AF_UNIX) */
3668#ifdef AF_UNSPEC
Fred Drake4baedc12002-04-01 14:53:37 +00003669 PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00003670#endif
Fred Drake4baedc12002-04-01 14:53:37 +00003671 PyModule_AddIntConstant(m, "AF_INET", AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003672#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00003673 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003674#endif /* AF_INET6 */
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00003675#if defined(AF_UNIX)
Fred Drake4baedc12002-04-01 14:53:37 +00003676 PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003677#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00003678#ifdef AF_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00003679 /* Amateur Radio AX.25 */
3680 PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003681#endif
3682#ifdef AF_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00003683 PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00003684#endif
3685#ifdef AF_APPLETALK
Fred Drake4baedc12002-04-01 14:53:37 +00003686 /* Appletalk DDP */
3687 PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00003688#endif
3689#ifdef AF_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00003690 /* Amateur radio NetROM */
3691 PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00003692#endif
3693#ifdef AF_BRIDGE
Fred Drake4baedc12002-04-01 14:53:37 +00003694 /* Multiprotocol bridge */
3695 PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003696#endif
3697#ifdef AF_AAL5
Fred Drake4baedc12002-04-01 14:53:37 +00003698 /* Reserved for Werner's ATM */
3699 PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00003700#endif
3701#ifdef AF_X25
Fred Drake4baedc12002-04-01 14:53:37 +00003702 /* Reserved for X.25 project */
3703 PyModule_AddIntConstant(m, "AF_X25", AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003704#endif
3705#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00003706 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00003707#endif
3708#ifdef AF_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00003709 /* Amateur Radio X.25 PLP */
3710 PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003711#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00003712
Hye-Shik Chang81268602004-02-02 06:05:24 +00003713#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00003714 PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH);
3715 PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP);
Hye-Shik Chang81268602004-02-02 06:05:24 +00003716#if !defined(__FreeBSD__)
Martin v. Löwis12af0482004-01-31 12:34:17 +00003717 PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO);
Hye-Shik Chang81268602004-02-02 06:05:24 +00003718#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00003719 PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM);
3720 PyModule_AddObject(m, "BDADDR_ANY", Py_BuildValue( "iiiiii", 0,0,0,0,0,0 ) );
3721 PyModule_AddObject(m, "BDADDR_LOCAL", Py_BuildValue( "iiiiii", 0,0,0,0xff,0xff,0xff ) );
3722#endif
3723
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00003724#ifdef HAVE_NETPACKET_PACKET_H
Fred Drake4baedc12002-04-01 14:53:37 +00003725 PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET);
3726 PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET);
3727 PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST);
3728 PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST);
3729 PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST);
3730 PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST);
3731 PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING);
3732 PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK);
3733 PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00003734#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003735
3736 /* Socket types */
Fred Drake4baedc12002-04-01 14:53:37 +00003737 PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
3738 PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00003739#ifndef __BEOS__
3740/* We have incomplete socket support. */
Fred Drake4baedc12002-04-01 14:53:37 +00003741 PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
3742 PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003743#if defined(SOCK_RDM)
Fred Drake4baedc12002-04-01 14:53:37 +00003744 PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00003745#endif
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003746#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003747
3748#ifdef SO_DEBUG
Fred Drake4baedc12002-04-01 14:53:37 +00003749 PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003750#endif
3751#ifdef SO_ACCEPTCONN
Fred Drake4baedc12002-04-01 14:53:37 +00003752 PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003753#endif
3754#ifdef SO_REUSEADDR
Fred Drake4baedc12002-04-01 14:53:37 +00003755 PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003756#endif
3757#ifdef SO_KEEPALIVE
Fred Drake4baedc12002-04-01 14:53:37 +00003758 PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003759#endif
3760#ifdef SO_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00003761 PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003762#endif
3763#ifdef SO_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00003764 PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003765#endif
3766#ifdef SO_USELOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00003767 PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003768#endif
3769#ifdef SO_LINGER
Fred Drake4baedc12002-04-01 14:53:37 +00003770 PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003771#endif
3772#ifdef SO_OOBINLINE
Fred Drake4baedc12002-04-01 14:53:37 +00003773 PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003774#endif
3775#ifdef SO_REUSEPORT
Fred Drake4baedc12002-04-01 14:53:37 +00003776 PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003777#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003778#ifdef SO_SNDBUF
Fred Drake4baedc12002-04-01 14:53:37 +00003779 PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003780#endif
3781#ifdef SO_RCVBUF
Fred Drake4baedc12002-04-01 14:53:37 +00003782 PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003783#endif
3784#ifdef SO_SNDLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00003785 PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003786#endif
3787#ifdef SO_RCVLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00003788 PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003789#endif
3790#ifdef SO_SNDTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00003791 PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003792#endif
3793#ifdef SO_RCVTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00003794 PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003795#endif
3796#ifdef SO_ERROR
Fred Drake4baedc12002-04-01 14:53:37 +00003797 PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003798#endif
3799#ifdef SO_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00003800 PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003801#endif
3802
3803 /* Maximum number of connections for "listen" */
3804#ifdef SOMAXCONN
Fred Drake4baedc12002-04-01 14:53:37 +00003805 PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003806#else
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003807 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003808#endif
3809
3810 /* Flags for send, recv */
3811#ifdef MSG_OOB
Fred Drake4baedc12002-04-01 14:53:37 +00003812 PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003813#endif
3814#ifdef MSG_PEEK
Fred Drake4baedc12002-04-01 14:53:37 +00003815 PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003816#endif
3817#ifdef MSG_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00003818 PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003819#endif
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00003820#ifdef MSG_DONTWAIT
Fred Drake4baedc12002-04-01 14:53:37 +00003821 PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00003822#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003823#ifdef MSG_EOR
Fred Drake4baedc12002-04-01 14:53:37 +00003824 PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003825#endif
3826#ifdef MSG_TRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00003827 PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003828#endif
3829#ifdef MSG_CTRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00003830 PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003831#endif
3832#ifdef MSG_WAITALL
Fred Drake4baedc12002-04-01 14:53:37 +00003833 PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003834#endif
3835#ifdef MSG_BTAG
Fred Drake4baedc12002-04-01 14:53:37 +00003836 PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003837#endif
3838#ifdef MSG_ETAG
Fred Drake4baedc12002-04-01 14:53:37 +00003839 PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003840#endif
3841
3842 /* Protocol level and numbers, usable for [gs]etsockopt */
3843#ifdef SOL_SOCKET
Fred Drake4baedc12002-04-01 14:53:37 +00003844 PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003845#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003846#ifdef SOL_IP
Fred Drake4baedc12002-04-01 14:53:37 +00003847 PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003848#else
Fred Drake4baedc12002-04-01 14:53:37 +00003849 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00003850#endif
3851#ifdef SOL_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00003852 PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00003853#endif
3854#ifdef SOL_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00003855 PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003856#endif
3857#ifdef SOL_ATALK
Fred Drake4baedc12002-04-01 14:53:37 +00003858 PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00003859#endif
3860#ifdef SOL_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00003861 PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00003862#endif
3863#ifdef SOL_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00003864 PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003865#endif
3866#ifdef SOL_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00003867 PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003868#else
Fred Drake4baedc12002-04-01 14:53:37 +00003869 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00003870#endif
3871#ifdef SOL_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00003872 PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003873#else
Fred Drake4baedc12002-04-01 14:53:37 +00003874 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00003875#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003876#ifdef IPPROTO_IP
Fred Drake4baedc12002-04-01 14:53:37 +00003877 PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00003878#else
Fred Drake4baedc12002-04-01 14:53:37 +00003879 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003880#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003881#ifdef IPPROTO_HOPOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003882 PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003883#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003884#ifdef IPPROTO_ICMP
Fred Drake4baedc12002-04-01 14:53:37 +00003885 PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00003886#else
Fred Drake4baedc12002-04-01 14:53:37 +00003887 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003888#endif
3889#ifdef IPPROTO_IGMP
Fred Drake4baedc12002-04-01 14:53:37 +00003890 PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003891#endif
3892#ifdef IPPROTO_GGP
Fred Drake4baedc12002-04-01 14:53:37 +00003893 PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003894#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003895#ifdef IPPROTO_IPV4
Fred Drake4baedc12002-04-01 14:53:37 +00003896 PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003897#endif
Martin v. Löwisa0f17342003-10-03 13:56:20 +00003898#ifdef IPPROTO_IPV6
3899 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
3900#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003901#ifdef IPPROTO_IPIP
Fred Drake4baedc12002-04-01 14:53:37 +00003902 PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003903#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003904#ifdef IPPROTO_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00003905 PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00003906#else
Fred Drake4baedc12002-04-01 14:53:37 +00003907 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003908#endif
3909#ifdef IPPROTO_EGP
Fred Drake4baedc12002-04-01 14:53:37 +00003910 PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003911#endif
3912#ifdef IPPROTO_PUP
Fred Drake4baedc12002-04-01 14:53:37 +00003913 PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003914#endif
3915#ifdef IPPROTO_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00003916 PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00003917#else
Fred Drake4baedc12002-04-01 14:53:37 +00003918 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003919#endif
3920#ifdef IPPROTO_IDP
Fred Drake4baedc12002-04-01 14:53:37 +00003921 PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003922#endif
3923#ifdef IPPROTO_HELLO
Fred Drake4baedc12002-04-01 14:53:37 +00003924 PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003925#endif
3926#ifdef IPPROTO_ND
Fred Drake4baedc12002-04-01 14:53:37 +00003927 PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003928#endif
3929#ifdef IPPROTO_TP
Fred Drake4baedc12002-04-01 14:53:37 +00003930 PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003931#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003932#ifdef IPPROTO_IPV6
Fred Drake4baedc12002-04-01 14:53:37 +00003933 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003934#endif
3935#ifdef IPPROTO_ROUTING
Fred Drake4baedc12002-04-01 14:53:37 +00003936 PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003937#endif
3938#ifdef IPPROTO_FRAGMENT
Fred Drake4baedc12002-04-01 14:53:37 +00003939 PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003940#endif
3941#ifdef IPPROTO_RSVP
Fred Drake4baedc12002-04-01 14:53:37 +00003942 PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003943#endif
3944#ifdef IPPROTO_GRE
Fred Drake4baedc12002-04-01 14:53:37 +00003945 PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003946#endif
3947#ifdef IPPROTO_ESP
Fred Drake4baedc12002-04-01 14:53:37 +00003948 PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003949#endif
3950#ifdef IPPROTO_AH
Fred Drake4baedc12002-04-01 14:53:37 +00003951 PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003952#endif
3953#ifdef IPPROTO_MOBILE
Fred Drake4baedc12002-04-01 14:53:37 +00003954 PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003955#endif
3956#ifdef IPPROTO_ICMPV6
Fred Drake4baedc12002-04-01 14:53:37 +00003957 PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003958#endif
3959#ifdef IPPROTO_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00003960 PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003961#endif
3962#ifdef IPPROTO_DSTOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003963 PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003964#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003965#ifdef IPPROTO_XTP
Fred Drake4baedc12002-04-01 14:53:37 +00003966 PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003967#endif
3968#ifdef IPPROTO_EON
Fred Drake4baedc12002-04-01 14:53:37 +00003969 PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003970#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003971#ifdef IPPROTO_PIM
Fred Drake4baedc12002-04-01 14:53:37 +00003972 PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003973#endif
3974#ifdef IPPROTO_IPCOMP
Fred Drake4baedc12002-04-01 14:53:37 +00003975 PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003976#endif
3977#ifdef IPPROTO_VRRP
Fred Drake4baedc12002-04-01 14:53:37 +00003978 PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003979#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003980#ifdef IPPROTO_BIP
Fred Drake4baedc12002-04-01 14:53:37 +00003981 PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003982#endif
3983/**/
3984#ifdef IPPROTO_RAW
Fred Drake4baedc12002-04-01 14:53:37 +00003985 PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00003986#else
Fred Drake4baedc12002-04-01 14:53:37 +00003987 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003988#endif
3989#ifdef IPPROTO_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00003990 PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003991#endif
3992
3993 /* Some port configuration */
3994#ifdef IPPORT_RESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00003995 PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003996#else
Fred Drake4baedc12002-04-01 14:53:37 +00003997 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003998#endif
3999#ifdef IPPORT_USERRESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00004000 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004001#else
Fred Drake4baedc12002-04-01 14:53:37 +00004002 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004003#endif
4004
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004005 /* Some reserved IP v.4 addresses */
4006#ifdef INADDR_ANY
Fred Drake4baedc12002-04-01 14:53:37 +00004007 PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004008#else
Fred Drake4baedc12002-04-01 14:53:37 +00004009 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004010#endif
4011#ifdef INADDR_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00004012 PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004013#else
Fred Drake4baedc12002-04-01 14:53:37 +00004014 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004015#endif
4016#ifdef INADDR_LOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00004017 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004018#else
Fred Drake4baedc12002-04-01 14:53:37 +00004019 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004020#endif
4021#ifdef INADDR_UNSPEC_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004022 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004023#else
Fred Drake4baedc12002-04-01 14:53:37 +00004024 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004025#endif
4026#ifdef INADDR_ALLHOSTS_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004027 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
4028 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004029#else
Fred Drake4baedc12002-04-01 14:53:37 +00004030 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004031#endif
4032#ifdef INADDR_MAX_LOCAL_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004033 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
4034 INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004035#else
Fred Drake4baedc12002-04-01 14:53:37 +00004036 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004037#endif
4038#ifdef INADDR_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00004039 PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004040#else
Fred Drake4baedc12002-04-01 14:53:37 +00004041 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004042#endif
4043
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004044 /* IPv4 [gs]etsockopt options */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004045#ifdef IP_OPTIONS
Fred Drake4baedc12002-04-01 14:53:37 +00004046 PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004047#endif
4048#ifdef IP_HDRINCL
Fred Drake4baedc12002-04-01 14:53:37 +00004049 PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004050#endif
4051#ifdef IP_TOS
Fred Drake4baedc12002-04-01 14:53:37 +00004052 PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004053#endif
4054#ifdef IP_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004055 PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004056#endif
4057#ifdef IP_RECVOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004058 PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004059#endif
4060#ifdef IP_RECVRETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004061 PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004062#endif
4063#ifdef IP_RECVDSTADDR
Fred Drake4baedc12002-04-01 14:53:37 +00004064 PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004065#endif
4066#ifdef IP_RETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004067 PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004068#endif
4069#ifdef IP_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00004070 PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004071#endif
4072#ifdef IP_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004073 PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004074#endif
4075#ifdef IP_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004076 PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004077#endif
4078#ifdef IP_ADD_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00004079 PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004080#endif
4081#ifdef IP_DROP_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00004082 PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004083#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004084#ifdef IP_DEFAULT_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004085 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
4086 IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00004087#endif
4088#ifdef IP_DEFAULT_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004089 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
4090 IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004091#endif
4092#ifdef IP_MAX_MEMBERSHIPS
Fred Drake4baedc12002-04-01 14:53:37 +00004093 PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00004094#endif
4095
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004096 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
4097#ifdef IPV6_JOIN_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004098 PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004099#endif
4100#ifdef IPV6_LEAVE_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004101 PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004102#endif
4103#ifdef IPV6_MULTICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00004104 PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004105#endif
4106#ifdef IPV6_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00004107 PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004108#endif
4109#ifdef IPV6_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004110 PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004111#endif
4112#ifdef IPV6_UNICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00004113 PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004114#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004115 /* Additional IPV6 socket options, defined in RFC 3493 */
4116#ifdef IPV6_V6ONLY
4117 PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY);
4118#endif
4119 /* Advanced IPV6 socket options, from RFC 3542 */
4120#ifdef IPV6_CHECKSUM
4121 PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM);
4122#endif
4123#ifdef IPV6_DONTFRAG
4124 PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG);
4125#endif
4126#ifdef IPV6_DSTOPTS
4127 PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS);
4128#endif
4129#ifdef IPV6_HOPLIMIT
4130 PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT);
4131#endif
4132#ifdef IPV6_HOPOPTS
4133 PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS);
4134#endif
4135#ifdef IPV6_NEXTHOP
4136 PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP);
4137#endif
4138#ifdef IPV6_PATHMTU
4139 PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU);
4140#endif
4141#ifdef IPV6_PKTINFO
4142 PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO);
4143#endif
4144#ifdef IPV6_RECVDSTOPTS
4145 PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS);
4146#endif
4147#ifdef IPV6_RECVHOPLIMIT
4148 PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT);
4149#endif
4150#ifdef IPV6_RECVHOPOPTS
4151 PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS);
4152#endif
4153#ifdef IPV6_RECVPKTINFO
4154 PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO);
4155#endif
4156#ifdef IPV6_RECVRTHDR
4157 PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR);
4158#endif
4159#ifdef IPV6_RECVTCLASS
4160 PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS);
4161#endif
4162#ifdef IPV6_RTHDR
4163 PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR);
4164#endif
4165#ifdef IPV6_RTHDRDSTOPTS
4166 PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS);
4167#endif
4168#ifdef IPV6_RTHDR_TYPE_0
4169 PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0);
4170#endif
4171#ifdef IPV6_RECVPATHMTU
4172 PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU);
4173#endif
4174#ifdef IPV6_TCLASS
4175 PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS);
4176#endif
4177#ifdef IPV6_USE_MIN_MTU
4178 PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU);
4179#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004180
Guido van Rossum09be4091999-08-09 14:40:40 +00004181 /* TCP options */
4182#ifdef TCP_NODELAY
Fred Drake4baedc12002-04-01 14:53:37 +00004183 PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00004184#endif
4185#ifdef TCP_MAXSEG
Fred Drake4baedc12002-04-01 14:53:37 +00004186 PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00004187#endif
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004188#ifdef TCP_CORK
Fred Drake4baedc12002-04-01 14:53:37 +00004189 PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004190#endif
4191#ifdef TCP_KEEPIDLE
Fred Drake4baedc12002-04-01 14:53:37 +00004192 PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004193#endif
4194#ifdef TCP_KEEPINTVL
Fred Drake4baedc12002-04-01 14:53:37 +00004195 PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004196#endif
4197#ifdef TCP_KEEPCNT
Fred Drake4baedc12002-04-01 14:53:37 +00004198 PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004199#endif
4200#ifdef TCP_SYNCNT
Fred Drake4baedc12002-04-01 14:53:37 +00004201 PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004202#endif
4203#ifdef TCP_LINGER2
Fred Drake4baedc12002-04-01 14:53:37 +00004204 PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004205#endif
4206#ifdef TCP_DEFER_ACCEPT
Fred Drake4baedc12002-04-01 14:53:37 +00004207 PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004208#endif
4209#ifdef TCP_WINDOW_CLAMP
Fred Drake4baedc12002-04-01 14:53:37 +00004210 PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004211#endif
4212#ifdef TCP_INFO
Fred Drake4baedc12002-04-01 14:53:37 +00004213 PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004214#endif
4215#ifdef TCP_QUICKACK
Fred Drake4baedc12002-04-01 14:53:37 +00004216 PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004217#endif
4218
Guido van Rossum09be4091999-08-09 14:40:40 +00004219
4220 /* IPX options */
4221#ifdef IPX_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004222 PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004223#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004224
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004225 /* get{addr,name}info parameters */
4226#ifdef EAI_ADDRFAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004227 PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004228#endif
4229#ifdef EAI_AGAIN
Fred Drake4baedc12002-04-01 14:53:37 +00004230 PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004231#endif
4232#ifdef EAI_BADFLAGS
Fred Drake4baedc12002-04-01 14:53:37 +00004233 PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004234#endif
4235#ifdef EAI_FAIL
Fred Drake4baedc12002-04-01 14:53:37 +00004236 PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004237#endif
4238#ifdef EAI_FAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004239 PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004240#endif
4241#ifdef EAI_MEMORY
Fred Drake4baedc12002-04-01 14:53:37 +00004242 PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004243#endif
4244#ifdef EAI_NODATA
Fred Drake4baedc12002-04-01 14:53:37 +00004245 PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004246#endif
4247#ifdef EAI_NONAME
Fred Drake4baedc12002-04-01 14:53:37 +00004248 PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004249#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004250#ifdef EAI_OVERFLOW
4251 PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW);
4252#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004253#ifdef EAI_SERVICE
Fred Drake4baedc12002-04-01 14:53:37 +00004254 PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004255#endif
4256#ifdef EAI_SOCKTYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004257 PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004258#endif
4259#ifdef EAI_SYSTEM
Fred Drake4baedc12002-04-01 14:53:37 +00004260 PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004261#endif
4262#ifdef EAI_BADHINTS
Fred Drake4baedc12002-04-01 14:53:37 +00004263 PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004264#endif
4265#ifdef EAI_PROTOCOL
Fred Drake4baedc12002-04-01 14:53:37 +00004266 PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004267#endif
4268#ifdef EAI_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00004269 PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004270#endif
4271#ifdef AI_PASSIVE
Fred Drake4baedc12002-04-01 14:53:37 +00004272 PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004273#endif
4274#ifdef AI_CANONNAME
Fred Drake4baedc12002-04-01 14:53:37 +00004275 PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004276#endif
4277#ifdef AI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004278 PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004279#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004280#ifdef AI_NUMERICSERV
4281 PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV);
4282#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004283#ifdef AI_MASK
Fred Drake4baedc12002-04-01 14:53:37 +00004284 PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004285#endif
4286#ifdef AI_ALL
Fred Drake4baedc12002-04-01 14:53:37 +00004287 PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004288#endif
4289#ifdef AI_V4MAPPED_CFG
Fred Drake4baedc12002-04-01 14:53:37 +00004290 PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004291#endif
4292#ifdef AI_ADDRCONFIG
Fred Drake4baedc12002-04-01 14:53:37 +00004293 PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004294#endif
4295#ifdef AI_V4MAPPED
Fred Drake4baedc12002-04-01 14:53:37 +00004296 PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004297#endif
4298#ifdef AI_DEFAULT
Fred Drake4baedc12002-04-01 14:53:37 +00004299 PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004300#endif
4301#ifdef NI_MAXHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004302 PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004303#endif
4304#ifdef NI_MAXSERV
Fred Drake4baedc12002-04-01 14:53:37 +00004305 PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004306#endif
4307#ifdef NI_NOFQDN
Fred Drake4baedc12002-04-01 14:53:37 +00004308 PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004309#endif
4310#ifdef NI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004311 PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004312#endif
4313#ifdef NI_NAMEREQD
Fred Drake4baedc12002-04-01 14:53:37 +00004314 PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004315#endif
4316#ifdef NI_NUMERICSERV
Fred Drake4baedc12002-04-01 14:53:37 +00004317 PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004318#endif
4319#ifdef NI_DGRAM
Fred Drake4baedc12002-04-01 14:53:37 +00004320 PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004321#endif
4322
Martin v. Löwis94681fc2003-11-27 19:40:22 +00004323 /* shutdown() parameters */
4324#ifdef SHUT_RD
4325 PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD);
4326#elif defined(SD_RECEIVE)
4327 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
4328#else
4329 PyModule_AddIntConstant(m, "SHUT_RD", 0);
4330#endif
4331#ifdef SHUT_WR
4332 PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR);
4333#elif defined(SD_SEND)
4334 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
4335#else
4336 PyModule_AddIntConstant(m, "SHUT_WR", 1);
4337#endif
4338#ifdef SHUT_RDWR
4339 PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR);
4340#elif defined(SD_BOTH)
4341 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
4342#else
4343 PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
4344#endif
4345
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004346 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00004347#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
4348 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004349#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004350}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004351
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004352
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004353#ifndef HAVE_INET_PTON
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004354
4355/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004356/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004357
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004358int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004359inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004360{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004361 if (af == AF_INET) {
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004362 long packed_addr;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004363 packed_addr = inet_addr(src);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004364 if (packed_addr == INADDR_NONE)
4365 return 0;
4366 memcpy(dst, &packed_addr, 4);
4367 return 1;
4368 }
4369 /* Should set errno to EAFNOSUPPORT */
4370 return -1;
4371}
4372
Martin v. Löwisc925b1532001-07-21 09:42:15 +00004373const char *
4374inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004375{
4376 if (af == AF_INET) {
4377 struct in_addr packed_addr;
4378 if (size < 16)
4379 /* Should set errno to ENOSPC. */
4380 return NULL;
4381 memcpy(&packed_addr, src, sizeof(packed_addr));
4382 return strncpy(dst, inet_ntoa(packed_addr), size);
4383 }
4384 /* Should set errno to EAFNOSUPPORT */
4385 return NULL;
4386}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004387
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004388#endif