blob: 8024006c66f6fee1d47da19c96f9329001dac38e [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 Rossum30a685f1991-06-27 15:51:29 +000022- socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd')
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000023- socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...])
Guido van Rossum27e177d1995-03-16 15:43:47 +000024- socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com')
Guido van Rossum25405c71996-12-19 16:42:52 +000025- socket.getprotobyname(protocolname) --> protocol number
Guido van Rossum27e177d1995-03-16 15:43:47 +000026- socket.getservbyname(servicename, protocolname) --> port number
Guido van Rossum4dd2a7e1991-07-01 18:51:33 +000027- socket.socket(family, type [, proto]) --> new socket object
Guido van Rossum006bf911996-06-12 04:04:55 +000028- socket.ntohs(16 bit value) --> new int object
29- socket.ntohl(32 bit value) --> new int object
30- socket.htons(16 bit value) --> new int object
31- socket.htonl(32 bit value) --> new int object
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000032- socket.getaddrinfo(host, port [, family, socktype, proto, flags])
33 --> List of (family, socktype, proto, canonname, sockaddr)
34- socket.getnameinfo(sockaddr, flags) --> (host, port)
Guido van Rossum27e177d1995-03-16 15:43:47 +000035- socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +000036- socket.inet_aton(IP address) -> 32-bit packed IP representation
37- socket.inet_ntoa(packed IP) -> IP address string
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +000038- socket.getdefaulttimeout() -> None | float
39- socket.setdefaulttimeout(None | float)
Guido van Rossum6574b3e1991-06-25 21:36:08 +000040- an Internet socket address is a pair (hostname, port)
41 where hostname can be anything recognized by gethostbyname()
42 (including the dd.dd.dd.dd notation) and port is in host byte order
43- where a hostname is returned, the dd.dd.dd.dd notation is used
Guido van Rossum27e177d1995-03-16 15:43:47 +000044- a UNIX domain socket address is a string specifying the pathname
Jeremy Hylton22308652001-02-02 03:23:09 +000045- an AF_PACKET socket address is a tuple containing a string
46 specifying the ethernet interface and an integer specifying
47 the Ethernet protocol number to be received. For example:
Jeremy Hyltondbfb6622001-02-02 19:55:17 +000048 ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple
49 specify packet-type and ha-type/addr -- these are ignored by
Jeremy Hylton22308652001-02-02 03:23:09 +000050 networking code, but accepted since they are returned by the
51 getsockname() method.
Guido van Rossum6574b3e1991-06-25 21:36:08 +000052
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000053Local naming conventions:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000054
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000055- names starting with sock_ are socket object methods
56- names starting with socket_ are module-level functions
57- names starting with PySocket are exported through socketmodule.h
Guido van Rossum30a685f1991-06-27 15:51:29 +000058
Guido van Rossum6574b3e1991-06-25 21:36:08 +000059*/
60
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000061#include "Python.h"
62
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000063/* Socket object documentation */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000064PyDoc_STRVAR(sock_doc,
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000065"socket([family[, type[, proto]]]) -> socket object\n\
66\n\
67Open a socket of the given type. The family argument specifies the\n\
68address family; it defaults to AF_INET. The type argument specifies\n\
69whether this is a stream (SOCK_STREAM, this is the default)\n\
70or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\
71specifying the default protocol. Keyword arguments are accepted.\n\
72\n\
73A socket object represents one endpoint of a network connection.\n\
74\n\
75Methods of socket objects (keyword arguments not allowed):\n\
76\n\
77accept() -- accept a connection, returning new socket and client address\n\
78bind(addr) -- bind the socket to a local address\n\
79close() -- close the socket\n\
80connect(addr) -- connect the socket to a remote address\n\
81connect_ex(addr) -- connect, return an error code instead of an exception\n\
82dup() -- return a new socket object identical to the current one [*]\n\
83fileno() -- return underlying file descriptor\n\
84getpeername() -- return remote address [*]\n\
85getsockname() -- return local address\n\
86getsockopt(level, optname[, buflen]) -- get socket options\n\
87gettimeout() -- return timeout or None\n\
88listen(n) -- start listening for incoming connections\n\
89makefile([mode, [bufsize]]) -- return a file object for the socket [*]\n\
90recv(buflen[, flags]) -- receive data\n\
91recvfrom(buflen[, flags]) -- receive data and sender's address\n\
92sendall(data[, flags]) -- send all data\n\
93send(data[, flags]) -- send data, may not send all of it\n\
94sendto(data[, flags], addr) -- send data to a given address\n\
95setblocking(0 | 1) -- set or clear the blocking I/O flag\n\
96setsockopt(level, optname, value) -- set socket options\n\
97settimeout(None | float) -- set or clear the timeout\n\
98shutdown(how) -- shut down traffic in one or both directions\n\
99\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000100 [*] not available on all platforms!");
Guido van Rossum3baaa131999-03-22 21:44:51 +0000101
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000102/* XXX This is a terrible mess of of platform-dependent preprocessor hacks.
103 I hope some day someone can clean this up please... */
104
Guido van Rossum9376b741999-09-15 22:01:40 +0000105/* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure
106 script doesn't get this right, so we hardcode some platform checks below.
107 On the other hand, not all Linux versions agree, so there the settings
108 computed by the configure script are needed! */
109
110#ifndef linux
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000111# undef HAVE_GETHOSTBYNAME_R_3_ARG
112# undef HAVE_GETHOSTBYNAME_R_5_ARG
113# undef HAVE_GETHOSTBYNAME_R_6_ARG
Guido van Rossum9376b741999-09-15 22:01:40 +0000114#endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000115
Guido van Rossum7a122991999-04-13 04:07:32 +0000116#ifndef WITH_THREAD
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000117# undef HAVE_GETHOSTBYNAME_R
Guido van Rossum7a122991999-04-13 04:07:32 +0000118#endif
119
Guido van Rossume7de2061999-03-24 17:24:33 +0000120#ifdef HAVE_GETHOSTBYNAME_R
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000121# if defined(_AIX) || defined(__osf__)
122# define HAVE_GETHOSTBYNAME_R_3_ARG
123# elif defined(__sun) || defined(__sgi)
124# define HAVE_GETHOSTBYNAME_R_5_ARG
125# elif defined(linux)
Guido van Rossum9376b741999-09-15 22:01:40 +0000126/* Rely on the configure script */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000127# else
128# undef HAVE_GETHOSTBYNAME_R
129# endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000130#endif
131
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000132#if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \
133 !defined(MS_WINDOWS)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000134# define USE_GETHOSTBYNAME_LOCK
Guido van Rossum3baaa131999-03-22 21:44:51 +0000135#endif
136
137#ifdef USE_GETHOSTBYNAME_LOCK
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000138# include "pythread.h"
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000139#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000140
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000141#if defined(PYCC_VACPP)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000142# include <types.h>
143# include <io.h>
144# include <sys/ioctl.h>
145# include <utils.h>
146# include <ctype.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000147#endif
148
149#if defined(PYOS_OS2)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000150# define INCL_DOS
151# define INCL_DOSERRORS
152# define INCL_NOPMAPI
153# include <os2.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000154#endif
155
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000156/* Generic includes */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000157#include <sys/types.h>
Guido van Rossum81194471991-07-27 21:42:02 +0000158#include <signal.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000159
Marc-André Lemburg976ade62002-02-16 18:47:07 +0000160/* Generic socket object definitions and includes */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000161#define PySocket_BUILDING_SOCKET
Marc-André Lemburgbb8b78b2002-02-16 18:44:52 +0000162#include "socketmodule.h"
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000163
164/* Addressing includes */
165
Guido van Rossum6f489d91996-06-28 20:15:15 +0000166#ifndef MS_WINDOWS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000167
168/* Non-MS WINDOWS includes */
169# include <netdb.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000170
Guido van Rossum9376b741999-09-15 22:01:40 +0000171/* Headers needed for inet_ntoa() and inet_addr() */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000172# ifdef __BEOS__
173# include <net/netdb.h>
174# elif defined(PYOS_OS2) && defined(PYCC_VACPP)
175# include <netdb.h>
Tim Peters603c6832001-11-05 02:45:59 +0000176typedef size_t socklen_t;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000177# else
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000178# include <arpa/inet.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000179# endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000180
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000181# ifndef RISCOS
182# include <fcntl.h>
183# else
184# include <sys/fcntl.h>
185# define NO_DUP
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000186int h_errno; /* not used */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000187# endif
188
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000189#else
Guido van Rossum48a680c2001-03-02 06:34:14 +0000190
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000191/* MS_WINDOWS includes */
192# include <fcntl.h>
Guido van Rossum48a680c2001-03-02 06:34:14 +0000193
Jeremy Hylton22308652001-02-02 03:23:09 +0000194#endif
195
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000196#ifdef HAVE_STDDEF_H
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000197# include <stddef.h>
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000198#endif
199
200#ifndef offsetof
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000201# define offsetof(type, member) ((size_t)(&((type *)0)->member))
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000202#endif
203
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000204#ifndef O_NDELAY
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000205# define O_NDELAY O_NONBLOCK /* For QNX only? */
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000206#endif
207
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000208#include "addrinfo.h"
209
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000210#ifndef HAVE_INET_PTON
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000211int inet_pton(int af, const char *src, void *dst);
Martin v. Löwisc925b1532001-07-21 09:42:15 +0000212const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000213#endif
214
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000215#ifdef __APPLE__
216/* On OS X, getaddrinfo returns no error indication of lookup
217 failure, so we must use the emulation instead of the libinfo
218 implementation. Unfortunately, performing an autoconf test
219 for this bug would require DNS access for the machine performing
220 the configuration, which is not acceptable. Therefore, we
221 determine the bug just by checking for __APPLE__. If this bug
222 gets ever fixed, perhaps checking for sys/version.h would be
223 appropriate, which is 10/0 on the system with the bug. */
Jack Jansen84262fb2002-07-02 14:40:42 +0000224#ifndef HAVE_GETNAMEINFO
225/* This bug seems to be fixed in Jaguar. Ths easiest way I could
226 Find to check for Jaguar is that it has getnameinfo(), which
227 older releases don't have */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000228#undef HAVE_GETADDRINFO
229/* avoid clashes with the C library definition of the symbol. */
230#define getaddrinfo fake_getaddrinfo
231#endif
Jack Jansen84262fb2002-07-02 14:40:42 +0000232#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000233
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000234/* I know this is a bad practice, but it is the easiest... */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000235#if !defined(HAVE_GETADDRINFO)
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000236#include "getaddrinfo.c"
237#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000238#if !defined(HAVE_GETNAMEINFO)
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000239#include "getnameinfo.c"
240#endif
241
Guido van Rossumbcc20741998-08-04 22:53:56 +0000242#if defined(MS_WINDOWS) || defined(__BEOS__)
243/* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000244/* seem to be a few differences in the API */
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000245#define SOCKETCLOSE closesocket
Guido van Rossumbe32c891996-06-20 16:25:29 +0000246#define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000247#endif
248
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000249#ifdef MS_WIN32
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000250#define EAFNOSUPPORT WSAEAFNOSUPPORT
251#define snprintf _snprintf
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000252#endif
Fred Drakea04eaad2000-06-30 02:46:07 +0000253
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000254#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000255#define SOCKETCLOSE soclose
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000256#define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000257#endif
258
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000259#ifndef SOCKETCLOSE
260#define SOCKETCLOSE close
261#endif
262
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000263/* XXX There's a problem here: *static* functions are not supposed to have
264 a Py prefix (or use CapitalizedWords). Later... */
265
Guido van Rossum30a685f1991-06-27 15:51:29 +0000266/* Global variable holding the exception type for errors detected
267 by this module (but not argument type or memory errors, etc.). */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000268static PyObject *socket_error;
269static PyObject *socket_herror;
270static PyObject *socket_gaierror;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000271
Guido van Rossum48a680c2001-03-02 06:34:14 +0000272#ifdef RISCOS
273/* Global variable which is !=0 if Python is running in a RISC OS taskwindow */
274static int taskwindow;
275#endif
276
Tim Peters643a7fc2002-02-17 04:13:21 +0000277/* A forward reference to the socket type object.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000278 The sock_type variable contains pointers to various functions,
279 some of which call new_sockobject(), which uses sock_type, so
Tim Peters643a7fc2002-02-17 04:13:21 +0000280 there has to be a circular reference. */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000281static PyTypeObject sock_type;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000282
Guido van Rossum30a685f1991-06-27 15:51:29 +0000283/* Convenience function to raise an error according to errno
284 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000285
Guido van Rossum73624e91994-10-10 17:59:00 +0000286static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000287set_error(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000288{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000289#ifdef MS_WINDOWS
Mark Hammond46a733d2000-07-24 01:45:11 +0000290 int err_no = WSAGetLastError();
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000291 static struct {
292 int no;
293 const char *msg;
294 } *msgp, msgs[] = {
295 {WSAEINTR, "Interrupted system call"},
296 {WSAEBADF, "Bad file descriptor"},
297 {WSAEACCES, "Permission denied"},
298 {WSAEFAULT, "Bad address"},
299 {WSAEINVAL, "Invalid argument"},
300 {WSAEMFILE, "Too many open files"},
301 {WSAEWOULDBLOCK,
302 "The socket operation could not complete "
303 "without blocking"},
304 {WSAEINPROGRESS, "Operation now in progress"},
305 {WSAEALREADY, "Operation already in progress"},
306 {WSAENOTSOCK, "Socket operation on non-socket"},
307 {WSAEDESTADDRREQ, "Destination address required"},
308 {WSAEMSGSIZE, "Message too long"},
309 {WSAEPROTOTYPE, "Protocol wrong type for socket"},
310 {WSAENOPROTOOPT, "Protocol not available"},
311 {WSAEPROTONOSUPPORT, "Protocol not supported"},
312 {WSAESOCKTNOSUPPORT, "Socket type not supported"},
313 {WSAEOPNOTSUPP, "Operation not supported"},
314 {WSAEPFNOSUPPORT, "Protocol family not supported"},
315 {WSAEAFNOSUPPORT, "Address family not supported"},
316 {WSAEADDRINUSE, "Address already in use"},
317 {WSAEADDRNOTAVAIL, "Can't assign requested address"},
318 {WSAENETDOWN, "Network is down"},
319 {WSAENETUNREACH, "Network is unreachable"},
320 {WSAENETRESET, "Network dropped connection on reset"},
321 {WSAECONNABORTED, "Software caused connection abort"},
322 {WSAECONNRESET, "Connection reset by peer"},
323 {WSAENOBUFS, "No buffer space available"},
324 {WSAEISCONN, "Socket is already connected"},
325 {WSAENOTCONN, "Socket is not connected"},
326 {WSAESHUTDOWN, "Can't send after socket shutdown"},
327 {WSAETOOMANYREFS, "Too many references: can't splice"},
328 {WSAETIMEDOUT, "Operation timed out"},
329 {WSAECONNREFUSED, "Connection refused"},
330 {WSAELOOP, "Too many levels of symbolic links"},
331 {WSAENAMETOOLONG, "File name too long"},
332 {WSAEHOSTDOWN, "Host is down"},
333 {WSAEHOSTUNREACH, "No route to host"},
334 {WSAENOTEMPTY, "Directory not empty"},
335 {WSAEPROCLIM, "Too many processes"},
336 {WSAEUSERS, "Too many users"},
337 {WSAEDQUOT, "Disc quota exceeded"},
338 {WSAESTALE, "Stale NFS file handle"},
339 {WSAEREMOTE, "Too many levels of remote in path"},
340 {WSASYSNOTREADY, "Network subsystem is unvailable"},
341 {WSAVERNOTSUPPORTED, "WinSock version is not supported"},
342 {WSANOTINITIALISED,
343 "Successful WSAStartup() not yet performed"},
344 {WSAEDISCON, "Graceful shutdown in progress"},
345 /* Resolver errors */
346 {WSAHOST_NOT_FOUND, "No such host is known"},
347 {WSATRY_AGAIN, "Host not found, or server failed"},
348 {WSANO_RECOVERY, "Unexpected server error encountered"},
349 {WSANO_DATA, "Valid name without requested data"},
350 {WSANO_ADDRESS, "No address, look for MX record"},
351 {0, NULL}
352 };
Mark Hammond46a733d2000-07-24 01:45:11 +0000353 if (err_no) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000354 PyObject *v;
Mark Hammond46a733d2000-07-24 01:45:11 +0000355 const char *msg = "winsock error";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000356
Mark Hammond46a733d2000-07-24 01:45:11 +0000357 for (msgp = msgs; msgp->msg; msgp++) {
358 if (err_no == msgp->no) {
359 msg = msgp->msg;
360 break;
361 }
362 }
363
364 v = Py_BuildValue("(is)", err_no, msg);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000365 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000366 PyErr_SetObject(socket_error, v);
Guido van Rossum73624e91994-10-10 17:59:00 +0000367 Py_DECREF(v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000368 }
369 return NULL;
370 }
371 else
372#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000373
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000374#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000375 if (sock_errno() != NO_ERROR) {
376 APIRET rc;
377 ULONG msglen;
378 char outbuf[100];
379 int myerrorcode = sock_errno();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000380
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000381 /* Retrieve socket-related error message from MPTN.MSG file */
382 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
383 myerrorcode - SOCBASEERR + 26,
384 "mptn.msg",
385 &msglen);
386 if (rc == NO_ERROR) {
387 PyObject *v;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000388
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000389 /* OS/2 doesn't guarantee a terminator */
390 outbuf[msglen] = '\0';
391 if (strlen(outbuf) > 0) {
392 /* If non-empty msg, trim CRLF */
393 char *lastc = &outbuf[ strlen(outbuf)-1 ];
394 while (lastc > outbuf && isspace(*lastc)) {
395 /* Trim trailing whitespace (CRLF) */
396 *lastc-- = '\0';
397 }
398 }
399 v = Py_BuildValue("(is)", myerrorcode, outbuf);
400 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000401 PyErr_SetObject(socket_error, v);
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000402 Py_DECREF(v);
403 }
404 return NULL;
405 }
406 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000407#endif
408
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000409 return PyErr_SetFromErrno(socket_error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000410}
411
Guido van Rossum30a685f1991-06-27 15:51:29 +0000412
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000413static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000414set_herror(int h_error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000415{
416 PyObject *v;
417
418#ifdef HAVE_HSTRERROR
419 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
420#else
421 v = Py_BuildValue("(is)", h_error, "host not found");
422#endif
423 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000424 PyErr_SetObject(socket_herror, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000425 Py_DECREF(v);
426 }
427
428 return NULL;
429}
430
431
432static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000433set_gaierror(int error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000434{
435 PyObject *v;
436
Martin v. Löwis272cb402002-03-01 08:31:07 +0000437#ifdef EAI_SYSTEM
438 /* EAI_SYSTEM is not available on Windows XP. */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000439 if (error == EAI_SYSTEM)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000440 return set_error();
Martin v. Löwis272cb402002-03-01 08:31:07 +0000441#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000442
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000443#ifdef HAVE_GAI_STRERROR
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000444 v = Py_BuildValue("(is)", error, gai_strerror(error));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000445#else
446 v = Py_BuildValue("(is)", error, "getaddrinfo failed");
447#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000448 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000449 PyErr_SetObject(socket_gaierror, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000450 Py_DECREF(v);
451 }
452
453 return NULL;
454}
455
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000456/* Function to perform the setting of socket blocking mode
457 internally. block = (1 | 0). */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000458static int
459internal_setblocking(PySocketSockObject *s, int block)
460{
461#ifndef RISCOS
462#ifndef MS_WINDOWS
463 int delay_flag;
464#endif
465#endif
466
467 Py_BEGIN_ALLOW_THREADS
468#ifdef __BEOS__
469 block = !block;
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000470 setsockopt(s->sock_fd, SOL_SOCKET, SO_NONBLOCK,
471 (void *)(&block), sizeof(int));
Guido van Rossum67f7a382002-06-06 21:08:16 +0000472#else
473#ifndef RISCOS
474#ifndef MS_WINDOWS
475#if defined(PYOS_OS2) && !defined(PYCC_GCC)
476 block = !block;
477 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
478#else /* !PYOS_OS2 */
479 delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
480 if (block)
481 delay_flag &= (~O_NDELAY);
482 else
483 delay_flag |= O_NDELAY;
484 fcntl(s->sock_fd, F_SETFL, delay_flag);
485#endif /* !PYOS_OS2 */
486#else /* MS_WINDOWS */
487 block = !block;
488 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
489#endif /* MS_WINDOWS */
490#endif /* __BEOS__ */
491#endif /* RISCOS */
492 Py_END_ALLOW_THREADS
493
494 /* Since these don't return anything */
495 return 1;
496}
497
Guido van Rossum11ba0942002-06-13 15:07:44 +0000498/* Do a select() on the socket, if necessary (sock_timeout > 0).
499 The argument writing indicates the direction.
500 This does not raise an exception or return a success indicator;
501 we'll let the actual socket call do that. */
502static void
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000503internal_select(PySocketSockObject *s, int writing)
Guido van Rossum67f7a382002-06-06 21:08:16 +0000504{
505 fd_set fds;
506 struct timeval tv;
Guido van Rossum11ba0942002-06-13 15:07:44 +0000507
Guido van Rossumad654902002-07-19 12:44:59 +0000508 /* Nothing to do unless we're in timeout mode (not non-blocking) */
Guido van Rossum11ba0942002-06-13 15:07:44 +0000509 if (s->sock_timeout <= 0.0)
510 return;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000511
Guido van Rossumad654902002-07-19 12:44:59 +0000512 /* Guard against closed socket */
513 if (s->sock_fd < 0)
514 return;
515
Guido van Rossum67f7a382002-06-06 21:08:16 +0000516 /* Construct the arguments to select */
517 tv.tv_sec = (int)s->sock_timeout;
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000518 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000519 FD_ZERO(&fds);
520 FD_SET(s->sock_fd, &fds);
521
522 /* See if the socket is ready */
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000523 if (writing)
Guido van Rossum11ba0942002-06-13 15:07:44 +0000524 select(s->sock_fd+1, NULL, &fds, NULL, &tv);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000525 else
Guido van Rossum11ba0942002-06-13 15:07:44 +0000526 select(s->sock_fd+1, &fds, NULL, NULL, &tv);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000527}
528
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000529/* Initialize a new socket object. */
530
Tim Petersa12b4cf2002-07-18 22:38:44 +0000531static double defaulttimeout = -1.0; /* Default timeout for new sockets */
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000532
Mark Hammond62b1ab12002-07-23 06:31:15 +0000533PyMODINIT_FUNC
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000534init_sockobject(PySocketSockObject *s,
535 SOCKET_T fd, int family, int type, int proto)
536{
537#ifdef RISCOS
538 int block = 1;
539#endif
540 s->sock_fd = fd;
541 s->sock_family = family;
542 s->sock_type = type;
543 s->sock_proto = proto;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000544 s->sock_timeout = defaulttimeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000545
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000546 s->errorhandler = &set_error;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000547
548 if (defaulttimeout >= 0.0)
549 internal_setblocking(s, 0);
550
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000551#ifdef RISCOS
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000552 if (taskwindow)
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000553 socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000554#endif
555}
556
557
Guido van Rossum30a685f1991-06-27 15:51:29 +0000558/* Create a new socket object.
559 This just creates the object and initializes it.
560 If the creation fails, return NULL and set an exception (implicit
561 in NEWOBJ()). */
562
Guido van Rossum73624e91994-10-10 17:59:00 +0000563static PySocketSockObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000564new_sockobject(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000565{
Guido van Rossum73624e91994-10-10 17:59:00 +0000566 PySocketSockObject *s;
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000567 s = (PySocketSockObject *)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000568 PyType_GenericNew(&sock_type, NULL, NULL);
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000569 if (s != NULL)
570 init_sockobject(s, fd, family, type, proto);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000571 return s;
572}
573
Guido van Rossum30a685f1991-06-27 15:51:29 +0000574
Guido van Rossum48a680c2001-03-02 06:34:14 +0000575/* Lock to allow python interpreter to continue, but only allow one
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000576 thread to be in gethostbyname */
Guido van Rossum3baaa131999-03-22 21:44:51 +0000577#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum65d5b571998-12-21 19:32:43 +0000578PyThread_type_lock gethostbyname_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000579#endif
580
581
Guido van Rossum30a685f1991-06-27 15:51:29 +0000582/* Convert a string specifying a host name or one of a few symbolic
583 names to a numeric IP address. This usually calls gethostbyname()
584 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000585 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000586 an error occurred; then an exception is raised. */
587
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000588static int
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000589setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000590{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000591 struct addrinfo hints, *res;
592 int error;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000593
Guido van Rossuma376cc51996-12-05 23:43:35 +0000594 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
Guido van Rossum30a685f1991-06-27 15:51:29 +0000595 if (name[0] == '\0') {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000596 int siz;
597 memset(&hints, 0, sizeof(hints));
598 hints.ai_family = af;
599 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
600 hints.ai_flags = AI_PASSIVE;
601 error = getaddrinfo(NULL, "0", &hints, &res);
602 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000603 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000604 return -1;
605 }
606 switch (res->ai_family) {
607 case AF_INET:
608 siz = 4;
609 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000610#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000611 case AF_INET6:
612 siz = 16;
613 break;
614#endif
615 default:
616 freeaddrinfo(res);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000617 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000618 "unsupported address family");
619 return -1;
620 }
621 if (res->ai_next) {
Martin v. Löwisf0b11d22001-11-07 08:31:03 +0000622 freeaddrinfo(res);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000623 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000624 "wildcard resolved to multiple address");
625 return -1;
626 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000627 if (res->ai_addrlen < addr_ret_size)
628 addr_ret_size = res->ai_addrlen;
629 memcpy(addr_ret, res->ai_addr, addr_ret_size);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000630 freeaddrinfo(res);
631 return siz;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000632 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000633 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000634 struct sockaddr_in *sin;
635 if (af != PF_INET && af != PF_UNSPEC) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000636 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000637 "address family mismatched");
638 return -1;
639 }
640 sin = (struct sockaddr_in *)addr_ret;
641 memset((void *) sin, '\0', sizeof(*sin));
642 sin->sin_family = AF_INET;
643#ifdef HAVE_SOCKADDR_SA_LEN
644 sin->sin_len = sizeof(*sin);
645#endif
646 sin->sin_addr.s_addr = INADDR_BROADCAST;
647 return sizeof(sin->sin_addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000648 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000649 memset(&hints, 0, sizeof(hints));
650 hints.ai_family = af;
651 error = getaddrinfo(name, NULL, &hints, &res);
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000652#if defined(__digital__) && defined(__unix__)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000653 if (error == EAI_NONAME && af == AF_UNSPEC) {
654 /* On Tru64 V5.1, numeric-to-addr conversion fails
655 if no address family is given. Assume IPv4 for now.*/
656 hints.ai_family = AF_INET;
657 error = getaddrinfo(name, NULL, &hints, &res);
658 }
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000659#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000660 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000661 set_gaierror(error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000662 return -1;
663 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000664 if (res->ai_addrlen < addr_ret_size)
665 addr_ret_size = res->ai_addrlen;
666 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000667 freeaddrinfo(res);
668 switch (addr_ret->sa_family) {
669 case AF_INET:
670 return 4;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000671#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000672 case AF_INET6:
673 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +0000674#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000675 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000676 PyErr_SetString(socket_error, "unknown address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000677 return -1;
678 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000679}
680
Guido van Rossum30a685f1991-06-27 15:51:29 +0000681
Guido van Rossum30a685f1991-06-27 15:51:29 +0000682/* Create a string object representing an IP address.
683 This is always a string of the form 'dd.dd.dd.dd' (with variable
684 size numbers). */
685
Guido van Rossum73624e91994-10-10 17:59:00 +0000686static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000687makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000688{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000689 char buf[NI_MAXHOST];
690 int error;
691
692 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
693 NI_NUMERICHOST);
694 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000695 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000696 return NULL;
697 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000698 return PyString_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000699}
700
701
702/* Create an object representing the given socket address,
703 suitable for passing it back to bind(), connect() etc.
704 The family field of the sockaddr structure is inspected
705 to determine what kind of address it really is. */
706
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000707/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000708static PyObject *
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000709makesockaddr(int sockfd, struct sockaddr *addr, int addrlen)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000710{
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000711 if (addrlen == 0) {
712 /* No address -- may be recvfrom() from known socket */
Guido van Rossum73624e91994-10-10 17:59:00 +0000713 Py_INCREF(Py_None);
714 return Py_None;
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000715 }
716
Guido van Rossumbcc20741998-08-04 22:53:56 +0000717#ifdef __BEOS__
Guido van Rossum2c8bcb82000-04-25 21:34:53 +0000718 /* XXX: BeOS version of accept() doesn't set family correctly */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000719 addr->sa_family = AF_INET;
720#endif
721
Guido van Rossum30a685f1991-06-27 15:51:29 +0000722 switch (addr->sa_family) {
723
724 case AF_INET:
725 {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000726 struct sockaddr_in *a;
727 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
Barry Warsaw752300b1997-01-03 17:18:10 +0000728 PyObject *ret = NULL;
729 if (addrobj) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000730 a = (struct sockaddr_in *)addr;
Barry Warsaw752300b1997-01-03 17:18:10 +0000731 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
732 Py_DECREF(addrobj);
733 }
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000734 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000735 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000736
Guido van Rossumb6775db1994-08-01 11:34:53 +0000737#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000738 case AF_UNIX:
739 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000740 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000741 return PyString_FromString(a->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000742 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000743#endif /* AF_UNIX */
744
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000745#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000746 case AF_INET6:
747 {
748 struct sockaddr_in6 *a;
749 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
750 PyObject *ret = NULL;
751 if (addrobj) {
752 a = (struct sockaddr_in6 *)addr;
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000753 ret = Py_BuildValue("Oiii",
754 addrobj,
755 ntohs(a->sin6_port),
756 a->sin6_flowinfo,
757 a->sin6_scope_id);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000758 Py_DECREF(addrobj);
759 }
760 return ret;
761 }
Jeremy Hylton22308652001-02-02 03:23:09 +0000762#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +0000763
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +0000764#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +0000765 case AF_PACKET:
766 {
767 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
768 char *ifname = "";
769 struct ifreq ifr;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000770 /* need to look up interface name give index */
771 if (a->sll_ifindex) {
Jeremy Hylton22308652001-02-02 03:23:09 +0000772 ifr.ifr_ifindex = a->sll_ifindex;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000773 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
Jeremy Hylton22308652001-02-02 03:23:09 +0000774 ifname = ifr.ifr_name;
Jeremy Hylton22308652001-02-02 03:23:09 +0000775 }
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000776 return Py_BuildValue("shbhs#",
777 ifname,
778 ntohs(a->sll_protocol),
779 a->sll_pkttype,
780 a->sll_hatype,
781 a->sll_addr,
782 a->sll_halen);
Jeremy Hylton22308652001-02-02 03:23:09 +0000783 }
784#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000785
Guido van Rossum30a685f1991-06-27 15:51:29 +0000786 /* More cases here... */
787
788 default:
Guido van Rossumaa948df1997-05-07 17:41:48 +0000789 /* If we don't know the address family, don't raise an
790 exception -- return it as a tuple. */
791 return Py_BuildValue("is#",
792 addr->sa_family,
793 addr->sa_data,
794 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000795
Guido van Rossum30a685f1991-06-27 15:51:29 +0000796 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000797}
798
Guido van Rossum30a685f1991-06-27 15:51:29 +0000799
800/* Parse a socket address argument according to the socket object's
801 address family. Return 1 if the address was in the proper format,
802 0 of not. The address is returned through addr_ret, its length
803 through len_ret. */
804
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000805static int
Guido van Rossum48a680c2001-03-02 06:34:14 +0000806getsockaddrarg(PySocketSockObject *s, PyObject *args,
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000807 struct sockaddr **addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000808{
Guido van Rossum30a685f1991-06-27 15:51:29 +0000809 switch (s->sock_family) {
810
Guido van Rossumb6775db1994-08-01 11:34:53 +0000811#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000812 case AF_UNIX:
813 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000814 struct sockaddr_un* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000815 char *path;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000816 int len;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000817 addr = (struct sockaddr_un*)&(s->sock_addr).un;
Guido van Rossum7e488981998-10-08 02:25:24 +0000818 if (!PyArg_Parse(args, "t#", &path, &len))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000819 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000820 if (len > sizeof addr->sun_path) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000821 PyErr_SetString(socket_error,
Barry Warsaw752300b1997-01-03 17:18:10 +0000822 "AF_UNIX path too long");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000823 return 0;
824 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000825 addr->sun_family = s->sock_family;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000826 memcpy(addr->sun_path, path, len);
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000827 addr->sun_path[len] = 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000828 *addr_ret = (struct sockaddr *) addr;
Guido van Rossum65af28a1996-06-11 18:36:33 +0000829 *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000830 return 1;
831 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000832#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000833
Guido van Rossum30a685f1991-06-27 15:51:29 +0000834 case AF_INET:
835 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000836 struct sockaddr_in* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000837 char *host;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000838 int port;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000839 addr=(struct sockaddr_in*)&(s->sock_addr).in;
Guido van Rossume4dad902000-12-01 13:13:11 +0000840 if (!PyTuple_Check(args)) {
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000841 PyErr_Format(
842 PyExc_TypeError,
843 "getsockaddrarg: "
844 "AF_INET address must be tuple, not %.500s",
845 args->ob_type->tp_name);
Guido van Rossume4dad902000-12-01 13:13:11 +0000846 return 0;
847 }
848 if (!PyArg_ParseTuple(args, "si:getsockaddrarg", &host, &port))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000849 return 0;
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000850 if (setipaddr(host, (struct sockaddr *)addr, sizeof(*addr), AF_INET) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000851 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000852 addr->sin_family = AF_INET;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000853 addr->sin_port = htons((short)port);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000854 *addr_ret = (struct sockaddr *) addr;
855 *len_ret = sizeof *addr;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000856 return 1;
857 }
858
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000859#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000860 case AF_INET6:
861 {
862 struct sockaddr_in6* addr;
863 char *host;
864 int port, flowinfo, scope_id;
865 addr = (struct sockaddr_in6*)&(s->sock_addr).in6;
866 flowinfo = scope_id = 0;
867 if (!PyArg_ParseTuple(args, "si|ii", &host, &port, &flowinfo,
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000868 &scope_id)) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000869 return 0;
870 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000871 if (setipaddr(host, (struct sockaddr *)addr, sizeof(*addr), AF_INET6) < 0)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000872 return 0;
873 addr->sin6_family = s->sock_family;
874 addr->sin6_port = htons((short)port);
875 addr->sin6_flowinfo = flowinfo;
876 addr->sin6_scope_id = scope_id;
877 *addr_ret = (struct sockaddr *) addr;
878 *len_ret = sizeof *addr;
879 return 1;
880 }
881#endif
882
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +0000883#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +0000884 case AF_PACKET:
885 {
886 struct sockaddr_ll* addr;
887 struct ifreq ifr;
888 char *interfaceName;
889 int protoNumber;
890 int hatype = 0;
891 int pkttype = 0;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000892 char *haddr;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000893
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000894 if (!PyArg_ParseTuple(args, "si|iis", &interfaceName,
895 &protoNumber, &pkttype, &hatype, &haddr))
Jeremy Hylton22308652001-02-02 03:23:09 +0000896 return 0;
897 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
898 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000899 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000900 s->errorhandler();
Jeremy Hylton22308652001-02-02 03:23:09 +0000901 return 0;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000902 }
Jeremy Hylton22308652001-02-02 03:23:09 +0000903 addr = &(s->sock_addr.ll);
904 addr->sll_family = AF_PACKET;
905 addr->sll_protocol = htons((short)protoNumber);
906 addr->sll_ifindex = ifr.ifr_ifindex;
907 addr->sll_pkttype = pkttype;
908 addr->sll_hatype = hatype;
909 *addr_ret = (struct sockaddr *) addr;
910 *len_ret = sizeof *addr;
911 return 1;
912 }
Guido van Rossum48a680c2001-03-02 06:34:14 +0000913#endif
914
Guido van Rossum30a685f1991-06-27 15:51:29 +0000915 /* More cases here... */
916
917 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000918 PyErr_SetString(socket_error, "getsockaddrarg: bad family");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000919 return 0;
920
921 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000922}
923
Guido van Rossum30a685f1991-06-27 15:51:29 +0000924
Guido van Rossum48a680c2001-03-02 06:34:14 +0000925/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +0000926 Return 1 if the family is known, 0 otherwise. The length is returned
927 through len_ret. */
928
929static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +0000930getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +0000931{
932 switch (s->sock_family) {
933
Guido van Rossumb6775db1994-08-01 11:34:53 +0000934#ifdef AF_UNIX
Guido van Rossum710e1df1992-06-12 10:39:36 +0000935 case AF_UNIX:
936 {
937 *len_ret = sizeof (struct sockaddr_un);
938 return 1;
939 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000940#endif /* AF_UNIX */
Guido van Rossum710e1df1992-06-12 10:39:36 +0000941
942 case AF_INET:
943 {
944 *len_ret = sizeof (struct sockaddr_in);
945 return 1;
946 }
947
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000948#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000949 case AF_INET6:
950 {
951 *len_ret = sizeof (struct sockaddr_in6);
952 return 1;
953 }
954#endif
955
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +0000956#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +0000957 case AF_PACKET:
958 {
959 *len_ret = sizeof (struct sockaddr_ll);
960 return 1;
961 }
962#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000963
Guido van Rossum710e1df1992-06-12 10:39:36 +0000964 /* More cases here... */
965
966 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000967 PyErr_SetString(socket_error, "getsockaddrlen: bad family");
Guido van Rossum710e1df1992-06-12 10:39:36 +0000968 return 0;
969
970 }
971}
972
973
Guido van Rossum30a685f1991-06-27 15:51:29 +0000974/* s.accept() method */
975
Guido van Rossum73624e91994-10-10 17:59:00 +0000976static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000977sock_accept(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000978{
979 char addrbuf[256];
Fred Drakea04eaad2000-06-30 02:46:07 +0000980 SOCKET_T newfd;
Guido van Rossumff3ab422000-04-24 15:16:03 +0000981 socklen_t addrlen;
Barry Warsaw752300b1997-01-03 17:18:10 +0000982 PyObject *sock = NULL;
983 PyObject *addr = NULL;
984 PyObject *res = NULL;
985
Guido van Rossum710e1df1992-06-12 10:39:36 +0000986 if (!getsockaddrlen(s, &addrlen))
987 return NULL;
Fred Drakefd168342001-05-09 19:11:33 +0000988 memset(addrbuf, 0, addrlen);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000989
Guido van Rossum73624e91994-10-10 17:59:00 +0000990 Py_BEGIN_ALLOW_THREADS
Guido van Rossum11ba0942002-06-13 15:07:44 +0000991 internal_select(s, 0);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000992 newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000993 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000994
Fred Drakea04eaad2000-06-30 02:46:07 +0000995#ifdef MS_WINDOWS
996 if (newfd == INVALID_SOCKET)
997#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000998 if (newfd < 0)
Fred Drakea04eaad2000-06-30 02:46:07 +0000999#endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001000 return s->errorhandler();
Barry Warsaw752300b1997-01-03 17:18:10 +00001001
Guido van Rossum30a685f1991-06-27 15:51:29 +00001002 /* Create the new object with unspecified family,
1003 to avoid calls to bind() etc. on it. */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001004 sock = (PyObject *) new_sockobject(newfd,
1005 s->sock_family,
1006 s->sock_type,
1007 s->sock_proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001008
Barry Warsaw752300b1997-01-03 17:18:10 +00001009 if (sock == NULL) {
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001010 SOCKETCLOSE(newfd);
Barry Warsaw752300b1997-01-03 17:18:10 +00001011 goto finally;
1012 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001013 addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001014 addrlen);
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001015 if (addr == NULL)
Barry Warsaw752300b1997-01-03 17:18:10 +00001016 goto finally;
1017
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001018 res = Py_BuildValue("OO", sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00001019
Guido van Rossum67f7a382002-06-06 21:08:16 +00001020finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001021 Py_XDECREF(sock);
1022 Py_XDECREF(addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001023 return res;
1024}
1025
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001026PyDoc_STRVAR(accept_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001027"accept() -> (socket object, address info)\n\
1028\n\
1029Wait for an incoming connection. Return a new socket representing the\n\
1030connection, and the address of the client. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001031info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001032
Guido van Rossum11ba0942002-06-13 15:07:44 +00001033/* s.setblocking(flag) method. Argument:
1034 False -- non-blocking mode; same as settimeout(0)
1035 True -- blocking mode; same as settimeout(None)
1036*/
Guido van Rossume4485b01994-09-07 14:32:49 +00001037
Guido van Rossum73624e91994-10-10 17:59:00 +00001038static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001039sock_setblocking(PySocketSockObject *s, PyObject *arg)
Guido van Rossume4485b01994-09-07 14:32:49 +00001040{
1041 int block;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001042
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001043 block = PyInt_AsLong(arg);
1044 if (block == -1 && PyErr_Occurred())
Guido van Rossume4485b01994-09-07 14:32:49 +00001045 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001046
Guido van Rossum11ba0942002-06-13 15:07:44 +00001047 s->sock_timeout = block ? -1.0 : 0.0;
Guido van Rossume8008f02002-06-07 03:36:20 +00001048 internal_setblocking(s, block);
Guido van Rossume4485b01994-09-07 14:32:49 +00001049
Guido van Rossum73624e91994-10-10 17:59:00 +00001050 Py_INCREF(Py_None);
1051 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +00001052}
Guido van Rossume4485b01994-09-07 14:32:49 +00001053
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001054PyDoc_STRVAR(setblocking_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001055"setblocking(flag)\n\
1056\n\
1057Set the socket to blocking (flag is true) or non-blocking (false).\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001058setblocking(True) is equivalent to settimeout(None);\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001059setblocking(False) is equivalent to settimeout(0.0).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001060
Guido van Rossum11ba0942002-06-13 15:07:44 +00001061/* s.settimeout(timeout) method. Argument:
1062 None -- no timeout, blocking mode; same as setblocking(True)
1063 0.0 -- non-blocking mode; same as setblocking(False)
1064 > 0 -- timeout mode; operations time out after timeout seconds
1065 < 0 -- illegal; raises an exception
1066*/
Guido van Rossum67f7a382002-06-06 21:08:16 +00001067static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001068sock_settimeout(PySocketSockObject *s, PyObject *arg)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001069{
Guido van Rossum11ba0942002-06-13 15:07:44 +00001070 double timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001071
1072 if (arg == Py_None)
Guido van Rossum11ba0942002-06-13 15:07:44 +00001073 timeout = -1.0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001074 else {
Guido van Rossum11ba0942002-06-13 15:07:44 +00001075 timeout = PyFloat_AsDouble(arg);
1076 if (timeout < 0.0) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00001077 if (!PyErr_Occurred())
1078 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00001079 "Timeout value out of range");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001080 return NULL;
1081 }
1082 }
1083
Guido van Rossum11ba0942002-06-13 15:07:44 +00001084 s->sock_timeout = timeout;
1085 internal_setblocking(s, timeout < 0.0);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001086
1087 Py_INCREF(Py_None);
1088 return Py_None;
1089}
1090
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001091PyDoc_STRVAR(settimeout_doc,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001092"settimeout(timeout)\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001093\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001094Set a timeout on socket operations. 'timeout' can be a float,\n\
1095giving in seconds, or None. Setting a timeout of None disables\n\
1096the timeout feature and is equivalent to setblocking(1).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001097Setting a timeout of zero is the same as setblocking(0).");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001098
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001099/* s.gettimeout() method.
1100 Returns the timeout associated with a socket. */
Guido van Rossum67f7a382002-06-06 21:08:16 +00001101static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001102sock_gettimeout(PySocketSockObject *s)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001103{
1104 if (s->sock_timeout < 0.0) {
1105 Py_INCREF(Py_None);
1106 return Py_None;
1107 }
1108 else
1109 return PyFloat_FromDouble(s->sock_timeout);
1110}
1111
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001112PyDoc_STRVAR(gettimeout_doc,
Guido van Rossum67f7a382002-06-06 21:08:16 +00001113"gettimeout()\n\
1114\n\
1115Returns the timeout in floating seconds associated with socket \n\
1116operations. A timeout of None indicates that timeouts on socket \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001117operations are disabled.");
Guido van Rossume4485b01994-09-07 14:32:49 +00001118
Guido van Rossum48a680c2001-03-02 06:34:14 +00001119#ifdef RISCOS
1120/* s.sleeptaskw(1 | 0) method */
1121
1122static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001123sock_sleeptaskw(PySocketSockObject *s,PyObject *args)
Guido van Rossum48a680c2001-03-02 06:34:14 +00001124{
Guido van Rossum67f7a382002-06-06 21:08:16 +00001125 int block;
1126 int delay_flag;
1127 if (!PyArg_Parse(args, "i", &block))
1128 return NULL;
1129 Py_BEGIN_ALLOW_THREADS
1130 socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
1131 Py_END_ALLOW_THREADS
Guido van Rossum48a680c2001-03-02 06:34:14 +00001132
Guido van Rossum67f7a382002-06-06 21:08:16 +00001133 Py_INCREF(Py_None);
1134 return Py_None;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001135}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001136PyDoc_STRVAR(sleeptaskw_doc,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001137"sleeptaskw(flag)\n\
1138\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001139Allow sleeps in taskwindows.");
Guido van Rossum48a680c2001-03-02 06:34:14 +00001140#endif
1141
1142
Guido van Rossumaee08791992-09-08 09:05:33 +00001143/* s.setsockopt() method.
1144 With an integer third argument, sets an integer option.
1145 With a string third argument, sets an option from a buffer;
1146 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001147
Guido van Rossum73624e91994-10-10 17:59:00 +00001148static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001149sock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001150{
1151 int level;
1152 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001153 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +00001154 char *buf;
1155 int buflen;
1156 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001157
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001158 if (PyArg_ParseTuple(args, "iii:setsockopt",
1159 &level, &optname, &flag)) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001160 buf = (char *) &flag;
1161 buflen = sizeof flag;
1162 }
1163 else {
Guido van Rossum73624e91994-10-10 17:59:00 +00001164 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001165 if (!PyArg_ParseTuple(args, "iis#:setsockopt",
1166 &level, &optname, &buf, &buflen))
Guido van Rossumaee08791992-09-08 09:05:33 +00001167 return NULL;
1168 }
Thomas Wouters334fb892000-07-25 12:56:38 +00001169 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001170 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001171 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001172 Py_INCREF(Py_None);
1173 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001174}
1175
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001176PyDoc_STRVAR(setsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001177"setsockopt(level, option, value)\n\
1178\n\
1179Set a socket option. See the Unix manual for level and option.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001180The value argument can either be an integer or a string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001181
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001182
Guido van Rossumaee08791992-09-08 09:05:33 +00001183/* s.getsockopt() method.
1184 With two arguments, retrieves an integer option.
1185 With a third integer argument, retrieves a string buffer of that size;
1186 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001187
Guido van Rossum73624e91994-10-10 17:59:00 +00001188static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001189sock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001190{
1191 int level;
1192 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001193 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +00001194 PyObject *buf;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001195 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001196
Guido van Rossumbcc20741998-08-04 22:53:56 +00001197#ifdef __BEOS__
Fred Drake564a6cc2001-05-11 20:12:26 +00001198 /* We have incomplete socket support. */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001199 PyErr_SetString(socket_error, "getsockopt not supported");
Guido van Rossumbcc20741998-08-04 22:53:56 +00001200 return NULL;
1201#else
1202
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001203 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
1204 &level, &optname, &buflen))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001205 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001206
Guido van Rossumbe32c891996-06-20 16:25:29 +00001207 if (buflen == 0) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001208 int flag = 0;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001209 socklen_t flagsize = sizeof flag;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001210 res = getsockopt(s->sock_fd, level, optname,
Thomas Wouters334fb892000-07-25 12:56:38 +00001211 (void *)&flag, &flagsize);
Guido van Rossumaee08791992-09-08 09:05:33 +00001212 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001213 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001214 return PyInt_FromLong(flag);
Guido van Rossumaee08791992-09-08 09:05:33 +00001215 }
Guido van Rossumaee08791992-09-08 09:05:33 +00001216 if (buflen <= 0 || buflen > 1024) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001217 PyErr_SetString(socket_error,
Barry Warsaw752300b1997-01-03 17:18:10 +00001218 "getsockopt buflen out of range");
Guido van Rossumaee08791992-09-08 09:05:33 +00001219 return NULL;
1220 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001221 buf = PyString_FromStringAndSize((char *)NULL, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001222 if (buf == NULL)
1223 return NULL;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001224 res = getsockopt(s->sock_fd, level, optname,
Fred Drake564a6cc2001-05-11 20:12:26 +00001225 (void *)PyString_AS_STRING(buf), &buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001226 if (res < 0) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001227 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001228 return s->errorhandler();
Guido van Rossumaee08791992-09-08 09:05:33 +00001229 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001230 _PyString_Resize(&buf, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001231 return buf;
Guido van Rossumbcc20741998-08-04 22:53:56 +00001232#endif /* __BEOS__ */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001233}
1234
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001235PyDoc_STRVAR(getsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001236"getsockopt(level, option[, buffersize]) -> value\n\
1237\n\
1238Get a socket option. See the Unix manual for level and option.\n\
1239If a nonzero buffersize argument is given, the return value is a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001240string of that length; otherwise it is an integer.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001241
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001242
Fred Drake728819a2000-07-01 03:40:12 +00001243/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001244
Guido van Rossum73624e91994-10-10 17:59:00 +00001245static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001246sock_bind(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001247{
1248 struct sockaddr *addr;
1249 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001250 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001251
Fred Drake728819a2000-07-01 03:40:12 +00001252 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001253 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001254 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001255 res = bind(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001256 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001257 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001258 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001259 Py_INCREF(Py_None);
1260 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001261}
1262
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001263PyDoc_STRVAR(bind_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001264"bind(address)\n\
1265\n\
1266Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00001267pair (host, port); the host must refer to the local host. For raw packet\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001268sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001269
Guido van Rossum30a685f1991-06-27 15:51:29 +00001270
1271/* s.close() method.
1272 Set the file descriptor to -1 so operations tried subsequently
1273 will surely fail. */
1274
Guido van Rossum73624e91994-10-10 17:59:00 +00001275static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001276sock_close(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001277{
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001278 SOCKET_T fd;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001279
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001280 if ((fd = s->sock_fd) != -1) {
1281 s->sock_fd = -1;
Guido van Rossum08481461996-10-12 14:07:22 +00001282 Py_BEGIN_ALLOW_THREADS
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001283 (void) SOCKETCLOSE(fd);
Guido van Rossum08481461996-10-12 14:07:22 +00001284 Py_END_ALLOW_THREADS
1285 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001286 Py_INCREF(Py_None);
1287 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001288}
1289
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001290PyDoc_STRVAR(close_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001291"close()\n\
1292\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001293Close the socket. It cannot be used after this call.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001294
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001295static int
1296internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen)
1297{
1298 int res;
1299
1300 res = connect(s->sock_fd, addr, addrlen);
1301
1302#ifdef MS_WINDOWS
1303
1304 if (s->sock_timeout > 0.0) {
1305 if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK) {
1306 internal_select(s, 1);
1307 res = connect(s->sock_fd, addr, addrlen);
1308 if (res < 0 && WSAGetLastError() == WSAEISCONN)
1309 res = 0;
1310 }
1311 }
1312
1313 if (res < 0)
1314 res = WSAGetLastError();
1315
1316#else
1317
1318 if (s->sock_timeout > 0.0) {
1319 if (res < 0 && errno == EINPROGRESS) {
1320 internal_select(s, 1);
1321 res = connect(s->sock_fd, addr, addrlen);
1322 }
1323 }
1324
1325 if (res < 0)
1326 res = errno;
1327
1328#endif
1329
1330 return res;
1331}
Guido van Rossum30a685f1991-06-27 15:51:29 +00001332
Fred Drake728819a2000-07-01 03:40:12 +00001333/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001334
Guido van Rossum73624e91994-10-10 17:59:00 +00001335static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001336sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001337{
1338 struct sockaddr *addr;
1339 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001340 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001341
Fred Drake728819a2000-07-01 03:40:12 +00001342 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001343 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001344
Guido van Rossum73624e91994-10-10 17:59:00 +00001345 Py_BEGIN_ALLOW_THREADS
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001346 res = internal_connect(s, addr, addrlen);
Guido van Rossum11ba0942002-06-13 15:07:44 +00001347 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001348
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001349 if (res != 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001350 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001351 Py_INCREF(Py_None);
1352 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001353}
1354
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001355PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001356"connect(address)\n\
1357\n\
1358Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001359is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001360
Guido van Rossum30a685f1991-06-27 15:51:29 +00001361
Fred Drake728819a2000-07-01 03:40:12 +00001362/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001363
1364static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001365sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001366{
1367 struct sockaddr *addr;
1368 int addrlen;
1369 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001370
Fred Drake728819a2000-07-01 03:40:12 +00001371 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001372 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001373
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001374 Py_BEGIN_ALLOW_THREADS
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001375 res = internal_connect(s, addr, addrlen);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001376 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001377
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001378 return PyInt_FromLong((long) res);
1379}
1380
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001381PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001382"connect_ex(address)\n\
1383\n\
1384This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001385instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001386
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001387
Guido van Rossumed233a51992-06-23 09:07:03 +00001388/* s.fileno() method */
1389
Guido van Rossum73624e91994-10-10 17:59:00 +00001390static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001391sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00001392{
Fred Drakea04eaad2000-06-30 02:46:07 +00001393#if SIZEOF_SOCKET_T <= SIZEOF_LONG
Guido van Rossum73624e91994-10-10 17:59:00 +00001394 return PyInt_FromLong((long) s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +00001395#else
1396 return PyLong_FromLongLong((LONG_LONG)s->sock_fd);
1397#endif
Guido van Rossumed233a51992-06-23 09:07:03 +00001398}
1399
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001400PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001401"fileno() -> integer\n\
1402\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001403Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001404
Guido van Rossumed233a51992-06-23 09:07:03 +00001405
Guido van Rossumbe32c891996-06-20 16:25:29 +00001406#ifndef NO_DUP
1407/* s.dup() method */
1408
1409static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001410sock_dup(PySocketSockObject *s)
Guido van Rossumbe32c891996-06-20 16:25:29 +00001411{
Fred Drakea04eaad2000-06-30 02:46:07 +00001412 SOCKET_T newfd;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001413 PyObject *sock;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001414
Guido van Rossumbe32c891996-06-20 16:25:29 +00001415 newfd = dup(s->sock_fd);
1416 if (newfd < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001417 return s->errorhandler();
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001418 sock = (PyObject *) new_sockobject(newfd,
1419 s->sock_family,
1420 s->sock_type,
1421 s->sock_proto);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001422 if (sock == NULL)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001423 SOCKETCLOSE(newfd);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001424 return sock;
1425}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001426
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001427PyDoc_STRVAR(dup_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001428"dup() -> socket object\n\
1429\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001430Return a new socket object connected to the same system resource.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001431
Guido van Rossumbe32c891996-06-20 16:25:29 +00001432#endif
1433
1434
Guido van Rossumc89705d1992-11-26 08:54:07 +00001435/* s.getsockname() method */
1436
Guido van Rossum73624e91994-10-10 17:59:00 +00001437static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001438sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001439{
1440 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001441 int res;
1442 socklen_t addrlen;
1443
Guido van Rossumc89705d1992-11-26 08:54:07 +00001444 if (!getsockaddrlen(s, &addrlen))
1445 return NULL;
Guido van Rossumca42b161996-01-12 01:36:05 +00001446 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001447 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001448 res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001449 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001450 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001451 return s->errorhandler();
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001452 return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001453}
1454
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001455PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001456"getsockname() -> address info\n\
1457\n\
1458Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001459info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001460
Guido van Rossumc89705d1992-11-26 08:54:07 +00001461
Guido van Rossumb6775db1994-08-01 11:34:53 +00001462#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001463/* s.getpeername() method */
1464
Guido van Rossum73624e91994-10-10 17:59:00 +00001465static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001466sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001467{
1468 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001469 int res;
1470 socklen_t addrlen;
1471
Guido van Rossumc89705d1992-11-26 08:54:07 +00001472 if (!getsockaddrlen(s, &addrlen))
1473 return NULL;
Fred Drakefd168342001-05-09 19:11:33 +00001474 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001475 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001476 res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001477 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001478 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001479 return s->errorhandler();
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001480 return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001481}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001482
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001483PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001484"getpeername() -> address info\n\
1485\n\
1486Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001487info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001488
Guido van Rossumb6775db1994-08-01 11:34:53 +00001489#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001490
1491
Guido van Rossum30a685f1991-06-27 15:51:29 +00001492/* s.listen(n) method */
1493
Guido van Rossum73624e91994-10-10 17:59:00 +00001494static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001495sock_listen(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001496{
1497 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001498 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001499
1500 backlog = PyInt_AsLong(arg);
1501 if (backlog == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001502 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001503 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +00001504 if (backlog < 1)
1505 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001506 res = listen(s->sock_fd, backlog);
Guido van Rossum73624e91994-10-10 17:59:00 +00001507 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001508 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001509 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001510 Py_INCREF(Py_None);
1511 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001512}
1513
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001514PyDoc_STRVAR(listen_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001515"listen(backlog)\n\
1516\n\
1517Enable a server to accept connections. The backlog argument must be at\n\
1518least 1; it specifies the number of unaccepted connection that the system\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001519will allow before refusing new connections.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001520
1521
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001522#ifndef NO_DUP
Guido van Rossum30a685f1991-06-27 15:51:29 +00001523/* s.makefile(mode) method.
1524 Create a new open file object referring to a dupped version of
1525 the socket's file descriptor. (The dup() call is necessary so
1526 that the open file and socket objects may be closed independent
1527 of each other.)
1528 The mode argument specifies 'r' or 'w' passed to fdopen(). */
1529
Guido van Rossum73624e91994-10-10 17:59:00 +00001530static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001531sock_makefile(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001532{
Tim Petersdbd9ba62000-07-09 03:09:57 +00001533 extern int fclose(FILE *);
Guido van Rossum6b144911995-03-14 15:05:13 +00001534 char *mode = "r";
1535 int bufsize = -1;
Fred Drakea04eaad2000-06-30 02:46:07 +00001536#ifdef MS_WIN32
Tim Peters79248aa2001-08-29 21:37:10 +00001537 Py_intptr_t fd;
Fred Drakea04eaad2000-06-30 02:46:07 +00001538#else
Guido van Rossum30a685f1991-06-27 15:51:29 +00001539 int fd;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001540#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001541 FILE *fp;
Guido van Rossum6b144911995-03-14 15:05:13 +00001542 PyObject *f;
1543
Guido van Rossum43713e52000-02-29 13:59:29 +00001544 if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001545 return NULL;
Guido van Rossum8d665e61996-06-26 18:22:49 +00001546#ifdef MS_WIN32
Guido van Rossum82a5c661998-07-07 20:45:43 +00001547 if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) ||
1548 ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL))
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001549#else
Guido van Rossum82a5c661998-07-07 20:45:43 +00001550 if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL)
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001551#endif
Guido van Rossum82a5c661998-07-07 20:45:43 +00001552 {
Guido van Rossum6b144911995-03-14 15:05:13 +00001553 if (fd >= 0)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001554 SOCKETCLOSE(fd);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001555 return s->errorhandler();
Guido van Rossum6b144911995-03-14 15:05:13 +00001556 }
Jack Jansen6d8898b2002-03-25 15:32:01 +00001557#ifdef USE_GUSI2
1558 /* Workaround for bug in Metrowerks MSL vs. GUSI I/O library */
Guido van Rossum67f7a382002-06-06 21:08:16 +00001559 if (strchr(mode, 'b') != NULL)
Jack Jansen6d8898b2002-03-25 15:32:01 +00001560 bufsize = 0;
1561#endif
Guido van Rossum6b144911995-03-14 15:05:13 +00001562 f = PyFile_FromFile(fp, "<socket>", mode, fclose);
1563 if (f != NULL)
1564 PyFile_SetBufSize(f, bufsize);
1565 return f;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001566}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001567
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001568PyDoc_STRVAR(makefile_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001569"makefile([mode[, buffersize]]) -> file object\n\
1570\n\
1571Return a regular file object corresponding to the socket.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001572The mode and buffersize arguments are as for the built-in open() function.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001573
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001574#endif /* NO_DUP */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001575
Guido van Rossum48a680c2001-03-02 06:34:14 +00001576
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001577/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001578
Guido van Rossum73624e91994-10-10 17:59:00 +00001579static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001580sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001581{
Guido van Rossumbe32c891996-06-20 16:25:29 +00001582 int len, n, flags = 0;
Guido van Rossum73624e91994-10-10 17:59:00 +00001583 PyObject *buf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001584
Guido van Rossum43713e52000-02-29 13:59:29 +00001585 if (!PyArg_ParseTuple(args, "i|i:recv", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001586 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001587
1588 if (len < 0) {
Martin v. Löwisfba64e12001-11-19 10:41:26 +00001589 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00001590 "negative buffersize in recv");
Martin v. Löwisfba64e12001-11-19 10:41:26 +00001591 return NULL;
1592 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00001593
Guido van Rossum73624e91994-10-10 17:59:00 +00001594 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001595 if (buf == NULL)
1596 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001597
Guido van Rossum73624e91994-10-10 17:59:00 +00001598 Py_BEGIN_ALLOW_THREADS
Guido van Rossum11ba0942002-06-13 15:07:44 +00001599 internal_select(s, 0);
Jeremy Hylton1ba6bad2001-08-20 19:06:36 +00001600 n = recv(s->sock_fd, PyString_AS_STRING(buf), len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001601 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001602
Guido van Rossum7c53b771995-09-13 18:39:47 +00001603 if (n < 0) {
1604 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001605 return s->errorhandler();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001606 }
Tim Peters5de98422002-04-27 18:44:32 +00001607 if (n != len)
1608 _PyString_Resize(&buf, n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001609 return buf;
1610}
1611
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001612PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001613"recv(buffersize[, flags]) -> data\n\
1614\n\
1615Receive up to buffersize bytes from the socket. For the optional flags\n\
1616argument, see the Unix manual. When no data is available, block until\n\
1617at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001618the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001619
Guido van Rossum30a685f1991-06-27 15:51:29 +00001620
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001621/* s.recvfrom(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001622
Guido van Rossum73624e91994-10-10 17:59:00 +00001623static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001624sock_recvfrom(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001625{
1626 char addrbuf[256];
Barry Warsaw752300b1997-01-03 17:18:10 +00001627 PyObject *buf = NULL;
1628 PyObject *addr = NULL;
1629 PyObject *ret = NULL;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001630 int len, n, flags = 0;
1631 socklen_t addrlen;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001632
Guido van Rossum43713e52000-02-29 13:59:29 +00001633 if (!PyArg_ParseTuple(args, "i|i:recvfrom", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001634 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001635
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00001636 if (!getsockaddrlen(s, &addrlen))
1637 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001638 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00001639 if (buf == NULL)
1640 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001641
Guido van Rossum73624e91994-10-10 17:59:00 +00001642 Py_BEGIN_ALLOW_THREADS
Fred Drakefd168342001-05-09 19:11:33 +00001643 memset(addrbuf, 0, addrlen);
Guido van Rossum11ba0942002-06-13 15:07:44 +00001644 internal_select(s, 0);
Fred Drakefd168342001-05-09 19:11:33 +00001645 n = recvfrom(s->sock_fd, PyString_AS_STRING(buf), len, flags,
Guido van Rossum8d665e61996-06-26 18:22:49 +00001646#ifndef MS_WINDOWS
Andrew MacIntyreba43e872002-03-03 03:03:52 +00001647#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001648 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00001649#else
Thomas Wouters334fb892000-07-25 12:56:38 +00001650 (void *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00001651#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001652#else
Barry Warsaw752300b1997-01-03 17:18:10 +00001653 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001654#endif
Barry Warsaw752300b1997-01-03 17:18:10 +00001655 );
Guido van Rossum73624e91994-10-10 17:59:00 +00001656 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001657
Guido van Rossum7c53b771995-09-13 18:39:47 +00001658 if (n < 0) {
1659 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001660 return s->errorhandler();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001661 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00001662
Guido van Rossum53a9bf81996-06-11 18:35:24 +00001663 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001664 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001665
Guido van Rossum67f7a382002-06-06 21:08:16 +00001666 if (!(addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001667 addrlen)))
Barry Warsaw752300b1997-01-03 17:18:10 +00001668 goto finally;
1669
Guido van Rossum73624e91994-10-10 17:59:00 +00001670 ret = Py_BuildValue("OO", buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001671
1672finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001673 Py_XDECREF(addr);
1674 Py_XDECREF(buf);
Guido van Rossum6f5afc91993-02-05 09:46:15 +00001675 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001676}
1677
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001678PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001679"recvfrom(buffersize[, flags]) -> (data, address info)\n\
1680\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001681Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001682
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001683/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001684
Guido van Rossum73624e91994-10-10 17:59:00 +00001685static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001686sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001687{
Guido van Rossumff4949e1992-08-05 19:58:53 +00001688 char *buf;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001689 int len, n, flags = 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001690
Guido van Rossum43713e52000-02-29 13:59:29 +00001691 if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001692 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001693
Guido van Rossum73624e91994-10-10 17:59:00 +00001694 Py_BEGIN_ALLOW_THREADS
Guido van Rossum11ba0942002-06-13 15:07:44 +00001695 internal_select(s, 1);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001696 n = send(s->sock_fd, buf, len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001697 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001698
Guido van Rossum30a685f1991-06-27 15:51:29 +00001699 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001700 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001701 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00001702}
1703
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001704PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001705"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001706\n\
1707Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001708argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001709sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001710
1711
1712/* s.sendall(data [,flags]) method */
1713
1714static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001715sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001716{
1717 char *buf;
Martin v. Löwise6cc5b62002-02-16 23:13:54 +00001718 int len, n, flags = 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001719
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001720 if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags))
1721 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001722
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001723 Py_BEGIN_ALLOW_THREADS
Guido van Rossum11ba0942002-06-13 15:07:44 +00001724 internal_select(s, 1);
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001725 do {
1726 n = send(s->sock_fd, buf, len, flags);
1727 if (n < 0)
1728 break;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001729 buf += n;
1730 len -= n;
1731 } while (len > 0);
1732 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001733
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001734 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001735 return s->errorhandler();
Guido van Rossum67f7a382002-06-06 21:08:16 +00001736
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001737 Py_INCREF(Py_None);
1738 return Py_None;
1739}
1740
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001741PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001742"sendall(data[, flags])\n\
1743\n\
1744Send a data string to the socket. For the optional flags\n\
1745argument, see the Unix manual. This calls send() repeatedly\n\
1746until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001747to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001748
Guido van Rossum30a685f1991-06-27 15:51:29 +00001749
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001750/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001751
Guido van Rossum73624e91994-10-10 17:59:00 +00001752static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001753sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001754{
Guido van Rossum73624e91994-10-10 17:59:00 +00001755 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001756 char *buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001757 struct sockaddr *addr;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001758 int addrlen, len, n, flags;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001759
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001760 flags = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001761 if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001762 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001763 if (!PyArg_ParseTuple(args, "s#iO:sendto",
1764 &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001765 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001766 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00001767
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001768 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001769 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001770
Guido van Rossum73624e91994-10-10 17:59:00 +00001771 Py_BEGIN_ALLOW_THREADS
Guido van Rossum11ba0942002-06-13 15:07:44 +00001772 internal_select(s, 1);
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001773 n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001774 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001775
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001776 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001777 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001778 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001779}
1780
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001781PyDoc_STRVAR(sendto_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001782"sendto(data[, flags], address)\n\
1783\n\
1784Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001785For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001786
Guido van Rossum30a685f1991-06-27 15:51:29 +00001787
1788/* s.shutdown(how) method */
1789
Guido van Rossum73624e91994-10-10 17:59:00 +00001790static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001791sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001792{
1793 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001794 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001795
1796 how = PyInt_AsLong(arg);
1797 if (how == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001798 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001799 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001800 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +00001801 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001802 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001803 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001804 Py_INCREF(Py_None);
1805 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001806}
1807
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001808PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001809"shutdown(flag)\n\
1810\n\
1811Shut down the reading side of the socket (flag == 0), the writing side\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001812of the socket (flag == 1), or both ends (flag == 2).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001813
Guido van Rossum30a685f1991-06-27 15:51:29 +00001814
1815/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001816
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001817static PyMethodDef sock_methods[] = {
1818 {"accept", (PyCFunction)sock_accept, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001819 accept_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001820 {"bind", (PyCFunction)sock_bind, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001821 bind_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001822 {"close", (PyCFunction)sock_close, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001823 close_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001824 {"connect", (PyCFunction)sock_connect, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001825 connect_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001826 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001827 connect_ex_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00001828#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001829 {"dup", (PyCFunction)sock_dup, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001830 dup_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00001831#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001832 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001833 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001834#ifdef HAVE_GETPEERNAME
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001835 {"getpeername", (PyCFunction)sock_getpeername,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001836 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00001837#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001838 {"getsockname", (PyCFunction)sock_getsockname,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001839 METH_NOARGS, getsockname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001840 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001841 getsockopt_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001842 {"listen", (PyCFunction)sock_listen, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001843 listen_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001844#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001845 {"makefile", (PyCFunction)sock_makefile, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001846 makefile_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001847#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001848 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001849 recv_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001850 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001851 recvfrom_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001852 {"send", (PyCFunction)sock_send, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001853 send_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001854 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001855 sendall_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001856 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001857 sendto_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001858 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001859 setblocking_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001860 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
Guido van Rossum67f7a382002-06-06 21:08:16 +00001861 settimeout_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001862 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
Guido van Rossum67f7a382002-06-06 21:08:16 +00001863 gettimeout_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001864 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001865 setsockopt_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001866 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001867 shutdown_doc},
1868#ifdef RISCOS
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001869 {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001870 sleeptaskw_doc},
1871#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00001872 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001873};
1874
Guido van Rossum30a685f1991-06-27 15:51:29 +00001875
Guido van Rossum73624e91994-10-10 17:59:00 +00001876/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00001877 First close the file description. */
1878
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001879static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001880sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001881{
Guido van Rossumfa972c92000-04-10 12:45:45 +00001882 if (s->sock_fd != -1)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001883 (void) SOCKETCLOSE(s->sock_fd);
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001884 s->ob_type->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001885}
1886
Guido van Rossum30a685f1991-06-27 15:51:29 +00001887
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001888static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001889sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001890{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001891 char buf[512];
Fred Drakea04eaad2000-06-30 02:46:07 +00001892#if SIZEOF_SOCKET_T > SIZEOF_LONG
1893 if (s->sock_fd > LONG_MAX) {
1894 /* this can occur on Win64, and actually there is a special
1895 ugly printf formatter for decimal pointer length integer
1896 printing, only bother if necessary*/
1897 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001898 "no printf formatter to display "
1899 "the socket descriptor in decimal");
Fred Drakea04eaad2000-06-30 02:46:07 +00001900 return NULL;
1901 }
1902#endif
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001903 PyOS_snprintf(
1904 buf, sizeof(buf),
1905 "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>",
1906 (long)s->sock_fd, s->sock_family,
1907 s->sock_type,
1908 s->sock_proto);
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00001909 return PyString_FromString(buf);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001910}
1911
1912
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001913/* Create a new, uninitialized socket object. */
1914
1915static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001916sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001917{
1918 PyObject *new;
1919
1920 new = type->tp_alloc(type, 0);
Guido van Rossume1c478f2002-06-06 20:08:25 +00001921 if (new != NULL) {
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001922 ((PySocketSockObject *)new)->sock_fd = -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001923 ((PySocketSockObject *)new)->sock_timeout = -1.0;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001924 ((PySocketSockObject *)new)->errorhandler = &set_error;
Guido van Rossume1c478f2002-06-06 20:08:25 +00001925 }
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001926 return new;
1927}
1928
1929
1930/* Initialize a new socket object. */
1931
1932/*ARGSUSED*/
1933static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00001934sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001935{
1936 PySocketSockObject *s = (PySocketSockObject *)self;
1937 SOCKET_T fd;
1938 int family = AF_INET, type = SOCK_STREAM, proto = 0;
1939 static char *keywords[] = {"family", "type", "proto", 0};
1940
1941 if (!PyArg_ParseTupleAndKeywords(args, kwds,
1942 "|iii:socket", keywords,
1943 &family, &type, &proto))
1944 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001945
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001946 Py_BEGIN_ALLOW_THREADS
1947 fd = socket(family, type, proto);
1948 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001949
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001950#ifdef MS_WINDOWS
1951 if (fd == INVALID_SOCKET)
1952#else
1953 if (fd < 0)
1954#endif
1955 {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001956 set_error();
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001957 return -1;
1958 }
1959 init_sockobject(s, fd, family, type, proto);
1960 /* From now on, ignore SIGPIPE and let the error checking
1961 do the work. */
1962#ifdef SIGPIPE
1963 (void) signal(SIGPIPE, SIG_IGN);
1964#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00001965
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001966 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001967
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001968}
1969
1970
Guido van Rossumb6775db1994-08-01 11:34:53 +00001971/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001972
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001973static PyTypeObject sock_type = {
Guido van Rossum55558541996-05-23 22:54:50 +00001974 PyObject_HEAD_INIT(0) /* Must fill in type value later */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001975 0, /* ob_size */
Guido van Rossumca6dfa52001-10-28 12:31:33 +00001976 "_socket.socket", /* tp_name */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001977 sizeof(PySocketSockObject), /* tp_basicsize */
1978 0, /* tp_itemsize */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001979 (destructor)sock_dealloc, /* tp_dealloc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001980 0, /* tp_print */
1981 0, /* tp_getattr */
1982 0, /* tp_setattr */
1983 0, /* tp_compare */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001984 (reprfunc)sock_repr, /* tp_repr */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001985 0, /* tp_as_number */
1986 0, /* tp_as_sequence */
1987 0, /* tp_as_mapping */
1988 0, /* tp_hash */
1989 0, /* tp_call */
1990 0, /* tp_str */
Michael W. Hudson03f96bd2001-11-09 10:06:23 +00001991 0, /* set below */ /* tp_getattro */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001992 0, /* tp_setattro */
1993 0, /* tp_as_buffer */
1994 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001995 sock_doc, /* tp_doc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001996 0, /* tp_traverse */
1997 0, /* tp_clear */
1998 0, /* tp_richcompare */
1999 0, /* tp_weaklistoffset */
2000 0, /* tp_iter */
2001 0, /* tp_iternext */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002002 sock_methods, /* tp_methods */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002003 0, /* tp_members */
2004 0, /* tp_getset */
2005 0, /* tp_base */
2006 0, /* tp_dict */
2007 0, /* tp_descr_get */
2008 0, /* tp_descr_set */
2009 0, /* tp_dictoffset */
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002010 sock_initobj, /* tp_init */
Michael W. Hudson03f96bd2001-11-09 10:06:23 +00002011 0, /* set below */ /* tp_alloc */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002012 sock_new, /* tp_new */
Michael W. Hudson03f96bd2001-11-09 10:06:23 +00002013 0, /* set below */ /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002014};
2015
Guido van Rossum30a685f1991-06-27 15:51:29 +00002016
Guido van Rossum81194471991-07-27 21:42:02 +00002017/* Python interface to gethostname(). */
2018
2019/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002020static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002021socket_gethostname(PyObject *self, PyObject *args)
Guido van Rossum81194471991-07-27 21:42:02 +00002022{
2023 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00002024 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002025 if (!PyArg_ParseTuple(args, ":gethostname"))
Guido van Rossum81194471991-07-27 21:42:02 +00002026 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002027 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002028 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00002029 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002030 if (res < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002031 return set_error();
Guido van Rossum81194471991-07-27 21:42:02 +00002032 buf[sizeof buf - 1] = '\0';
Guido van Rossum73624e91994-10-10 17:59:00 +00002033 return PyString_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00002034}
Guido van Rossumff4949e1992-08-05 19:58:53 +00002035
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002036PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002037"gethostname() -> string\n\
2038\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002039Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002040
Guido van Rossumff4949e1992-08-05 19:58:53 +00002041
Guido van Rossum30a685f1991-06-27 15:51:29 +00002042/* Python interface to gethostbyname(name). */
2043
2044/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002045static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002046socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002047{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002048 char *name;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002049 struct sockaddr_storage addrbuf;
2050
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002051 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002052 return NULL;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002053 if (setipaddr(name, (struct sockaddr *)&addrbuf, sizeof(addrbuf), AF_INET) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002054 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002055 return makeipaddr((struct sockaddr *)&addrbuf,
2056 sizeof(struct sockaddr_in));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002057}
2058
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002059PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002060"gethostbyname(host) -> address\n\
2061\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002062Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002063
2064
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002065/* Convenience function common to gethostbyname_ex and gethostbyaddr */
2066
2067static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002068gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002069{
2070 char **pch;
2071 PyObject *rtn_tuple = (PyObject *)NULL;
2072 PyObject *name_list = (PyObject *)NULL;
2073 PyObject *addr_list = (PyObject *)NULL;
2074 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002075
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002076 if (h == NULL) {
Guido van Rossum48a680c2001-03-02 06:34:14 +00002077 /* Let's get real error message to return */
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002078#ifndef RISCOS
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002079 set_herror(h_errno);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002080#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002081 PyErr_SetString(socket_error, "host not found");
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002082#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002083 return NULL;
2084 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002085
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002086 if (h->h_addrtype != af) {
2087#ifdef HAVE_STRERROR
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002088 /* Let's get real error message to return */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002089 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002090 (char *)strerror(EAFNOSUPPORT));
2091#else
2092 PyErr_SetString(
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002093 socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002094 "Address family not supported by protocol family");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002095#endif
2096 return NULL;
2097 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002098
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002099 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002100
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002101 case AF_INET:
2102 if (alen < sizeof(struct sockaddr_in))
2103 return NULL;
2104 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002105
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002106#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002107 case AF_INET6:
2108 if (alen < sizeof(struct sockaddr_in6))
2109 return NULL;
2110 break;
2111#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002112
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002113 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002114
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002115 if ((name_list = PyList_New(0)) == NULL)
2116 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002117
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002118 if ((addr_list = PyList_New(0)) == NULL)
2119 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002120
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002121 for (pch = h->h_aliases; *pch != NULL; pch++) {
2122 int status;
2123 tmp = PyString_FromString(*pch);
2124 if (tmp == NULL)
2125 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002126
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002127 status = PyList_Append(name_list, tmp);
2128 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002129
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002130 if (status)
2131 goto err;
2132 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002133
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002134 for (pch = h->h_addr_list; *pch != NULL; pch++) {
2135 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002136
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002137 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002138
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002139 case AF_INET:
2140 {
2141 struct sockaddr_in sin;
2142 memset(&sin, 0, sizeof(sin));
2143 sin.sin_family = af;
2144#ifdef HAVE_SOCKADDR_SA_LEN
2145 sin.sin_len = sizeof(sin);
2146#endif
2147 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
2148 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002149
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002150 if (pch == h->h_addr_list && alen >= sizeof(sin))
2151 memcpy((char *) addr, &sin, sizeof(sin));
2152 break;
2153 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002154
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002155#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002156 case AF_INET6:
2157 {
2158 struct sockaddr_in6 sin6;
2159 memset(&sin6, 0, sizeof(sin6));
2160 sin6.sin6_family = af;
2161#ifdef HAVE_SOCKADDR_SA_LEN
2162 sin6.sin6_len = sizeof(sin6);
2163#endif
2164 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
2165 tmp = makeipaddr((struct sockaddr *)&sin6,
2166 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002167
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002168 if (pch == h->h_addr_list && alen >= sizeof(sin6))
2169 memcpy((char *) addr, &sin6, sizeof(sin6));
2170 break;
2171 }
2172#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002173
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002174 default: /* can't happen */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002175 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002176 "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002177 return NULL;
2178 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002179
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002180 if (tmp == NULL)
2181 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002182
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002183 status = PyList_Append(addr_list, tmp);
2184 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002185
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002186 if (status)
2187 goto err;
2188 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002189
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002190 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002191
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002192 err:
2193 Py_XDECREF(name_list);
2194 Py_XDECREF(addr_list);
2195 return rtn_tuple;
2196}
2197
2198
2199/* Python interface to gethostbyname_ex(name). */
2200
2201/*ARGSUSED*/
2202static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002203socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002204{
2205 char *name;
2206 struct hostent *h;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002207 struct sockaddr_storage addr;
Martin v. Löwis9db2f572001-07-23 01:30:10 +00002208 struct sockaddr *sa;
Guido van Rossum955becc1999-03-22 20:14:53 +00002209 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002210#ifdef HAVE_GETHOSTBYNAME_R
2211 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002212#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
2213 struct hostent_data data;
2214#else
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002215 char buf[16384];
2216 int buf_len = (sizeof buf) - 1;
2217 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002218#endif
2219#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002220 int result;
2221#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002222#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002223
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002224 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002225 return NULL;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002226 if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), PF_INET) < 0)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002227 return NULL;
2228 Py_BEGIN_ALLOW_THREADS
2229#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002230#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002231 result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
2232 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002233#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002234 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002235#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00002236 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002237 result = gethostbyname_r(name, &hp_allocated, &data);
2238 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002239#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002240#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002241#ifdef USE_GETHOSTBYNAME_LOCK
2242 PyThread_acquire_lock(gethostbyname_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002243#endif
2244 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002245#endif /* HAVE_GETHOSTBYNAME_R */
2246 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002247 /* Some C libraries would require addr.__ss_family instead of
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002248 addr.ss_family.
2249 Therefore, we cast the sockaddr_storage into sockaddr to
2250 access sa_family. */
Martin v. Löwis9db2f572001-07-23 01:30:10 +00002251 sa = (struct sockaddr*)&addr;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002252 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002253 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002254#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum955becc1999-03-22 20:14:53 +00002255 PyThread_release_lock(gethostbyname_lock);
2256#endif
2257 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002258}
2259
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002260PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002261"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
2262\n\
2263Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002264for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002265
2266
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002267/* Python interface to gethostbyaddr(IP). */
2268
2269/*ARGSUSED*/
2270static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002271socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002272{
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002273#ifdef ENABLE_IPV6
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002274 struct sockaddr_storage addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002275#else
Guido van Rossum48a680c2001-03-02 06:34:14 +00002276 struct sockaddr_in addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002277#endif
2278 struct sockaddr *sa = (struct sockaddr *)&addr;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002279 char *ip_num;
2280 struct hostent *h;
Guido van Rossum3baaa131999-03-22 21:44:51 +00002281 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002282#ifdef HAVE_GETHOSTBYNAME_R
2283 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002284#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
2285 struct hostent_data data;
2286#else
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002287 char buf[16384];
2288 int buf_len = (sizeof buf) - 1;
2289 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002290#endif
2291#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002292 int result;
2293#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002294#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002295 char *ap;
2296 int al;
2297 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002298
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002299 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002300 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002301 af = PF_UNSPEC;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002302 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002303 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002304 af = sa->sa_family;
2305 ap = NULL;
2306 al = 0;
2307 switch (af) {
2308 case AF_INET:
2309 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
2310 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
2311 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002312#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002313 case AF_INET6:
2314 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
2315 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
2316 break;
2317#endif
2318 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002319 PyErr_SetString(socket_error, "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002320 return NULL;
2321 }
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002322 Py_BEGIN_ALLOW_THREADS
2323#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002324#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002325 result = gethostbyaddr_r(ap, al, af,
2326 &hp_allocated, buf, buf_len,
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002327 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002328#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002329 h = gethostbyaddr_r(ap, al, af,
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002330 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002331#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00002332 memset((void *) &data, '\0', sizeof(data));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002333 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002334 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002335#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002336#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002337#ifdef USE_GETHOSTBYNAME_LOCK
2338 PyThread_acquire_lock(gethostbyname_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002339#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002340 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002341#endif /* HAVE_GETHOSTBYNAME_R */
2342 Py_END_ALLOW_THREADS
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002343 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002344#ifdef USE_GETHOSTBYNAME_LOCK
2345 PyThread_release_lock(gethostbyname_lock);
2346#endif
2347 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002348}
2349
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002350PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002351"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
2352\n\
2353Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002354for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002355
Guido van Rossum30a685f1991-06-27 15:51:29 +00002356
2357/* Python interface to getservbyname(name).
2358 This only returns the port number, since the other info is already
2359 known or not useful (like the list of aliases). */
2360
2361/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002362static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002363socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002364{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002365 char *name, *proto;
Guido van Rossum30a685f1991-06-27 15:51:29 +00002366 struct servent *sp;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002367 if (!PyArg_ParseTuple(args, "ss:getservbyname", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002368 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002369 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002370 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00002371 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00002372 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002373 PyErr_SetString(socket_error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00002374 return NULL;
2375 }
Guido van Rossum73624e91994-10-10 17:59:00 +00002376 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002377}
2378
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002379PyDoc_STRVAR(getservbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002380"getservbyname(servicename, protocolname) -> integer\n\
2381\n\
2382Return a port number from a service name and protocol name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002383The protocol name should be 'tcp' or 'udp'.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002384
Guido van Rossum30a685f1991-06-27 15:51:29 +00002385
Guido van Rossum3901d851996-12-19 16:35:04 +00002386/* Python interface to getprotobyname(name).
2387 This only returns the protocol number, since the other info is
2388 already known or not useful (like the list of aliases). */
2389
2390/*ARGSUSED*/
2391static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002392socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00002393{
2394 char *name;
2395 struct protoent *sp;
Guido van Rossumbcc20741998-08-04 22:53:56 +00002396#ifdef __BEOS__
2397/* Not available in BeOS yet. - [cjh] */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002398 PyErr_SetString(socket_error, "getprotobyname not supported");
Guido van Rossumbcc20741998-08-04 22:53:56 +00002399 return NULL;
2400#else
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002401 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
Guido van Rossum3901d851996-12-19 16:35:04 +00002402 return NULL;
2403 Py_BEGIN_ALLOW_THREADS
2404 sp = getprotobyname(name);
2405 Py_END_ALLOW_THREADS
2406 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002407 PyErr_SetString(socket_error, "protocol not found");
Guido van Rossum3901d851996-12-19 16:35:04 +00002408 return NULL;
2409 }
2410 return PyInt_FromLong((long) sp->p_proto);
Guido van Rossumbcc20741998-08-04 22:53:56 +00002411#endif
Guido van Rossum3901d851996-12-19 16:35:04 +00002412}
2413
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002414PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002415"getprotobyname(name) -> integer\n\
2416\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002417Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002418
Guido van Rossum3901d851996-12-19 16:35:04 +00002419
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002420#ifndef NO_DUP
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002421/* Create a socket object from a numeric file description.
2422 Useful e.g. if stdin is a socket.
2423 Additional arguments as for socket(). */
2424
2425/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002426static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002427socket_fromfd(PyObject *self, PyObject *args)
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002428{
Guido van Rossum73624e91994-10-10 17:59:00 +00002429 PySocketSockObject *s;
Fred Drakea04eaad2000-06-30 02:46:07 +00002430 SOCKET_T fd;
2431 int family, type, proto = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002432 if (!PyArg_ParseTuple(args, "iii|i:fromfd",
2433 &fd, &family, &type, &proto))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002434 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +00002435 /* Dup the fd so it and the socket can be closed independently */
2436 fd = dup(fd);
2437 if (fd < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002438 return set_error();
2439 s = new_sockobject(fd, family, type, proto);
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002440 /* From now on, ignore SIGPIPE and let the error checking
2441 do the work. */
Guido van Rossum48a680c2001-03-02 06:34:14 +00002442#ifdef SIGPIPE
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002443 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum48a680c2001-03-02 06:34:14 +00002444#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00002445 return (PyObject *) s;
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002446}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002447
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002448PyDoc_STRVAR(fromfd_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002449"fromfd(fd, family, type[, proto]) -> socket object\n\
2450\n\
2451Create a socket object from the given file descriptor.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002452The remaining arguments are the same as for socket().");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002453
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002454#endif /* NO_DUP */
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002455
Guido van Rossum82a5c661998-07-07 20:45:43 +00002456
Guido van Rossum006bf911996-06-12 04:04:55 +00002457static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002458socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00002459{
2460 int x1, x2;
2461
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002462 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00002463 return NULL;
2464 }
2465 x2 = (int)ntohs((short)x1);
2466 return PyInt_FromLong(x2);
2467}
2468
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002469PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002470"ntohs(integer) -> integer\n\
2471\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002472Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002473
2474
Guido van Rossum006bf911996-06-12 04:04:55 +00002475static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002476socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00002477{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002478 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00002479
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002480 if (PyInt_Check(arg)) {
2481 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002482 if (x == (unsigned long) -1 && PyErr_Occurred())
2483 return NULL;
Guido van Rossum006bf911996-06-12 04:04:55 +00002484 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002485 else if (PyLong_Check(arg)) {
2486 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002487 if (x == (unsigned long) -1 && PyErr_Occurred())
2488 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002489#if SIZEOF_LONG > 4
2490 {
2491 unsigned long y;
2492 /* only want the trailing 32 bits */
2493 y = x & 0xFFFFFFFFUL;
2494 if (y ^ x)
2495 return PyErr_Format(PyExc_OverflowError,
2496 "long int larger than 32 bits");
2497 x = y;
2498 }
2499#endif
2500 }
2501 else
2502 return PyErr_Format(PyExc_TypeError,
2503 "expected int/long, %s found",
2504 arg->ob_type->tp_name);
2505 if (x == (unsigned long) -1 && PyErr_Occurred())
2506 return NULL;
2507 return PyInt_FromLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00002508}
2509
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002510PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002511"ntohl(integer) -> integer\n\
2512\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002513Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002514
2515
Guido van Rossum006bf911996-06-12 04:04:55 +00002516static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002517socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00002518{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002519 unsigned long x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00002520
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002521 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00002522 return NULL;
2523 }
2524 x2 = (int)htons((short)x1);
2525 return PyInt_FromLong(x2);
2526}
2527
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002528PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002529"htons(integer) -> integer\n\
2530\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002531Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002532
2533
Guido van Rossum006bf911996-06-12 04:04:55 +00002534static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002535socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00002536{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002537 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00002538
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002539 if (PyInt_Check(arg)) {
2540 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002541 if (x == (unsigned long) -1 && PyErr_Occurred())
2542 return NULL;
Guido van Rossum006bf911996-06-12 04:04:55 +00002543 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002544 else if (PyLong_Check(arg)) {
2545 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002546 if (x == (unsigned long) -1 && PyErr_Occurred())
2547 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002548#if SIZEOF_LONG > 4
2549 {
2550 unsigned long y;
2551 /* only want the trailing 32 bits */
2552 y = x & 0xFFFFFFFFUL;
2553 if (y ^ x)
2554 return PyErr_Format(PyExc_OverflowError,
2555 "long int larger than 32 bits");
2556 x = y;
2557 }
2558#endif
2559 }
2560 else
2561 return PyErr_Format(PyExc_TypeError,
2562 "expected int/long, %s found",
2563 arg->ob_type->tp_name);
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002564 return PyInt_FromLong(htonl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00002565}
2566
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002567PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002568"htonl(integer) -> integer\n\
2569\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002570Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002571
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002572/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002573
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002574PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002575"inet_aton(string) -> packed 32-bit IP representation\n\
2576\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002577Convert 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 +00002578binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002579
2580static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002581socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002582{
Guido van Rossuma2e48551999-09-09 15:42:59 +00002583#ifndef INADDR_NONE
2584#define INADDR_NONE (-1)
2585#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002586
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002587 /* Have to use inet_addr() instead */
2588 char *ip_addr;
Fred Drakee5065292001-07-19 21:16:41 +00002589 unsigned long packed_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002590
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002591 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) {
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002592 return NULL;
2593 }
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002594 packed_addr = inet_addr(ip_addr);
2595
2596 if (packed_addr == INADDR_NONE) { /* invalid address */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002597 PyErr_SetString(socket_error,
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002598 "illegal IP address string passed to inet_aton");
2599 return NULL;
2600 }
2601
2602 return PyString_FromStringAndSize((char *) &packed_addr,
2603 sizeof(packed_addr));
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002604}
2605
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002606PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00002607"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002608\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002609Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002610
2611static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002612socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002613{
2614 char *packed_str;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002615 int addr_len;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002616 struct in_addr packed_addr;
2617
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002618 if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) {
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002619 return NULL;
2620 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00002621
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002622 if (addr_len != sizeof(packed_addr)) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002623 PyErr_SetString(socket_error,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002624 "packed IP wrong length for inet_ntoa");
2625 return NULL;
2626 }
2627
2628 memcpy(&packed_addr, packed_str, addr_len);
2629
2630 return PyString_FromString(inet_ntoa(packed_addr));
2631}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002632
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002633/* Python interface to getaddrinfo(host, port). */
2634
2635/*ARGSUSED*/
2636static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002637socket_getaddrinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002638{
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00002639 struct addrinfo hints, *res;
2640 struct addrinfo *res0 = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002641 PyObject *pobj = (PyObject *)NULL;
Martin v. Löwisf65b1a12001-08-12 09:28:40 +00002642 char pbuf[30];
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002643 char *hptr, *pptr;
2644 int family, socktype, protocol, flags;
2645 int error;
2646 PyObject *all = (PyObject *)NULL;
2647 PyObject *single = (PyObject *)NULL;
2648
2649 family = socktype = protocol = flags = 0;
2650 family = PF_UNSPEC;
2651 if (!PyArg_ParseTuple(args, "zO|iiii:getaddrinfo",
2652 &hptr, &pobj, &family, &socktype,
2653 &protocol, &flags)) {
2654 return NULL;
2655 }
2656 if (PyInt_Check(pobj)) {
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00002657 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002658 pptr = pbuf;
2659 } else if (PyString_Check(pobj)) {
2660 pptr = PyString_AsString(pobj);
2661 } else if (pobj == Py_None) {
2662 pptr = (char *)NULL;
2663 } else {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002664 PyErr_SetString(socket_error, "Int or String expected");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002665 return NULL;
2666 }
2667 memset(&hints, 0, sizeof(hints));
2668 hints.ai_family = family;
2669 hints.ai_socktype = socktype;
2670 hints.ai_protocol = protocol;
2671 hints.ai_flags = flags;
2672 error = getaddrinfo(hptr, pptr, &hints, &res0);
2673 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002674 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002675 return NULL;
2676 }
2677
2678 if ((all = PyList_New(0)) == NULL)
2679 goto err;
2680 for (res = res0; res; res = res->ai_next) {
Guido van Rossum716aac02001-10-12 18:59:27 +00002681 PyObject *addr =
2682 makesockaddr(-1, res->ai_addr, res->ai_addrlen);
2683 if (addr == NULL)
2684 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002685 single = Py_BuildValue("iiisO", res->ai_family,
2686 res->ai_socktype, res->ai_protocol,
2687 res->ai_canonname ? res->ai_canonname : "",
Guido van Rossum716aac02001-10-12 18:59:27 +00002688 addr);
2689 Py_DECREF(addr);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002690 if (single == NULL)
2691 goto err;
2692
2693 if (PyList_Append(all, single))
2694 goto err;
2695 Py_XDECREF(single);
2696 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002697 return all;
2698 err:
2699 Py_XDECREF(single);
2700 Py_XDECREF(all);
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00002701 if (res0)
2702 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002703 return (PyObject *)NULL;
2704}
2705
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002706PyDoc_STRVAR(getaddrinfo_doc,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002707"socket.getaddrinfo(host, port [, family, socktype, proto, flags])\n\
2708 --> List of (family, socktype, proto, canonname, sockaddr)\n\
2709\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002710Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002711
2712/* Python interface to getnameinfo(sa, flags). */
2713
2714/*ARGSUSED*/
2715static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002716socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002717{
2718 PyObject *sa = (PyObject *)NULL;
2719 int flags;
2720 char *hostp;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00002721 int port, flowinfo, scope_id;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002722 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
2723 struct addrinfo hints, *res = NULL;
2724 int error;
2725 PyObject *ret = (PyObject *)NULL;
2726
2727 flags = flowinfo = scope_id = 0;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00002728 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002729 return NULL;
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002730 if (!PyArg_ParseTuple(sa, "si|ii",
2731 &hostp, &port, &flowinfo, &scope_id))
Martin v. Löwis06b1d212001-11-02 23:34:52 +00002732 return NULL;
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00002733 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002734 memset(&hints, 0, sizeof(hints));
2735 hints.ai_family = PF_UNSPEC;
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00002736 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002737 error = getaddrinfo(hostp, pbuf, &hints, &res);
2738 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002739 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002740 goto fail;
2741 }
2742 if (res->ai_next) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002743 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002744 "sockaddr resolved to multiple addresses");
2745 goto fail;
2746 }
2747 switch (res->ai_family) {
2748 case AF_INET:
2749 {
2750 char *t1;
2751 int t2;
2752 if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002753 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002754 "IPv4 sockaddr must be 2 tuple");
2755 goto fail;
2756 }
2757 break;
2758 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002759#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002760 case AF_INET6:
2761 {
2762 struct sockaddr_in6 *sin6;
2763 sin6 = (struct sockaddr_in6 *)res->ai_addr;
2764 sin6->sin6_flowinfo = flowinfo;
2765 sin6->sin6_scope_id = scope_id;
2766 break;
2767 }
2768#endif
2769 }
2770 error = getnameinfo(res->ai_addr, res->ai_addrlen,
2771 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
2772 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002773 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002774 goto fail;
2775 }
2776 ret = Py_BuildValue("ss", hbuf, pbuf);
2777
2778fail:
2779 if (res)
2780 freeaddrinfo(res);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002781 return ret;
2782}
2783
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002784PyDoc_STRVAR(getnameinfo_doc,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002785"socket.getnameinfo(sockaddr, flags) --> (host, port)\n\
2786\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002787Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002788
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00002789
2790/* Python API to getting and setting the default timeout value. */
2791
2792static PyObject *
2793socket_getdefaulttimeout(PyObject *self)
2794{
2795 if (defaulttimeout < 0.0) {
2796 Py_INCREF(Py_None);
2797 return Py_None;
2798 }
2799 else
2800 return PyFloat_FromDouble(defaulttimeout);
2801}
2802
2803PyDoc_STRVAR(getdefaulttimeout_doc,
2804"socket.getdefaulttimeout() -> None | float\n\
2805\n\
2806Returns the default timeout in floating seconds for new socket objects.\n\
2807A value of None indicates that new socket objects have no timeout.\n\
2808When the socket module is first imported, the default is None.");
2809
2810static PyObject *
2811socket_setdefaulttimeout(PyObject *self, PyObject *arg)
2812{
2813 double timeout;
2814
2815 if (arg == Py_None)
2816 timeout = -1.0;
2817 else {
2818 timeout = PyFloat_AsDouble(arg);
2819 if (timeout < 0.0) {
2820 if (!PyErr_Occurred())
2821 PyErr_SetString(PyExc_ValueError,
2822 "Timeout value out of range");
2823 return NULL;
2824 }
2825 }
2826
2827 defaulttimeout = timeout;
2828
2829 Py_INCREF(Py_None);
2830 return Py_None;
2831}
2832
2833PyDoc_STRVAR(setdefaulttimeout_doc,
2834"socket.setdefaulttimeout(None | float)\n\
2835\n\
2836Set the default timeout in floating seconds for new socket objects.\n\
2837A value of None indicates that new socket objects have no timeout.\n\
2838When the socket module is first imported, the default is None.");
2839
2840
Guido van Rossum30a685f1991-06-27 15:51:29 +00002841/* List of functions exported by this module. */
2842
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002843static PyMethodDef socket_methods[] = {
2844 {"gethostbyname", socket_gethostbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002845 METH_VARARGS, gethostbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002846 {"gethostbyname_ex", socket_gethostbyname_ex,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002847 METH_VARARGS, ghbn_ex_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002848 {"gethostbyaddr", socket_gethostbyaddr,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002849 METH_VARARGS, gethostbyaddr_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002850 {"gethostname", socket_gethostname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002851 METH_VARARGS, gethostname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002852 {"getservbyname", socket_getservbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002853 METH_VARARGS, getservbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002854 {"getprotobyname", socket_getprotobyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002855 METH_VARARGS,getprotobyname_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002856#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002857 {"fromfd", socket_fromfd,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002858 METH_VARARGS, fromfd_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002859#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002860 {"ntohs", socket_ntohs,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002861 METH_VARARGS, ntohs_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002862 {"ntohl", socket_ntohl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002863 METH_O, ntohl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002864 {"htons", socket_htons,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002865 METH_VARARGS, htons_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002866 {"htonl", socket_htonl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002867 METH_O, htonl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002868 {"inet_aton", socket_inet_aton,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002869 METH_VARARGS, inet_aton_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002870 {"inet_ntoa", socket_inet_ntoa,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002871 METH_VARARGS, inet_ntoa_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002872 {"getaddrinfo", socket_getaddrinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002873 METH_VARARGS, getaddrinfo_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002874 {"getnameinfo", socket_getnameinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002875 METH_VARARGS, getnameinfo_doc},
Guido van Rossum1693ba82002-07-18 21:11:26 +00002876 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00002877 METH_NOARGS, getdefaulttimeout_doc},
2878 {"setdefaulttimeout", socket_setdefaulttimeout,
2879 METH_O, setdefaulttimeout_doc},
Guido van Rossum30a685f1991-06-27 15:51:29 +00002880 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002881};
2882
Guido van Rossum30a685f1991-06-27 15:51:29 +00002883
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002884#ifdef RISCOS
2885#define OS_INIT_DEFINED
Guido van Rossumbe32c891996-06-20 16:25:29 +00002886
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002887static int
2888os_init(void)
2889{
2890 _kernel_swi_regs r;
2891
2892 r.r[0] = 0;
2893 _kernel_swi(0x43380, &r, &r);
2894 taskwindow = r.r[0];
2895
2896 return 0;
2897}
2898
2899#endif /* RISCOS */
2900
2901
2902#ifdef MS_WINDOWS
2903#define OS_INIT_DEFINED
2904
2905/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00002906
2907static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002908os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00002909{
2910 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00002911}
2912
2913static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002914os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00002915{
2916 WSADATA WSAData;
2917 int ret;
2918 char buf[100];
2919 ret = WSAStartup(0x0101, &WSAData);
2920 switch (ret) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002921 case 0: /* No error */
2922 atexit(os_cleanup);
2923 return 1; /* Success */
Guido van Rossumbe32c891996-06-20 16:25:29 +00002924 case WSASYSNOTREADY:
2925 PyErr_SetString(PyExc_ImportError,
2926 "WSAStartup failed: network not ready");
2927 break;
2928 case WSAVERNOTSUPPORTED:
2929 case WSAEINVAL:
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002930 PyErr_SetString(
2931 PyExc_ImportError,
2932 "WSAStartup failed: requested version not supported");
Guido van Rossumbe32c891996-06-20 16:25:29 +00002933 break;
2934 default:
Tim Peters885d4572001-11-28 20:27:42 +00002935 PyOS_snprintf(buf, sizeof(buf),
2936 "WSAStartup failed: error code %d", ret);
Guido van Rossumbe32c891996-06-20 16:25:29 +00002937 PyErr_SetString(PyExc_ImportError, buf);
2938 break;
2939 }
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002940 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00002941}
2942
Guido van Rossum8d665e61996-06-26 18:22:49 +00002943#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00002944
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002945
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002946#ifdef PYOS_OS2
2947#define OS_INIT_DEFINED
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002948
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002949/* Additional initialization for OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002950
2951static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002952os_init(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002953{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002954#ifndef PYCC_GCC
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002955 char reason[64];
2956 int rc = sock_init();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002957
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002958 if (rc == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002959 return 1; /* Success */
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002960 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002961
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002962 PyOS_snprintf(reason, sizeof(reason),
2963 "OS/2 TCP/IP Error# %d", sock_errno());
2964 PyErr_SetString(PyExc_ImportError, reason);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002965
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002966 return 0; /* Failure */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00002967#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002968 /* No need to initialise sockets with GCC/EMX */
2969 return 1; /* Success */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00002970#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002971}
2972
2973#endif /* PYOS_OS2 */
2974
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002975
2976#ifndef OS_INIT_DEFINED
2977static int
2978os_init(void)
2979{
2980 return 1; /* Success */
2981}
2982#endif
2983
2984
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002985/* C API table - always add new things to the end for binary
2986 compatibility. */
2987static
2988PySocketModule_APIObject PySocketModuleAPI =
2989{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002990 &sock_type,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002991};
2992
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002993
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002994/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002995
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002996 This module is actually called "_socket", and there's a wrapper
2997 "socket.py" which implements some additional functionality. On some
2998 platforms (e.g. Windows and OS/2), socket.py also implements a
2999 wrapper for the socket type that provides missing functionality such
3000 as makefile(), dup() and fromfd(). The import of "_socket" may fail
3001 with an ImportError exception if os-specific initialization fails.
3002 On Windows, this does WINSOCK initialization. When WINSOCK is
3003 initialized succesfully, a call to WSACleanup() is scheduled to be
3004 made at exit time.
3005*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003006
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003007PyDoc_STRVAR(socket_doc,
Fred Drakea136d492000-08-16 14:18:30 +00003008"Implementation module for socket operations. See the socket module\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003009for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003010
Guido van Rossum3886bb61998-12-04 18:50:17 +00003011DL_EXPORT(void)
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003012init_socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003013{
Fred Drake4baedc12002-04-01 14:53:37 +00003014 PyObject *m;
Fred Drake4baedc12002-04-01 14:53:37 +00003015
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003016 if (!os_init())
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003017 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003018
3019 sock_type.ob_type = &PyType_Type;
3020 sock_type.tp_getattro = PyObject_GenericGetAttr;
3021 sock_type.tp_alloc = PyType_GenericAlloc;
3022 sock_type.tp_free = PyObject_Del;
3023 m = Py_InitModule3(PySocket_MODULE_NAME,
3024 socket_methods,
3025 socket_doc);
3026
3027 socket_error = PyErr_NewException("socket.error", NULL, NULL);
3028 if (socket_error == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003029 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003030 Py_INCREF(socket_error);
3031 PyModule_AddObject(m, "error", socket_error);
3032 socket_herror = PyErr_NewException("socket.herror",
3033 socket_error, NULL);
3034 if (socket_herror == NULL)
3035 return;
3036 Py_INCREF(socket_herror);
3037 PyModule_AddObject(m, "herror", socket_herror);
3038 socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003039 NULL);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003040 if (socket_gaierror == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003041 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003042 Py_INCREF(socket_gaierror);
3043 PyModule_AddObject(m, "gaierror", socket_gaierror);
3044 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00003045 if (PyModule_AddObject(m, "SocketType",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003046 (PyObject *)&sock_type) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003047 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003048 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00003049 if (PyModule_AddObject(m, "socket",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003050 (PyObject *)&sock_type) != 0)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003051 return;
Guido van Rossum09be4091999-08-09 14:40:40 +00003052
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003053 /* Export C API */
Fred Drake4baedc12002-04-01 14:53:37 +00003054 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003055 PyCObject_FromVoidPtr((void *)&PySocketModuleAPI, NULL)
3056 ) != 0)
3057 return;
3058
Guido van Rossum09be4091999-08-09 14:40:40 +00003059 /* Address families (we only support AF_INET and AF_UNIX) */
3060#ifdef AF_UNSPEC
Fred Drake4baedc12002-04-01 14:53:37 +00003061 PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00003062#endif
Fred Drake4baedc12002-04-01 14:53:37 +00003063 PyModule_AddIntConstant(m, "AF_INET", AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003064#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00003065 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003066#endif /* AF_INET6 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003067#ifdef AF_UNIX
Fred Drake4baedc12002-04-01 14:53:37 +00003068 PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003069#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00003070#ifdef AF_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00003071 /* Amateur Radio AX.25 */
3072 PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003073#endif
3074#ifdef AF_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00003075 PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00003076#endif
3077#ifdef AF_APPLETALK
Fred Drake4baedc12002-04-01 14:53:37 +00003078 /* Appletalk DDP */
3079 PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00003080#endif
3081#ifdef AF_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00003082 /* Amateur radio NetROM */
3083 PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00003084#endif
3085#ifdef AF_BRIDGE
Fred Drake4baedc12002-04-01 14:53:37 +00003086 /* Multiprotocol bridge */
3087 PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003088#endif
3089#ifdef AF_AAL5
Fred Drake4baedc12002-04-01 14:53:37 +00003090 /* Reserved for Werner's ATM */
3091 PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00003092#endif
3093#ifdef AF_X25
Fred Drake4baedc12002-04-01 14:53:37 +00003094 /* Reserved for X.25 project */
3095 PyModule_AddIntConstant(m, "AF_X25", AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003096#endif
3097#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00003098 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00003099#endif
3100#ifdef AF_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00003101 /* Amateur Radio X.25 PLP */
3102 PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003103#endif
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00003104#ifdef HAVE_NETPACKET_PACKET_H
Fred Drake4baedc12002-04-01 14:53:37 +00003105 PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET);
3106 PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET);
3107 PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST);
3108 PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST);
3109 PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST);
3110 PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST);
3111 PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING);
3112 PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK);
3113 PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00003114#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003115
3116 /* Socket types */
Fred Drake4baedc12002-04-01 14:53:37 +00003117 PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
3118 PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00003119#ifndef __BEOS__
3120/* We have incomplete socket support. */
Fred Drake4baedc12002-04-01 14:53:37 +00003121 PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
3122 PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
3123 PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00003124#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003125
3126#ifdef SO_DEBUG
Fred Drake4baedc12002-04-01 14:53:37 +00003127 PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003128#endif
3129#ifdef SO_ACCEPTCONN
Fred Drake4baedc12002-04-01 14:53:37 +00003130 PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003131#endif
3132#ifdef SO_REUSEADDR
Fred Drake4baedc12002-04-01 14:53:37 +00003133 PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003134#endif
3135#ifdef SO_KEEPALIVE
Fred Drake4baedc12002-04-01 14:53:37 +00003136 PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003137#endif
3138#ifdef SO_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00003139 PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003140#endif
3141#ifdef SO_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00003142 PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003143#endif
3144#ifdef SO_USELOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00003145 PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003146#endif
3147#ifdef SO_LINGER
Fred Drake4baedc12002-04-01 14:53:37 +00003148 PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003149#endif
3150#ifdef SO_OOBINLINE
Fred Drake4baedc12002-04-01 14:53:37 +00003151 PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003152#endif
3153#ifdef SO_REUSEPORT
Fred Drake4baedc12002-04-01 14:53:37 +00003154 PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003155#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003156#ifdef SO_SNDBUF
Fred Drake4baedc12002-04-01 14:53:37 +00003157 PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003158#endif
3159#ifdef SO_RCVBUF
Fred Drake4baedc12002-04-01 14:53:37 +00003160 PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003161#endif
3162#ifdef SO_SNDLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00003163 PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003164#endif
3165#ifdef SO_RCVLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00003166 PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003167#endif
3168#ifdef SO_SNDTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00003169 PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003170#endif
3171#ifdef SO_RCVTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00003172 PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003173#endif
3174#ifdef SO_ERROR
Fred Drake4baedc12002-04-01 14:53:37 +00003175 PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003176#endif
3177#ifdef SO_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00003178 PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003179#endif
3180
3181 /* Maximum number of connections for "listen" */
3182#ifdef SOMAXCONN
Fred Drake4baedc12002-04-01 14:53:37 +00003183 PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003184#else
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003185 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003186#endif
3187
3188 /* Flags for send, recv */
3189#ifdef MSG_OOB
Fred Drake4baedc12002-04-01 14:53:37 +00003190 PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003191#endif
3192#ifdef MSG_PEEK
Fred Drake4baedc12002-04-01 14:53:37 +00003193 PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003194#endif
3195#ifdef MSG_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00003196 PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003197#endif
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00003198#ifdef MSG_DONTWAIT
Fred Drake4baedc12002-04-01 14:53:37 +00003199 PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00003200#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003201#ifdef MSG_EOR
Fred Drake4baedc12002-04-01 14:53:37 +00003202 PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003203#endif
3204#ifdef MSG_TRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00003205 PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003206#endif
3207#ifdef MSG_CTRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00003208 PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003209#endif
3210#ifdef MSG_WAITALL
Fred Drake4baedc12002-04-01 14:53:37 +00003211 PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003212#endif
3213#ifdef MSG_BTAG
Fred Drake4baedc12002-04-01 14:53:37 +00003214 PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003215#endif
3216#ifdef MSG_ETAG
Fred Drake4baedc12002-04-01 14:53:37 +00003217 PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003218#endif
3219
3220 /* Protocol level and numbers, usable for [gs]etsockopt */
3221#ifdef SOL_SOCKET
Fred Drake4baedc12002-04-01 14:53:37 +00003222 PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003223#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003224#ifdef SOL_IP
Fred Drake4baedc12002-04-01 14:53:37 +00003225 PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003226#else
Fred Drake4baedc12002-04-01 14:53:37 +00003227 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00003228#endif
3229#ifdef SOL_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00003230 PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00003231#endif
3232#ifdef SOL_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00003233 PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003234#endif
3235#ifdef SOL_ATALK
Fred Drake4baedc12002-04-01 14:53:37 +00003236 PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00003237#endif
3238#ifdef SOL_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00003239 PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00003240#endif
3241#ifdef SOL_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00003242 PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003243#endif
3244#ifdef SOL_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00003245 PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003246#else
Fred Drake4baedc12002-04-01 14:53:37 +00003247 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00003248#endif
3249#ifdef SOL_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00003250 PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003251#else
Fred Drake4baedc12002-04-01 14:53:37 +00003252 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00003253#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003254#ifdef IPPROTO_IP
Fred Drake4baedc12002-04-01 14:53:37 +00003255 PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00003256#else
Fred Drake4baedc12002-04-01 14:53:37 +00003257 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003258#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003259#ifdef IPPROTO_HOPOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003260 PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003261#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003262#ifdef IPPROTO_ICMP
Fred Drake4baedc12002-04-01 14:53:37 +00003263 PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00003264#else
Fred Drake4baedc12002-04-01 14:53:37 +00003265 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003266#endif
3267#ifdef IPPROTO_IGMP
Fred Drake4baedc12002-04-01 14:53:37 +00003268 PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003269#endif
3270#ifdef IPPROTO_GGP
Fred Drake4baedc12002-04-01 14:53:37 +00003271 PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003272#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003273#ifdef IPPROTO_IPV4
Fred Drake4baedc12002-04-01 14:53:37 +00003274 PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003275#endif
3276#ifdef IPPROTO_IPIP
Fred Drake4baedc12002-04-01 14:53:37 +00003277 PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003278#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003279#ifdef IPPROTO_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00003280 PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00003281#else
Fred Drake4baedc12002-04-01 14:53:37 +00003282 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003283#endif
3284#ifdef IPPROTO_EGP
Fred Drake4baedc12002-04-01 14:53:37 +00003285 PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003286#endif
3287#ifdef IPPROTO_PUP
Fred Drake4baedc12002-04-01 14:53:37 +00003288 PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003289#endif
3290#ifdef IPPROTO_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00003291 PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00003292#else
Fred Drake4baedc12002-04-01 14:53:37 +00003293 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003294#endif
3295#ifdef IPPROTO_IDP
Fred Drake4baedc12002-04-01 14:53:37 +00003296 PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003297#endif
3298#ifdef IPPROTO_HELLO
Fred Drake4baedc12002-04-01 14:53:37 +00003299 PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003300#endif
3301#ifdef IPPROTO_ND
Fred Drake4baedc12002-04-01 14:53:37 +00003302 PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003303#endif
3304#ifdef IPPROTO_TP
Fred Drake4baedc12002-04-01 14:53:37 +00003305 PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003306#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003307#ifdef IPPROTO_IPV6
Fred Drake4baedc12002-04-01 14:53:37 +00003308 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003309#endif
3310#ifdef IPPROTO_ROUTING
Fred Drake4baedc12002-04-01 14:53:37 +00003311 PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003312#endif
3313#ifdef IPPROTO_FRAGMENT
Fred Drake4baedc12002-04-01 14:53:37 +00003314 PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003315#endif
3316#ifdef IPPROTO_RSVP
Fred Drake4baedc12002-04-01 14:53:37 +00003317 PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003318#endif
3319#ifdef IPPROTO_GRE
Fred Drake4baedc12002-04-01 14:53:37 +00003320 PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003321#endif
3322#ifdef IPPROTO_ESP
Fred Drake4baedc12002-04-01 14:53:37 +00003323 PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003324#endif
3325#ifdef IPPROTO_AH
Fred Drake4baedc12002-04-01 14:53:37 +00003326 PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003327#endif
3328#ifdef IPPROTO_MOBILE
Fred Drake4baedc12002-04-01 14:53:37 +00003329 PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003330#endif
3331#ifdef IPPROTO_ICMPV6
Fred Drake4baedc12002-04-01 14:53:37 +00003332 PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003333#endif
3334#ifdef IPPROTO_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00003335 PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003336#endif
3337#ifdef IPPROTO_DSTOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003338 PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003339#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003340#ifdef IPPROTO_XTP
Fred Drake4baedc12002-04-01 14:53:37 +00003341 PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003342#endif
3343#ifdef IPPROTO_EON
Fred Drake4baedc12002-04-01 14:53:37 +00003344 PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003345#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003346#ifdef IPPROTO_PIM
Fred Drake4baedc12002-04-01 14:53:37 +00003347 PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003348#endif
3349#ifdef IPPROTO_IPCOMP
Fred Drake4baedc12002-04-01 14:53:37 +00003350 PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003351#endif
3352#ifdef IPPROTO_VRRP
Fred Drake4baedc12002-04-01 14:53:37 +00003353 PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003354#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003355#ifdef IPPROTO_BIP
Fred Drake4baedc12002-04-01 14:53:37 +00003356 PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003357#endif
3358/**/
3359#ifdef IPPROTO_RAW
Fred Drake4baedc12002-04-01 14:53:37 +00003360 PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00003361#else
Fred Drake4baedc12002-04-01 14:53:37 +00003362 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003363#endif
3364#ifdef IPPROTO_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00003365 PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003366#endif
3367
3368 /* Some port configuration */
3369#ifdef IPPORT_RESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00003370 PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003371#else
Fred Drake4baedc12002-04-01 14:53:37 +00003372 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003373#endif
3374#ifdef IPPORT_USERRESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00003375 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003376#else
Fred Drake4baedc12002-04-01 14:53:37 +00003377 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003378#endif
3379
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003380 /* Some reserved IP v.4 addresses */
3381#ifdef INADDR_ANY
Fred Drake4baedc12002-04-01 14:53:37 +00003382 PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003383#else
Fred Drake4baedc12002-04-01 14:53:37 +00003384 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003385#endif
3386#ifdef INADDR_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00003387 PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003388#else
Fred Drake4baedc12002-04-01 14:53:37 +00003389 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003390#endif
3391#ifdef INADDR_LOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00003392 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003393#else
Fred Drake4baedc12002-04-01 14:53:37 +00003394 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003395#endif
3396#ifdef INADDR_UNSPEC_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003397 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003398#else
Fred Drake4baedc12002-04-01 14:53:37 +00003399 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003400#endif
3401#ifdef INADDR_ALLHOSTS_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003402 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
3403 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003404#else
Fred Drake4baedc12002-04-01 14:53:37 +00003405 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003406#endif
3407#ifdef INADDR_MAX_LOCAL_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003408 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
3409 INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003410#else
Fred Drake4baedc12002-04-01 14:53:37 +00003411 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003412#endif
3413#ifdef INADDR_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00003414 PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003415#else
Fred Drake4baedc12002-04-01 14:53:37 +00003416 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003417#endif
3418
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003419 /* IPv4 [gs]etsockopt options */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003420#ifdef IP_OPTIONS
Fred Drake4baedc12002-04-01 14:53:37 +00003421 PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003422#endif
3423#ifdef IP_HDRINCL
Fred Drake4baedc12002-04-01 14:53:37 +00003424 PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003425#endif
3426#ifdef IP_TOS
Fred Drake4baedc12002-04-01 14:53:37 +00003427 PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003428#endif
3429#ifdef IP_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00003430 PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003431#endif
3432#ifdef IP_RECVOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003433 PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003434#endif
3435#ifdef IP_RECVRETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003436 PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003437#endif
3438#ifdef IP_RECVDSTADDR
Fred Drake4baedc12002-04-01 14:53:37 +00003439 PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003440#endif
3441#ifdef IP_RETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003442 PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003443#endif
3444#ifdef IP_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00003445 PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003446#endif
3447#ifdef IP_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00003448 PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003449#endif
3450#ifdef IP_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00003451 PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003452#endif
3453#ifdef IP_ADD_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00003454 PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003455#endif
3456#ifdef IP_DROP_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00003457 PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003458#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003459#ifdef IP_DEFAULT_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00003460 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
3461 IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00003462#endif
3463#ifdef IP_DEFAULT_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00003464 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
3465 IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003466#endif
3467#ifdef IP_MAX_MEMBERSHIPS
Fred Drake4baedc12002-04-01 14:53:37 +00003468 PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00003469#endif
3470
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003471 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
3472#ifdef IPV6_JOIN_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003473 PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003474#endif
3475#ifdef IPV6_LEAVE_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003476 PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003477#endif
3478#ifdef IPV6_MULTICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00003479 PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003480#endif
3481#ifdef IPV6_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00003482 PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003483#endif
3484#ifdef IPV6_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00003485 PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003486#endif
3487#ifdef IPV6_UNICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00003488 PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003489#endif
3490
Guido van Rossum09be4091999-08-09 14:40:40 +00003491 /* TCP options */
3492#ifdef TCP_NODELAY
Fred Drake4baedc12002-04-01 14:53:37 +00003493 PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00003494#endif
3495#ifdef TCP_MAXSEG
Fred Drake4baedc12002-04-01 14:53:37 +00003496 PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00003497#endif
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003498#ifdef TCP_CORK
Fred Drake4baedc12002-04-01 14:53:37 +00003499 PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003500#endif
3501#ifdef TCP_KEEPIDLE
Fred Drake4baedc12002-04-01 14:53:37 +00003502 PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003503#endif
3504#ifdef TCP_KEEPINTVL
Fred Drake4baedc12002-04-01 14:53:37 +00003505 PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003506#endif
3507#ifdef TCP_KEEPCNT
Fred Drake4baedc12002-04-01 14:53:37 +00003508 PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003509#endif
3510#ifdef TCP_SYNCNT
Fred Drake4baedc12002-04-01 14:53:37 +00003511 PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003512#endif
3513#ifdef TCP_LINGER2
Fred Drake4baedc12002-04-01 14:53:37 +00003514 PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003515#endif
3516#ifdef TCP_DEFER_ACCEPT
Fred Drake4baedc12002-04-01 14:53:37 +00003517 PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003518#endif
3519#ifdef TCP_WINDOW_CLAMP
Fred Drake4baedc12002-04-01 14:53:37 +00003520 PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003521#endif
3522#ifdef TCP_INFO
Fred Drake4baedc12002-04-01 14:53:37 +00003523 PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003524#endif
3525#ifdef TCP_QUICKACK
Fred Drake4baedc12002-04-01 14:53:37 +00003526 PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003527#endif
3528
Guido van Rossum09be4091999-08-09 14:40:40 +00003529
3530 /* IPX options */
3531#ifdef IPX_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00003532 PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003533#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003534
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003535 /* get{addr,name}info parameters */
3536#ifdef EAI_ADDRFAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00003537 PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003538#endif
3539#ifdef EAI_AGAIN
Fred Drake4baedc12002-04-01 14:53:37 +00003540 PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003541#endif
3542#ifdef EAI_BADFLAGS
Fred Drake4baedc12002-04-01 14:53:37 +00003543 PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003544#endif
3545#ifdef EAI_FAIL
Fred Drake4baedc12002-04-01 14:53:37 +00003546 PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003547#endif
3548#ifdef EAI_FAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00003549 PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003550#endif
3551#ifdef EAI_MEMORY
Fred Drake4baedc12002-04-01 14:53:37 +00003552 PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003553#endif
3554#ifdef EAI_NODATA
Fred Drake4baedc12002-04-01 14:53:37 +00003555 PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003556#endif
3557#ifdef EAI_NONAME
Fred Drake4baedc12002-04-01 14:53:37 +00003558 PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003559#endif
3560#ifdef EAI_SERVICE
Fred Drake4baedc12002-04-01 14:53:37 +00003561 PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003562#endif
3563#ifdef EAI_SOCKTYPE
Fred Drake4baedc12002-04-01 14:53:37 +00003564 PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003565#endif
3566#ifdef EAI_SYSTEM
Fred Drake4baedc12002-04-01 14:53:37 +00003567 PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003568#endif
3569#ifdef EAI_BADHINTS
Fred Drake4baedc12002-04-01 14:53:37 +00003570 PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003571#endif
3572#ifdef EAI_PROTOCOL
Fred Drake4baedc12002-04-01 14:53:37 +00003573 PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003574#endif
3575#ifdef EAI_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00003576 PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003577#endif
3578#ifdef AI_PASSIVE
Fred Drake4baedc12002-04-01 14:53:37 +00003579 PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003580#endif
3581#ifdef AI_CANONNAME
Fred Drake4baedc12002-04-01 14:53:37 +00003582 PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003583#endif
3584#ifdef AI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00003585 PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003586#endif
3587#ifdef AI_MASK
Fred Drake4baedc12002-04-01 14:53:37 +00003588 PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003589#endif
3590#ifdef AI_ALL
Fred Drake4baedc12002-04-01 14:53:37 +00003591 PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003592#endif
3593#ifdef AI_V4MAPPED_CFG
Fred Drake4baedc12002-04-01 14:53:37 +00003594 PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003595#endif
3596#ifdef AI_ADDRCONFIG
Fred Drake4baedc12002-04-01 14:53:37 +00003597 PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003598#endif
3599#ifdef AI_V4MAPPED
Fred Drake4baedc12002-04-01 14:53:37 +00003600 PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003601#endif
3602#ifdef AI_DEFAULT
Fred Drake4baedc12002-04-01 14:53:37 +00003603 PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003604#endif
3605#ifdef NI_MAXHOST
Fred Drake4baedc12002-04-01 14:53:37 +00003606 PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003607#endif
3608#ifdef NI_MAXSERV
Fred Drake4baedc12002-04-01 14:53:37 +00003609 PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003610#endif
3611#ifdef NI_NOFQDN
Fred Drake4baedc12002-04-01 14:53:37 +00003612 PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003613#endif
3614#ifdef NI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00003615 PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003616#endif
3617#ifdef NI_NAMEREQD
Fred Drake4baedc12002-04-01 14:53:37 +00003618 PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003619#endif
3620#ifdef NI_NUMERICSERV
Fred Drake4baedc12002-04-01 14:53:37 +00003621 PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003622#endif
3623#ifdef NI_DGRAM
Fred Drake4baedc12002-04-01 14:53:37 +00003624 PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003625#endif
3626
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003627 /* Initialize gethostbyname lock */
Guido van Rossum3baaa131999-03-22 21:44:51 +00003628#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum65d5b571998-12-21 19:32:43 +00003629 gethostbyname_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003630#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003631}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003632
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003633
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003634#ifndef HAVE_INET_PTON
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003635
3636/* Simplistic emulation code for inet_pton that only works for IPv4 */
3637
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003638int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003639inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003640{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003641 if (af == AF_INET) {
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003642 long packed_addr;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003643 packed_addr = inet_addr(src);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003644 if (packed_addr == INADDR_NONE)
3645 return 0;
3646 memcpy(dst, &packed_addr, 4);
3647 return 1;
3648 }
3649 /* Should set errno to EAFNOSUPPORT */
3650 return -1;
3651}
3652
Martin v. Löwisc925b1532001-07-21 09:42:15 +00003653const char *
3654inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003655{
3656 if (af == AF_INET) {
3657 struct in_addr packed_addr;
3658 if (size < 16)
3659 /* Should set errno to ENOSPC. */
3660 return NULL;
3661 memcpy(&packed_addr, src, sizeof(packed_addr));
3662 return strncpy(dst, inet_ntoa(packed_addr), size);
3663 }
3664 /* Should set errno to EAFNOSUPPORT */
3665 return NULL;
3666}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003667
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003668#endif