blob: 29b6339fe747a6f77d018da3203f06b9d38edee1 [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 Rossum22a97152002-08-08 20:37:08 +00001113"gettimeout() -> timeout\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001114\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);
Tim Peters58141872002-08-06 22:25:02 +00001308 if (res < 0) {
1309 /* On Win98, WSAEISCONN was seen here. But
1310 * on Win2K, WSAEINVAL. So accept both as
1311 * meaning "fine".
1312 */
1313 int code = WSAGetLastError();
1314 if (code == WSAEISCONN ||
1315 code == WSAEINVAL)
1316 res = 0;
1317 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001318 }
1319 }
1320
1321 if (res < 0)
1322 res = WSAGetLastError();
1323
1324#else
1325
1326 if (s->sock_timeout > 0.0) {
1327 if (res < 0 && errno == EINPROGRESS) {
1328 internal_select(s, 1);
1329 res = connect(s->sock_fd, addr, addrlen);
1330 }
1331 }
1332
1333 if (res < 0)
1334 res = errno;
1335
1336#endif
1337
1338 return res;
1339}
Guido van Rossum30a685f1991-06-27 15:51:29 +00001340
Fred Drake728819a2000-07-01 03:40:12 +00001341/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001342
Guido van Rossum73624e91994-10-10 17:59:00 +00001343static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001344sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001345{
1346 struct sockaddr *addr;
1347 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001348 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001349
Fred Drake728819a2000-07-01 03:40:12 +00001350 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001351 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001352
Guido van Rossum73624e91994-10-10 17:59:00 +00001353 Py_BEGIN_ALLOW_THREADS
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001354 res = internal_connect(s, addr, addrlen);
Guido van Rossum11ba0942002-06-13 15:07:44 +00001355 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001356
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001357 if (res != 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001358 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001359 Py_INCREF(Py_None);
1360 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001361}
1362
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001363PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001364"connect(address)\n\
1365\n\
1366Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001367is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001368
Guido van Rossum30a685f1991-06-27 15:51:29 +00001369
Fred Drake728819a2000-07-01 03:40:12 +00001370/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001371
1372static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001373sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001374{
1375 struct sockaddr *addr;
1376 int addrlen;
1377 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001378
Fred Drake728819a2000-07-01 03:40:12 +00001379 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001380 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001381
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001382 Py_BEGIN_ALLOW_THREADS
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001383 res = internal_connect(s, addr, addrlen);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001384 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001385
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001386 return PyInt_FromLong((long) res);
1387}
1388
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001389PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001390"connect_ex(address) -> errno\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001391\n\
1392This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001393instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001394
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001395
Guido van Rossumed233a51992-06-23 09:07:03 +00001396/* s.fileno() method */
1397
Guido van Rossum73624e91994-10-10 17:59:00 +00001398static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001399sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00001400{
Fred Drakea04eaad2000-06-30 02:46:07 +00001401#if SIZEOF_SOCKET_T <= SIZEOF_LONG
Guido van Rossum73624e91994-10-10 17:59:00 +00001402 return PyInt_FromLong((long) s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +00001403#else
1404 return PyLong_FromLongLong((LONG_LONG)s->sock_fd);
1405#endif
Guido van Rossumed233a51992-06-23 09:07:03 +00001406}
1407
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001408PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001409"fileno() -> integer\n\
1410\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001411Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001412
Guido van Rossumed233a51992-06-23 09:07:03 +00001413
Guido van Rossumbe32c891996-06-20 16:25:29 +00001414#ifndef NO_DUP
1415/* s.dup() method */
1416
1417static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001418sock_dup(PySocketSockObject *s)
Guido van Rossumbe32c891996-06-20 16:25:29 +00001419{
Fred Drakea04eaad2000-06-30 02:46:07 +00001420 SOCKET_T newfd;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001421 PyObject *sock;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001422
Guido van Rossumbe32c891996-06-20 16:25:29 +00001423 newfd = dup(s->sock_fd);
1424 if (newfd < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001425 return s->errorhandler();
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001426 sock = (PyObject *) new_sockobject(newfd,
1427 s->sock_family,
1428 s->sock_type,
1429 s->sock_proto);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001430 if (sock == NULL)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001431 SOCKETCLOSE(newfd);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001432 return sock;
1433}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001434
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001435PyDoc_STRVAR(dup_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001436"dup() -> socket object\n\
1437\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001438Return a new socket object connected to the same system resource.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001439
Guido van Rossumbe32c891996-06-20 16:25:29 +00001440#endif
1441
1442
Guido van Rossumc89705d1992-11-26 08:54:07 +00001443/* s.getsockname() method */
1444
Guido van Rossum73624e91994-10-10 17:59:00 +00001445static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001446sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001447{
1448 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001449 int res;
1450 socklen_t addrlen;
1451
Guido van Rossumc89705d1992-11-26 08:54:07 +00001452 if (!getsockaddrlen(s, &addrlen))
1453 return NULL;
Guido van Rossumca42b161996-01-12 01:36:05 +00001454 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001455 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001456 res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001457 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001458 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001459 return s->errorhandler();
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001460 return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001461}
1462
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001463PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001464"getsockname() -> address info\n\
1465\n\
1466Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001467info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001468
Guido van Rossumc89705d1992-11-26 08:54:07 +00001469
Guido van Rossumb6775db1994-08-01 11:34:53 +00001470#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001471/* s.getpeername() method */
1472
Guido van Rossum73624e91994-10-10 17:59:00 +00001473static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001474sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001475{
1476 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001477 int res;
1478 socklen_t addrlen;
1479
Guido van Rossumc89705d1992-11-26 08:54:07 +00001480 if (!getsockaddrlen(s, &addrlen))
1481 return NULL;
Fred Drakefd168342001-05-09 19:11:33 +00001482 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001483 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001484 res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001485 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001486 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001487 return s->errorhandler();
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001488 return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001489}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001490
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001491PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001492"getpeername() -> address info\n\
1493\n\
1494Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001495info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001496
Guido van Rossumb6775db1994-08-01 11:34:53 +00001497#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001498
1499
Guido van Rossum30a685f1991-06-27 15:51:29 +00001500/* s.listen(n) method */
1501
Guido van Rossum73624e91994-10-10 17:59:00 +00001502static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001503sock_listen(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001504{
1505 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001506 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001507
1508 backlog = PyInt_AsLong(arg);
1509 if (backlog == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001510 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001511 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +00001512 if (backlog < 1)
1513 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001514 res = listen(s->sock_fd, backlog);
Guido van Rossum73624e91994-10-10 17:59:00 +00001515 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001516 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001517 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001518 Py_INCREF(Py_None);
1519 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001520}
1521
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001522PyDoc_STRVAR(listen_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001523"listen(backlog)\n\
1524\n\
1525Enable a server to accept connections. The backlog argument must be at\n\
1526least 1; it specifies the number of unaccepted connection that the system\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001527will allow before refusing new connections.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001528
1529
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001530#ifndef NO_DUP
Guido van Rossum30a685f1991-06-27 15:51:29 +00001531/* s.makefile(mode) method.
1532 Create a new open file object referring to a dupped version of
1533 the socket's file descriptor. (The dup() call is necessary so
1534 that the open file and socket objects may be closed independent
1535 of each other.)
1536 The mode argument specifies 'r' or 'w' passed to fdopen(). */
1537
Guido van Rossum73624e91994-10-10 17:59:00 +00001538static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001539sock_makefile(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001540{
Tim Petersdbd9ba62000-07-09 03:09:57 +00001541 extern int fclose(FILE *);
Guido van Rossum6b144911995-03-14 15:05:13 +00001542 char *mode = "r";
1543 int bufsize = -1;
Fred Drakea04eaad2000-06-30 02:46:07 +00001544#ifdef MS_WIN32
Tim Peters79248aa2001-08-29 21:37:10 +00001545 Py_intptr_t fd;
Fred Drakea04eaad2000-06-30 02:46:07 +00001546#else
Guido van Rossum30a685f1991-06-27 15:51:29 +00001547 int fd;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001548#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001549 FILE *fp;
Guido van Rossum6b144911995-03-14 15:05:13 +00001550 PyObject *f;
1551
Guido van Rossum43713e52000-02-29 13:59:29 +00001552 if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001553 return NULL;
Guido van Rossum8d665e61996-06-26 18:22:49 +00001554#ifdef MS_WIN32
Guido van Rossum82a5c661998-07-07 20:45:43 +00001555 if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) ||
1556 ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL))
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001557#else
Guido van Rossum82a5c661998-07-07 20:45:43 +00001558 if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL)
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001559#endif
Guido van Rossum82a5c661998-07-07 20:45:43 +00001560 {
Guido van Rossum6b144911995-03-14 15:05:13 +00001561 if (fd >= 0)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001562 SOCKETCLOSE(fd);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001563 return s->errorhandler();
Guido van Rossum6b144911995-03-14 15:05:13 +00001564 }
Jack Jansen6d8898b2002-03-25 15:32:01 +00001565#ifdef USE_GUSI2
1566 /* Workaround for bug in Metrowerks MSL vs. GUSI I/O library */
Guido van Rossum67f7a382002-06-06 21:08:16 +00001567 if (strchr(mode, 'b') != NULL)
Jack Jansen6d8898b2002-03-25 15:32:01 +00001568 bufsize = 0;
1569#endif
Guido van Rossum6b144911995-03-14 15:05:13 +00001570 f = PyFile_FromFile(fp, "<socket>", mode, fclose);
1571 if (f != NULL)
1572 PyFile_SetBufSize(f, bufsize);
1573 return f;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001574}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001575
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001576PyDoc_STRVAR(makefile_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001577"makefile([mode[, buffersize]]) -> file object\n\
1578\n\
1579Return a regular file object corresponding to the socket.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001580The mode and buffersize arguments are as for the built-in open() function.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001581
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001582#endif /* NO_DUP */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001583
Guido van Rossum48a680c2001-03-02 06:34:14 +00001584
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001585/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001586
Guido van Rossum73624e91994-10-10 17:59:00 +00001587static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001588sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001589{
Guido van Rossumbe32c891996-06-20 16:25:29 +00001590 int len, n, flags = 0;
Guido van Rossum73624e91994-10-10 17:59:00 +00001591 PyObject *buf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001592
Guido van Rossum43713e52000-02-29 13:59:29 +00001593 if (!PyArg_ParseTuple(args, "i|i:recv", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001594 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001595
1596 if (len < 0) {
Martin v. Löwisfba64e12001-11-19 10:41:26 +00001597 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00001598 "negative buffersize in recv");
Martin v. Löwisfba64e12001-11-19 10:41:26 +00001599 return NULL;
1600 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00001601
Guido van Rossum73624e91994-10-10 17:59:00 +00001602 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001603 if (buf == NULL)
1604 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001605
Guido van Rossum73624e91994-10-10 17:59:00 +00001606 Py_BEGIN_ALLOW_THREADS
Guido van Rossum11ba0942002-06-13 15:07:44 +00001607 internal_select(s, 0);
Jeremy Hylton1ba6bad2001-08-20 19:06:36 +00001608 n = recv(s->sock_fd, PyString_AS_STRING(buf), len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001609 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001610
Guido van Rossum7c53b771995-09-13 18:39:47 +00001611 if (n < 0) {
1612 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001613 return s->errorhandler();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001614 }
Tim Peters5de98422002-04-27 18:44:32 +00001615 if (n != len)
1616 _PyString_Resize(&buf, n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001617 return buf;
1618}
1619
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001620PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001621"recv(buffersize[, flags]) -> data\n\
1622\n\
1623Receive up to buffersize bytes from the socket. For the optional flags\n\
1624argument, see the Unix manual. When no data is available, block until\n\
1625at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001626the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001627
Guido van Rossum30a685f1991-06-27 15:51:29 +00001628
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001629/* s.recvfrom(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001630
Guido van Rossum73624e91994-10-10 17:59:00 +00001631static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001632sock_recvfrom(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001633{
1634 char addrbuf[256];
Barry Warsaw752300b1997-01-03 17:18:10 +00001635 PyObject *buf = NULL;
1636 PyObject *addr = NULL;
1637 PyObject *ret = NULL;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001638 int len, n, flags = 0;
1639 socklen_t addrlen;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001640
Guido van Rossum43713e52000-02-29 13:59:29 +00001641 if (!PyArg_ParseTuple(args, "i|i:recvfrom", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001642 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001643
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00001644 if (!getsockaddrlen(s, &addrlen))
1645 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001646 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00001647 if (buf == NULL)
1648 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001649
Guido van Rossum73624e91994-10-10 17:59:00 +00001650 Py_BEGIN_ALLOW_THREADS
Fred Drakefd168342001-05-09 19:11:33 +00001651 memset(addrbuf, 0, addrlen);
Guido van Rossum11ba0942002-06-13 15:07:44 +00001652 internal_select(s, 0);
Fred Drakefd168342001-05-09 19:11:33 +00001653 n = recvfrom(s->sock_fd, PyString_AS_STRING(buf), len, flags,
Guido van Rossum8d665e61996-06-26 18:22:49 +00001654#ifndef MS_WINDOWS
Andrew MacIntyreba43e872002-03-03 03:03:52 +00001655#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001656 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00001657#else
Thomas Wouters334fb892000-07-25 12:56:38 +00001658 (void *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00001659#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001660#else
Barry Warsaw752300b1997-01-03 17:18:10 +00001661 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001662#endif
Barry Warsaw752300b1997-01-03 17:18:10 +00001663 );
Guido van Rossum73624e91994-10-10 17:59:00 +00001664 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001665
Guido van Rossum7c53b771995-09-13 18:39:47 +00001666 if (n < 0) {
1667 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001668 return s->errorhandler();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001669 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00001670
Guido van Rossum53a9bf81996-06-11 18:35:24 +00001671 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001672 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001673
Guido van Rossum67f7a382002-06-06 21:08:16 +00001674 if (!(addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001675 addrlen)))
Barry Warsaw752300b1997-01-03 17:18:10 +00001676 goto finally;
1677
Guido van Rossum73624e91994-10-10 17:59:00 +00001678 ret = Py_BuildValue("OO", buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001679
1680finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001681 Py_XDECREF(addr);
1682 Py_XDECREF(buf);
Guido van Rossum6f5afc91993-02-05 09:46:15 +00001683 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001684}
1685
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001686PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001687"recvfrom(buffersize[, flags]) -> (data, address info)\n\
1688\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001689Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001690
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001691/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001692
Guido van Rossum73624e91994-10-10 17:59:00 +00001693static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001694sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001695{
Guido van Rossumff4949e1992-08-05 19:58:53 +00001696 char *buf;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001697 int len, n, flags = 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001698
Guido van Rossum43713e52000-02-29 13:59:29 +00001699 if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001700 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001701
Guido van Rossum73624e91994-10-10 17:59:00 +00001702 Py_BEGIN_ALLOW_THREADS
Guido van Rossum11ba0942002-06-13 15:07:44 +00001703 internal_select(s, 1);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001704 n = send(s->sock_fd, buf, len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001705 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001706
Guido van Rossum30a685f1991-06-27 15:51:29 +00001707 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001708 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001709 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00001710}
1711
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001712PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001713"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001714\n\
1715Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001716argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001717sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001718
1719
1720/* s.sendall(data [,flags]) method */
1721
1722static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001723sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001724{
1725 char *buf;
Martin v. Löwise6cc5b62002-02-16 23:13:54 +00001726 int len, n, flags = 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001727
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001728 if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags))
1729 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001730
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001731 Py_BEGIN_ALLOW_THREADS
Guido van Rossum11ba0942002-06-13 15:07:44 +00001732 internal_select(s, 1);
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001733 do {
1734 n = send(s->sock_fd, buf, len, flags);
1735 if (n < 0)
1736 break;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001737 buf += n;
1738 len -= n;
1739 } while (len > 0);
1740 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001741
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001742 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001743 return s->errorhandler();
Guido van Rossum67f7a382002-06-06 21:08:16 +00001744
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001745 Py_INCREF(Py_None);
1746 return Py_None;
1747}
1748
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001749PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001750"sendall(data[, flags])\n\
1751\n\
1752Send a data string to the socket. For the optional flags\n\
1753argument, see the Unix manual. This calls send() repeatedly\n\
1754until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001755to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001756
Guido van Rossum30a685f1991-06-27 15:51:29 +00001757
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001758/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001759
Guido van Rossum73624e91994-10-10 17:59:00 +00001760static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001761sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001762{
Guido van Rossum73624e91994-10-10 17:59:00 +00001763 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001764 char *buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001765 struct sockaddr *addr;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001766 int addrlen, len, n, flags;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001767
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001768 flags = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001769 if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001770 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001771 if (!PyArg_ParseTuple(args, "s#iO:sendto",
1772 &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001773 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001774 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00001775
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001776 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001777 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001778
Guido van Rossum73624e91994-10-10 17:59:00 +00001779 Py_BEGIN_ALLOW_THREADS
Guido van Rossum11ba0942002-06-13 15:07:44 +00001780 internal_select(s, 1);
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001781 n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001782 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001783
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001784 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001785 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001786 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001787}
1788
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001789PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001790"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001791\n\
1792Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001793For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001794
Guido van Rossum30a685f1991-06-27 15:51:29 +00001795
1796/* s.shutdown(how) method */
1797
Guido van Rossum73624e91994-10-10 17:59:00 +00001798static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001799sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001800{
1801 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001802 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001803
1804 how = PyInt_AsLong(arg);
1805 if (how == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001806 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001807 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001808 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +00001809 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001810 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001811 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001812 Py_INCREF(Py_None);
1813 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001814}
1815
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001816PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001817"shutdown(flag)\n\
1818\n\
1819Shut down the reading side of the socket (flag == 0), the writing side\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001820of the socket (flag == 1), or both ends (flag == 2).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001821
Guido van Rossum30a685f1991-06-27 15:51:29 +00001822
1823/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001824
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001825static PyMethodDef sock_methods[] = {
1826 {"accept", (PyCFunction)sock_accept, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001827 accept_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001828 {"bind", (PyCFunction)sock_bind, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001829 bind_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001830 {"close", (PyCFunction)sock_close, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001831 close_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001832 {"connect", (PyCFunction)sock_connect, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001833 connect_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001834 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001835 connect_ex_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00001836#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001837 {"dup", (PyCFunction)sock_dup, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001838 dup_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00001839#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001840 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001841 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001842#ifdef HAVE_GETPEERNAME
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001843 {"getpeername", (PyCFunction)sock_getpeername,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001844 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00001845#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001846 {"getsockname", (PyCFunction)sock_getsockname,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001847 METH_NOARGS, getsockname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001848 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001849 getsockopt_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001850 {"listen", (PyCFunction)sock_listen, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001851 listen_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001852#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001853 {"makefile", (PyCFunction)sock_makefile, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001854 makefile_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001855#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001856 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001857 recv_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001858 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001859 recvfrom_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001860 {"send", (PyCFunction)sock_send, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001861 send_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001862 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001863 sendall_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001864 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001865 sendto_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001866 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001867 setblocking_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001868 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
Guido van Rossum67f7a382002-06-06 21:08:16 +00001869 settimeout_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001870 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
Guido van Rossum67f7a382002-06-06 21:08:16 +00001871 gettimeout_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001872 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001873 setsockopt_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001874 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001875 shutdown_doc},
1876#ifdef RISCOS
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001877 {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001878 sleeptaskw_doc},
1879#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00001880 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001881};
1882
Guido van Rossum30a685f1991-06-27 15:51:29 +00001883
Guido van Rossum73624e91994-10-10 17:59:00 +00001884/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00001885 First close the file description. */
1886
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001887static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001888sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001889{
Guido van Rossumfa972c92000-04-10 12:45:45 +00001890 if (s->sock_fd != -1)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001891 (void) SOCKETCLOSE(s->sock_fd);
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001892 s->ob_type->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001893}
1894
Guido van Rossum30a685f1991-06-27 15:51:29 +00001895
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001896static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001897sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001898{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001899 char buf[512];
Fred Drakea04eaad2000-06-30 02:46:07 +00001900#if SIZEOF_SOCKET_T > SIZEOF_LONG
1901 if (s->sock_fd > LONG_MAX) {
1902 /* this can occur on Win64, and actually there is a special
1903 ugly printf formatter for decimal pointer length integer
1904 printing, only bother if necessary*/
1905 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001906 "no printf formatter to display "
1907 "the socket descriptor in decimal");
Fred Drakea04eaad2000-06-30 02:46:07 +00001908 return NULL;
1909 }
1910#endif
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001911 PyOS_snprintf(
1912 buf, sizeof(buf),
1913 "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>",
1914 (long)s->sock_fd, s->sock_family,
1915 s->sock_type,
1916 s->sock_proto);
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00001917 return PyString_FromString(buf);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001918}
1919
1920
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001921/* Create a new, uninitialized socket object. */
1922
1923static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001924sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001925{
1926 PyObject *new;
1927
1928 new = type->tp_alloc(type, 0);
Guido van Rossume1c478f2002-06-06 20:08:25 +00001929 if (new != NULL) {
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001930 ((PySocketSockObject *)new)->sock_fd = -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001931 ((PySocketSockObject *)new)->sock_timeout = -1.0;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001932 ((PySocketSockObject *)new)->errorhandler = &set_error;
Guido van Rossume1c478f2002-06-06 20:08:25 +00001933 }
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001934 return new;
1935}
1936
1937
1938/* Initialize a new socket object. */
1939
1940/*ARGSUSED*/
1941static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00001942sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001943{
1944 PySocketSockObject *s = (PySocketSockObject *)self;
1945 SOCKET_T fd;
1946 int family = AF_INET, type = SOCK_STREAM, proto = 0;
1947 static char *keywords[] = {"family", "type", "proto", 0};
1948
1949 if (!PyArg_ParseTupleAndKeywords(args, kwds,
1950 "|iii:socket", keywords,
1951 &family, &type, &proto))
1952 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001953
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001954 Py_BEGIN_ALLOW_THREADS
1955 fd = socket(family, type, proto);
1956 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001957
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001958#ifdef MS_WINDOWS
1959 if (fd == INVALID_SOCKET)
1960#else
1961 if (fd < 0)
1962#endif
1963 {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001964 set_error();
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001965 return -1;
1966 }
1967 init_sockobject(s, fd, family, type, proto);
1968 /* From now on, ignore SIGPIPE and let the error checking
1969 do the work. */
1970#ifdef SIGPIPE
1971 (void) signal(SIGPIPE, SIG_IGN);
1972#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00001973
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001974 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001975
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001976}
1977
1978
Guido van Rossumb6775db1994-08-01 11:34:53 +00001979/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001980
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001981static PyTypeObject sock_type = {
Guido van Rossum55558541996-05-23 22:54:50 +00001982 PyObject_HEAD_INIT(0) /* Must fill in type value later */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001983 0, /* ob_size */
Guido van Rossumca6dfa52001-10-28 12:31:33 +00001984 "_socket.socket", /* tp_name */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001985 sizeof(PySocketSockObject), /* tp_basicsize */
1986 0, /* tp_itemsize */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001987 (destructor)sock_dealloc, /* tp_dealloc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001988 0, /* tp_print */
1989 0, /* tp_getattr */
1990 0, /* tp_setattr */
1991 0, /* tp_compare */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001992 (reprfunc)sock_repr, /* tp_repr */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001993 0, /* tp_as_number */
1994 0, /* tp_as_sequence */
1995 0, /* tp_as_mapping */
1996 0, /* tp_hash */
1997 0, /* tp_call */
1998 0, /* tp_str */
Michael W. Hudson03f96bd2001-11-09 10:06:23 +00001999 0, /* set below */ /* tp_getattro */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002000 0, /* tp_setattro */
2001 0, /* tp_as_buffer */
2002 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002003 sock_doc, /* tp_doc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002004 0, /* tp_traverse */
2005 0, /* tp_clear */
2006 0, /* tp_richcompare */
2007 0, /* tp_weaklistoffset */
2008 0, /* tp_iter */
2009 0, /* tp_iternext */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002010 sock_methods, /* tp_methods */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002011 0, /* tp_members */
2012 0, /* tp_getset */
2013 0, /* tp_base */
2014 0, /* tp_dict */
2015 0, /* tp_descr_get */
2016 0, /* tp_descr_set */
2017 0, /* tp_dictoffset */
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002018 sock_initobj, /* tp_init */
Michael W. Hudson03f96bd2001-11-09 10:06:23 +00002019 0, /* set below */ /* tp_alloc */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002020 sock_new, /* tp_new */
Michael W. Hudson03f96bd2001-11-09 10:06:23 +00002021 0, /* set below */ /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002022};
2023
Guido van Rossum30a685f1991-06-27 15:51:29 +00002024
Guido van Rossum81194471991-07-27 21:42:02 +00002025/* Python interface to gethostname(). */
2026
2027/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002028static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002029socket_gethostname(PyObject *self, PyObject *args)
Guido van Rossum81194471991-07-27 21:42:02 +00002030{
2031 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00002032 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002033 if (!PyArg_ParseTuple(args, ":gethostname"))
Guido van Rossum81194471991-07-27 21:42:02 +00002034 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002035 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002036 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00002037 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002038 if (res < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002039 return set_error();
Guido van Rossum81194471991-07-27 21:42:02 +00002040 buf[sizeof buf - 1] = '\0';
Guido van Rossum73624e91994-10-10 17:59:00 +00002041 return PyString_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00002042}
Guido van Rossumff4949e1992-08-05 19:58:53 +00002043
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002044PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002045"gethostname() -> string\n\
2046\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002047Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002048
Guido van Rossumff4949e1992-08-05 19:58:53 +00002049
Guido van Rossum30a685f1991-06-27 15:51:29 +00002050/* Python interface to gethostbyname(name). */
2051
2052/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002053static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002054socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002055{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002056 char *name;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002057 struct sockaddr_storage addrbuf;
2058
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002059 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002060 return NULL;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002061 if (setipaddr(name, (struct sockaddr *)&addrbuf, sizeof(addrbuf), AF_INET) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002062 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002063 return makeipaddr((struct sockaddr *)&addrbuf,
2064 sizeof(struct sockaddr_in));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002065}
2066
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002067PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002068"gethostbyname(host) -> address\n\
2069\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002070Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002071
2072
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002073/* Convenience function common to gethostbyname_ex and gethostbyaddr */
2074
2075static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002076gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002077{
2078 char **pch;
2079 PyObject *rtn_tuple = (PyObject *)NULL;
2080 PyObject *name_list = (PyObject *)NULL;
2081 PyObject *addr_list = (PyObject *)NULL;
2082 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002083
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002084 if (h == NULL) {
Guido van Rossum48a680c2001-03-02 06:34:14 +00002085 /* Let's get real error message to return */
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002086#ifndef RISCOS
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002087 set_herror(h_errno);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002088#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002089 PyErr_SetString(socket_error, "host not found");
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002090#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002091 return NULL;
2092 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002093
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002094 if (h->h_addrtype != af) {
2095#ifdef HAVE_STRERROR
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002096 /* Let's get real error message to return */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002097 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002098 (char *)strerror(EAFNOSUPPORT));
2099#else
2100 PyErr_SetString(
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002101 socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002102 "Address family not supported by protocol family");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002103#endif
2104 return NULL;
2105 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002106
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002107 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002108
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002109 case AF_INET:
2110 if (alen < sizeof(struct sockaddr_in))
2111 return NULL;
2112 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002113
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002114#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002115 case AF_INET6:
2116 if (alen < sizeof(struct sockaddr_in6))
2117 return NULL;
2118 break;
2119#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002120
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002121 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002122
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002123 if ((name_list = PyList_New(0)) == NULL)
2124 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002125
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002126 if ((addr_list = PyList_New(0)) == NULL)
2127 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002128
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002129 for (pch = h->h_aliases; *pch != NULL; pch++) {
2130 int status;
2131 tmp = PyString_FromString(*pch);
2132 if (tmp == NULL)
2133 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002134
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002135 status = PyList_Append(name_list, tmp);
2136 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002137
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002138 if (status)
2139 goto err;
2140 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002141
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002142 for (pch = h->h_addr_list; *pch != NULL; pch++) {
2143 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002144
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002145 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002146
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002147 case AF_INET:
2148 {
2149 struct sockaddr_in sin;
2150 memset(&sin, 0, sizeof(sin));
2151 sin.sin_family = af;
2152#ifdef HAVE_SOCKADDR_SA_LEN
2153 sin.sin_len = sizeof(sin);
2154#endif
2155 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
2156 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002157
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002158 if (pch == h->h_addr_list && alen >= sizeof(sin))
2159 memcpy((char *) addr, &sin, sizeof(sin));
2160 break;
2161 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002162
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002163#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002164 case AF_INET6:
2165 {
2166 struct sockaddr_in6 sin6;
2167 memset(&sin6, 0, sizeof(sin6));
2168 sin6.sin6_family = af;
2169#ifdef HAVE_SOCKADDR_SA_LEN
2170 sin6.sin6_len = sizeof(sin6);
2171#endif
2172 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
2173 tmp = makeipaddr((struct sockaddr *)&sin6,
2174 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002175
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002176 if (pch == h->h_addr_list && alen >= sizeof(sin6))
2177 memcpy((char *) addr, &sin6, sizeof(sin6));
2178 break;
2179 }
2180#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002181
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002182 default: /* can't happen */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002183 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002184 "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002185 return NULL;
2186 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002187
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002188 if (tmp == NULL)
2189 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002190
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002191 status = PyList_Append(addr_list, tmp);
2192 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002193
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002194 if (status)
2195 goto err;
2196 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002197
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002198 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002199
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002200 err:
2201 Py_XDECREF(name_list);
2202 Py_XDECREF(addr_list);
2203 return rtn_tuple;
2204}
2205
2206
2207/* Python interface to gethostbyname_ex(name). */
2208
2209/*ARGSUSED*/
2210static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002211socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002212{
2213 char *name;
2214 struct hostent *h;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002215 struct sockaddr_storage addr;
Martin v. Löwis9db2f572001-07-23 01:30:10 +00002216 struct sockaddr *sa;
Guido van Rossum955becc1999-03-22 20:14:53 +00002217 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002218#ifdef HAVE_GETHOSTBYNAME_R
2219 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002220#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
2221 struct hostent_data data;
2222#else
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002223 char buf[16384];
2224 int buf_len = (sizeof buf) - 1;
2225 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002226#endif
2227#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002228 int result;
2229#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002230#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002231
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002232 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002233 return NULL;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002234 if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), PF_INET) < 0)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002235 return NULL;
2236 Py_BEGIN_ALLOW_THREADS
2237#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002238#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002239 result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
2240 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002241#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002242 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002243#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00002244 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002245 result = gethostbyname_r(name, &hp_allocated, &data);
2246 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002247#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002248#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002249#ifdef USE_GETHOSTBYNAME_LOCK
2250 PyThread_acquire_lock(gethostbyname_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002251#endif
2252 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002253#endif /* HAVE_GETHOSTBYNAME_R */
2254 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002255 /* Some C libraries would require addr.__ss_family instead of
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002256 addr.ss_family.
2257 Therefore, we cast the sockaddr_storage into sockaddr to
2258 access sa_family. */
Martin v. Löwis9db2f572001-07-23 01:30:10 +00002259 sa = (struct sockaddr*)&addr;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002260 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002261 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002262#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum955becc1999-03-22 20:14:53 +00002263 PyThread_release_lock(gethostbyname_lock);
2264#endif
2265 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002266}
2267
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002268PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002269"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
2270\n\
2271Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002272for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002273
2274
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002275/* Python interface to gethostbyaddr(IP). */
2276
2277/*ARGSUSED*/
2278static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002279socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002280{
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002281#ifdef ENABLE_IPV6
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002282 struct sockaddr_storage addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002283#else
Guido van Rossum48a680c2001-03-02 06:34:14 +00002284 struct sockaddr_in addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002285#endif
2286 struct sockaddr *sa = (struct sockaddr *)&addr;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002287 char *ip_num;
2288 struct hostent *h;
Guido van Rossum3baaa131999-03-22 21:44:51 +00002289 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002290#ifdef HAVE_GETHOSTBYNAME_R
2291 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002292#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
2293 struct hostent_data data;
2294#else
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002295 char buf[16384];
2296 int buf_len = (sizeof buf) - 1;
2297 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002298#endif
2299#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002300 int result;
2301#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002302#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002303 char *ap;
2304 int al;
2305 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002306
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002307 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002308 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002309 af = PF_UNSPEC;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002310 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002311 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002312 af = sa->sa_family;
2313 ap = NULL;
2314 al = 0;
2315 switch (af) {
2316 case AF_INET:
2317 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
2318 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
2319 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002320#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002321 case AF_INET6:
2322 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
2323 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
2324 break;
2325#endif
2326 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002327 PyErr_SetString(socket_error, "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002328 return NULL;
2329 }
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002330 Py_BEGIN_ALLOW_THREADS
2331#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002332#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002333 result = gethostbyaddr_r(ap, al, af,
2334 &hp_allocated, buf, buf_len,
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002335 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002336#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002337 h = gethostbyaddr_r(ap, al, af,
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002338 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002339#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00002340 memset((void *) &data, '\0', sizeof(data));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002341 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002342 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002343#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002344#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002345#ifdef USE_GETHOSTBYNAME_LOCK
2346 PyThread_acquire_lock(gethostbyname_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002347#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002348 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002349#endif /* HAVE_GETHOSTBYNAME_R */
2350 Py_END_ALLOW_THREADS
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002351 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002352#ifdef USE_GETHOSTBYNAME_LOCK
2353 PyThread_release_lock(gethostbyname_lock);
2354#endif
2355 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002356}
2357
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002358PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002359"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
2360\n\
2361Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002362for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002363
Guido van Rossum30a685f1991-06-27 15:51:29 +00002364
2365/* Python interface to getservbyname(name).
2366 This only returns the port number, since the other info is already
2367 known or not useful (like the list of aliases). */
2368
2369/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002370static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002371socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002372{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002373 char *name, *proto;
Guido van Rossum30a685f1991-06-27 15:51:29 +00002374 struct servent *sp;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002375 if (!PyArg_ParseTuple(args, "ss:getservbyname", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002376 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002377 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002378 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00002379 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00002380 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002381 PyErr_SetString(socket_error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00002382 return NULL;
2383 }
Guido van Rossum73624e91994-10-10 17:59:00 +00002384 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002385}
2386
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002387PyDoc_STRVAR(getservbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002388"getservbyname(servicename, protocolname) -> integer\n\
2389\n\
2390Return a port number from a service name and protocol name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002391The protocol name should be 'tcp' or 'udp'.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002392
Guido van Rossum30a685f1991-06-27 15:51:29 +00002393
Guido van Rossum3901d851996-12-19 16:35:04 +00002394/* Python interface to getprotobyname(name).
2395 This only returns the protocol number, since the other info is
2396 already known or not useful (like the list of aliases). */
2397
2398/*ARGSUSED*/
2399static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002400socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00002401{
2402 char *name;
2403 struct protoent *sp;
Guido van Rossumbcc20741998-08-04 22:53:56 +00002404#ifdef __BEOS__
2405/* Not available in BeOS yet. - [cjh] */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002406 PyErr_SetString(socket_error, "getprotobyname not supported");
Guido van Rossumbcc20741998-08-04 22:53:56 +00002407 return NULL;
2408#else
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002409 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
Guido van Rossum3901d851996-12-19 16:35:04 +00002410 return NULL;
2411 Py_BEGIN_ALLOW_THREADS
2412 sp = getprotobyname(name);
2413 Py_END_ALLOW_THREADS
2414 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002415 PyErr_SetString(socket_error, "protocol not found");
Guido van Rossum3901d851996-12-19 16:35:04 +00002416 return NULL;
2417 }
2418 return PyInt_FromLong((long) sp->p_proto);
Guido van Rossumbcc20741998-08-04 22:53:56 +00002419#endif
Guido van Rossum3901d851996-12-19 16:35:04 +00002420}
2421
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002422PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002423"getprotobyname(name) -> integer\n\
2424\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002425Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002426
Guido van Rossum3901d851996-12-19 16:35:04 +00002427
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002428#ifndef NO_DUP
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002429/* Create a socket object from a numeric file description.
2430 Useful e.g. if stdin is a socket.
2431 Additional arguments as for socket(). */
2432
2433/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002434static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002435socket_fromfd(PyObject *self, PyObject *args)
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002436{
Guido van Rossum73624e91994-10-10 17:59:00 +00002437 PySocketSockObject *s;
Fred Drakea04eaad2000-06-30 02:46:07 +00002438 SOCKET_T fd;
2439 int family, type, proto = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002440 if (!PyArg_ParseTuple(args, "iii|i:fromfd",
2441 &fd, &family, &type, &proto))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002442 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +00002443 /* Dup the fd so it and the socket can be closed independently */
2444 fd = dup(fd);
2445 if (fd < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002446 return set_error();
2447 s = new_sockobject(fd, family, type, proto);
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002448 /* From now on, ignore SIGPIPE and let the error checking
2449 do the work. */
Guido van Rossum48a680c2001-03-02 06:34:14 +00002450#ifdef SIGPIPE
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002451 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum48a680c2001-03-02 06:34:14 +00002452#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00002453 return (PyObject *) s;
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002454}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002455
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002456PyDoc_STRVAR(fromfd_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002457"fromfd(fd, family, type[, proto]) -> socket object\n\
2458\n\
2459Create a socket object from the given file descriptor.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002460The remaining arguments are the same as for socket().");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002461
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002462#endif /* NO_DUP */
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002463
Guido van Rossum82a5c661998-07-07 20:45:43 +00002464
Guido van Rossum006bf911996-06-12 04:04:55 +00002465static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002466socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00002467{
2468 int x1, x2;
2469
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002470 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00002471 return NULL;
2472 }
2473 x2 = (int)ntohs((short)x1);
2474 return PyInt_FromLong(x2);
2475}
2476
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002477PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002478"ntohs(integer) -> integer\n\
2479\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002480Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002481
2482
Guido van Rossum006bf911996-06-12 04:04:55 +00002483static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002484socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00002485{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002486 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00002487
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002488 if (PyInt_Check(arg)) {
2489 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002490 if (x == (unsigned long) -1 && PyErr_Occurred())
2491 return NULL;
Guido van Rossum006bf911996-06-12 04:04:55 +00002492 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002493 else if (PyLong_Check(arg)) {
2494 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002495 if (x == (unsigned long) -1 && PyErr_Occurred())
2496 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002497#if SIZEOF_LONG > 4
2498 {
2499 unsigned long y;
2500 /* only want the trailing 32 bits */
2501 y = x & 0xFFFFFFFFUL;
2502 if (y ^ x)
2503 return PyErr_Format(PyExc_OverflowError,
2504 "long int larger than 32 bits");
2505 x = y;
Tim Peters58141872002-08-06 22:25:02 +00002506 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002507#endif
2508 }
2509 else
Tim Peters58141872002-08-06 22:25:02 +00002510 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002511 "expected int/long, %s found",
2512 arg->ob_type->tp_name);
2513 if (x == (unsigned long) -1 && PyErr_Occurred())
2514 return NULL;
2515 return PyInt_FromLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00002516}
2517
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002518PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002519"ntohl(integer) -> integer\n\
2520\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002521Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002522
2523
Guido van Rossum006bf911996-06-12 04:04:55 +00002524static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002525socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00002526{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002527 unsigned long x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00002528
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002529 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00002530 return NULL;
2531 }
2532 x2 = (int)htons((short)x1);
2533 return PyInt_FromLong(x2);
2534}
2535
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002536PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002537"htons(integer) -> integer\n\
2538\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002539Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002540
2541
Guido van Rossum006bf911996-06-12 04:04:55 +00002542static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002543socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00002544{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002545 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00002546
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002547 if (PyInt_Check(arg)) {
2548 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002549 if (x == (unsigned long) -1 && PyErr_Occurred())
2550 return NULL;
Guido van Rossum006bf911996-06-12 04:04:55 +00002551 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002552 else if (PyLong_Check(arg)) {
2553 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002554 if (x == (unsigned long) -1 && PyErr_Occurred())
2555 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002556#if SIZEOF_LONG > 4
2557 {
2558 unsigned long y;
2559 /* only want the trailing 32 bits */
2560 y = x & 0xFFFFFFFFUL;
2561 if (y ^ x)
2562 return PyErr_Format(PyExc_OverflowError,
2563 "long int larger than 32 bits");
2564 x = y;
Tim Peters58141872002-08-06 22:25:02 +00002565 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002566#endif
2567 }
2568 else
Tim Peters58141872002-08-06 22:25:02 +00002569 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002570 "expected int/long, %s found",
2571 arg->ob_type->tp_name);
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002572 return PyInt_FromLong(htonl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00002573}
2574
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002575PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002576"htonl(integer) -> integer\n\
2577\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002578Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002579
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002580/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002581
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002582PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002583"inet_aton(string) -> packed 32-bit IP representation\n\
2584\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002585Convert 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 +00002586binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002587
2588static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002589socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002590{
Guido van Rossuma2e48551999-09-09 15:42:59 +00002591#ifndef INADDR_NONE
2592#define INADDR_NONE (-1)
2593#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002594
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002595 /* Have to use inet_addr() instead */
2596 char *ip_addr;
Fred Drakee5065292001-07-19 21:16:41 +00002597 unsigned long packed_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002598
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002599 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) {
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002600 return NULL;
2601 }
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002602 packed_addr = inet_addr(ip_addr);
2603
2604 if (packed_addr == INADDR_NONE) { /* invalid address */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002605 PyErr_SetString(socket_error,
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002606 "illegal IP address string passed to inet_aton");
2607 return NULL;
2608 }
2609
2610 return PyString_FromStringAndSize((char *) &packed_addr,
2611 sizeof(packed_addr));
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002612}
2613
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002614PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00002615"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002616\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002617Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002618
2619static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002620socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002621{
2622 char *packed_str;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002623 int addr_len;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002624 struct in_addr packed_addr;
2625
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002626 if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) {
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002627 return NULL;
2628 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00002629
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002630 if (addr_len != sizeof(packed_addr)) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002631 PyErr_SetString(socket_error,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002632 "packed IP wrong length for inet_ntoa");
2633 return NULL;
2634 }
2635
2636 memcpy(&packed_addr, packed_str, addr_len);
2637
2638 return PyString_FromString(inet_ntoa(packed_addr));
2639}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002640
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002641/* Python interface to getaddrinfo(host, port). */
2642
2643/*ARGSUSED*/
2644static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002645socket_getaddrinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002646{
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00002647 struct addrinfo hints, *res;
2648 struct addrinfo *res0 = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002649 PyObject *pobj = (PyObject *)NULL;
Martin v. Löwisf65b1a12001-08-12 09:28:40 +00002650 char pbuf[30];
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002651 char *hptr, *pptr;
2652 int family, socktype, protocol, flags;
2653 int error;
2654 PyObject *all = (PyObject *)NULL;
2655 PyObject *single = (PyObject *)NULL;
2656
2657 family = socktype = protocol = flags = 0;
2658 family = PF_UNSPEC;
2659 if (!PyArg_ParseTuple(args, "zO|iiii:getaddrinfo",
2660 &hptr, &pobj, &family, &socktype,
2661 &protocol, &flags)) {
2662 return NULL;
2663 }
2664 if (PyInt_Check(pobj)) {
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00002665 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002666 pptr = pbuf;
2667 } else if (PyString_Check(pobj)) {
2668 pptr = PyString_AsString(pobj);
2669 } else if (pobj == Py_None) {
2670 pptr = (char *)NULL;
2671 } else {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002672 PyErr_SetString(socket_error, "Int or String expected");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002673 return NULL;
2674 }
2675 memset(&hints, 0, sizeof(hints));
2676 hints.ai_family = family;
2677 hints.ai_socktype = socktype;
2678 hints.ai_protocol = protocol;
2679 hints.ai_flags = flags;
2680 error = getaddrinfo(hptr, pptr, &hints, &res0);
2681 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002682 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002683 return NULL;
2684 }
2685
2686 if ((all = PyList_New(0)) == NULL)
2687 goto err;
2688 for (res = res0; res; res = res->ai_next) {
Guido van Rossum716aac02001-10-12 18:59:27 +00002689 PyObject *addr =
2690 makesockaddr(-1, res->ai_addr, res->ai_addrlen);
2691 if (addr == NULL)
2692 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002693 single = Py_BuildValue("iiisO", res->ai_family,
2694 res->ai_socktype, res->ai_protocol,
2695 res->ai_canonname ? res->ai_canonname : "",
Guido van Rossum716aac02001-10-12 18:59:27 +00002696 addr);
2697 Py_DECREF(addr);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002698 if (single == NULL)
2699 goto err;
2700
2701 if (PyList_Append(all, single))
2702 goto err;
2703 Py_XDECREF(single);
2704 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002705 return all;
2706 err:
2707 Py_XDECREF(single);
2708 Py_XDECREF(all);
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00002709 if (res0)
2710 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002711 return (PyObject *)NULL;
2712}
2713
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002714PyDoc_STRVAR(getaddrinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002715"getaddrinfo(host, port [, family, socktype, proto, flags])\n\
2716 -> list of (family, socktype, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002717\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002718Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002719
2720/* Python interface to getnameinfo(sa, flags). */
2721
2722/*ARGSUSED*/
2723static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002724socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002725{
2726 PyObject *sa = (PyObject *)NULL;
2727 int flags;
2728 char *hostp;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00002729 int port, flowinfo, scope_id;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002730 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
2731 struct addrinfo hints, *res = NULL;
2732 int error;
2733 PyObject *ret = (PyObject *)NULL;
2734
2735 flags = flowinfo = scope_id = 0;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00002736 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002737 return NULL;
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002738 if (!PyArg_ParseTuple(sa, "si|ii",
2739 &hostp, &port, &flowinfo, &scope_id))
Martin v. Löwis06b1d212001-11-02 23:34:52 +00002740 return NULL;
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00002741 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002742 memset(&hints, 0, sizeof(hints));
2743 hints.ai_family = PF_UNSPEC;
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00002744 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002745 error = getaddrinfo(hostp, pbuf, &hints, &res);
2746 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002747 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002748 goto fail;
2749 }
2750 if (res->ai_next) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002751 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002752 "sockaddr resolved to multiple addresses");
2753 goto fail;
2754 }
2755 switch (res->ai_family) {
2756 case AF_INET:
2757 {
2758 char *t1;
2759 int t2;
2760 if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002761 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002762 "IPv4 sockaddr must be 2 tuple");
2763 goto fail;
2764 }
2765 break;
2766 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002767#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002768 case AF_INET6:
2769 {
2770 struct sockaddr_in6 *sin6;
2771 sin6 = (struct sockaddr_in6 *)res->ai_addr;
2772 sin6->sin6_flowinfo = flowinfo;
2773 sin6->sin6_scope_id = scope_id;
2774 break;
2775 }
2776#endif
2777 }
2778 error = getnameinfo(res->ai_addr, res->ai_addrlen,
2779 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
2780 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002781 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002782 goto fail;
2783 }
2784 ret = Py_BuildValue("ss", hbuf, pbuf);
2785
2786fail:
2787 if (res)
2788 freeaddrinfo(res);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002789 return ret;
2790}
2791
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002792PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002793"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002794\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002795Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002796
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00002797
2798/* Python API to getting and setting the default timeout value. */
2799
2800static PyObject *
2801socket_getdefaulttimeout(PyObject *self)
2802{
2803 if (defaulttimeout < 0.0) {
2804 Py_INCREF(Py_None);
2805 return Py_None;
2806 }
2807 else
2808 return PyFloat_FromDouble(defaulttimeout);
2809}
2810
2811PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002812"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00002813\n\
2814Returns the default timeout in floating seconds for new socket objects.\n\
2815A value of None indicates that new socket objects have no timeout.\n\
2816When the socket module is first imported, the default is None.");
2817
2818static PyObject *
2819socket_setdefaulttimeout(PyObject *self, PyObject *arg)
2820{
2821 double timeout;
2822
2823 if (arg == Py_None)
2824 timeout = -1.0;
2825 else {
2826 timeout = PyFloat_AsDouble(arg);
2827 if (timeout < 0.0) {
2828 if (!PyErr_Occurred())
2829 PyErr_SetString(PyExc_ValueError,
2830 "Timeout value out of range");
2831 return NULL;
2832 }
2833 }
2834
2835 defaulttimeout = timeout;
2836
2837 Py_INCREF(Py_None);
2838 return Py_None;
2839}
2840
2841PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002842"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00002843\n\
2844Set the default timeout in floating seconds for new socket objects.\n\
2845A value of None indicates that new socket objects have no timeout.\n\
2846When the socket module is first imported, the default is None.");
2847
2848
Guido van Rossum30a685f1991-06-27 15:51:29 +00002849/* List of functions exported by this module. */
2850
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002851static PyMethodDef socket_methods[] = {
2852 {"gethostbyname", socket_gethostbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002853 METH_VARARGS, gethostbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002854 {"gethostbyname_ex", socket_gethostbyname_ex,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002855 METH_VARARGS, ghbn_ex_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002856 {"gethostbyaddr", socket_gethostbyaddr,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002857 METH_VARARGS, gethostbyaddr_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002858 {"gethostname", socket_gethostname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002859 METH_VARARGS, gethostname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002860 {"getservbyname", socket_getservbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002861 METH_VARARGS, getservbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002862 {"getprotobyname", socket_getprotobyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002863 METH_VARARGS,getprotobyname_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002864#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002865 {"fromfd", socket_fromfd,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002866 METH_VARARGS, fromfd_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002867#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002868 {"ntohs", socket_ntohs,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002869 METH_VARARGS, ntohs_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002870 {"ntohl", socket_ntohl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002871 METH_O, ntohl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002872 {"htons", socket_htons,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002873 METH_VARARGS, htons_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002874 {"htonl", socket_htonl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002875 METH_O, htonl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002876 {"inet_aton", socket_inet_aton,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002877 METH_VARARGS, inet_aton_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002878 {"inet_ntoa", socket_inet_ntoa,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002879 METH_VARARGS, inet_ntoa_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002880 {"getaddrinfo", socket_getaddrinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002881 METH_VARARGS, getaddrinfo_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002882 {"getnameinfo", socket_getnameinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002883 METH_VARARGS, getnameinfo_doc},
Guido van Rossum1693ba82002-07-18 21:11:26 +00002884 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00002885 METH_NOARGS, getdefaulttimeout_doc},
2886 {"setdefaulttimeout", socket_setdefaulttimeout,
2887 METH_O, setdefaulttimeout_doc},
Guido van Rossum30a685f1991-06-27 15:51:29 +00002888 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002889};
2890
Guido van Rossum30a685f1991-06-27 15:51:29 +00002891
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002892#ifdef RISCOS
2893#define OS_INIT_DEFINED
Guido van Rossumbe32c891996-06-20 16:25:29 +00002894
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002895static int
2896os_init(void)
2897{
2898 _kernel_swi_regs r;
2899
2900 r.r[0] = 0;
2901 _kernel_swi(0x43380, &r, &r);
2902 taskwindow = r.r[0];
2903
2904 return 0;
2905}
2906
2907#endif /* RISCOS */
2908
2909
2910#ifdef MS_WINDOWS
2911#define OS_INIT_DEFINED
2912
2913/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00002914
2915static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002916os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00002917{
2918 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00002919}
2920
2921static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002922os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00002923{
2924 WSADATA WSAData;
2925 int ret;
2926 char buf[100];
2927 ret = WSAStartup(0x0101, &WSAData);
2928 switch (ret) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002929 case 0: /* No error */
2930 atexit(os_cleanup);
2931 return 1; /* Success */
Guido van Rossumbe32c891996-06-20 16:25:29 +00002932 case WSASYSNOTREADY:
2933 PyErr_SetString(PyExc_ImportError,
2934 "WSAStartup failed: network not ready");
2935 break;
2936 case WSAVERNOTSUPPORTED:
2937 case WSAEINVAL:
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002938 PyErr_SetString(
2939 PyExc_ImportError,
2940 "WSAStartup failed: requested version not supported");
Guido van Rossumbe32c891996-06-20 16:25:29 +00002941 break;
2942 default:
Tim Peters885d4572001-11-28 20:27:42 +00002943 PyOS_snprintf(buf, sizeof(buf),
2944 "WSAStartup failed: error code %d", ret);
Guido van Rossumbe32c891996-06-20 16:25:29 +00002945 PyErr_SetString(PyExc_ImportError, buf);
2946 break;
2947 }
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002948 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00002949}
2950
Guido van Rossum8d665e61996-06-26 18:22:49 +00002951#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00002952
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002953
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002954#ifdef PYOS_OS2
2955#define OS_INIT_DEFINED
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002956
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002957/* Additional initialization for OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002958
2959static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002960os_init(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002961{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002962#ifndef PYCC_GCC
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002963 char reason[64];
2964 int rc = sock_init();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002965
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002966 if (rc == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002967 return 1; /* Success */
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002968 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002969
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002970 PyOS_snprintf(reason, sizeof(reason),
2971 "OS/2 TCP/IP Error# %d", sock_errno());
2972 PyErr_SetString(PyExc_ImportError, reason);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002973
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002974 return 0; /* Failure */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00002975#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002976 /* No need to initialise sockets with GCC/EMX */
2977 return 1; /* Success */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00002978#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002979}
2980
2981#endif /* PYOS_OS2 */
2982
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002983
2984#ifndef OS_INIT_DEFINED
2985static int
2986os_init(void)
2987{
2988 return 1; /* Success */
2989}
2990#endif
2991
2992
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002993/* C API table - always add new things to the end for binary
2994 compatibility. */
2995static
2996PySocketModule_APIObject PySocketModuleAPI =
2997{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002998 &sock_type,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002999};
3000
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003001
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003002/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003003
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003004 This module is actually called "_socket", and there's a wrapper
3005 "socket.py" which implements some additional functionality. On some
3006 platforms (e.g. Windows and OS/2), socket.py also implements a
3007 wrapper for the socket type that provides missing functionality such
3008 as makefile(), dup() and fromfd(). The import of "_socket" may fail
3009 with an ImportError exception if os-specific initialization fails.
3010 On Windows, this does WINSOCK initialization. When WINSOCK is
3011 initialized succesfully, a call to WSACleanup() is scheduled to be
3012 made at exit time.
3013*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003014
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003015PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003016"Implementation module for socket operations.\n\
3017\n\
3018See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003019
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003020PyMODINIT_FUNC
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003021init_socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003022{
Fred Drake4baedc12002-04-01 14:53:37 +00003023 PyObject *m;
Fred Drake4baedc12002-04-01 14:53:37 +00003024
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003025 if (!os_init())
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003026 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003027
3028 sock_type.ob_type = &PyType_Type;
3029 sock_type.tp_getattro = PyObject_GenericGetAttr;
3030 sock_type.tp_alloc = PyType_GenericAlloc;
3031 sock_type.tp_free = PyObject_Del;
3032 m = Py_InitModule3(PySocket_MODULE_NAME,
3033 socket_methods,
3034 socket_doc);
3035
3036 socket_error = PyErr_NewException("socket.error", NULL, NULL);
3037 if (socket_error == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003038 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003039 Py_INCREF(socket_error);
3040 PyModule_AddObject(m, "error", socket_error);
3041 socket_herror = PyErr_NewException("socket.herror",
3042 socket_error, NULL);
3043 if (socket_herror == NULL)
3044 return;
3045 Py_INCREF(socket_herror);
3046 PyModule_AddObject(m, "herror", socket_herror);
3047 socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003048 NULL);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003049 if (socket_gaierror == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003050 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003051 Py_INCREF(socket_gaierror);
3052 PyModule_AddObject(m, "gaierror", socket_gaierror);
3053 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00003054 if (PyModule_AddObject(m, "SocketType",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003055 (PyObject *)&sock_type) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003056 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003057 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00003058 if (PyModule_AddObject(m, "socket",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003059 (PyObject *)&sock_type) != 0)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003060 return;
Guido van Rossum09be4091999-08-09 14:40:40 +00003061
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003062 /* Export C API */
Fred Drake4baedc12002-04-01 14:53:37 +00003063 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003064 PyCObject_FromVoidPtr((void *)&PySocketModuleAPI, NULL)
3065 ) != 0)
3066 return;
3067
Guido van Rossum09be4091999-08-09 14:40:40 +00003068 /* Address families (we only support AF_INET and AF_UNIX) */
3069#ifdef AF_UNSPEC
Fred Drake4baedc12002-04-01 14:53:37 +00003070 PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00003071#endif
Fred Drake4baedc12002-04-01 14:53:37 +00003072 PyModule_AddIntConstant(m, "AF_INET", AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003073#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00003074 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003075#endif /* AF_INET6 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003076#ifdef AF_UNIX
Fred Drake4baedc12002-04-01 14:53:37 +00003077 PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003078#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00003079#ifdef AF_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00003080 /* Amateur Radio AX.25 */
3081 PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003082#endif
3083#ifdef AF_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00003084 PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00003085#endif
3086#ifdef AF_APPLETALK
Fred Drake4baedc12002-04-01 14:53:37 +00003087 /* Appletalk DDP */
3088 PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00003089#endif
3090#ifdef AF_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00003091 /* Amateur radio NetROM */
3092 PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00003093#endif
3094#ifdef AF_BRIDGE
Fred Drake4baedc12002-04-01 14:53:37 +00003095 /* Multiprotocol bridge */
3096 PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003097#endif
3098#ifdef AF_AAL5
Fred Drake4baedc12002-04-01 14:53:37 +00003099 /* Reserved for Werner's ATM */
3100 PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00003101#endif
3102#ifdef AF_X25
Fred Drake4baedc12002-04-01 14:53:37 +00003103 /* Reserved for X.25 project */
3104 PyModule_AddIntConstant(m, "AF_X25", AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003105#endif
3106#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00003107 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00003108#endif
3109#ifdef AF_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00003110 /* Amateur Radio X.25 PLP */
3111 PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003112#endif
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00003113#ifdef HAVE_NETPACKET_PACKET_H
Fred Drake4baedc12002-04-01 14:53:37 +00003114 PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET);
3115 PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET);
3116 PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST);
3117 PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST);
3118 PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST);
3119 PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST);
3120 PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING);
3121 PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK);
3122 PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00003123#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003124
3125 /* Socket types */
Fred Drake4baedc12002-04-01 14:53:37 +00003126 PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
3127 PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00003128#ifndef __BEOS__
3129/* We have incomplete socket support. */
Fred Drake4baedc12002-04-01 14:53:37 +00003130 PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
3131 PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
3132 PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00003133#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003134
3135#ifdef SO_DEBUG
Fred Drake4baedc12002-04-01 14:53:37 +00003136 PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003137#endif
3138#ifdef SO_ACCEPTCONN
Fred Drake4baedc12002-04-01 14:53:37 +00003139 PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003140#endif
3141#ifdef SO_REUSEADDR
Fred Drake4baedc12002-04-01 14:53:37 +00003142 PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003143#endif
3144#ifdef SO_KEEPALIVE
Fred Drake4baedc12002-04-01 14:53:37 +00003145 PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003146#endif
3147#ifdef SO_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00003148 PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003149#endif
3150#ifdef SO_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00003151 PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003152#endif
3153#ifdef SO_USELOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00003154 PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003155#endif
3156#ifdef SO_LINGER
Fred Drake4baedc12002-04-01 14:53:37 +00003157 PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003158#endif
3159#ifdef SO_OOBINLINE
Fred Drake4baedc12002-04-01 14:53:37 +00003160 PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003161#endif
3162#ifdef SO_REUSEPORT
Fred Drake4baedc12002-04-01 14:53:37 +00003163 PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003164#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003165#ifdef SO_SNDBUF
Fred Drake4baedc12002-04-01 14:53:37 +00003166 PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003167#endif
3168#ifdef SO_RCVBUF
Fred Drake4baedc12002-04-01 14:53:37 +00003169 PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003170#endif
3171#ifdef SO_SNDLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00003172 PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003173#endif
3174#ifdef SO_RCVLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00003175 PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003176#endif
3177#ifdef SO_SNDTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00003178 PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003179#endif
3180#ifdef SO_RCVTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00003181 PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003182#endif
3183#ifdef SO_ERROR
Fred Drake4baedc12002-04-01 14:53:37 +00003184 PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003185#endif
3186#ifdef SO_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00003187 PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003188#endif
3189
3190 /* Maximum number of connections for "listen" */
3191#ifdef SOMAXCONN
Fred Drake4baedc12002-04-01 14:53:37 +00003192 PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003193#else
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003194 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003195#endif
3196
3197 /* Flags for send, recv */
3198#ifdef MSG_OOB
Fred Drake4baedc12002-04-01 14:53:37 +00003199 PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003200#endif
3201#ifdef MSG_PEEK
Fred Drake4baedc12002-04-01 14:53:37 +00003202 PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003203#endif
3204#ifdef MSG_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00003205 PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003206#endif
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00003207#ifdef MSG_DONTWAIT
Fred Drake4baedc12002-04-01 14:53:37 +00003208 PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00003209#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003210#ifdef MSG_EOR
Fred Drake4baedc12002-04-01 14:53:37 +00003211 PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003212#endif
3213#ifdef MSG_TRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00003214 PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003215#endif
3216#ifdef MSG_CTRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00003217 PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003218#endif
3219#ifdef MSG_WAITALL
Fred Drake4baedc12002-04-01 14:53:37 +00003220 PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003221#endif
3222#ifdef MSG_BTAG
Fred Drake4baedc12002-04-01 14:53:37 +00003223 PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003224#endif
3225#ifdef MSG_ETAG
Fred Drake4baedc12002-04-01 14:53:37 +00003226 PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003227#endif
3228
3229 /* Protocol level and numbers, usable for [gs]etsockopt */
3230#ifdef SOL_SOCKET
Fred Drake4baedc12002-04-01 14:53:37 +00003231 PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003232#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003233#ifdef SOL_IP
Fred Drake4baedc12002-04-01 14:53:37 +00003234 PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003235#else
Fred Drake4baedc12002-04-01 14:53:37 +00003236 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00003237#endif
3238#ifdef SOL_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00003239 PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00003240#endif
3241#ifdef SOL_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00003242 PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003243#endif
3244#ifdef SOL_ATALK
Fred Drake4baedc12002-04-01 14:53:37 +00003245 PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00003246#endif
3247#ifdef SOL_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00003248 PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00003249#endif
3250#ifdef SOL_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00003251 PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003252#endif
3253#ifdef SOL_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00003254 PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003255#else
Fred Drake4baedc12002-04-01 14:53:37 +00003256 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00003257#endif
3258#ifdef SOL_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00003259 PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003260#else
Fred Drake4baedc12002-04-01 14:53:37 +00003261 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00003262#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003263#ifdef IPPROTO_IP
Fred Drake4baedc12002-04-01 14:53:37 +00003264 PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00003265#else
Fred Drake4baedc12002-04-01 14:53:37 +00003266 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003267#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003268#ifdef IPPROTO_HOPOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003269 PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003270#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003271#ifdef IPPROTO_ICMP
Fred Drake4baedc12002-04-01 14:53:37 +00003272 PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00003273#else
Fred Drake4baedc12002-04-01 14:53:37 +00003274 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003275#endif
3276#ifdef IPPROTO_IGMP
Fred Drake4baedc12002-04-01 14:53:37 +00003277 PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003278#endif
3279#ifdef IPPROTO_GGP
Fred Drake4baedc12002-04-01 14:53:37 +00003280 PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003281#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003282#ifdef IPPROTO_IPV4
Fred Drake4baedc12002-04-01 14:53:37 +00003283 PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003284#endif
3285#ifdef IPPROTO_IPIP
Fred Drake4baedc12002-04-01 14:53:37 +00003286 PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003287#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003288#ifdef IPPROTO_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00003289 PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00003290#else
Fred Drake4baedc12002-04-01 14:53:37 +00003291 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003292#endif
3293#ifdef IPPROTO_EGP
Fred Drake4baedc12002-04-01 14:53:37 +00003294 PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003295#endif
3296#ifdef IPPROTO_PUP
Fred Drake4baedc12002-04-01 14:53:37 +00003297 PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003298#endif
3299#ifdef IPPROTO_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00003300 PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00003301#else
Fred Drake4baedc12002-04-01 14:53:37 +00003302 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003303#endif
3304#ifdef IPPROTO_IDP
Fred Drake4baedc12002-04-01 14:53:37 +00003305 PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003306#endif
3307#ifdef IPPROTO_HELLO
Fred Drake4baedc12002-04-01 14:53:37 +00003308 PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003309#endif
3310#ifdef IPPROTO_ND
Fred Drake4baedc12002-04-01 14:53:37 +00003311 PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003312#endif
3313#ifdef IPPROTO_TP
Fred Drake4baedc12002-04-01 14:53:37 +00003314 PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003315#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003316#ifdef IPPROTO_IPV6
Fred Drake4baedc12002-04-01 14:53:37 +00003317 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003318#endif
3319#ifdef IPPROTO_ROUTING
Fred Drake4baedc12002-04-01 14:53:37 +00003320 PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003321#endif
3322#ifdef IPPROTO_FRAGMENT
Fred Drake4baedc12002-04-01 14:53:37 +00003323 PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003324#endif
3325#ifdef IPPROTO_RSVP
Fred Drake4baedc12002-04-01 14:53:37 +00003326 PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003327#endif
3328#ifdef IPPROTO_GRE
Fred Drake4baedc12002-04-01 14:53:37 +00003329 PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003330#endif
3331#ifdef IPPROTO_ESP
Fred Drake4baedc12002-04-01 14:53:37 +00003332 PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003333#endif
3334#ifdef IPPROTO_AH
Fred Drake4baedc12002-04-01 14:53:37 +00003335 PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003336#endif
3337#ifdef IPPROTO_MOBILE
Fred Drake4baedc12002-04-01 14:53:37 +00003338 PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003339#endif
3340#ifdef IPPROTO_ICMPV6
Fred Drake4baedc12002-04-01 14:53:37 +00003341 PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003342#endif
3343#ifdef IPPROTO_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00003344 PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003345#endif
3346#ifdef IPPROTO_DSTOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003347 PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003348#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003349#ifdef IPPROTO_XTP
Fred Drake4baedc12002-04-01 14:53:37 +00003350 PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003351#endif
3352#ifdef IPPROTO_EON
Fred Drake4baedc12002-04-01 14:53:37 +00003353 PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003354#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003355#ifdef IPPROTO_PIM
Fred Drake4baedc12002-04-01 14:53:37 +00003356 PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003357#endif
3358#ifdef IPPROTO_IPCOMP
Fred Drake4baedc12002-04-01 14:53:37 +00003359 PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003360#endif
3361#ifdef IPPROTO_VRRP
Fred Drake4baedc12002-04-01 14:53:37 +00003362 PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003363#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003364#ifdef IPPROTO_BIP
Fred Drake4baedc12002-04-01 14:53:37 +00003365 PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003366#endif
3367/**/
3368#ifdef IPPROTO_RAW
Fred Drake4baedc12002-04-01 14:53:37 +00003369 PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00003370#else
Fred Drake4baedc12002-04-01 14:53:37 +00003371 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003372#endif
3373#ifdef IPPROTO_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00003374 PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003375#endif
3376
3377 /* Some port configuration */
3378#ifdef IPPORT_RESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00003379 PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003380#else
Fred Drake4baedc12002-04-01 14:53:37 +00003381 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003382#endif
3383#ifdef IPPORT_USERRESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00003384 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003385#else
Fred Drake4baedc12002-04-01 14:53:37 +00003386 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003387#endif
3388
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003389 /* Some reserved IP v.4 addresses */
3390#ifdef INADDR_ANY
Fred Drake4baedc12002-04-01 14:53:37 +00003391 PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003392#else
Fred Drake4baedc12002-04-01 14:53:37 +00003393 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003394#endif
3395#ifdef INADDR_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00003396 PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003397#else
Fred Drake4baedc12002-04-01 14:53:37 +00003398 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003399#endif
3400#ifdef INADDR_LOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00003401 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003402#else
Fred Drake4baedc12002-04-01 14:53:37 +00003403 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003404#endif
3405#ifdef INADDR_UNSPEC_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003406 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003407#else
Fred Drake4baedc12002-04-01 14:53:37 +00003408 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003409#endif
3410#ifdef INADDR_ALLHOSTS_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003411 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
3412 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003413#else
Fred Drake4baedc12002-04-01 14:53:37 +00003414 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003415#endif
3416#ifdef INADDR_MAX_LOCAL_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003417 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
3418 INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003419#else
Fred Drake4baedc12002-04-01 14:53:37 +00003420 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003421#endif
3422#ifdef INADDR_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00003423 PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003424#else
Fred Drake4baedc12002-04-01 14:53:37 +00003425 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003426#endif
3427
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003428 /* IPv4 [gs]etsockopt options */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003429#ifdef IP_OPTIONS
Fred Drake4baedc12002-04-01 14:53:37 +00003430 PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003431#endif
3432#ifdef IP_HDRINCL
Fred Drake4baedc12002-04-01 14:53:37 +00003433 PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003434#endif
3435#ifdef IP_TOS
Fred Drake4baedc12002-04-01 14:53:37 +00003436 PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003437#endif
3438#ifdef IP_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00003439 PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003440#endif
3441#ifdef IP_RECVOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003442 PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003443#endif
3444#ifdef IP_RECVRETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003445 PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003446#endif
3447#ifdef IP_RECVDSTADDR
Fred Drake4baedc12002-04-01 14:53:37 +00003448 PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003449#endif
3450#ifdef IP_RETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003451 PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003452#endif
3453#ifdef IP_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00003454 PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003455#endif
3456#ifdef IP_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00003457 PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003458#endif
3459#ifdef IP_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00003460 PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003461#endif
3462#ifdef IP_ADD_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00003463 PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003464#endif
3465#ifdef IP_DROP_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00003466 PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003467#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003468#ifdef IP_DEFAULT_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00003469 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
3470 IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00003471#endif
3472#ifdef IP_DEFAULT_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00003473 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
3474 IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003475#endif
3476#ifdef IP_MAX_MEMBERSHIPS
Fred Drake4baedc12002-04-01 14:53:37 +00003477 PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00003478#endif
3479
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003480 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
3481#ifdef IPV6_JOIN_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003482 PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003483#endif
3484#ifdef IPV6_LEAVE_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003485 PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003486#endif
3487#ifdef IPV6_MULTICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00003488 PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003489#endif
3490#ifdef IPV6_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00003491 PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003492#endif
3493#ifdef IPV6_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00003494 PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003495#endif
3496#ifdef IPV6_UNICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00003497 PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003498#endif
3499
Guido van Rossum09be4091999-08-09 14:40:40 +00003500 /* TCP options */
3501#ifdef TCP_NODELAY
Fred Drake4baedc12002-04-01 14:53:37 +00003502 PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00003503#endif
3504#ifdef TCP_MAXSEG
Fred Drake4baedc12002-04-01 14:53:37 +00003505 PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00003506#endif
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003507#ifdef TCP_CORK
Fred Drake4baedc12002-04-01 14:53:37 +00003508 PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003509#endif
3510#ifdef TCP_KEEPIDLE
Fred Drake4baedc12002-04-01 14:53:37 +00003511 PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003512#endif
3513#ifdef TCP_KEEPINTVL
Fred Drake4baedc12002-04-01 14:53:37 +00003514 PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003515#endif
3516#ifdef TCP_KEEPCNT
Fred Drake4baedc12002-04-01 14:53:37 +00003517 PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003518#endif
3519#ifdef TCP_SYNCNT
Fred Drake4baedc12002-04-01 14:53:37 +00003520 PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003521#endif
3522#ifdef TCP_LINGER2
Fred Drake4baedc12002-04-01 14:53:37 +00003523 PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003524#endif
3525#ifdef TCP_DEFER_ACCEPT
Fred Drake4baedc12002-04-01 14:53:37 +00003526 PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003527#endif
3528#ifdef TCP_WINDOW_CLAMP
Fred Drake4baedc12002-04-01 14:53:37 +00003529 PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003530#endif
3531#ifdef TCP_INFO
Fred Drake4baedc12002-04-01 14:53:37 +00003532 PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003533#endif
3534#ifdef TCP_QUICKACK
Fred Drake4baedc12002-04-01 14:53:37 +00003535 PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003536#endif
3537
Guido van Rossum09be4091999-08-09 14:40:40 +00003538
3539 /* IPX options */
3540#ifdef IPX_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00003541 PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003542#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003543
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003544 /* get{addr,name}info parameters */
3545#ifdef EAI_ADDRFAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00003546 PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003547#endif
3548#ifdef EAI_AGAIN
Fred Drake4baedc12002-04-01 14:53:37 +00003549 PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003550#endif
3551#ifdef EAI_BADFLAGS
Fred Drake4baedc12002-04-01 14:53:37 +00003552 PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003553#endif
3554#ifdef EAI_FAIL
Fred Drake4baedc12002-04-01 14:53:37 +00003555 PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003556#endif
3557#ifdef EAI_FAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00003558 PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003559#endif
3560#ifdef EAI_MEMORY
Fred Drake4baedc12002-04-01 14:53:37 +00003561 PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003562#endif
3563#ifdef EAI_NODATA
Fred Drake4baedc12002-04-01 14:53:37 +00003564 PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003565#endif
3566#ifdef EAI_NONAME
Fred Drake4baedc12002-04-01 14:53:37 +00003567 PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003568#endif
3569#ifdef EAI_SERVICE
Fred Drake4baedc12002-04-01 14:53:37 +00003570 PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003571#endif
3572#ifdef EAI_SOCKTYPE
Fred Drake4baedc12002-04-01 14:53:37 +00003573 PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003574#endif
3575#ifdef EAI_SYSTEM
Fred Drake4baedc12002-04-01 14:53:37 +00003576 PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003577#endif
3578#ifdef EAI_BADHINTS
Fred Drake4baedc12002-04-01 14:53:37 +00003579 PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003580#endif
3581#ifdef EAI_PROTOCOL
Fred Drake4baedc12002-04-01 14:53:37 +00003582 PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003583#endif
3584#ifdef EAI_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00003585 PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003586#endif
3587#ifdef AI_PASSIVE
Fred Drake4baedc12002-04-01 14:53:37 +00003588 PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003589#endif
3590#ifdef AI_CANONNAME
Fred Drake4baedc12002-04-01 14:53:37 +00003591 PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003592#endif
3593#ifdef AI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00003594 PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003595#endif
3596#ifdef AI_MASK
Fred Drake4baedc12002-04-01 14:53:37 +00003597 PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003598#endif
3599#ifdef AI_ALL
Fred Drake4baedc12002-04-01 14:53:37 +00003600 PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003601#endif
3602#ifdef AI_V4MAPPED_CFG
Fred Drake4baedc12002-04-01 14:53:37 +00003603 PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003604#endif
3605#ifdef AI_ADDRCONFIG
Fred Drake4baedc12002-04-01 14:53:37 +00003606 PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003607#endif
3608#ifdef AI_V4MAPPED
Fred Drake4baedc12002-04-01 14:53:37 +00003609 PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003610#endif
3611#ifdef AI_DEFAULT
Fred Drake4baedc12002-04-01 14:53:37 +00003612 PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003613#endif
3614#ifdef NI_MAXHOST
Fred Drake4baedc12002-04-01 14:53:37 +00003615 PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003616#endif
3617#ifdef NI_MAXSERV
Fred Drake4baedc12002-04-01 14:53:37 +00003618 PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003619#endif
3620#ifdef NI_NOFQDN
Fred Drake4baedc12002-04-01 14:53:37 +00003621 PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003622#endif
3623#ifdef NI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00003624 PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003625#endif
3626#ifdef NI_NAMEREQD
Fred Drake4baedc12002-04-01 14:53:37 +00003627 PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003628#endif
3629#ifdef NI_NUMERICSERV
Fred Drake4baedc12002-04-01 14:53:37 +00003630 PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003631#endif
3632#ifdef NI_DGRAM
Fred Drake4baedc12002-04-01 14:53:37 +00003633 PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003634#endif
3635
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003636 /* Initialize gethostbyname lock */
Guido van Rossum3baaa131999-03-22 21:44:51 +00003637#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum65d5b571998-12-21 19:32:43 +00003638 gethostbyname_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003639#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003640}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003641
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003642
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003643#ifndef HAVE_INET_PTON
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003644
3645/* Simplistic emulation code for inet_pton that only works for IPv4 */
3646
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003647int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003648inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003649{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003650 if (af == AF_INET) {
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003651 long packed_addr;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003652 packed_addr = inet_addr(src);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003653 if (packed_addr == INADDR_NONE)
3654 return 0;
3655 memcpy(dst, &packed_addr, 4);
3656 return 1;
3657 }
3658 /* Should set errno to EAFNOSUPPORT */
3659 return -1;
3660}
3661
Martin v. Löwisc925b1532001-07-21 09:42:15 +00003662const char *
3663inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003664{
3665 if (af == AF_INET) {
3666 struct in_addr packed_addr;
3667 if (size < 16)
3668 /* Should set errno to ENOSPC. */
3669 return NULL;
3670 memcpy(&packed_addr, src, sizeof(packed_addr));
3671 return strncpy(dst, inet_ntoa(packed_addr), size);
3672 }
3673 /* Should set errno to EAFNOSUPPORT */
3674 return NULL;
3675}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003676
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003677#endif