blob: 255e4dfc4d5f9155621f7f39c22eb3aee53ec108 [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
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000861#ifdef USE_BLUETOOTH
862/* Convert a string representation of a Bluetooth address into a numeric
863 address. Returns the length (6), or raises an exception and returns -1 if
864 an error occurred. */
865
866static int
867setbdaddr(char *name, bdaddr_t *bdaddr)
868{
869 unsigned int b0, b1, b2, b3, b4, b5;
870 char ch;
871 int n;
872
873 n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",
874 &b5, &b4, &b3, &b2, &b1, &b0, &ch);
875 if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {
876 bdaddr->b[0] = b0;
877 bdaddr->b[1] = b1;
878 bdaddr->b[2] = b2;
879 bdaddr->b[3] = b3;
880 bdaddr->b[4] = b4;
881 bdaddr->b[5] = b5;
882 return 6;
883 } else {
884 PyErr_SetString(socket_error, "bad bluetooth address");
885 return -1;
886 }
887}
888
889/* Create a string representation of the Bluetooth address. This is always a
890 string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal
891 value (zero padded if necessary). */
892
893static PyObject *
894makebdaddr(bdaddr_t *bdaddr)
895{
896 char buf[(6 * 2) + 5 + 1];
897
898 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
899 bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
900 bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
901 return PyString_FromString(buf);
902}
903#endif
904
905
Guido van Rossum30a685f1991-06-27 15:51:29 +0000906/* Create an object representing the given socket address,
907 suitable for passing it back to bind(), connect() etc.
908 The family field of the sockaddr structure is inspected
909 to determine what kind of address it really is. */
910
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000911/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000912static PyObject *
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000913makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000914{
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000915 if (addrlen == 0) {
916 /* No address -- may be recvfrom() from known socket */
Guido van Rossum73624e91994-10-10 17:59:00 +0000917 Py_INCREF(Py_None);
918 return Py_None;
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000919 }
920
Guido van Rossumbcc20741998-08-04 22:53:56 +0000921#ifdef __BEOS__
Guido van Rossum2c8bcb82000-04-25 21:34:53 +0000922 /* XXX: BeOS version of accept() doesn't set family correctly */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000923 addr->sa_family = AF_INET;
924#endif
925
Guido van Rossum30a685f1991-06-27 15:51:29 +0000926 switch (addr->sa_family) {
927
928 case AF_INET:
929 {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000930 struct sockaddr_in *a;
931 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
Barry Warsaw752300b1997-01-03 17:18:10 +0000932 PyObject *ret = NULL;
933 if (addrobj) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000934 a = (struct sockaddr_in *)addr;
Barry Warsaw752300b1997-01-03 17:18:10 +0000935 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
936 Py_DECREF(addrobj);
937 }
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000938 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000939 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000940
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +0000941#if defined(AF_UNIX)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000942 case AF_UNIX:
943 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000944 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000945 return PyString_FromString(a->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000946 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000947#endif /* AF_UNIX */
948
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000949#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000950 case AF_INET6:
951 {
952 struct sockaddr_in6 *a;
953 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
954 PyObject *ret = NULL;
955 if (addrobj) {
956 a = (struct sockaddr_in6 *)addr;
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000957 ret = Py_BuildValue("Oiii",
958 addrobj,
959 ntohs(a->sin6_port),
960 a->sin6_flowinfo,
961 a->sin6_scope_id);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000962 Py_DECREF(addrobj);
963 }
964 return ret;
965 }
Jeremy Hylton22308652001-02-02 03:23:09 +0000966#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +0000967
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000968#ifdef USE_BLUETOOTH
969 case AF_BLUETOOTH:
970 switch (proto) {
971
972 case BTPROTO_L2CAP:
973 {
974 struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;
975 PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));
976 PyObject *ret = NULL;
977 if (addrobj) {
978 ret = Py_BuildValue("Oi",
979 addrobj,
980 _BT_L2_MEMB(a, psm));
981 Py_DECREF(addrobj);
982 }
983 return ret;
984 }
985
986 case BTPROTO_RFCOMM:
987 {
988 struct sockaddr_rc *a = (struct sockaddr_rc *) addr;
989 PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));
990 PyObject *ret = NULL;
991 if (addrobj) {
992 ret = Py_BuildValue("Oi",
993 addrobj,
994 _BT_RC_MEMB(a, channel));
995 Py_DECREF(addrobj);
996 }
997 return ret;
998 }
999
1000#if !defined(__FreeBSD__)
1001 case BTPROTO_SCO:
1002 {
1003 struct sockaddr_sco *a = (struct sockaddr_sco *) addr;
1004 return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));
1005 }
1006#endif
1007
1008 }
1009#endif
1010
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001011#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001012 case AF_PACKET:
1013 {
1014 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
1015 char *ifname = "";
1016 struct ifreq ifr;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001017 /* need to look up interface name give index */
1018 if (a->sll_ifindex) {
Jeremy Hylton22308652001-02-02 03:23:09 +00001019 ifr.ifr_ifindex = a->sll_ifindex;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001020 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
Jeremy Hylton22308652001-02-02 03:23:09 +00001021 ifname = ifr.ifr_name;
Jeremy Hylton22308652001-02-02 03:23:09 +00001022 }
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001023 return Py_BuildValue("shbhs#",
1024 ifname,
1025 ntohs(a->sll_protocol),
1026 a->sll_pkttype,
1027 a->sll_hatype,
1028 a->sll_addr,
1029 a->sll_halen);
Jeremy Hylton22308652001-02-02 03:23:09 +00001030 }
1031#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001032
Guido van Rossum30a685f1991-06-27 15:51:29 +00001033 /* More cases here... */
1034
1035 default:
Guido van Rossumaa948df1997-05-07 17:41:48 +00001036 /* If we don't know the address family, don't raise an
1037 exception -- return it as a tuple. */
1038 return Py_BuildValue("is#",
1039 addr->sa_family,
1040 addr->sa_data,
1041 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001042
Guido van Rossum30a685f1991-06-27 15:51:29 +00001043 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001044}
1045
Guido van Rossum30a685f1991-06-27 15:51:29 +00001046
1047/* Parse a socket address argument according to the socket object's
1048 address family. Return 1 if the address was in the proper format,
1049 0 of not. The address is returned through addr_ret, its length
1050 through len_ret. */
1051
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001052static int
Guido van Rossum48a680c2001-03-02 06:34:14 +00001053getsockaddrarg(PySocketSockObject *s, PyObject *args,
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001054 struct sockaddr **addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001055{
Guido van Rossum30a685f1991-06-27 15:51:29 +00001056 switch (s->sock_family) {
1057
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001058#if defined(AF_UNIX)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001059 case AF_UNIX:
1060 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001061 struct sockaddr_un* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001062 char *path;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001063 int len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001064 addr = (struct sockaddr_un*)&(s->sock_addr).un;
Guido van Rossum7e488981998-10-08 02:25:24 +00001065 if (!PyArg_Parse(args, "t#", &path, &len))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001066 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001067 if (len > sizeof addr->sun_path) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001068 PyErr_SetString(socket_error,
Barry Warsaw752300b1997-01-03 17:18:10 +00001069 "AF_UNIX path too long");
Guido van Rossum30a685f1991-06-27 15:51:29 +00001070 return 0;
1071 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001072 addr->sun_family = s->sock_family;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001073 memcpy(addr->sun_path, path, len);
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001074 addr->sun_path[len] = 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001075 *addr_ret = (struct sockaddr *) addr;
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001076#if defined(PYOS_OS2)
1077 *len_ret = sizeof(*addr);
1078#else
Guido van Rossum65af28a1996-06-11 18:36:33 +00001079 *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001080#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001081 return 1;
1082 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001083#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001084
Guido van Rossum30a685f1991-06-27 15:51:29 +00001085 case AF_INET:
1086 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001087 struct sockaddr_in* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001088 char *host;
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001089 int port, result;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001090 addr=(struct sockaddr_in*)&(s->sock_addr).in;
Guido van Rossume4dad902000-12-01 13:13:11 +00001091 if (!PyTuple_Check(args)) {
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001092 PyErr_Format(
1093 PyExc_TypeError,
1094 "getsockaddrarg: "
1095 "AF_INET address must be tuple, not %.500s",
1096 args->ob_type->tp_name);
Guido van Rossume4dad902000-12-01 13:13:11 +00001097 return 0;
1098 }
Martin v. Löwis2548c732003-04-18 10:39:54 +00001099 if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
1100 "idna", &host, &port))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001101 return 0;
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001102 result = setipaddr(host, (struct sockaddr *)addr,
1103 sizeof(*addr), AF_INET);
1104 PyMem_Free(host);
1105 if (result < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001106 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001107 addr->sin_family = AF_INET;
Guido van Rossum644a12b1997-04-09 19:24:53 +00001108 addr->sin_port = htons((short)port);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001109 *addr_ret = (struct sockaddr *) addr;
1110 *len_ret = sizeof *addr;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001111 return 1;
1112 }
1113
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001114#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001115 case AF_INET6:
1116 {
1117 struct sockaddr_in6* addr;
1118 char *host;
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001119 int port, flowinfo, scope_id, result;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001120 addr = (struct sockaddr_in6*)&(s->sock_addr).in6;
1121 flowinfo = scope_id = 0;
Martin v. Löwis2548c732003-04-18 10:39:54 +00001122 if (!PyArg_ParseTuple(args, "eti|ii",
1123 "idna", &host, &port, &flowinfo,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001124 &scope_id)) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001125 return 0;
1126 }
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001127 result = setipaddr(host, (struct sockaddr *)addr,
1128 sizeof(*addr), AF_INET6);
1129 PyMem_Free(host);
1130 if (result < 0)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001131 return 0;
1132 addr->sin6_family = s->sock_family;
1133 addr->sin6_port = htons((short)port);
1134 addr->sin6_flowinfo = flowinfo;
1135 addr->sin6_scope_id = scope_id;
1136 *addr_ret = (struct sockaddr *) addr;
1137 *len_ret = sizeof *addr;
1138 return 1;
1139 }
1140#endif
1141
Hye-Shik Chang81268602004-02-02 06:05:24 +00001142#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00001143 case AF_BLUETOOTH:
1144 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001145 switch (s->sock_proto) {
1146 case BTPROTO_L2CAP:
Martin v. Löwis12af0482004-01-31 12:34:17 +00001147 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001148 struct sockaddr_l2 *addr = (struct sockaddr_l2 *) _BT_SOCKADDR_MEMB(s, l2);
1149 char *straddr;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001150
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001151 _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
1152 if (!PyArg_ParseTuple(args, "si", &straddr,
1153 &_BT_L2_MEMB(addr, psm))) {
1154 PyErr_SetString(socket_error, "getsockaddrarg: "
1155 "wrong format");
Martin v. Löwis12af0482004-01-31 12:34:17 +00001156 return 0;
1157 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001158 if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)
1159 return 0;
1160
1161 *addr_ret = (struct sockaddr *) addr;
1162 *len_ret = sizeof *addr;
1163 return 1;
1164 }
1165 case BTPROTO_RFCOMM:
1166 {
1167 struct sockaddr_rc *addr = (struct sockaddr_rc *) _BT_SOCKADDR_MEMB(s, rc);
1168 char *straddr;
1169
1170 _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
1171 if (!PyArg_ParseTuple(args, "si", &straddr,
1172 &_BT_RC_MEMB(addr, channel))) {
1173 PyErr_SetString(socket_error, "getsockaddrarg: "
1174 "wrong format");
1175 return 0;
1176 }
1177 if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)
1178 return 0;
1179
1180 *addr_ret = (struct sockaddr *) addr;
1181 *len_ret = sizeof *addr;
1182 return 1;
1183 }
1184#if !defined(__FreeBSD__)
1185 case BTPROTO_SCO:
1186 {
1187 struct sockaddr_sco *addr = (struct sockaddr_sco *) _BT_SOCKADDR_MEMB(s, sco);
1188 char *straddr;
1189
1190 _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
1191 straddr = PyString_AsString(args);
1192 if (straddr == NULL) {
1193 PyErr_SetString(socket_error, "getsockaddrarg: "
1194 "wrong format");
1195 return 0;
1196 }
1197 if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
1198 return 0;
1199
1200 *addr_ret = (struct sockaddr *) addr;
1201 *len_ret = sizeof *addr;
1202 return 1;
1203 }
1204#endif
1205 default:
1206 PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol");
1207 return 0;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001208 }
1209 }
1210#endif
1211
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001212#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001213 case AF_PACKET:
1214 {
1215 struct sockaddr_ll* addr;
1216 struct ifreq ifr;
1217 char *interfaceName;
1218 int protoNumber;
1219 int hatype = 0;
1220 int pkttype = 0;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001221 char *haddr;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001222
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001223 if (!PyArg_ParseTuple(args, "si|iis", &interfaceName,
1224 &protoNumber, &pkttype, &hatype, &haddr))
Jeremy Hylton22308652001-02-02 03:23:09 +00001225 return 0;
1226 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
1227 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001228 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001229 s->errorhandler();
Jeremy Hylton22308652001-02-02 03:23:09 +00001230 return 0;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001231 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001232 addr = &(s->sock_addr.ll);
1233 addr->sll_family = AF_PACKET;
1234 addr->sll_protocol = htons((short)protoNumber);
1235 addr->sll_ifindex = ifr.ifr_ifindex;
1236 addr->sll_pkttype = pkttype;
1237 addr->sll_hatype = hatype;
1238 *addr_ret = (struct sockaddr *) addr;
1239 *len_ret = sizeof *addr;
1240 return 1;
1241 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001242#endif
1243
Guido van Rossum30a685f1991-06-27 15:51:29 +00001244 /* More cases here... */
1245
1246 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001247 PyErr_SetString(socket_error, "getsockaddrarg: bad family");
Guido van Rossum30a685f1991-06-27 15:51:29 +00001248 return 0;
1249
1250 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001251}
1252
Guido van Rossum30a685f1991-06-27 15:51:29 +00001253
Guido van Rossum48a680c2001-03-02 06:34:14 +00001254/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +00001255 Return 1 if the family is known, 0 otherwise. The length is returned
1256 through len_ret. */
1257
1258static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +00001259getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001260{
1261 switch (s->sock_family) {
1262
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001263#if defined(AF_UNIX)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001264 case AF_UNIX:
1265 {
1266 *len_ret = sizeof (struct sockaddr_un);
1267 return 1;
1268 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001269#endif /* AF_UNIX */
Guido van Rossum710e1df1992-06-12 10:39:36 +00001270
1271 case AF_INET:
1272 {
1273 *len_ret = sizeof (struct sockaddr_in);
1274 return 1;
1275 }
1276
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001277#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001278 case AF_INET6:
1279 {
1280 *len_ret = sizeof (struct sockaddr_in6);
1281 return 1;
1282 }
1283#endif
1284
Hye-Shik Chang81268602004-02-02 06:05:24 +00001285#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00001286 case AF_BLUETOOTH:
1287 {
1288 switch(s->sock_proto)
1289 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001290
1291 case BTPROTO_L2CAP:
1292 *len_ret = sizeof (struct sockaddr_l2);
1293 return 1;
1294 case BTPROTO_RFCOMM:
1295 *len_ret = sizeof (struct sockaddr_rc);
1296 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001297#if !defined(__FreeBSD__)
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001298 case BTPROTO_SCO:
1299 *len_ret = sizeof (struct sockaddr_sco);
1300 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001301#endif
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001302 default:
1303 PyErr_SetString(socket_error, "getsockaddrlen: "
1304 "unknown BT protocol");
1305 return 0;
1306
Martin v. Löwis12af0482004-01-31 12:34:17 +00001307 }
1308 }
1309#endif
1310
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001311#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001312 case AF_PACKET:
1313 {
1314 *len_ret = sizeof (struct sockaddr_ll);
1315 return 1;
1316 }
1317#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001318
Guido van Rossum710e1df1992-06-12 10:39:36 +00001319 /* More cases here... */
1320
1321 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001322 PyErr_SetString(socket_error, "getsockaddrlen: bad family");
Guido van Rossum710e1df1992-06-12 10:39:36 +00001323 return 0;
1324
1325 }
1326}
1327
1328
Guido van Rossum30a685f1991-06-27 15:51:29 +00001329/* s.accept() method */
1330
Guido van Rossum73624e91994-10-10 17:59:00 +00001331static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001332sock_accept(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001333{
1334 char addrbuf[256];
Fred Drakea04eaad2000-06-30 02:46:07 +00001335 SOCKET_T newfd;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001336 socklen_t addrlen;
Barry Warsaw752300b1997-01-03 17:18:10 +00001337 PyObject *sock = NULL;
1338 PyObject *addr = NULL;
1339 PyObject *res = NULL;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001340 int timeout;
Barry Warsaw752300b1997-01-03 17:18:10 +00001341
Guido van Rossum710e1df1992-06-12 10:39:36 +00001342 if (!getsockaddrlen(s, &addrlen))
1343 return NULL;
Fred Drakefd168342001-05-09 19:11:33 +00001344 memset(addrbuf, 0, addrlen);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001345
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001346#ifdef MS_WINDOWS
1347 newfd = INVALID_SOCKET;
1348#else
1349 newfd = -1;
1350#endif
1351
Guido van Rossum73624e91994-10-10 17:59:00 +00001352 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001353 timeout = internal_select(s, 0);
1354 if (!timeout)
1355 newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf,
1356 &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001357 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001358
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001359 if (timeout) {
1360 PyErr_SetString(socket_timeout, "timed out");
1361 return NULL;
1362 }
1363
Fred Drakea04eaad2000-06-30 02:46:07 +00001364#ifdef MS_WINDOWS
1365 if (newfd == INVALID_SOCKET)
1366#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001367 if (newfd < 0)
Fred Drakea04eaad2000-06-30 02:46:07 +00001368#endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001369 return s->errorhandler();
Barry Warsaw752300b1997-01-03 17:18:10 +00001370
Guido van Rossum30a685f1991-06-27 15:51:29 +00001371 /* Create the new object with unspecified family,
1372 to avoid calls to bind() etc. on it. */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001373 sock = (PyObject *) new_sockobject(newfd,
1374 s->sock_family,
1375 s->sock_type,
1376 s->sock_proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001377
Barry Warsaw752300b1997-01-03 17:18:10 +00001378 if (sock == NULL) {
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001379 SOCKETCLOSE(newfd);
Barry Warsaw752300b1997-01-03 17:18:10 +00001380 goto finally;
1381 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001382 addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf,
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001383 addrlen, s->sock_proto);
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001384 if (addr == NULL)
Barry Warsaw752300b1997-01-03 17:18:10 +00001385 goto finally;
1386
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001387 res = PyTuple_Pack(2, sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00001388
Guido van Rossum67f7a382002-06-06 21:08:16 +00001389finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001390 Py_XDECREF(sock);
1391 Py_XDECREF(addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001392 return res;
1393}
1394
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001395PyDoc_STRVAR(accept_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001396"accept() -> (socket object, address info)\n\
1397\n\
1398Wait for an incoming connection. Return a new socket representing the\n\
1399connection, and the address of the client. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001400info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001401
Guido van Rossum11ba0942002-06-13 15:07:44 +00001402/* s.setblocking(flag) method. Argument:
1403 False -- non-blocking mode; same as settimeout(0)
1404 True -- blocking mode; same as settimeout(None)
1405*/
Guido van Rossume4485b01994-09-07 14:32:49 +00001406
Guido van Rossum73624e91994-10-10 17:59:00 +00001407static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001408sock_setblocking(PySocketSockObject *s, PyObject *arg)
Guido van Rossume4485b01994-09-07 14:32:49 +00001409{
1410 int block;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001411
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001412 block = PyInt_AsLong(arg);
1413 if (block == -1 && PyErr_Occurred())
Guido van Rossume4485b01994-09-07 14:32:49 +00001414 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001415
Guido van Rossum11ba0942002-06-13 15:07:44 +00001416 s->sock_timeout = block ? -1.0 : 0.0;
Guido van Rossume8008f02002-06-07 03:36:20 +00001417 internal_setblocking(s, block);
Guido van Rossume4485b01994-09-07 14:32:49 +00001418
Guido van Rossum73624e91994-10-10 17:59:00 +00001419 Py_INCREF(Py_None);
1420 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +00001421}
Guido van Rossume4485b01994-09-07 14:32:49 +00001422
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001423PyDoc_STRVAR(setblocking_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001424"setblocking(flag)\n\
1425\n\
1426Set the socket to blocking (flag is true) or non-blocking (false).\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001427setblocking(True) is equivalent to settimeout(None);\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001428setblocking(False) is equivalent to settimeout(0.0).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001429
Guido van Rossum11ba0942002-06-13 15:07:44 +00001430/* s.settimeout(timeout) method. Argument:
1431 None -- no timeout, blocking mode; same as setblocking(True)
1432 0.0 -- non-blocking mode; same as setblocking(False)
1433 > 0 -- timeout mode; operations time out after timeout seconds
1434 < 0 -- illegal; raises an exception
1435*/
Guido van Rossum67f7a382002-06-06 21:08:16 +00001436static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001437sock_settimeout(PySocketSockObject *s, PyObject *arg)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001438{
Guido van Rossum11ba0942002-06-13 15:07:44 +00001439 double timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001440
1441 if (arg == Py_None)
Guido van Rossum11ba0942002-06-13 15:07:44 +00001442 timeout = -1.0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001443 else {
Guido van Rossum11ba0942002-06-13 15:07:44 +00001444 timeout = PyFloat_AsDouble(arg);
1445 if (timeout < 0.0) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00001446 if (!PyErr_Occurred())
1447 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00001448 "Timeout value out of range");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001449 return NULL;
1450 }
1451 }
1452
Guido van Rossum11ba0942002-06-13 15:07:44 +00001453 s->sock_timeout = timeout;
1454 internal_setblocking(s, timeout < 0.0);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001455
1456 Py_INCREF(Py_None);
1457 return Py_None;
1458}
1459
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001460PyDoc_STRVAR(settimeout_doc,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001461"settimeout(timeout)\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001462\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001463Set a timeout on socket operations. 'timeout' can be a float,\n\
1464giving in seconds, or None. Setting a timeout of None disables\n\
1465the timeout feature and is equivalent to setblocking(1).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001466Setting a timeout of zero is the same as setblocking(0).");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001467
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001468/* s.gettimeout() method.
1469 Returns the timeout associated with a socket. */
Guido van Rossum67f7a382002-06-06 21:08:16 +00001470static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001471sock_gettimeout(PySocketSockObject *s)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001472{
1473 if (s->sock_timeout < 0.0) {
1474 Py_INCREF(Py_None);
1475 return Py_None;
1476 }
1477 else
1478 return PyFloat_FromDouble(s->sock_timeout);
1479}
1480
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001481PyDoc_STRVAR(gettimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001482"gettimeout() -> timeout\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001483\n\
1484Returns the timeout in floating seconds associated with socket \n\
1485operations. A timeout of None indicates that timeouts on socket \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001486operations are disabled.");
Guido van Rossume4485b01994-09-07 14:32:49 +00001487
Guido van Rossum48a680c2001-03-02 06:34:14 +00001488#ifdef RISCOS
1489/* s.sleeptaskw(1 | 0) method */
1490
1491static PyObject *
Martin v. Löwisa94568a2003-05-10 07:36:56 +00001492sock_sleeptaskw(PySocketSockObject *s,PyObject *arg)
Guido van Rossum48a680c2001-03-02 06:34:14 +00001493{
Guido van Rossum67f7a382002-06-06 21:08:16 +00001494 int block;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00001495 block = PyInt_AsLong(arg);
1496 if (block == -1 && PyErr_Occurred())
Guido van Rossum67f7a382002-06-06 21:08:16 +00001497 return NULL;
1498 Py_BEGIN_ALLOW_THREADS
1499 socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
1500 Py_END_ALLOW_THREADS
Guido van Rossum48a680c2001-03-02 06:34:14 +00001501
Guido van Rossum67f7a382002-06-06 21:08:16 +00001502 Py_INCREF(Py_None);
1503 return Py_None;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001504}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001505PyDoc_STRVAR(sleeptaskw_doc,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001506"sleeptaskw(flag)\n\
1507\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001508Allow sleeps in taskwindows.");
Guido van Rossum48a680c2001-03-02 06:34:14 +00001509#endif
1510
1511
Guido van Rossumaee08791992-09-08 09:05:33 +00001512/* s.setsockopt() method.
1513 With an integer third argument, sets an integer option.
1514 With a string third argument, sets an option from a buffer;
1515 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001516
Guido van Rossum73624e91994-10-10 17:59:00 +00001517static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001518sock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001519{
1520 int level;
1521 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001522 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +00001523 char *buf;
1524 int buflen;
1525 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001526
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001527 if (PyArg_ParseTuple(args, "iii:setsockopt",
1528 &level, &optname, &flag)) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001529 buf = (char *) &flag;
1530 buflen = sizeof flag;
1531 }
1532 else {
Guido van Rossum73624e91994-10-10 17:59:00 +00001533 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001534 if (!PyArg_ParseTuple(args, "iis#:setsockopt",
1535 &level, &optname, &buf, &buflen))
Guido van Rossumaee08791992-09-08 09:05:33 +00001536 return NULL;
1537 }
Thomas Wouters334fb892000-07-25 12:56:38 +00001538 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001539 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001540 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001541 Py_INCREF(Py_None);
1542 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001543}
1544
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001545PyDoc_STRVAR(setsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001546"setsockopt(level, option, value)\n\
1547\n\
1548Set a socket option. See the Unix manual for level and option.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001549The value argument can either be an integer or a string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001550
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001551
Guido van Rossumaee08791992-09-08 09:05:33 +00001552/* s.getsockopt() method.
1553 With two arguments, retrieves an integer option.
1554 With a third integer argument, retrieves a string buffer of that size;
1555 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001556
Guido van Rossum73624e91994-10-10 17:59:00 +00001557static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001558sock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001559{
1560 int level;
1561 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001562 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +00001563 PyObject *buf;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001564 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001565
Guido van Rossumbcc20741998-08-04 22:53:56 +00001566#ifdef __BEOS__
Fred Drake564a6cc2001-05-11 20:12:26 +00001567 /* We have incomplete socket support. */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001568 PyErr_SetString(socket_error, "getsockopt not supported");
Guido van Rossumbcc20741998-08-04 22:53:56 +00001569 return NULL;
1570#else
1571
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001572 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
1573 &level, &optname, &buflen))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001574 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001575
Guido van Rossumbe32c891996-06-20 16:25:29 +00001576 if (buflen == 0) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001577 int flag = 0;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001578 socklen_t flagsize = sizeof flag;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001579 res = getsockopt(s->sock_fd, level, optname,
Thomas Wouters334fb892000-07-25 12:56:38 +00001580 (void *)&flag, &flagsize);
Guido van Rossumaee08791992-09-08 09:05:33 +00001581 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001582 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001583 return PyInt_FromLong(flag);
Guido van Rossumaee08791992-09-08 09:05:33 +00001584 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001585#ifdef __VMS
1586 if (buflen > 1024) {
1587#else
Guido van Rossumaee08791992-09-08 09:05:33 +00001588 if (buflen <= 0 || buflen > 1024) {
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001589#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001590 PyErr_SetString(socket_error,
Barry Warsaw752300b1997-01-03 17:18:10 +00001591 "getsockopt buflen out of range");
Guido van Rossumaee08791992-09-08 09:05:33 +00001592 return NULL;
1593 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001594 buf = PyString_FromStringAndSize((char *)NULL, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001595 if (buf == NULL)
1596 return NULL;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001597 res = getsockopt(s->sock_fd, level, optname,
Fred Drake564a6cc2001-05-11 20:12:26 +00001598 (void *)PyString_AS_STRING(buf), &buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001599 if (res < 0) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001600 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001601 return s->errorhandler();
Guido van Rossumaee08791992-09-08 09:05:33 +00001602 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001603 _PyString_Resize(&buf, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001604 return buf;
Guido van Rossumbcc20741998-08-04 22:53:56 +00001605#endif /* __BEOS__ */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001606}
1607
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001608PyDoc_STRVAR(getsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001609"getsockopt(level, option[, buffersize]) -> value\n\
1610\n\
1611Get a socket option. See the Unix manual for level and option.\n\
1612If a nonzero buffersize argument is given, the return value is a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001613string of that length; otherwise it is an integer.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001614
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001615
Fred Drake728819a2000-07-01 03:40:12 +00001616/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001617
Guido van Rossum73624e91994-10-10 17:59:00 +00001618static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001619sock_bind(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001620{
1621 struct sockaddr *addr;
1622 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001623 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001624
Fred Drake728819a2000-07-01 03:40:12 +00001625 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001626 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001627 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001628 res = bind(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001629 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001630 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001631 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001632 Py_INCREF(Py_None);
1633 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001634}
1635
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001636PyDoc_STRVAR(bind_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001637"bind(address)\n\
1638\n\
1639Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00001640pair (host, port); the host must refer to the local host. For raw packet\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001641sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001642
Guido van Rossum30a685f1991-06-27 15:51:29 +00001643
1644/* s.close() method.
1645 Set the file descriptor to -1 so operations tried subsequently
1646 will surely fail. */
1647
Guido van Rossum73624e91994-10-10 17:59:00 +00001648static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001649sock_close(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001650{
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001651 SOCKET_T fd;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001652
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001653 if ((fd = s->sock_fd) != -1) {
1654 s->sock_fd = -1;
Guido van Rossum08481461996-10-12 14:07:22 +00001655 Py_BEGIN_ALLOW_THREADS
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001656 (void) SOCKETCLOSE(fd);
Guido van Rossum08481461996-10-12 14:07:22 +00001657 Py_END_ALLOW_THREADS
1658 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001659 Py_INCREF(Py_None);
1660 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001661}
1662
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001663PyDoc_STRVAR(close_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001664"close()\n\
1665\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001666Close the socket. It cannot be used after this call.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001667
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001668static int
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001669internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
1670 int *timeoutp)
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001671{
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001672 int res, timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001673
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001674 timeout = 0;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001675 res = connect(s->sock_fd, addr, addrlen);
1676
1677#ifdef MS_WINDOWS
1678
1679 if (s->sock_timeout > 0.0) {
1680 if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK) {
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001681 /* This is a mess. Best solution: trust select */
1682 fd_set fds;
1683 struct timeval tv;
1684 tv.tv_sec = (int)s->sock_timeout;
1685 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
1686 FD_ZERO(&fds);
1687 FD_SET(s->sock_fd, &fds);
1688 res = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001689 if (res == 0) {
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001690 res = WSAEWOULDBLOCK;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001691 timeout = 1;
1692 } else if (res > 0)
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001693 res = 0;
1694 /* else if (res < 0) an error occurred */
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001695 }
1696 }
1697
1698 if (res < 0)
1699 res = WSAGetLastError();
1700
1701#else
1702
1703 if (s->sock_timeout > 0.0) {
1704 if (res < 0 && errno == EINPROGRESS) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001705 timeout = internal_select(s, 1);
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001706 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum12e3c712002-08-08 20:39:30 +00001707 if (res < 0 && errno == EISCONN)
1708 res = 0;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001709 }
1710 }
1711
1712 if (res < 0)
1713 res = errno;
1714
1715#endif
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001716 *timeoutp = timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001717
1718 return res;
1719}
Guido van Rossum30a685f1991-06-27 15:51:29 +00001720
Fred Drake728819a2000-07-01 03:40:12 +00001721/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001722
Guido van Rossum73624e91994-10-10 17:59:00 +00001723static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001724sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001725{
1726 struct sockaddr *addr;
1727 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001728 int res;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001729 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001730
Fred Drake728819a2000-07-01 03:40:12 +00001731 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001732 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001733
Guido van Rossum73624e91994-10-10 17:59:00 +00001734 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001735 res = internal_connect(s, addr, addrlen, &timeout);
Guido van Rossum11ba0942002-06-13 15:07:44 +00001736 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001737
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001738 if (timeout) {
1739 PyErr_SetString(socket_timeout, "timed out");
1740 return NULL;
1741 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001742 if (res != 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001743 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001744 Py_INCREF(Py_None);
1745 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001746}
1747
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001748PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001749"connect(address)\n\
1750\n\
1751Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001752is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001753
Guido van Rossum30a685f1991-06-27 15:51:29 +00001754
Fred Drake728819a2000-07-01 03:40:12 +00001755/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001756
1757static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001758sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001759{
1760 struct sockaddr *addr;
1761 int addrlen;
1762 int res;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001763 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001764
Fred Drake728819a2000-07-01 03:40:12 +00001765 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001766 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001767
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001768 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001769 res = internal_connect(s, addr, addrlen, &timeout);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001770 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001771
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001772 return PyInt_FromLong((long) res);
1773}
1774
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001775PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001776"connect_ex(address) -> errno\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001777\n\
1778This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001779instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001780
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001781
Guido van Rossumed233a51992-06-23 09:07:03 +00001782/* s.fileno() method */
1783
Guido van Rossum73624e91994-10-10 17:59:00 +00001784static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001785sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00001786{
Fred Drakea04eaad2000-06-30 02:46:07 +00001787#if SIZEOF_SOCKET_T <= SIZEOF_LONG
Guido van Rossum73624e91994-10-10 17:59:00 +00001788 return PyInt_FromLong((long) s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +00001789#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001790 return PyLong_FromLongLong((PY_LONG_LONG)s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +00001791#endif
Guido van Rossumed233a51992-06-23 09:07:03 +00001792}
1793
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001794PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001795"fileno() -> integer\n\
1796\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001797Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001798
Guido van Rossumed233a51992-06-23 09:07:03 +00001799
Guido van Rossumbe32c891996-06-20 16:25:29 +00001800#ifndef NO_DUP
1801/* s.dup() method */
1802
1803static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001804sock_dup(PySocketSockObject *s)
Guido van Rossumbe32c891996-06-20 16:25:29 +00001805{
Fred Drakea04eaad2000-06-30 02:46:07 +00001806 SOCKET_T newfd;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001807 PyObject *sock;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001808
Guido van Rossumbe32c891996-06-20 16:25:29 +00001809 newfd = dup(s->sock_fd);
1810 if (newfd < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001811 return s->errorhandler();
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001812 sock = (PyObject *) new_sockobject(newfd,
1813 s->sock_family,
1814 s->sock_type,
1815 s->sock_proto);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001816 if (sock == NULL)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001817 SOCKETCLOSE(newfd);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001818 return sock;
1819}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001820
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001821PyDoc_STRVAR(dup_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001822"dup() -> socket object\n\
1823\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001824Return a new socket object connected to the same system resource.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001825
Guido van Rossumbe32c891996-06-20 16:25:29 +00001826#endif
1827
1828
Guido van Rossumc89705d1992-11-26 08:54:07 +00001829/* s.getsockname() method */
1830
Guido van Rossum73624e91994-10-10 17:59:00 +00001831static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001832sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001833{
1834 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001835 int res;
1836 socklen_t addrlen;
1837
Guido van Rossumc89705d1992-11-26 08:54:07 +00001838 if (!getsockaddrlen(s, &addrlen))
1839 return NULL;
Guido van Rossumca42b161996-01-12 01:36:05 +00001840 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001841 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001842 res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001843 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001844 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001845 return s->errorhandler();
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001846 return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen,
1847 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001848}
1849
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001850PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001851"getsockname() -> address info\n\
1852\n\
1853Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001854info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001855
Guido van Rossumc89705d1992-11-26 08:54:07 +00001856
Guido van Rossumb6775db1994-08-01 11:34:53 +00001857#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001858/* s.getpeername() method */
1859
Guido van Rossum73624e91994-10-10 17:59:00 +00001860static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001861sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001862{
1863 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001864 int res;
1865 socklen_t addrlen;
1866
Guido van Rossumc89705d1992-11-26 08:54:07 +00001867 if (!getsockaddrlen(s, &addrlen))
1868 return NULL;
Fred Drakefd168342001-05-09 19:11:33 +00001869 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001870 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001871 res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001872 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001873 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001874 return s->errorhandler();
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001875 return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen,
1876 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001877}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001878
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001879PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001880"getpeername() -> address info\n\
1881\n\
1882Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001883info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001884
Guido van Rossumb6775db1994-08-01 11:34:53 +00001885#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001886
1887
Guido van Rossum30a685f1991-06-27 15:51:29 +00001888/* s.listen(n) method */
1889
Guido van Rossum73624e91994-10-10 17:59:00 +00001890static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001891sock_listen(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001892{
1893 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001894 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001895
1896 backlog = PyInt_AsLong(arg);
1897 if (backlog == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001898 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001899 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +00001900 if (backlog < 1)
1901 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001902 res = listen(s->sock_fd, backlog);
Guido van Rossum73624e91994-10-10 17:59:00 +00001903 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001904 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001905 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001906 Py_INCREF(Py_None);
1907 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001908}
1909
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001910PyDoc_STRVAR(listen_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001911"listen(backlog)\n\
1912\n\
1913Enable a server to accept connections. The backlog argument must be at\n\
1914least 1; it specifies the number of unaccepted connection that the system\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001915will allow before refusing new connections.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001916
1917
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001918#ifndef NO_DUP
Guido van Rossum30a685f1991-06-27 15:51:29 +00001919/* s.makefile(mode) method.
1920 Create a new open file object referring to a dupped version of
1921 the socket's file descriptor. (The dup() call is necessary so
1922 that the open file and socket objects may be closed independent
1923 of each other.)
1924 The mode argument specifies 'r' or 'w' passed to fdopen(). */
1925
Guido van Rossum73624e91994-10-10 17:59:00 +00001926static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001927sock_makefile(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001928{
Tim Petersdbd9ba62000-07-09 03:09:57 +00001929 extern int fclose(FILE *);
Guido van Rossum6b144911995-03-14 15:05:13 +00001930 char *mode = "r";
1931 int bufsize = -1;
Fred Drakea04eaad2000-06-30 02:46:07 +00001932#ifdef MS_WIN32
Tim Peters79248aa2001-08-29 21:37:10 +00001933 Py_intptr_t fd;
Fred Drakea04eaad2000-06-30 02:46:07 +00001934#else
Guido van Rossum30a685f1991-06-27 15:51:29 +00001935 int fd;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001936#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001937 FILE *fp;
Guido van Rossum6b144911995-03-14 15:05:13 +00001938 PyObject *f;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001939#ifdef __VMS
1940 char *mode_r = "r";
1941 char *mode_w = "w";
1942#endif
Guido van Rossum6b144911995-03-14 15:05:13 +00001943
Guido van Rossum43713e52000-02-29 13:59:29 +00001944 if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001945 return NULL;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001946#ifdef __VMS
1947 if (strcmp(mode,"rb") == 0) {
1948 mode = mode_r;
1949 }
1950 else {
1951 if (strcmp(mode,"wb") == 0) {
1952 mode = mode_w;
1953 }
1954 }
1955#endif
Guido van Rossum8d665e61996-06-26 18:22:49 +00001956#ifdef MS_WIN32
Guido van Rossum82a5c661998-07-07 20:45:43 +00001957 if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) ||
1958 ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL))
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001959#else
Guido van Rossum82a5c661998-07-07 20:45:43 +00001960 if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL)
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001961#endif
Guido van Rossum82a5c661998-07-07 20:45:43 +00001962 {
Guido van Rossum6b144911995-03-14 15:05:13 +00001963 if (fd >= 0)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001964 SOCKETCLOSE(fd);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001965 return s->errorhandler();
Guido van Rossum6b144911995-03-14 15:05:13 +00001966 }
1967 f = PyFile_FromFile(fp, "<socket>", mode, fclose);
1968 if (f != NULL)
1969 PyFile_SetBufSize(f, bufsize);
1970 return f;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001971}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001972
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001973PyDoc_STRVAR(makefile_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001974"makefile([mode[, buffersize]]) -> file object\n\
1975\n\
1976Return a regular file object corresponding to the socket.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001977The mode and buffersize arguments are as for the built-in open() function.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001978
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001979#endif /* NO_DUP */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001980
Guido van Rossum48a680c2001-03-02 06:34:14 +00001981
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001982/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001983
Guido van Rossum73624e91994-10-10 17:59:00 +00001984static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001985sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001986{
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001987 int len, n = 0, flags = 0, timeout;
Guido van Rossum73624e91994-10-10 17:59:00 +00001988 PyObject *buf;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001989#ifdef __VMS
1990 int read_length;
1991 char *read_buf;
1992#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00001993
Guido van Rossum43713e52000-02-29 13:59:29 +00001994 if (!PyArg_ParseTuple(args, "i|i:recv", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001995 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001996
1997 if (len < 0) {
Martin v. Löwisfba64e12001-11-19 10:41:26 +00001998 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00001999 "negative buffersize in recv");
Martin v. Löwisfba64e12001-11-19 10:41:26 +00002000 return NULL;
2001 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002002
Guido van Rossum73624e91994-10-10 17:59:00 +00002003 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002004 if (buf == NULL)
2005 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002006
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002007#ifndef __VMS
Guido van Rossum73624e91994-10-10 17:59:00 +00002008 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002009 timeout = internal_select(s, 0);
2010 if (!timeout)
2011 n = recv(s->sock_fd, PyString_AS_STRING(buf), len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00002012 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002013
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002014 if (timeout) {
2015 Py_DECREF(buf);
2016 PyErr_SetString(socket_timeout, "timed out");
2017 return NULL;
2018 }
Guido van Rossum7c53b771995-09-13 18:39:47 +00002019 if (n < 0) {
2020 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002021 return s->errorhandler();
Guido van Rossum7c53b771995-09-13 18:39:47 +00002022 }
Tim Peters5de98422002-04-27 18:44:32 +00002023 if (n != len)
2024 _PyString_Resize(&buf, n);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002025#else
2026 read_buf = PyString_AsString(buf);
2027 read_length = len;
2028 while (read_length != 0) {
2029 unsigned int segment;
2030
2031 segment = read_length /SEGMENT_SIZE;
2032 if (segment != 0) {
2033 segment = SEGMENT_SIZE;
2034 }
2035 else {
2036 segment = read_length;
2037 }
2038
2039 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002040 timeout = internal_select(s, 0);
2041 if (!timeout)
2042 n = recv(s->sock_fd, read_buf, segment, flags);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002043 Py_END_ALLOW_THREADS
2044
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002045 if (timeout) {
2046 Py_DECREF(buf);
2047 PyErr_SetString(socket_timeout, "timed out");
2048 return NULL;
2049 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002050 if (n < 0) {
2051 Py_DECREF(buf);
2052 return s->errorhandler();
2053 }
2054 if (n != read_length) {
2055 read_buf += n;
2056 break;
2057 }
2058
2059 read_length -= segment;
2060 read_buf += segment;
2061 }
2062 if (_PyString_Resize(&buf, (read_buf - PyString_AsString(buf))) < 0)
2063 {
2064 return NULL;
2065 }
2066#endif /* !__VMS */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002067 return buf;
2068}
2069
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002070PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002071"recv(buffersize[, flags]) -> data\n\
2072\n\
2073Receive up to buffersize bytes from the socket. For the optional flags\n\
2074argument, see the Unix manual. When no data is available, block until\n\
2075at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002076the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002077
Guido van Rossum30a685f1991-06-27 15:51:29 +00002078
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002079/* s.recvfrom(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002080
Guido van Rossum73624e91994-10-10 17:59:00 +00002081static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002082sock_recvfrom(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002083{
2084 char addrbuf[256];
Barry Warsaw752300b1997-01-03 17:18:10 +00002085 PyObject *buf = NULL;
2086 PyObject *addr = NULL;
2087 PyObject *ret = NULL;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002088 int len, n = 0, flags = 0, timeout;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002089 socklen_t addrlen;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002090
Guido van Rossum43713e52000-02-29 13:59:29 +00002091 if (!PyArg_ParseTuple(args, "i|i:recvfrom", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002092 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002093
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00002094 if (!getsockaddrlen(s, &addrlen))
2095 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002096 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00002097 if (buf == NULL)
2098 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002099
Guido van Rossum73624e91994-10-10 17:59:00 +00002100 Py_BEGIN_ALLOW_THREADS
Fred Drakefd168342001-05-09 19:11:33 +00002101 memset(addrbuf, 0, addrlen);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002102 timeout = internal_select(s, 0);
2103 if (!timeout)
2104 n = recvfrom(s->sock_fd, PyString_AS_STRING(buf), len, flags,
Guido van Rossum8d665e61996-06-26 18:22:49 +00002105#ifndef MS_WINDOWS
Andrew MacIntyreba43e872002-03-03 03:03:52 +00002106#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002107 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00002108#else
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002109 (void *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00002110#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002111#else
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002112 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002113#endif
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002114 );
Guido van Rossum73624e91994-10-10 17:59:00 +00002115 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002116
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002117 if (timeout) {
2118 Py_DECREF(buf);
2119 PyErr_SetString(socket_timeout, "timed out");
2120 return NULL;
2121 }
Guido van Rossum7c53b771995-09-13 18:39:47 +00002122 if (n < 0) {
2123 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002124 return s->errorhandler();
Guido van Rossum7c53b771995-09-13 18:39:47 +00002125 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002126
Guido van Rossum53a9bf81996-06-11 18:35:24 +00002127 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002128 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002129
Guido van Rossum67f7a382002-06-06 21:08:16 +00002130 if (!(addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf,
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00002131 addrlen, s->sock_proto)))
Barry Warsaw752300b1997-01-03 17:18:10 +00002132 goto finally;
2133
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002134 ret = PyTuple_Pack(2, buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002135
2136finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00002137 Py_XDECREF(addr);
2138 Py_XDECREF(buf);
Guido van Rossum6f5afc91993-02-05 09:46:15 +00002139 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002140}
2141
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002142PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002143"recvfrom(buffersize[, flags]) -> (data, address info)\n\
2144\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002145Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002146
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002147/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002148
Guido van Rossum73624e91994-10-10 17:59:00 +00002149static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002150sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002151{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002152 char *buf;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002153 int len, n = 0, flags = 0, timeout;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002154#ifdef __VMS
2155 int send_length;
2156#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002157
Guido van Rossum43713e52000-02-29 13:59:29 +00002158 if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002159 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002160
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002161#ifndef __VMS
Guido van Rossum73624e91994-10-10 17:59:00 +00002162 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002163 timeout = internal_select(s, 1);
2164 if (!timeout)
2165 n = send(s->sock_fd, buf, len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00002166 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002167
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002168 if (timeout) {
2169 PyErr_SetString(socket_timeout, "timed out");
2170 return NULL;
2171 }
Guido van Rossum30a685f1991-06-27 15:51:29 +00002172 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002173 return s->errorhandler();
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002174#else
2175 /* Divide packet into smaller segments for */
2176 /* TCP/IP Services for OpenVMS */
2177 send_length = len;
2178 while (send_length != 0) {
2179 unsigned int segment;
2180
2181 segment = send_length / SEGMENT_SIZE;
2182 if (segment != 0) {
2183 segment = SEGMENT_SIZE;
2184 }
2185 else {
2186 segment = send_length;
2187 }
2188 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002189 timeout = internal_select(s, 1);
2190 if (!timeout)
2191 n = send(s->sock_fd, buf, segment, flags);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002192 Py_END_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002193 if (timeout) {
2194 PyErr_SetString(socket_timeout, "timed out");
2195 return NULL;
2196 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002197 if (n < 0) {
2198 return s->errorhandler();
2199 }
2200 send_length -= segment;
2201 buf += segment;
2202 } /* end while */
2203#endif /* !__VMS */
Guido van Rossum73624e91994-10-10 17:59:00 +00002204 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00002205}
2206
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002207PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002208"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002209\n\
2210Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002211argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002212sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002213
2214
2215/* s.sendall(data [,flags]) method */
2216
2217static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002218sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002219{
2220 char *buf;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002221 int len, n = 0, flags = 0, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002222
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002223 if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags))
2224 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002225
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002226 Py_BEGIN_ALLOW_THREADS
2227 do {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002228 timeout = internal_select(s, 1);
2229 if (timeout)
2230 break;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002231 n = send(s->sock_fd, buf, len, flags);
2232 if (n < 0)
2233 break;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002234 buf += n;
2235 len -= n;
2236 } while (len > 0);
2237 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002238
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002239 if (timeout) {
2240 PyErr_SetString(socket_timeout, "timed out");
2241 return NULL;
2242 }
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002243 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002244 return s->errorhandler();
Guido van Rossum67f7a382002-06-06 21:08:16 +00002245
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002246 Py_INCREF(Py_None);
2247 return Py_None;
2248}
2249
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002250PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002251"sendall(data[, flags])\n\
2252\n\
2253Send a data string to the socket. For the optional flags\n\
2254argument, see the Unix manual. This calls send() repeatedly\n\
2255until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002256to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002257
Guido van Rossum30a685f1991-06-27 15:51:29 +00002258
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002259/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002260
Guido van Rossum73624e91994-10-10 17:59:00 +00002261static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002262sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002263{
Guido van Rossum73624e91994-10-10 17:59:00 +00002264 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002265 char *buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002266 struct sockaddr *addr;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002267 int addrlen, len, n = 0, flags, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002268
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002269 flags = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002270 if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) {
Guido van Rossum73624e91994-10-10 17:59:00 +00002271 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002272 if (!PyArg_ParseTuple(args, "s#iO:sendto",
2273 &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002274 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002275 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002276
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002277 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002278 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002279
Guido van Rossum73624e91994-10-10 17:59:00 +00002280 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002281 timeout = internal_select(s, 1);
2282 if (!timeout)
2283 n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002284 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002285
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002286 if (timeout) {
2287 PyErr_SetString(socket_timeout, "timed out");
2288 return NULL;
2289 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002290 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002291 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002292 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002293}
2294
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002295PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002296"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002297\n\
2298Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002299For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002300
Guido van Rossum30a685f1991-06-27 15:51:29 +00002301
2302/* s.shutdown(how) method */
2303
Guido van Rossum73624e91994-10-10 17:59:00 +00002304static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002305sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002306{
2307 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002308 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002309
2310 how = PyInt_AsLong(arg);
2311 if (how == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002312 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002313 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002314 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +00002315 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002316 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002317 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002318 Py_INCREF(Py_None);
2319 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002320}
2321
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002322PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002323"shutdown(flag)\n\
2324\n\
Martin v. Löwis94681fc2003-11-27 19:40:22 +00002325Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\
2326of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002327
Guido van Rossum30a685f1991-06-27 15:51:29 +00002328
2329/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002330
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002331static PyMethodDef sock_methods[] = {
2332 {"accept", (PyCFunction)sock_accept, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002333 accept_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002334 {"bind", (PyCFunction)sock_bind, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002335 bind_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002336 {"close", (PyCFunction)sock_close, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002337 close_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002338 {"connect", (PyCFunction)sock_connect, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002339 connect_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002340 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002341 connect_ex_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00002342#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002343 {"dup", (PyCFunction)sock_dup, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002344 dup_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00002345#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002346 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002347 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002348#ifdef HAVE_GETPEERNAME
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002349 {"getpeername", (PyCFunction)sock_getpeername,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002350 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00002351#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002352 {"getsockname", (PyCFunction)sock_getsockname,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002353 METH_NOARGS, getsockname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002354 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002355 getsockopt_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002356 {"listen", (PyCFunction)sock_listen, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002357 listen_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002358#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002359 {"makefile", (PyCFunction)sock_makefile, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002360 makefile_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002361#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002362 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002363 recv_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002364 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002365 recvfrom_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002366 {"send", (PyCFunction)sock_send, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002367 send_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002368 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002369 sendall_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002370 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002371 sendto_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002372 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002373 setblocking_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002374 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
Guido van Rossum67f7a382002-06-06 21:08:16 +00002375 settimeout_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002376 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
Guido van Rossum67f7a382002-06-06 21:08:16 +00002377 gettimeout_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002378 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002379 setsockopt_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002380 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002381 shutdown_doc},
2382#ifdef RISCOS
Martin v. Löwisa94568a2003-05-10 07:36:56 +00002383 {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002384 sleeptaskw_doc},
2385#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00002386 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002387};
2388
Guido van Rossum30a685f1991-06-27 15:51:29 +00002389
Guido van Rossum73624e91994-10-10 17:59:00 +00002390/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00002391 First close the file description. */
2392
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002393static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002394sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002395{
Guido van Rossumfa972c92000-04-10 12:45:45 +00002396 if (s->sock_fd != -1)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00002397 (void) SOCKETCLOSE(s->sock_fd);
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002398 s->ob_type->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002399}
2400
Guido van Rossum30a685f1991-06-27 15:51:29 +00002401
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002402static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002403sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002404{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002405 char buf[512];
Fred Drakea04eaad2000-06-30 02:46:07 +00002406#if SIZEOF_SOCKET_T > SIZEOF_LONG
2407 if (s->sock_fd > LONG_MAX) {
2408 /* this can occur on Win64, and actually there is a special
2409 ugly printf formatter for decimal pointer length integer
2410 printing, only bother if necessary*/
2411 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002412 "no printf formatter to display "
2413 "the socket descriptor in decimal");
Fred Drakea04eaad2000-06-30 02:46:07 +00002414 return NULL;
2415 }
2416#endif
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002417 PyOS_snprintf(
2418 buf, sizeof(buf),
2419 "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>",
2420 (long)s->sock_fd, s->sock_family,
2421 s->sock_type,
2422 s->sock_proto);
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00002423 return PyString_FromString(buf);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002424}
2425
2426
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002427/* Create a new, uninitialized socket object. */
2428
2429static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002430sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002431{
2432 PyObject *new;
2433
2434 new = type->tp_alloc(type, 0);
Guido van Rossume1c478f2002-06-06 20:08:25 +00002435 if (new != NULL) {
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002436 ((PySocketSockObject *)new)->sock_fd = -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002437 ((PySocketSockObject *)new)->sock_timeout = -1.0;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002438 ((PySocketSockObject *)new)->errorhandler = &set_error;
Guido van Rossume1c478f2002-06-06 20:08:25 +00002439 }
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002440 return new;
2441}
2442
2443
2444/* Initialize a new socket object. */
2445
2446/*ARGSUSED*/
2447static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002448sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002449{
2450 PySocketSockObject *s = (PySocketSockObject *)self;
2451 SOCKET_T fd;
2452 int family = AF_INET, type = SOCK_STREAM, proto = 0;
2453 static char *keywords[] = {"family", "type", "proto", 0};
2454
2455 if (!PyArg_ParseTupleAndKeywords(args, kwds,
2456 "|iii:socket", keywords,
2457 &family, &type, &proto))
2458 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002459
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002460 Py_BEGIN_ALLOW_THREADS
2461 fd = socket(family, type, proto);
2462 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002463
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002464#ifdef MS_WINDOWS
2465 if (fd == INVALID_SOCKET)
2466#else
2467 if (fd < 0)
2468#endif
2469 {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002470 set_error();
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002471 return -1;
2472 }
2473 init_sockobject(s, fd, family, type, proto);
2474 /* From now on, ignore SIGPIPE and let the error checking
2475 do the work. */
2476#ifdef SIGPIPE
2477 (void) signal(SIGPIPE, SIG_IGN);
2478#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002479
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002480 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002481
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002482}
2483
2484
Guido van Rossumb6775db1994-08-01 11:34:53 +00002485/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002486
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002487static PyTypeObject sock_type = {
Guido van Rossum55558541996-05-23 22:54:50 +00002488 PyObject_HEAD_INIT(0) /* Must fill in type value later */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002489 0, /* ob_size */
Guido van Rossumca6dfa52001-10-28 12:31:33 +00002490 "_socket.socket", /* tp_name */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002491 sizeof(PySocketSockObject), /* tp_basicsize */
2492 0, /* tp_itemsize */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002493 (destructor)sock_dealloc, /* tp_dealloc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002494 0, /* tp_print */
2495 0, /* tp_getattr */
2496 0, /* tp_setattr */
2497 0, /* tp_compare */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002498 (reprfunc)sock_repr, /* tp_repr */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002499 0, /* tp_as_number */
2500 0, /* tp_as_sequence */
2501 0, /* tp_as_mapping */
2502 0, /* tp_hash */
2503 0, /* tp_call */
2504 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002505 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002506 0, /* tp_setattro */
2507 0, /* tp_as_buffer */
2508 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002509 sock_doc, /* tp_doc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002510 0, /* tp_traverse */
2511 0, /* tp_clear */
2512 0, /* tp_richcompare */
2513 0, /* tp_weaklistoffset */
2514 0, /* tp_iter */
2515 0, /* tp_iternext */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002516 sock_methods, /* tp_methods */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002517 0, /* tp_members */
2518 0, /* tp_getset */
2519 0, /* tp_base */
2520 0, /* tp_dict */
2521 0, /* tp_descr_get */
2522 0, /* tp_descr_set */
2523 0, /* tp_dictoffset */
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002524 sock_initobj, /* tp_init */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002525 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002526 sock_new, /* tp_new */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002527 PyObject_Del, /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002528};
2529
Guido van Rossum30a685f1991-06-27 15:51:29 +00002530
Guido van Rossum81194471991-07-27 21:42:02 +00002531/* Python interface to gethostname(). */
2532
2533/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002534static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002535socket_gethostname(PyObject *self, PyObject *args)
Guido van Rossum81194471991-07-27 21:42:02 +00002536{
2537 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00002538 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002539 if (!PyArg_ParseTuple(args, ":gethostname"))
Guido van Rossum81194471991-07-27 21:42:02 +00002540 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002541 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002542 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00002543 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002544 if (res < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002545 return set_error();
Guido van Rossum81194471991-07-27 21:42:02 +00002546 buf[sizeof buf - 1] = '\0';
Guido van Rossum73624e91994-10-10 17:59:00 +00002547 return PyString_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00002548}
Guido van Rossumff4949e1992-08-05 19:58:53 +00002549
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002550PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002551"gethostname() -> string\n\
2552\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002553Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002554
Guido van Rossumff4949e1992-08-05 19:58:53 +00002555
Guido van Rossum30a685f1991-06-27 15:51:29 +00002556/* Python interface to gethostbyname(name). */
2557
2558/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002559static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002560socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002561{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002562 char *name;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00002563#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002564 struct sockaddr_storage addrbuf;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00002565#else
2566 struct sockaddr_in addrbuf;
2567#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002568
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002569 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002570 return NULL;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002571 if (setipaddr(name, (struct sockaddr *)&addrbuf, sizeof(addrbuf), AF_INET) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002572 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002573 return makeipaddr((struct sockaddr *)&addrbuf,
2574 sizeof(struct sockaddr_in));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002575}
2576
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002577PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002578"gethostbyname(host) -> address\n\
2579\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002580Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002581
2582
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002583/* Convenience function common to gethostbyname_ex and gethostbyaddr */
2584
2585static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002586gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002587{
2588 char **pch;
2589 PyObject *rtn_tuple = (PyObject *)NULL;
2590 PyObject *name_list = (PyObject *)NULL;
2591 PyObject *addr_list = (PyObject *)NULL;
2592 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002593
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002594 if (h == NULL) {
Guido van Rossum48a680c2001-03-02 06:34:14 +00002595 /* Let's get real error message to return */
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002596#ifndef RISCOS
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002597 set_herror(h_errno);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002598#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002599 PyErr_SetString(socket_error, "host not found");
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002600#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002601 return NULL;
2602 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002603
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002604 if (h->h_addrtype != af) {
2605#ifdef HAVE_STRERROR
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002606 /* Let's get real error message to return */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002607 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002608 (char *)strerror(EAFNOSUPPORT));
2609#else
2610 PyErr_SetString(
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002611 socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002612 "Address family not supported by protocol family");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002613#endif
2614 return NULL;
2615 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002616
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002617 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002618
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002619 case AF_INET:
2620 if (alen < sizeof(struct sockaddr_in))
2621 return NULL;
2622 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002623
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002624#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002625 case AF_INET6:
2626 if (alen < sizeof(struct sockaddr_in6))
2627 return NULL;
2628 break;
2629#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002630
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002631 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002632
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002633 if ((name_list = PyList_New(0)) == NULL)
2634 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002635
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002636 if ((addr_list = PyList_New(0)) == NULL)
2637 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002638
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002639 for (pch = h->h_aliases; *pch != NULL; pch++) {
2640 int status;
2641 tmp = PyString_FromString(*pch);
2642 if (tmp == NULL)
2643 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002644
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002645 status = PyList_Append(name_list, tmp);
2646 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002647
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002648 if (status)
2649 goto err;
2650 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002651
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002652 for (pch = h->h_addr_list; *pch != NULL; pch++) {
2653 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002654
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002655 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002656
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002657 case AF_INET:
2658 {
2659 struct sockaddr_in sin;
2660 memset(&sin, 0, sizeof(sin));
2661 sin.sin_family = af;
2662#ifdef HAVE_SOCKADDR_SA_LEN
2663 sin.sin_len = sizeof(sin);
2664#endif
2665 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
2666 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002667
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002668 if (pch == h->h_addr_list && alen >= sizeof(sin))
2669 memcpy((char *) addr, &sin, sizeof(sin));
2670 break;
2671 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002672
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002673#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002674 case AF_INET6:
2675 {
2676 struct sockaddr_in6 sin6;
2677 memset(&sin6, 0, sizeof(sin6));
2678 sin6.sin6_family = af;
2679#ifdef HAVE_SOCKADDR_SA_LEN
2680 sin6.sin6_len = sizeof(sin6);
2681#endif
2682 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
2683 tmp = makeipaddr((struct sockaddr *)&sin6,
2684 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002685
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002686 if (pch == h->h_addr_list && alen >= sizeof(sin6))
2687 memcpy((char *) addr, &sin6, sizeof(sin6));
2688 break;
2689 }
2690#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002691
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002692 default: /* can't happen */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002693 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002694 "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002695 return NULL;
2696 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002697
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002698 if (tmp == NULL)
2699 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002700
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002701 status = PyList_Append(addr_list, tmp);
2702 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002703
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002704 if (status)
2705 goto err;
2706 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002707
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002708 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002709
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002710 err:
2711 Py_XDECREF(name_list);
2712 Py_XDECREF(addr_list);
2713 return rtn_tuple;
2714}
2715
2716
2717/* Python interface to gethostbyname_ex(name). */
2718
2719/*ARGSUSED*/
2720static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002721socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002722{
2723 char *name;
2724 struct hostent *h;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00002725#ifdef ENABLE_IPV6
2726 struct sockaddr_storage addr;
2727#else
2728 struct sockaddr_in addr;
2729#endif
Martin v. Löwis9db2f572001-07-23 01:30:10 +00002730 struct sockaddr *sa;
Guido van Rossum955becc1999-03-22 20:14:53 +00002731 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002732#ifdef HAVE_GETHOSTBYNAME_R
2733 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002734#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
2735 struct hostent_data data;
2736#else
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002737 char buf[16384];
2738 int buf_len = (sizeof buf) - 1;
2739 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002740#endif
2741#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002742 int result;
2743#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002744#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002745
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002746 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002747 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00002748 if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002749 return NULL;
2750 Py_BEGIN_ALLOW_THREADS
2751#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002752#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002753 result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
2754 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002755#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002756 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002757#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00002758 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002759 result = gethostbyname_r(name, &hp_allocated, &data);
2760 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002761#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002762#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002763#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002764 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002765#endif
2766 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002767#endif /* HAVE_GETHOSTBYNAME_R */
2768 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002769 /* Some C libraries would require addr.__ss_family instead of
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002770 addr.ss_family.
2771 Therefore, we cast the sockaddr_storage into sockaddr to
2772 access sa_family. */
Martin v. Löwis9db2f572001-07-23 01:30:10 +00002773 sa = (struct sockaddr*)&addr;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002774 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002775 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002776#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002777 PyThread_release_lock(netdb_lock);
Guido van Rossum955becc1999-03-22 20:14:53 +00002778#endif
2779 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002780}
2781
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002782PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002783"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
2784\n\
2785Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002786for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002787
2788
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002789/* Python interface to gethostbyaddr(IP). */
2790
2791/*ARGSUSED*/
2792static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002793socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002794{
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002795#ifdef ENABLE_IPV6
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002796 struct sockaddr_storage addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002797#else
Guido van Rossum48a680c2001-03-02 06:34:14 +00002798 struct sockaddr_in addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002799#endif
2800 struct sockaddr *sa = (struct sockaddr *)&addr;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002801 char *ip_num;
2802 struct hostent *h;
Guido van Rossum3baaa131999-03-22 21:44:51 +00002803 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002804#ifdef HAVE_GETHOSTBYNAME_R
2805 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002806#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
2807 struct hostent_data data;
2808#else
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002809 char buf[16384];
2810 int buf_len = (sizeof buf) - 1;
2811 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002812#endif
2813#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002814 int result;
2815#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002816#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002817 char *ap;
2818 int al;
2819 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002820
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002821 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002822 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00002823 af = AF_UNSPEC;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002824 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002825 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002826 af = sa->sa_family;
2827 ap = NULL;
2828 al = 0;
2829 switch (af) {
2830 case AF_INET:
2831 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
2832 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
2833 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002834#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002835 case AF_INET6:
2836 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
2837 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
2838 break;
2839#endif
2840 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002841 PyErr_SetString(socket_error, "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002842 return NULL;
2843 }
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002844 Py_BEGIN_ALLOW_THREADS
2845#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002846#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002847 result = gethostbyaddr_r(ap, al, af,
2848 &hp_allocated, buf, buf_len,
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002849 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002850#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002851 h = gethostbyaddr_r(ap, al, af,
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002852 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002853#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00002854 memset((void *) &data, '\0', sizeof(data));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002855 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002856 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002857#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002858#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002859#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002860 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002861#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002862 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002863#endif /* HAVE_GETHOSTBYNAME_R */
2864 Py_END_ALLOW_THREADS
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002865 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002866#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002867 PyThread_release_lock(netdb_lock);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002868#endif
2869 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002870}
2871
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002872PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002873"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
2874\n\
2875Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002876for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002877
Guido van Rossum30a685f1991-06-27 15:51:29 +00002878
2879/* Python interface to getservbyname(name).
2880 This only returns the port number, since the other info is already
2881 known or not useful (like the list of aliases). */
2882
2883/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002884static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002885socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002886{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002887 char *name, *proto;
Guido van Rossum30a685f1991-06-27 15:51:29 +00002888 struct servent *sp;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002889 if (!PyArg_ParseTuple(args, "ss:getservbyname", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002890 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002891 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002892 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00002893 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00002894 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002895 PyErr_SetString(socket_error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00002896 return NULL;
2897 }
Guido van Rossum73624e91994-10-10 17:59:00 +00002898 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002899}
2900
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002901PyDoc_STRVAR(getservbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002902"getservbyname(servicename, protocolname) -> integer\n\
2903\n\
2904Return a port number from a service name and protocol name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002905The protocol name should be 'tcp' or 'udp'.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002906
Guido van Rossum30a685f1991-06-27 15:51:29 +00002907
Guido van Rossum3901d851996-12-19 16:35:04 +00002908/* Python interface to getprotobyname(name).
2909 This only returns the protocol number, since the other info is
2910 already known or not useful (like the list of aliases). */
2911
2912/*ARGSUSED*/
2913static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002914socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00002915{
2916 char *name;
2917 struct protoent *sp;
Guido van Rossumbcc20741998-08-04 22:53:56 +00002918#ifdef __BEOS__
2919/* Not available in BeOS yet. - [cjh] */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002920 PyErr_SetString(socket_error, "getprotobyname not supported");
Guido van Rossumbcc20741998-08-04 22:53:56 +00002921 return NULL;
2922#else
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002923 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
Guido van Rossum3901d851996-12-19 16:35:04 +00002924 return NULL;
2925 Py_BEGIN_ALLOW_THREADS
2926 sp = getprotobyname(name);
2927 Py_END_ALLOW_THREADS
2928 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002929 PyErr_SetString(socket_error, "protocol not found");
Guido van Rossum3901d851996-12-19 16:35:04 +00002930 return NULL;
2931 }
2932 return PyInt_FromLong((long) sp->p_proto);
Guido van Rossumbcc20741998-08-04 22:53:56 +00002933#endif
Guido van Rossum3901d851996-12-19 16:35:04 +00002934}
2935
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002936PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002937"getprotobyname(name) -> integer\n\
2938\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002939Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002940
Guido van Rossum3901d851996-12-19 16:35:04 +00002941
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002942#ifndef NO_DUP
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002943/* Create a socket object from a numeric file description.
2944 Useful e.g. if stdin is a socket.
2945 Additional arguments as for socket(). */
2946
2947/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002948static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002949socket_fromfd(PyObject *self, PyObject *args)
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002950{
Guido van Rossum73624e91994-10-10 17:59:00 +00002951 PySocketSockObject *s;
Fred Drakea04eaad2000-06-30 02:46:07 +00002952 SOCKET_T fd;
2953 int family, type, proto = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002954 if (!PyArg_ParseTuple(args, "iii|i:fromfd",
2955 &fd, &family, &type, &proto))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002956 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +00002957 /* Dup the fd so it and the socket can be closed independently */
2958 fd = dup(fd);
2959 if (fd < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002960 return set_error();
2961 s = new_sockobject(fd, family, type, proto);
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002962 /* From now on, ignore SIGPIPE and let the error checking
2963 do the work. */
Guido van Rossum48a680c2001-03-02 06:34:14 +00002964#ifdef SIGPIPE
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002965 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum48a680c2001-03-02 06:34:14 +00002966#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00002967 return (PyObject *) s;
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002968}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002969
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002970PyDoc_STRVAR(fromfd_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002971"fromfd(fd, family, type[, proto]) -> socket object\n\
2972\n\
2973Create a socket object from the given file descriptor.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002974The remaining arguments are the same as for socket().");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002975
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002976#endif /* NO_DUP */
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002977
Guido van Rossum82a5c661998-07-07 20:45:43 +00002978
Guido van Rossum006bf911996-06-12 04:04:55 +00002979static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002980socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00002981{
2982 int x1, x2;
2983
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002984 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00002985 return NULL;
2986 }
2987 x2 = (int)ntohs((short)x1);
2988 return PyInt_FromLong(x2);
2989}
2990
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002991PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002992"ntohs(integer) -> integer\n\
2993\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002994Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002995
2996
Guido van Rossum006bf911996-06-12 04:04:55 +00002997static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002998socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00002999{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003000 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003001
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003002 if (PyInt_Check(arg)) {
3003 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003004 if (x == (unsigned long) -1 && PyErr_Occurred())
3005 return NULL;
Guido van Rossum006bf911996-06-12 04:04:55 +00003006 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003007 else if (PyLong_Check(arg)) {
3008 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003009 if (x == (unsigned long) -1 && PyErr_Occurred())
3010 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003011#if SIZEOF_LONG > 4
3012 {
3013 unsigned long y;
3014 /* only want the trailing 32 bits */
3015 y = x & 0xFFFFFFFFUL;
3016 if (y ^ x)
3017 return PyErr_Format(PyExc_OverflowError,
3018 "long int larger than 32 bits");
3019 x = y;
Tim Peters58141872002-08-06 22:25:02 +00003020 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003021#endif
3022 }
3023 else
Tim Peters58141872002-08-06 22:25:02 +00003024 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003025 "expected int/long, %s found",
3026 arg->ob_type->tp_name);
3027 if (x == (unsigned long) -1 && PyErr_Occurred())
3028 return NULL;
3029 return PyInt_FromLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003030}
3031
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003032PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003033"ntohl(integer) -> integer\n\
3034\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003035Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003036
3037
Guido van Rossum006bf911996-06-12 04:04:55 +00003038static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003039socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003040{
Hye-Shik Changa7b673f2003-12-17 09:40:23 +00003041 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00003042
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003043 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00003044 return NULL;
3045 }
3046 x2 = (int)htons((short)x1);
3047 return PyInt_FromLong(x2);
3048}
3049
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003050PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003051"htons(integer) -> integer\n\
3052\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003053Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003054
3055
Guido van Rossum006bf911996-06-12 04:04:55 +00003056static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003057socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003058{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003059 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003060
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003061 if (PyInt_Check(arg)) {
3062 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003063 if (x == (unsigned long) -1 && PyErr_Occurred())
3064 return NULL;
Guido van Rossum006bf911996-06-12 04:04:55 +00003065 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003066 else if (PyLong_Check(arg)) {
3067 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003068 if (x == (unsigned long) -1 && PyErr_Occurred())
3069 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003070#if SIZEOF_LONG > 4
3071 {
3072 unsigned long y;
3073 /* only want the trailing 32 bits */
3074 y = x & 0xFFFFFFFFUL;
3075 if (y ^ x)
3076 return PyErr_Format(PyExc_OverflowError,
3077 "long int larger than 32 bits");
3078 x = y;
Tim Peters58141872002-08-06 22:25:02 +00003079 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003080#endif
3081 }
3082 else
Tim Peters58141872002-08-06 22:25:02 +00003083 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003084 "expected int/long, %s found",
3085 arg->ob_type->tp_name);
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003086 return PyInt_FromLong(htonl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003087}
3088
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003089PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003090"htonl(integer) -> integer\n\
3091\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003092Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003093
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003094/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003095
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003096PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003097"inet_aton(string) -> packed 32-bit IP representation\n\
3098\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003099Convert 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 +00003100binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003101
3102static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003103socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003104{
Guido van Rossuma2e48551999-09-09 15:42:59 +00003105#ifndef INADDR_NONE
3106#define INADDR_NONE (-1)
3107#endif
Neal Norwitz88f115b2003-02-13 02:15:42 +00003108#ifdef HAVE_INET_ATON
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003109 struct in_addr buf;
Tim Peters1df9fdd2003-02-13 03:13:40 +00003110#else
3111 /* Have to use inet_addr() instead */
3112 unsigned long packed_addr;
3113#endif
3114 char *ip_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003115
Tim Peters1df9fdd2003-02-13 03:13:40 +00003116 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003117 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003118
Tim Peters1df9fdd2003-02-13 03:13:40 +00003119
3120#ifdef HAVE_INET_ATON
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003121 if (inet_aton(ip_addr, &buf))
3122 return PyString_FromStringAndSize((char *)(&buf),
3123 sizeof(buf));
3124
3125 PyErr_SetString(socket_error,
3126 "illegal IP address string passed to inet_aton");
3127 return NULL;
3128
Tim Peters1df9fdd2003-02-13 03:13:40 +00003129#else /* ! HAVE_INET_ATON */
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003130 /* XXX Problem here: inet_aton('255.255.255.255') raises
3131 an exception while it should be a valid address. */
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003132 packed_addr = inet_addr(ip_addr);
3133
3134 if (packed_addr == INADDR_NONE) { /* invalid address */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003135 PyErr_SetString(socket_error,
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003136 "illegal IP address string passed to inet_aton");
3137 return NULL;
3138 }
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003139 return PyString_FromStringAndSize((char *) &packed_addr,
3140 sizeof(packed_addr));
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003141#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003142}
3143
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003144PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00003145"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003146\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003147Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003148
3149static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003150socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003151{
3152 char *packed_str;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003153 int addr_len;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003154 struct in_addr packed_addr;
3155
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003156 if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) {
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003157 return NULL;
3158 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00003159
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003160 if (addr_len != sizeof(packed_addr)) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003161 PyErr_SetString(socket_error,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003162 "packed IP wrong length for inet_ntoa");
3163 return NULL;
3164 }
3165
3166 memcpy(&packed_addr, packed_str, addr_len);
3167
3168 return PyString_FromString(inet_ntoa(packed_addr));
3169}
Guido van Rossum82a5c661998-07-07 20:45:43 +00003170
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003171#ifdef HAVE_INET_PTON
3172
3173PyDoc_STRVAR(inet_pton_doc,
3174"inet_pton(af, ip) -> packed IP address string\n\
3175\n\
3176Convert an IP address from string format to a packed string suitable\n\
3177for use with low-level network functions.");
3178
3179static PyObject *
3180socket_inet_pton(PyObject *self, PyObject *args)
3181{
3182 int af;
3183 char* ip;
3184 int retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003185#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003186 char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003187#else
3188 char packed[sizeof(struct in_addr)];
3189#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003190 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
3191 return NULL;
3192 }
3193
Martin v. Löwis04697e82004-06-02 12:35:29 +00003194#if !defined(ENABLE_IPV6) && defined(AF_INET6)
Martin v. Löwis10649092003-08-05 06:25:06 +00003195 if(af == AF_INET6) {
3196 PyErr_SetString(socket_error,
3197 "can't use AF_INET6, IPv6 is disabled");
3198 return NULL;
3199 }
3200#endif
3201
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003202 retval = inet_pton(af, ip, packed);
3203 if (retval < 0) {
3204 PyErr_SetFromErrno(socket_error);
3205 return NULL;
3206 } else if (retval == 0) {
3207 PyErr_SetString(socket_error,
3208 "illegal IP address string passed to inet_pton");
3209 return NULL;
3210 } else if (af == AF_INET) {
3211 return PyString_FromStringAndSize(packed,
3212 sizeof(struct in_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003213#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003214 } else if (af == AF_INET6) {
3215 return PyString_FromStringAndSize(packed,
3216 sizeof(struct in6_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003217#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003218 } else {
3219 PyErr_SetString(socket_error, "unknown address family");
3220 return NULL;
3221 }
3222}
3223
3224PyDoc_STRVAR(inet_ntop_doc,
3225"inet_ntop(af, packed_ip) -> string formatted IP address\n\
3226\n\
3227Convert a packed IP address of the given family to string format.");
3228
3229static PyObject *
3230socket_inet_ntop(PyObject *self, PyObject *args)
3231{
3232 int af;
3233 char* packed;
3234 int len;
3235 const char* retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003236#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003237 char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003238#else
3239 char ip[INET_ADDRSTRLEN + 1];
3240#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003241
3242 /* Guarantee NUL-termination for PyString_FromString() below */
3243 memset((void *) &ip[0], '\0', sizeof(ip) + 1);
3244
3245 if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) {
3246 return NULL;
3247 }
3248
3249 if (af == AF_INET) {
3250 if (len != sizeof(struct in_addr)) {
3251 PyErr_SetString(PyExc_ValueError,
3252 "invalid length of packed IP address string");
3253 return NULL;
3254 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003255#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003256 } else if (af == AF_INET6) {
3257 if (len != sizeof(struct in6_addr)) {
3258 PyErr_SetString(PyExc_ValueError,
3259 "invalid length of packed IP address string");
3260 return NULL;
3261 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003262#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003263 } else {
3264 PyErr_Format(PyExc_ValueError,
3265 "unknown address family %d", af);
3266 return NULL;
3267 }
3268
3269 retval = inet_ntop(af, packed, ip, sizeof(ip));
3270 if (!retval) {
3271 PyErr_SetFromErrno(socket_error);
3272 return NULL;
3273 } else {
3274 return PyString_FromString(retval);
3275 }
3276
3277 /* NOTREACHED */
3278 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
3279 return NULL;
3280}
3281
3282#endif /* HAVE_INET_PTON */
3283
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003284/* Python interface to getaddrinfo(host, port). */
3285
3286/*ARGSUSED*/
3287static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003288socket_getaddrinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003289{
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003290 struct addrinfo hints, *res;
3291 struct addrinfo *res0 = NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003292 PyObject *hobj = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003293 PyObject *pobj = (PyObject *)NULL;
Martin v. Löwisf65b1a12001-08-12 09:28:40 +00003294 char pbuf[30];
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003295 char *hptr, *pptr;
3296 int family, socktype, protocol, flags;
3297 int error;
3298 PyObject *all = (PyObject *)NULL;
3299 PyObject *single = (PyObject *)NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003300 PyObject *idna = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003301
3302 family = socktype = protocol = flags = 0;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003303 family = AF_UNSPEC;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003304 if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo",
3305 &hobj, &pobj, &family, &socktype,
3306 &protocol, &flags)) {
3307 return NULL;
3308 }
3309 if (hobj == Py_None) {
3310 hptr = NULL;
3311 } else if (PyUnicode_Check(hobj)) {
3312 idna = PyObject_CallMethod(hobj, "encode", "s", "idna");
3313 if (!idna)
3314 return NULL;
3315 hptr = PyString_AsString(idna);
3316 } else if (PyString_Check(hobj)) {
3317 hptr = PyString_AsString(hobj);
3318 } else {
3319 PyErr_SetString(PyExc_TypeError,
3320 "getaddrinfo() argument 1 must be string or None");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003321 return NULL;
3322 }
3323 if (PyInt_Check(pobj)) {
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003324 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003325 pptr = pbuf;
3326 } else if (PyString_Check(pobj)) {
3327 pptr = PyString_AsString(pobj);
3328 } else if (pobj == Py_None) {
3329 pptr = (char *)NULL;
3330 } else {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003331 PyErr_SetString(socket_error, "Int or String expected");
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003332 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003333 }
3334 memset(&hints, 0, sizeof(hints));
3335 hints.ai_family = family;
3336 hints.ai_socktype = socktype;
3337 hints.ai_protocol = protocol;
3338 hints.ai_flags = flags;
Just van Rossum1040d2c2003-05-09 07:53:18 +00003339 Py_BEGIN_ALLOW_THREADS
3340 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003341 error = getaddrinfo(hptr, pptr, &hints, &res0);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003342 Py_END_ALLOW_THREADS
3343 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003344 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003345 set_gaierror(error);
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003346 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003347 }
3348
3349 if ((all = PyList_New(0)) == NULL)
3350 goto err;
3351 for (res = res0; res; res = res->ai_next) {
Guido van Rossum716aac02001-10-12 18:59:27 +00003352 PyObject *addr =
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00003353 makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
Guido van Rossum716aac02001-10-12 18:59:27 +00003354 if (addr == NULL)
3355 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003356 single = Py_BuildValue("iiisO", res->ai_family,
3357 res->ai_socktype, res->ai_protocol,
3358 res->ai_canonname ? res->ai_canonname : "",
Guido van Rossum716aac02001-10-12 18:59:27 +00003359 addr);
3360 Py_DECREF(addr);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003361 if (single == NULL)
3362 goto err;
3363
3364 if (PyList_Append(all, single))
3365 goto err;
3366 Py_XDECREF(single);
3367 }
Martin v. Löwis2548c732003-04-18 10:39:54 +00003368 Py_XDECREF(idna);
Neal Norwitz90128ba2002-08-09 03:37:42 +00003369 if (res0)
3370 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003371 return all;
3372 err:
3373 Py_XDECREF(single);
3374 Py_XDECREF(all);
Martin v. Löwis2548c732003-04-18 10:39:54 +00003375 Py_XDECREF(idna);
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003376 if (res0)
3377 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003378 return (PyObject *)NULL;
3379}
3380
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003381PyDoc_STRVAR(getaddrinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003382"getaddrinfo(host, port [, family, socktype, proto, flags])\n\
3383 -> list of (family, socktype, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003384\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003385Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003386
3387/* Python interface to getnameinfo(sa, flags). */
3388
3389/*ARGSUSED*/
3390static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003391socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003392{
3393 PyObject *sa = (PyObject *)NULL;
3394 int flags;
3395 char *hostp;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003396 int port, flowinfo, scope_id;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003397 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
3398 struct addrinfo hints, *res = NULL;
3399 int error;
3400 PyObject *ret = (PyObject *)NULL;
3401
3402 flags = flowinfo = scope_id = 0;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003403 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003404 return NULL;
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003405 if (!PyArg_ParseTuple(sa, "si|ii",
3406 &hostp, &port, &flowinfo, &scope_id))
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003407 return NULL;
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003408 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003409 memset(&hints, 0, sizeof(hints));
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003410 hints.ai_family = AF_UNSPEC;
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003411 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Just van Rossum1040d2c2003-05-09 07:53:18 +00003412 Py_BEGIN_ALLOW_THREADS
3413 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003414 error = getaddrinfo(hostp, pbuf, &hints, &res);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003415 Py_END_ALLOW_THREADS
3416 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003417 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003418 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003419 goto fail;
3420 }
3421 if (res->ai_next) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003422 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003423 "sockaddr resolved to multiple addresses");
3424 goto fail;
3425 }
3426 switch (res->ai_family) {
3427 case AF_INET:
3428 {
3429 char *t1;
3430 int t2;
3431 if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003432 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003433 "IPv4 sockaddr must be 2 tuple");
3434 goto fail;
3435 }
3436 break;
3437 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003438#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003439 case AF_INET6:
3440 {
3441 struct sockaddr_in6 *sin6;
3442 sin6 = (struct sockaddr_in6 *)res->ai_addr;
3443 sin6->sin6_flowinfo = flowinfo;
3444 sin6->sin6_scope_id = scope_id;
3445 break;
3446 }
3447#endif
3448 }
3449 error = getnameinfo(res->ai_addr, res->ai_addrlen,
3450 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
3451 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003452 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003453 goto fail;
3454 }
3455 ret = Py_BuildValue("ss", hbuf, pbuf);
3456
3457fail:
3458 if (res)
3459 freeaddrinfo(res);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003460 return ret;
3461}
3462
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003463PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003464"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003465\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003466Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003467
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003468
3469/* Python API to getting and setting the default timeout value. */
3470
3471static PyObject *
3472socket_getdefaulttimeout(PyObject *self)
3473{
3474 if (defaulttimeout < 0.0) {
3475 Py_INCREF(Py_None);
3476 return Py_None;
3477 }
3478 else
3479 return PyFloat_FromDouble(defaulttimeout);
3480}
3481
3482PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003483"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003484\n\
3485Returns the default timeout in floating seconds for new socket objects.\n\
3486A value of None indicates that new socket objects have no timeout.\n\
3487When the socket module is first imported, the default is None.");
3488
3489static PyObject *
3490socket_setdefaulttimeout(PyObject *self, PyObject *arg)
3491{
3492 double timeout;
3493
3494 if (arg == Py_None)
3495 timeout = -1.0;
3496 else {
3497 timeout = PyFloat_AsDouble(arg);
3498 if (timeout < 0.0) {
3499 if (!PyErr_Occurred())
3500 PyErr_SetString(PyExc_ValueError,
3501 "Timeout value out of range");
3502 return NULL;
3503 }
3504 }
3505
3506 defaulttimeout = timeout;
3507
3508 Py_INCREF(Py_None);
3509 return Py_None;
3510}
3511
3512PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003513"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003514\n\
3515Set the default timeout in floating seconds for new socket objects.\n\
3516A value of None indicates that new socket objects have no timeout.\n\
3517When the socket module is first imported, the default is None.");
3518
3519
Guido van Rossum30a685f1991-06-27 15:51:29 +00003520/* List of functions exported by this module. */
3521
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003522static PyMethodDef socket_methods[] = {
3523 {"gethostbyname", socket_gethostbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003524 METH_VARARGS, gethostbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003525 {"gethostbyname_ex", socket_gethostbyname_ex,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003526 METH_VARARGS, ghbn_ex_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003527 {"gethostbyaddr", socket_gethostbyaddr,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003528 METH_VARARGS, gethostbyaddr_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003529 {"gethostname", socket_gethostname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003530 METH_VARARGS, gethostname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003531 {"getservbyname", socket_getservbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003532 METH_VARARGS, getservbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003533 {"getprotobyname", socket_getprotobyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003534 METH_VARARGS,getprotobyname_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003535#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003536 {"fromfd", socket_fromfd,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003537 METH_VARARGS, fromfd_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003538#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003539 {"ntohs", socket_ntohs,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003540 METH_VARARGS, ntohs_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003541 {"ntohl", socket_ntohl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003542 METH_O, ntohl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003543 {"htons", socket_htons,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003544 METH_VARARGS, htons_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003545 {"htonl", socket_htonl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003546 METH_O, htonl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003547 {"inet_aton", socket_inet_aton,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003548 METH_VARARGS, inet_aton_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003549 {"inet_ntoa", socket_inet_ntoa,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003550 METH_VARARGS, inet_ntoa_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003551#ifdef HAVE_INET_PTON
3552 {"inet_pton", socket_inet_pton,
3553 METH_VARARGS, inet_pton_doc},
3554 {"inet_ntop", socket_inet_ntop,
3555 METH_VARARGS, inet_ntop_doc},
3556#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003557 {"getaddrinfo", socket_getaddrinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003558 METH_VARARGS, getaddrinfo_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003559 {"getnameinfo", socket_getnameinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003560 METH_VARARGS, getnameinfo_doc},
Guido van Rossum1693ba82002-07-18 21:11:26 +00003561 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003562 METH_NOARGS, getdefaulttimeout_doc},
3563 {"setdefaulttimeout", socket_setdefaulttimeout,
3564 METH_O, setdefaulttimeout_doc},
Guido van Rossum30a685f1991-06-27 15:51:29 +00003565 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003566};
3567
Guido van Rossum30a685f1991-06-27 15:51:29 +00003568
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003569#ifdef RISCOS
3570#define OS_INIT_DEFINED
Guido van Rossumbe32c891996-06-20 16:25:29 +00003571
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003572static int
3573os_init(void)
3574{
3575 _kernel_swi_regs r;
3576
3577 r.r[0] = 0;
3578 _kernel_swi(0x43380, &r, &r);
3579 taskwindow = r.r[0];
3580
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003581 return 1;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003582}
3583
3584#endif /* RISCOS */
3585
3586
3587#ifdef MS_WINDOWS
3588#define OS_INIT_DEFINED
3589
3590/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003591
3592static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003593os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00003594{
3595 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00003596}
3597
3598static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003599os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00003600{
3601 WSADATA WSAData;
3602 int ret;
3603 char buf[100];
3604 ret = WSAStartup(0x0101, &WSAData);
3605 switch (ret) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003606 case 0: /* No error */
Mark Hammond14350ab2003-07-09 04:57:46 +00003607 Py_AtExit(os_cleanup);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003608 return 1; /* Success */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003609 case WSASYSNOTREADY:
3610 PyErr_SetString(PyExc_ImportError,
3611 "WSAStartup failed: network not ready");
3612 break;
3613 case WSAVERNOTSUPPORTED:
3614 case WSAEINVAL:
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003615 PyErr_SetString(
3616 PyExc_ImportError,
3617 "WSAStartup failed: requested version not supported");
Guido van Rossumbe32c891996-06-20 16:25:29 +00003618 break;
3619 default:
Tim Peters885d4572001-11-28 20:27:42 +00003620 PyOS_snprintf(buf, sizeof(buf),
3621 "WSAStartup failed: error code %d", ret);
Guido van Rossumbe32c891996-06-20 16:25:29 +00003622 PyErr_SetString(PyExc_ImportError, buf);
3623 break;
3624 }
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003625 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003626}
3627
Guido van Rossum8d665e61996-06-26 18:22:49 +00003628#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003629
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003630
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003631#ifdef PYOS_OS2
3632#define OS_INIT_DEFINED
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003633
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003634/* Additional initialization for OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003635
3636static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003637os_init(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003638{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003639#ifndef PYCC_GCC
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003640 char reason[64];
3641 int rc = sock_init();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003642
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003643 if (rc == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003644 return 1; /* Success */
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003645 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003646
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003647 PyOS_snprintf(reason, sizeof(reason),
3648 "OS/2 TCP/IP Error# %d", sock_errno());
3649 PyErr_SetString(PyExc_ImportError, reason);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003650
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003651 return 0; /* Failure */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00003652#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003653 /* No need to initialise sockets with GCC/EMX */
3654 return 1; /* Success */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00003655#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003656}
3657
3658#endif /* PYOS_OS2 */
3659
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003660
3661#ifndef OS_INIT_DEFINED
3662static int
3663os_init(void)
3664{
3665 return 1; /* Success */
3666}
3667#endif
3668
3669
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003670/* C API table - always add new things to the end for binary
3671 compatibility. */
3672static
3673PySocketModule_APIObject PySocketModuleAPI =
3674{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003675 &sock_type,
Brett Cannon06c34792004-03-23 23:16:54 +00003676 NULL
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003677};
3678
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003679
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003680/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003681
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003682 This module is actually called "_socket", and there's a wrapper
3683 "socket.py" which implements some additional functionality. On some
3684 platforms (e.g. Windows and OS/2), socket.py also implements a
3685 wrapper for the socket type that provides missing functionality such
3686 as makefile(), dup() and fromfd(). The import of "_socket" may fail
3687 with an ImportError exception if os-specific initialization fails.
3688 On Windows, this does WINSOCK initialization. When WINSOCK is
3689 initialized succesfully, a call to WSACleanup() is scheduled to be
3690 made at exit time.
3691*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003692
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003693PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003694"Implementation module for socket operations.\n\
3695\n\
3696See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003697
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003698PyMODINIT_FUNC
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003699init_socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003700{
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003701 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00003702
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003703 if (!os_init())
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003704 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003705
3706 sock_type.ob_type = &PyType_Type;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003707 m = Py_InitModule3(PySocket_MODULE_NAME,
3708 socket_methods,
3709 socket_doc);
3710
3711 socket_error = PyErr_NewException("socket.error", NULL, NULL);
3712 if (socket_error == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003713 return;
Brett Cannon06c34792004-03-23 23:16:54 +00003714 PySocketModuleAPI.error = socket_error;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003715 Py_INCREF(socket_error);
3716 PyModule_AddObject(m, "error", socket_error);
3717 socket_herror = PyErr_NewException("socket.herror",
3718 socket_error, NULL);
3719 if (socket_herror == NULL)
3720 return;
3721 Py_INCREF(socket_herror);
3722 PyModule_AddObject(m, "herror", socket_herror);
3723 socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003724 NULL);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003725 if (socket_gaierror == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003726 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003727 Py_INCREF(socket_gaierror);
3728 PyModule_AddObject(m, "gaierror", socket_gaierror);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00003729 socket_timeout = PyErr_NewException("socket.timeout",
3730 socket_error, NULL);
3731 if (socket_timeout == NULL)
3732 return;
3733 Py_INCREF(socket_timeout);
3734 PyModule_AddObject(m, "timeout", socket_timeout);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003735 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00003736 if (PyModule_AddObject(m, "SocketType",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003737 (PyObject *)&sock_type) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003738 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003739 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00003740 if (PyModule_AddObject(m, "socket",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003741 (PyObject *)&sock_type) != 0)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003742 return;
Guido van Rossum09be4091999-08-09 14:40:40 +00003743
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003744#ifdef ENABLE_IPV6
3745 has_ipv6 = Py_True;
3746#else
3747 has_ipv6 = Py_False;
3748#endif
3749 Py_INCREF(has_ipv6);
3750 PyModule_AddObject(m, "has_ipv6", has_ipv6);
3751
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003752 /* Export C API */
Fred Drake4baedc12002-04-01 14:53:37 +00003753 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003754 PyCObject_FromVoidPtr((void *)&PySocketModuleAPI, NULL)
3755 ) != 0)
3756 return;
3757
Guido van Rossum09be4091999-08-09 14:40:40 +00003758 /* Address families (we only support AF_INET and AF_UNIX) */
3759#ifdef AF_UNSPEC
Fred Drake4baedc12002-04-01 14:53:37 +00003760 PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00003761#endif
Fred Drake4baedc12002-04-01 14:53:37 +00003762 PyModule_AddIntConstant(m, "AF_INET", AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003763#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00003764 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003765#endif /* AF_INET6 */
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00003766#if defined(AF_UNIX)
Fred Drake4baedc12002-04-01 14:53:37 +00003767 PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003768#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00003769#ifdef AF_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00003770 /* Amateur Radio AX.25 */
3771 PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003772#endif
3773#ifdef AF_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00003774 PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00003775#endif
3776#ifdef AF_APPLETALK
Fred Drake4baedc12002-04-01 14:53:37 +00003777 /* Appletalk DDP */
3778 PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00003779#endif
3780#ifdef AF_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00003781 /* Amateur radio NetROM */
3782 PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00003783#endif
3784#ifdef AF_BRIDGE
Fred Drake4baedc12002-04-01 14:53:37 +00003785 /* Multiprotocol bridge */
3786 PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003787#endif
3788#ifdef AF_AAL5
Fred Drake4baedc12002-04-01 14:53:37 +00003789 /* Reserved for Werner's ATM */
3790 PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00003791#endif
3792#ifdef AF_X25
Fred Drake4baedc12002-04-01 14:53:37 +00003793 /* Reserved for X.25 project */
3794 PyModule_AddIntConstant(m, "AF_X25", AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003795#endif
3796#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00003797 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00003798#endif
3799#ifdef AF_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00003800 /* Amateur Radio X.25 PLP */
3801 PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003802#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00003803
Hye-Shik Chang81268602004-02-02 06:05:24 +00003804#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00003805 PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH);
3806 PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP);
Hye-Shik Chang81268602004-02-02 06:05:24 +00003807#if !defined(__FreeBSD__)
Martin v. Löwis12af0482004-01-31 12:34:17 +00003808 PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO);
Hye-Shik Chang81268602004-02-02 06:05:24 +00003809#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00003810 PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM);
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00003811 PyModule_AddObject(m, "BDADDR_ANY", Py_BuildValue("s", "00:00:00:00:00:00"));
3812 PyModule_AddObject(m, "BDADDR_LOCAL", Py_BuildValue("s", "00:00:00:FF:FF:FF"));
Martin v. Löwis12af0482004-01-31 12:34:17 +00003813#endif
3814
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00003815#ifdef HAVE_NETPACKET_PACKET_H
Fred Drake4baedc12002-04-01 14:53:37 +00003816 PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET);
3817 PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET);
3818 PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST);
3819 PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST);
3820 PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST);
3821 PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST);
3822 PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING);
3823 PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK);
3824 PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00003825#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003826
3827 /* Socket types */
Fred Drake4baedc12002-04-01 14:53:37 +00003828 PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
3829 PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00003830#ifndef __BEOS__
3831/* We have incomplete socket support. */
Fred Drake4baedc12002-04-01 14:53:37 +00003832 PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
3833 PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003834#if defined(SOCK_RDM)
Fred Drake4baedc12002-04-01 14:53:37 +00003835 PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00003836#endif
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003837#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003838
3839#ifdef SO_DEBUG
Fred Drake4baedc12002-04-01 14:53:37 +00003840 PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003841#endif
3842#ifdef SO_ACCEPTCONN
Fred Drake4baedc12002-04-01 14:53:37 +00003843 PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003844#endif
3845#ifdef SO_REUSEADDR
Fred Drake4baedc12002-04-01 14:53:37 +00003846 PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003847#endif
3848#ifdef SO_KEEPALIVE
Fred Drake4baedc12002-04-01 14:53:37 +00003849 PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003850#endif
3851#ifdef SO_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00003852 PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003853#endif
3854#ifdef SO_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00003855 PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003856#endif
3857#ifdef SO_USELOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00003858 PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003859#endif
3860#ifdef SO_LINGER
Fred Drake4baedc12002-04-01 14:53:37 +00003861 PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003862#endif
3863#ifdef SO_OOBINLINE
Fred Drake4baedc12002-04-01 14:53:37 +00003864 PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003865#endif
3866#ifdef SO_REUSEPORT
Fred Drake4baedc12002-04-01 14:53:37 +00003867 PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003868#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003869#ifdef SO_SNDBUF
Fred Drake4baedc12002-04-01 14:53:37 +00003870 PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003871#endif
3872#ifdef SO_RCVBUF
Fred Drake4baedc12002-04-01 14:53:37 +00003873 PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003874#endif
3875#ifdef SO_SNDLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00003876 PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003877#endif
3878#ifdef SO_RCVLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00003879 PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003880#endif
3881#ifdef SO_SNDTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00003882 PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003883#endif
3884#ifdef SO_RCVTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00003885 PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003886#endif
3887#ifdef SO_ERROR
Fred Drake4baedc12002-04-01 14:53:37 +00003888 PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003889#endif
3890#ifdef SO_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00003891 PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003892#endif
3893
3894 /* Maximum number of connections for "listen" */
3895#ifdef SOMAXCONN
Fred Drake4baedc12002-04-01 14:53:37 +00003896 PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003897#else
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003898 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003899#endif
3900
3901 /* Flags for send, recv */
3902#ifdef MSG_OOB
Fred Drake4baedc12002-04-01 14:53:37 +00003903 PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003904#endif
3905#ifdef MSG_PEEK
Fred Drake4baedc12002-04-01 14:53:37 +00003906 PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003907#endif
3908#ifdef MSG_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00003909 PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003910#endif
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00003911#ifdef MSG_DONTWAIT
Fred Drake4baedc12002-04-01 14:53:37 +00003912 PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00003913#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003914#ifdef MSG_EOR
Fred Drake4baedc12002-04-01 14:53:37 +00003915 PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003916#endif
3917#ifdef MSG_TRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00003918 PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003919#endif
3920#ifdef MSG_CTRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00003921 PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003922#endif
3923#ifdef MSG_WAITALL
Fred Drake4baedc12002-04-01 14:53:37 +00003924 PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003925#endif
3926#ifdef MSG_BTAG
Fred Drake4baedc12002-04-01 14:53:37 +00003927 PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003928#endif
3929#ifdef MSG_ETAG
Fred Drake4baedc12002-04-01 14:53:37 +00003930 PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003931#endif
3932
3933 /* Protocol level and numbers, usable for [gs]etsockopt */
3934#ifdef SOL_SOCKET
Fred Drake4baedc12002-04-01 14:53:37 +00003935 PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003936#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003937#ifdef SOL_IP
Fred Drake4baedc12002-04-01 14:53:37 +00003938 PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003939#else
Fred Drake4baedc12002-04-01 14:53:37 +00003940 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00003941#endif
3942#ifdef SOL_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00003943 PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00003944#endif
3945#ifdef SOL_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00003946 PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003947#endif
3948#ifdef SOL_ATALK
Fred Drake4baedc12002-04-01 14:53:37 +00003949 PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00003950#endif
3951#ifdef SOL_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00003952 PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00003953#endif
3954#ifdef SOL_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00003955 PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003956#endif
3957#ifdef SOL_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00003958 PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003959#else
Fred Drake4baedc12002-04-01 14:53:37 +00003960 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00003961#endif
3962#ifdef SOL_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00003963 PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003964#else
Fred Drake4baedc12002-04-01 14:53:37 +00003965 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00003966#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003967#ifdef IPPROTO_IP
Fred Drake4baedc12002-04-01 14:53:37 +00003968 PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00003969#else
Fred Drake4baedc12002-04-01 14:53:37 +00003970 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003971#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003972#ifdef IPPROTO_HOPOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003973 PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003974#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003975#ifdef IPPROTO_ICMP
Fred Drake4baedc12002-04-01 14:53:37 +00003976 PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00003977#else
Fred Drake4baedc12002-04-01 14:53:37 +00003978 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003979#endif
3980#ifdef IPPROTO_IGMP
Fred Drake4baedc12002-04-01 14:53:37 +00003981 PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003982#endif
3983#ifdef IPPROTO_GGP
Fred Drake4baedc12002-04-01 14:53:37 +00003984 PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003985#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003986#ifdef IPPROTO_IPV4
Fred Drake4baedc12002-04-01 14:53:37 +00003987 PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003988#endif
Martin v. Löwisa0f17342003-10-03 13:56:20 +00003989#ifdef IPPROTO_IPV6
3990 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
3991#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003992#ifdef IPPROTO_IPIP
Fred Drake4baedc12002-04-01 14:53:37 +00003993 PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003994#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003995#ifdef IPPROTO_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00003996 PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00003997#else
Fred Drake4baedc12002-04-01 14:53:37 +00003998 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003999#endif
4000#ifdef IPPROTO_EGP
Fred Drake4baedc12002-04-01 14:53:37 +00004001 PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004002#endif
4003#ifdef IPPROTO_PUP
Fred Drake4baedc12002-04-01 14:53:37 +00004004 PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004005#endif
4006#ifdef IPPROTO_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00004007 PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00004008#else
Fred Drake4baedc12002-04-01 14:53:37 +00004009 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004010#endif
4011#ifdef IPPROTO_IDP
Fred Drake4baedc12002-04-01 14:53:37 +00004012 PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004013#endif
4014#ifdef IPPROTO_HELLO
Fred Drake4baedc12002-04-01 14:53:37 +00004015 PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004016#endif
4017#ifdef IPPROTO_ND
Fred Drake4baedc12002-04-01 14:53:37 +00004018 PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004019#endif
4020#ifdef IPPROTO_TP
Fred Drake4baedc12002-04-01 14:53:37 +00004021 PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004022#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004023#ifdef IPPROTO_IPV6
Fred Drake4baedc12002-04-01 14:53:37 +00004024 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004025#endif
4026#ifdef IPPROTO_ROUTING
Fred Drake4baedc12002-04-01 14:53:37 +00004027 PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004028#endif
4029#ifdef IPPROTO_FRAGMENT
Fred Drake4baedc12002-04-01 14:53:37 +00004030 PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004031#endif
4032#ifdef IPPROTO_RSVP
Fred Drake4baedc12002-04-01 14:53:37 +00004033 PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004034#endif
4035#ifdef IPPROTO_GRE
Fred Drake4baedc12002-04-01 14:53:37 +00004036 PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004037#endif
4038#ifdef IPPROTO_ESP
Fred Drake4baedc12002-04-01 14:53:37 +00004039 PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004040#endif
4041#ifdef IPPROTO_AH
Fred Drake4baedc12002-04-01 14:53:37 +00004042 PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004043#endif
4044#ifdef IPPROTO_MOBILE
Fred Drake4baedc12002-04-01 14:53:37 +00004045 PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004046#endif
4047#ifdef IPPROTO_ICMPV6
Fred Drake4baedc12002-04-01 14:53:37 +00004048 PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004049#endif
4050#ifdef IPPROTO_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00004051 PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004052#endif
4053#ifdef IPPROTO_DSTOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004054 PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004055#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004056#ifdef IPPROTO_XTP
Fred Drake4baedc12002-04-01 14:53:37 +00004057 PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004058#endif
4059#ifdef IPPROTO_EON
Fred Drake4baedc12002-04-01 14:53:37 +00004060 PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004061#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004062#ifdef IPPROTO_PIM
Fred Drake4baedc12002-04-01 14:53:37 +00004063 PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004064#endif
4065#ifdef IPPROTO_IPCOMP
Fred Drake4baedc12002-04-01 14:53:37 +00004066 PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004067#endif
4068#ifdef IPPROTO_VRRP
Fred Drake4baedc12002-04-01 14:53:37 +00004069 PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004070#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004071#ifdef IPPROTO_BIP
Fred Drake4baedc12002-04-01 14:53:37 +00004072 PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004073#endif
4074/**/
4075#ifdef IPPROTO_RAW
Fred Drake4baedc12002-04-01 14:53:37 +00004076 PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00004077#else
Fred Drake4baedc12002-04-01 14:53:37 +00004078 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004079#endif
4080#ifdef IPPROTO_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00004081 PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004082#endif
4083
4084 /* Some port configuration */
4085#ifdef IPPORT_RESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00004086 PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004087#else
Fred Drake4baedc12002-04-01 14:53:37 +00004088 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004089#endif
4090#ifdef IPPORT_USERRESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00004091 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004092#else
Fred Drake4baedc12002-04-01 14:53:37 +00004093 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004094#endif
4095
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004096 /* Some reserved IP v.4 addresses */
4097#ifdef INADDR_ANY
Fred Drake4baedc12002-04-01 14:53:37 +00004098 PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004099#else
Fred Drake4baedc12002-04-01 14:53:37 +00004100 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004101#endif
4102#ifdef INADDR_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00004103 PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004104#else
Fred Drake4baedc12002-04-01 14:53:37 +00004105 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004106#endif
4107#ifdef INADDR_LOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00004108 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004109#else
Fred Drake4baedc12002-04-01 14:53:37 +00004110 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004111#endif
4112#ifdef INADDR_UNSPEC_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004113 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004114#else
Fred Drake4baedc12002-04-01 14:53:37 +00004115 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004116#endif
4117#ifdef INADDR_ALLHOSTS_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004118 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
4119 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004120#else
Fred Drake4baedc12002-04-01 14:53:37 +00004121 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004122#endif
4123#ifdef INADDR_MAX_LOCAL_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004124 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
4125 INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004126#else
Fred Drake4baedc12002-04-01 14:53:37 +00004127 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004128#endif
4129#ifdef INADDR_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00004130 PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004131#else
Fred Drake4baedc12002-04-01 14:53:37 +00004132 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004133#endif
4134
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004135 /* IPv4 [gs]etsockopt options */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004136#ifdef IP_OPTIONS
Fred Drake4baedc12002-04-01 14:53:37 +00004137 PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004138#endif
4139#ifdef IP_HDRINCL
Fred Drake4baedc12002-04-01 14:53:37 +00004140 PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004141#endif
4142#ifdef IP_TOS
Fred Drake4baedc12002-04-01 14:53:37 +00004143 PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004144#endif
4145#ifdef IP_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004146 PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004147#endif
4148#ifdef IP_RECVOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004149 PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004150#endif
4151#ifdef IP_RECVRETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004152 PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004153#endif
4154#ifdef IP_RECVDSTADDR
Fred Drake4baedc12002-04-01 14:53:37 +00004155 PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004156#endif
4157#ifdef IP_RETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004158 PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004159#endif
4160#ifdef IP_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00004161 PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004162#endif
4163#ifdef IP_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004164 PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004165#endif
4166#ifdef IP_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004167 PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004168#endif
4169#ifdef IP_ADD_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00004170 PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004171#endif
4172#ifdef IP_DROP_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00004173 PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004174#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004175#ifdef IP_DEFAULT_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004176 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
4177 IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00004178#endif
4179#ifdef IP_DEFAULT_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004180 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
4181 IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004182#endif
4183#ifdef IP_MAX_MEMBERSHIPS
Fred Drake4baedc12002-04-01 14:53:37 +00004184 PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00004185#endif
4186
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004187 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
4188#ifdef IPV6_JOIN_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004189 PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004190#endif
4191#ifdef IPV6_LEAVE_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004192 PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004193#endif
4194#ifdef IPV6_MULTICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00004195 PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004196#endif
4197#ifdef IPV6_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00004198 PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004199#endif
4200#ifdef IPV6_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004201 PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004202#endif
4203#ifdef IPV6_UNICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00004204 PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004205#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004206 /* Additional IPV6 socket options, defined in RFC 3493 */
4207#ifdef IPV6_V6ONLY
4208 PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY);
4209#endif
4210 /* Advanced IPV6 socket options, from RFC 3542 */
4211#ifdef IPV6_CHECKSUM
4212 PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM);
4213#endif
4214#ifdef IPV6_DONTFRAG
4215 PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG);
4216#endif
4217#ifdef IPV6_DSTOPTS
4218 PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS);
4219#endif
4220#ifdef IPV6_HOPLIMIT
4221 PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT);
4222#endif
4223#ifdef IPV6_HOPOPTS
4224 PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS);
4225#endif
4226#ifdef IPV6_NEXTHOP
4227 PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP);
4228#endif
4229#ifdef IPV6_PATHMTU
4230 PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU);
4231#endif
4232#ifdef IPV6_PKTINFO
4233 PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO);
4234#endif
4235#ifdef IPV6_RECVDSTOPTS
4236 PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS);
4237#endif
4238#ifdef IPV6_RECVHOPLIMIT
4239 PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT);
4240#endif
4241#ifdef IPV6_RECVHOPOPTS
4242 PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS);
4243#endif
4244#ifdef IPV6_RECVPKTINFO
4245 PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO);
4246#endif
4247#ifdef IPV6_RECVRTHDR
4248 PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR);
4249#endif
4250#ifdef IPV6_RECVTCLASS
4251 PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS);
4252#endif
4253#ifdef IPV6_RTHDR
4254 PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR);
4255#endif
4256#ifdef IPV6_RTHDRDSTOPTS
4257 PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS);
4258#endif
4259#ifdef IPV6_RTHDR_TYPE_0
4260 PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0);
4261#endif
4262#ifdef IPV6_RECVPATHMTU
4263 PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU);
4264#endif
4265#ifdef IPV6_TCLASS
4266 PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS);
4267#endif
4268#ifdef IPV6_USE_MIN_MTU
4269 PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU);
4270#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004271
Guido van Rossum09be4091999-08-09 14:40:40 +00004272 /* TCP options */
4273#ifdef TCP_NODELAY
Fred Drake4baedc12002-04-01 14:53:37 +00004274 PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00004275#endif
4276#ifdef TCP_MAXSEG
Fred Drake4baedc12002-04-01 14:53:37 +00004277 PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00004278#endif
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004279#ifdef TCP_CORK
Fred Drake4baedc12002-04-01 14:53:37 +00004280 PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004281#endif
4282#ifdef TCP_KEEPIDLE
Fred Drake4baedc12002-04-01 14:53:37 +00004283 PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004284#endif
4285#ifdef TCP_KEEPINTVL
Fred Drake4baedc12002-04-01 14:53:37 +00004286 PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004287#endif
4288#ifdef TCP_KEEPCNT
Fred Drake4baedc12002-04-01 14:53:37 +00004289 PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004290#endif
4291#ifdef TCP_SYNCNT
Fred Drake4baedc12002-04-01 14:53:37 +00004292 PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004293#endif
4294#ifdef TCP_LINGER2
Fred Drake4baedc12002-04-01 14:53:37 +00004295 PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004296#endif
4297#ifdef TCP_DEFER_ACCEPT
Fred Drake4baedc12002-04-01 14:53:37 +00004298 PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004299#endif
4300#ifdef TCP_WINDOW_CLAMP
Fred Drake4baedc12002-04-01 14:53:37 +00004301 PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004302#endif
4303#ifdef TCP_INFO
Fred Drake4baedc12002-04-01 14:53:37 +00004304 PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004305#endif
4306#ifdef TCP_QUICKACK
Fred Drake4baedc12002-04-01 14:53:37 +00004307 PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004308#endif
4309
Guido van Rossum09be4091999-08-09 14:40:40 +00004310
4311 /* IPX options */
4312#ifdef IPX_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004313 PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004314#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004315
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004316 /* get{addr,name}info parameters */
4317#ifdef EAI_ADDRFAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004318 PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004319#endif
4320#ifdef EAI_AGAIN
Fred Drake4baedc12002-04-01 14:53:37 +00004321 PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004322#endif
4323#ifdef EAI_BADFLAGS
Fred Drake4baedc12002-04-01 14:53:37 +00004324 PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004325#endif
4326#ifdef EAI_FAIL
Fred Drake4baedc12002-04-01 14:53:37 +00004327 PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004328#endif
4329#ifdef EAI_FAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004330 PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004331#endif
4332#ifdef EAI_MEMORY
Fred Drake4baedc12002-04-01 14:53:37 +00004333 PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004334#endif
4335#ifdef EAI_NODATA
Fred Drake4baedc12002-04-01 14:53:37 +00004336 PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004337#endif
4338#ifdef EAI_NONAME
Fred Drake4baedc12002-04-01 14:53:37 +00004339 PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004340#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004341#ifdef EAI_OVERFLOW
4342 PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW);
4343#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004344#ifdef EAI_SERVICE
Fred Drake4baedc12002-04-01 14:53:37 +00004345 PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004346#endif
4347#ifdef EAI_SOCKTYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004348 PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004349#endif
4350#ifdef EAI_SYSTEM
Fred Drake4baedc12002-04-01 14:53:37 +00004351 PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004352#endif
4353#ifdef EAI_BADHINTS
Fred Drake4baedc12002-04-01 14:53:37 +00004354 PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004355#endif
4356#ifdef EAI_PROTOCOL
Fred Drake4baedc12002-04-01 14:53:37 +00004357 PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004358#endif
4359#ifdef EAI_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00004360 PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004361#endif
4362#ifdef AI_PASSIVE
Fred Drake4baedc12002-04-01 14:53:37 +00004363 PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004364#endif
4365#ifdef AI_CANONNAME
Fred Drake4baedc12002-04-01 14:53:37 +00004366 PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004367#endif
4368#ifdef AI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004369 PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004370#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004371#ifdef AI_NUMERICSERV
4372 PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV);
4373#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004374#ifdef AI_MASK
Fred Drake4baedc12002-04-01 14:53:37 +00004375 PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004376#endif
4377#ifdef AI_ALL
Fred Drake4baedc12002-04-01 14:53:37 +00004378 PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004379#endif
4380#ifdef AI_V4MAPPED_CFG
Fred Drake4baedc12002-04-01 14:53:37 +00004381 PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004382#endif
4383#ifdef AI_ADDRCONFIG
Fred Drake4baedc12002-04-01 14:53:37 +00004384 PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004385#endif
4386#ifdef AI_V4MAPPED
Fred Drake4baedc12002-04-01 14:53:37 +00004387 PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004388#endif
4389#ifdef AI_DEFAULT
Fred Drake4baedc12002-04-01 14:53:37 +00004390 PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004391#endif
4392#ifdef NI_MAXHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004393 PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004394#endif
4395#ifdef NI_MAXSERV
Fred Drake4baedc12002-04-01 14:53:37 +00004396 PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004397#endif
4398#ifdef NI_NOFQDN
Fred Drake4baedc12002-04-01 14:53:37 +00004399 PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004400#endif
4401#ifdef NI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004402 PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004403#endif
4404#ifdef NI_NAMEREQD
Fred Drake4baedc12002-04-01 14:53:37 +00004405 PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004406#endif
4407#ifdef NI_NUMERICSERV
Fred Drake4baedc12002-04-01 14:53:37 +00004408 PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004409#endif
4410#ifdef NI_DGRAM
Fred Drake4baedc12002-04-01 14:53:37 +00004411 PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004412#endif
4413
Martin v. Löwis94681fc2003-11-27 19:40:22 +00004414 /* shutdown() parameters */
4415#ifdef SHUT_RD
4416 PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD);
4417#elif defined(SD_RECEIVE)
4418 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
4419#else
4420 PyModule_AddIntConstant(m, "SHUT_RD", 0);
4421#endif
4422#ifdef SHUT_WR
4423 PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR);
4424#elif defined(SD_SEND)
4425 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
4426#else
4427 PyModule_AddIntConstant(m, "SHUT_WR", 1);
4428#endif
4429#ifdef SHUT_RDWR
4430 PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR);
4431#elif defined(SD_BOTH)
4432 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
4433#else
4434 PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
4435#endif
4436
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004437 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00004438#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
4439 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004440#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004441}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004442
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004443
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004444#ifndef HAVE_INET_PTON
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004445
4446/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004447/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004448
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004449int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004450inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004451{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004452 if (af == AF_INET) {
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004453 long packed_addr;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004454 packed_addr = inet_addr(src);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004455 if (packed_addr == INADDR_NONE)
4456 return 0;
4457 memcpy(dst, &packed_addr, 4);
4458 return 1;
4459 }
4460 /* Should set errno to EAFNOSUPPORT */
4461 return -1;
4462}
4463
Martin v. Löwisc925b1532001-07-21 09:42:15 +00004464const char *
4465inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004466{
4467 if (af == AF_INET) {
4468 struct in_addr packed_addr;
4469 if (size < 16)
4470 /* Should set errno to ENOSPC. */
4471 return NULL;
4472 memcpy(&packed_addr, src, sizeof(packed_addr));
4473 return strncpy(dst, inet_ntoa(packed_addr), size);
4474 }
4475 /* Should set errno to EAFNOSUPPORT */
4476 return NULL;
4477}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004478
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004479#endif