blob: bdeddea0c71934711669159f03c386edd471abda [file] [log] [blame]
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001/* Socket module */
2
3/*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005This module provides an interface to Berkeley socket IPC.
6
7Limitations:
8
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00009- Only AF_INET, AF_INET6 and AF_UNIX address families are supported in a
Jeremy Hylton22308652001-02-02 03:23:09 +000010 portable manner, though AF_PACKET is supported under Linux.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000011- No read/write operations (use sendall/recv or makefile instead).
12- Additional restrictions apply on some non-Unix platforms (compensated
13 for by socket.py).
Guido van Rossum6574b3e1991-06-25 21:36:08 +000014
Guido van Rossum27e177d1995-03-16 15:43:47 +000015Module interface:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000016
Guido van Rossum27e177d1995-03-16 15:43:47 +000017- socket.error: exception raised for socket specific errors
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000018- socket.gaierror: exception raised for getaddrinfo/getnameinfo errors,
19 a subclass of socket.error
20- socket.herror: exception raised for gethostby* errors,
21 a subclass of socket.error
Guido van Rossum83a072d2002-09-03 19:10:18 +000022- socket.fromfd(fd, family, type[, proto]) --> new socket object (created
23 from an existing file descriptor)
Guido van Rossum30a685f1991-06-27 15:51:29 +000024- socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd')
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000025- socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...])
Guido van Rossum27e177d1995-03-16 15:43:47 +000026- socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com')
Guido van Rossum25405c71996-12-19 16:42:52 +000027- socket.getprotobyname(protocolname) --> protocol number
Guido van Rossum27e177d1995-03-16 15:43:47 +000028- socket.getservbyname(servicename, protocolname) --> port number
Guido van Rossum83a072d2002-09-03 19:10:18 +000029- socket.socket([family[, type [, proto]]]) --> new socket object
Guido van Rossum006bf911996-06-12 04:04:55 +000030- socket.ntohs(16 bit value) --> new int object
31- socket.ntohl(32 bit value) --> new int object
32- socket.htons(16 bit value) --> new int object
33- socket.htonl(32 bit value) --> new int object
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000034- socket.getaddrinfo(host, port [, family, socktype, proto, flags])
35 --> List of (family, socktype, proto, canonname, sockaddr)
36- socket.getnameinfo(sockaddr, flags) --> (host, port)
Guido van Rossum27e177d1995-03-16 15:43:47 +000037- socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +000038- socket.inet_aton(IP address) -> 32-bit packed IP representation
39- socket.inet_ntoa(packed IP) -> IP address string
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +000040- socket.getdefaulttimeout() -> None | float
41- socket.setdefaulttimeout(None | float)
Guido van Rossum6574b3e1991-06-25 21:36:08 +000042- an Internet socket address is a pair (hostname, port)
43 where hostname can be anything recognized by gethostbyname()
44 (including the dd.dd.dd.dd notation) and port is in host byte order
45- where a hostname is returned, the dd.dd.dd.dd notation is used
Guido van Rossum27e177d1995-03-16 15:43:47 +000046- a UNIX domain socket address is a string specifying the pathname
Jeremy Hylton22308652001-02-02 03:23:09 +000047- an AF_PACKET socket address is a tuple containing a string
48 specifying the ethernet interface and an integer specifying
49 the Ethernet protocol number to be received. For example:
Jeremy Hyltondbfb6622001-02-02 19:55:17 +000050 ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple
51 specify packet-type and ha-type/addr -- these are ignored by
Jeremy Hylton22308652001-02-02 03:23:09 +000052 networking code, but accepted since they are returned by the
53 getsockname() method.
Guido van Rossum6574b3e1991-06-25 21:36:08 +000054
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000055Local naming conventions:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000056
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000057- names starting with sock_ are socket object methods
58- names starting with socket_ are module-level functions
59- names starting with PySocket are exported through socketmodule.h
Guido van Rossum30a685f1991-06-27 15:51:29 +000060
Guido van Rossum6574b3e1991-06-25 21:36:08 +000061*/
62
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000063#include "Python.h"
64
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000065/* Socket object documentation */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000066PyDoc_STRVAR(sock_doc,
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000067"socket([family[, type[, proto]]]) -> socket object\n\
68\n\
69Open a socket of the given type. The family argument specifies the\n\
70address family; it defaults to AF_INET. The type argument specifies\n\
71whether this is a stream (SOCK_STREAM, this is the default)\n\
72or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\
73specifying the default protocol. Keyword arguments are accepted.\n\
74\n\
75A socket object represents one endpoint of a network connection.\n\
76\n\
77Methods of socket objects (keyword arguments not allowed):\n\
78\n\
79accept() -- accept a connection, returning new socket and client address\n\
80bind(addr) -- bind the socket to a local address\n\
81close() -- close the socket\n\
82connect(addr) -- connect the socket to a remote address\n\
83connect_ex(addr) -- connect, return an error code instead of an exception\n\
84dup() -- return a new socket object identical to the current one [*]\n\
85fileno() -- return underlying file descriptor\n\
86getpeername() -- return remote address [*]\n\
87getsockname() -- return local address\n\
88getsockopt(level, optname[, buflen]) -- get socket options\n\
89gettimeout() -- return timeout or None\n\
90listen(n) -- start listening for incoming connections\n\
91makefile([mode, [bufsize]]) -- return a file object for the socket [*]\n\
92recv(buflen[, flags]) -- receive data\n\
93recvfrom(buflen[, flags]) -- receive data and sender's address\n\
94sendall(data[, flags]) -- send all data\n\
95send(data[, flags]) -- send data, may not send all of it\n\
96sendto(data[, flags], addr) -- send data to a given address\n\
97setblocking(0 | 1) -- set or clear the blocking I/O flag\n\
98setsockopt(level, optname, value) -- set socket options\n\
99settimeout(None | float) -- set or clear the timeout\n\
100shutdown(how) -- shut down traffic in one or both directions\n\
101\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000102 [*] not available on all platforms!");
Guido van Rossum3baaa131999-03-22 21:44:51 +0000103
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000104/* XXX This is a terrible mess of of platform-dependent preprocessor hacks.
105 I hope some day someone can clean this up please... */
106
Guido van Rossum9376b741999-09-15 22:01:40 +0000107/* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure
108 script doesn't get this right, so we hardcode some platform checks below.
109 On the other hand, not all Linux versions agree, so there the settings
110 computed by the configure script are needed! */
111
112#ifndef linux
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000113# undef HAVE_GETHOSTBYNAME_R_3_ARG
114# undef HAVE_GETHOSTBYNAME_R_5_ARG
115# undef HAVE_GETHOSTBYNAME_R_6_ARG
Guido van Rossum9376b741999-09-15 22:01:40 +0000116#endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000117
Guido van Rossum7a122991999-04-13 04:07:32 +0000118#ifndef WITH_THREAD
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000119# undef HAVE_GETHOSTBYNAME_R
Guido van Rossum7a122991999-04-13 04:07:32 +0000120#endif
121
Guido van Rossume7de2061999-03-24 17:24:33 +0000122#ifdef HAVE_GETHOSTBYNAME_R
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000123# if defined(_AIX) || defined(__osf__)
124# define HAVE_GETHOSTBYNAME_R_3_ARG
125# elif defined(__sun) || defined(__sgi)
126# define HAVE_GETHOSTBYNAME_R_5_ARG
127# elif defined(linux)
Guido van Rossum9376b741999-09-15 22:01:40 +0000128/* Rely on the configure script */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000129# else
130# undef HAVE_GETHOSTBYNAME_R
131# endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000132#endif
133
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000134#if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \
135 !defined(MS_WINDOWS)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000136# define USE_GETHOSTBYNAME_LOCK
Guido van Rossum3baaa131999-03-22 21:44:51 +0000137#endif
138
139#ifdef USE_GETHOSTBYNAME_LOCK
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000140# include "pythread.h"
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000141#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000142
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000143#if defined(PYCC_VACPP)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000144# include <types.h>
145# include <io.h>
146# include <sys/ioctl.h>
147# include <utils.h>
148# include <ctype.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000149#endif
150
151#if defined(PYOS_OS2)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000152# define INCL_DOS
153# define INCL_DOSERRORS
154# define INCL_NOPMAPI
155# include <os2.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000156#endif
157
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000158#if defined(__sgi)&&_COMPILER_VERSION>700 && !_SGIAPI
159/* make sure that the reentrant (gethostbyaddr_r etc)
160 functions are declared correctly if compiling with
161 MIPSPro 7.x in ANSI C mode (default) */
162#define _SGIAPI 1
163#include "netdb.h"
164#endif
165
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000166/* Generic includes */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000167#include <sys/types.h>
Guido van Rossum81194471991-07-27 21:42:02 +0000168#include <signal.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000169
Marc-André Lemburg976ade62002-02-16 18:47:07 +0000170/* Generic socket object definitions and includes */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000171#define PySocket_BUILDING_SOCKET
Marc-André Lemburgbb8b78b2002-02-16 18:44:52 +0000172#include "socketmodule.h"
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000173
174/* Addressing includes */
175
Guido van Rossum6f489d91996-06-28 20:15:15 +0000176#ifndef MS_WINDOWS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000177
178/* Non-MS WINDOWS includes */
179# include <netdb.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000180
Guido van Rossum9376b741999-09-15 22:01:40 +0000181/* Headers needed for inet_ntoa() and inet_addr() */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000182# ifdef __BEOS__
183# include <net/netdb.h>
184# elif defined(PYOS_OS2) && defined(PYCC_VACPP)
185# include <netdb.h>
Tim Peters603c6832001-11-05 02:45:59 +0000186typedef size_t socklen_t;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000187# else
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000188# include <arpa/inet.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000189# endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000190
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000191# ifndef RISCOS
192# include <fcntl.h>
193# else
194# include <sys/fcntl.h>
195# define NO_DUP
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000196int h_errno; /* not used */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000197# endif
198
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000199#else
Guido van Rossum48a680c2001-03-02 06:34:14 +0000200
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000201/* MS_WINDOWS includes */
202# include <fcntl.h>
Guido van Rossum48a680c2001-03-02 06:34:14 +0000203
Jeremy Hylton22308652001-02-02 03:23:09 +0000204#endif
205
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000206#ifdef HAVE_STDDEF_H
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000207# include <stddef.h>
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000208#endif
209
210#ifndef offsetof
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000211# define offsetof(type, member) ((size_t)(&((type *)0)->member))
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000212#endif
213
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000214#ifndef O_NDELAY
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000215# define O_NDELAY O_NONBLOCK /* For QNX only? */
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000216#endif
217
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000218#include "addrinfo.h"
219
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000220#ifndef HAVE_INET_PTON
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000221int inet_pton(int af, const char *src, void *dst);
Martin v. Löwisc925b1532001-07-21 09:42:15 +0000222const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000223#endif
224
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000225#ifdef __APPLE__
226/* On OS X, getaddrinfo returns no error indication of lookup
227 failure, so we must use the emulation instead of the libinfo
228 implementation. Unfortunately, performing an autoconf test
229 for this bug would require DNS access for the machine performing
230 the configuration, which is not acceptable. Therefore, we
231 determine the bug just by checking for __APPLE__. If this bug
232 gets ever fixed, perhaps checking for sys/version.h would be
233 appropriate, which is 10/0 on the system with the bug. */
Jack Jansen84262fb2002-07-02 14:40:42 +0000234#ifndef HAVE_GETNAMEINFO
235/* This bug seems to be fixed in Jaguar. Ths easiest way I could
236 Find to check for Jaguar is that it has getnameinfo(), which
237 older releases don't have */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000238#undef HAVE_GETADDRINFO
239/* avoid clashes with the C library definition of the symbol. */
240#define getaddrinfo fake_getaddrinfo
241#endif
Jack Jansen84262fb2002-07-02 14:40:42 +0000242#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000243
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000244/* I know this is a bad practice, but it is the easiest... */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000245#if !defined(HAVE_GETADDRINFO)
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000246#include "getaddrinfo.c"
247#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000248#if !defined(HAVE_GETNAMEINFO)
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000249#include "getnameinfo.c"
250#endif
251
Guido van Rossumbcc20741998-08-04 22:53:56 +0000252#if defined(MS_WINDOWS) || defined(__BEOS__)
253/* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000254/* seem to be a few differences in the API */
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000255#define SOCKETCLOSE closesocket
Guido van Rossumbe32c891996-06-20 16:25:29 +0000256#define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000257#endif
258
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000259#ifdef MS_WIN32
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000260#define EAFNOSUPPORT WSAEAFNOSUPPORT
261#define snprintf _snprintf
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000262#endif
Fred Drakea04eaad2000-06-30 02:46:07 +0000263
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000264#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000265#define SOCKETCLOSE soclose
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000266#define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000267#endif
268
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000269#ifndef SOCKETCLOSE
270#define SOCKETCLOSE close
271#endif
272
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000273/* XXX There's a problem here: *static* functions are not supposed to have
274 a Py prefix (or use CapitalizedWords). Later... */
275
Guido van Rossum30a685f1991-06-27 15:51:29 +0000276/* Global variable holding the exception type for errors detected
277 by this module (but not argument type or memory errors, etc.). */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000278static PyObject *socket_error;
279static PyObject *socket_herror;
280static PyObject *socket_gaierror;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000281
Guido van Rossum48a680c2001-03-02 06:34:14 +0000282#ifdef RISCOS
283/* Global variable which is !=0 if Python is running in a RISC OS taskwindow */
284static int taskwindow;
285#endif
286
Tim Peters643a7fc2002-02-17 04:13:21 +0000287/* A forward reference to the socket type object.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000288 The sock_type variable contains pointers to various functions,
289 some of which call new_sockobject(), which uses sock_type, so
Tim Peters643a7fc2002-02-17 04:13:21 +0000290 there has to be a circular reference. */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000291static PyTypeObject sock_type;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000292
Guido van Rossum30a685f1991-06-27 15:51:29 +0000293/* Convenience function to raise an error according to errno
294 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000295
Guido van Rossum73624e91994-10-10 17:59:00 +0000296static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000297set_error(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000298{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000299#ifdef MS_WINDOWS
Mark Hammond46a733d2000-07-24 01:45:11 +0000300 int err_no = WSAGetLastError();
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000301 static struct {
302 int no;
303 const char *msg;
304 } *msgp, msgs[] = {
305 {WSAEINTR, "Interrupted system call"},
306 {WSAEBADF, "Bad file descriptor"},
307 {WSAEACCES, "Permission denied"},
308 {WSAEFAULT, "Bad address"},
309 {WSAEINVAL, "Invalid argument"},
310 {WSAEMFILE, "Too many open files"},
311 {WSAEWOULDBLOCK,
312 "The socket operation could not complete "
313 "without blocking"},
314 {WSAEINPROGRESS, "Operation now in progress"},
315 {WSAEALREADY, "Operation already in progress"},
316 {WSAENOTSOCK, "Socket operation on non-socket"},
317 {WSAEDESTADDRREQ, "Destination address required"},
318 {WSAEMSGSIZE, "Message too long"},
319 {WSAEPROTOTYPE, "Protocol wrong type for socket"},
320 {WSAENOPROTOOPT, "Protocol not available"},
321 {WSAEPROTONOSUPPORT, "Protocol not supported"},
322 {WSAESOCKTNOSUPPORT, "Socket type not supported"},
323 {WSAEOPNOTSUPP, "Operation not supported"},
324 {WSAEPFNOSUPPORT, "Protocol family not supported"},
325 {WSAEAFNOSUPPORT, "Address family not supported"},
326 {WSAEADDRINUSE, "Address already in use"},
327 {WSAEADDRNOTAVAIL, "Can't assign requested address"},
328 {WSAENETDOWN, "Network is down"},
329 {WSAENETUNREACH, "Network is unreachable"},
330 {WSAENETRESET, "Network dropped connection on reset"},
331 {WSAECONNABORTED, "Software caused connection abort"},
332 {WSAECONNRESET, "Connection reset by peer"},
333 {WSAENOBUFS, "No buffer space available"},
334 {WSAEISCONN, "Socket is already connected"},
335 {WSAENOTCONN, "Socket is not connected"},
336 {WSAESHUTDOWN, "Can't send after socket shutdown"},
337 {WSAETOOMANYREFS, "Too many references: can't splice"},
338 {WSAETIMEDOUT, "Operation timed out"},
339 {WSAECONNREFUSED, "Connection refused"},
340 {WSAELOOP, "Too many levels of symbolic links"},
341 {WSAENAMETOOLONG, "File name too long"},
342 {WSAEHOSTDOWN, "Host is down"},
343 {WSAEHOSTUNREACH, "No route to host"},
344 {WSAENOTEMPTY, "Directory not empty"},
345 {WSAEPROCLIM, "Too many processes"},
346 {WSAEUSERS, "Too many users"},
347 {WSAEDQUOT, "Disc quota exceeded"},
348 {WSAESTALE, "Stale NFS file handle"},
349 {WSAEREMOTE, "Too many levels of remote in path"},
350 {WSASYSNOTREADY, "Network subsystem is unvailable"},
351 {WSAVERNOTSUPPORTED, "WinSock version is not supported"},
352 {WSANOTINITIALISED,
353 "Successful WSAStartup() not yet performed"},
354 {WSAEDISCON, "Graceful shutdown in progress"},
355 /* Resolver errors */
356 {WSAHOST_NOT_FOUND, "No such host is known"},
357 {WSATRY_AGAIN, "Host not found, or server failed"},
358 {WSANO_RECOVERY, "Unexpected server error encountered"},
359 {WSANO_DATA, "Valid name without requested data"},
360 {WSANO_ADDRESS, "No address, look for MX record"},
361 {0, NULL}
362 };
Mark Hammond46a733d2000-07-24 01:45:11 +0000363 if (err_no) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000364 PyObject *v;
Mark Hammond46a733d2000-07-24 01:45:11 +0000365 const char *msg = "winsock error";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000366
Mark Hammond46a733d2000-07-24 01:45:11 +0000367 for (msgp = msgs; msgp->msg; msgp++) {
368 if (err_no == msgp->no) {
369 msg = msgp->msg;
370 break;
371 }
372 }
373
374 v = Py_BuildValue("(is)", err_no, msg);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000375 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000376 PyErr_SetObject(socket_error, v);
Guido van Rossum73624e91994-10-10 17:59:00 +0000377 Py_DECREF(v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000378 }
379 return NULL;
380 }
381 else
382#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000383
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000384#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000385 if (sock_errno() != NO_ERROR) {
386 APIRET rc;
387 ULONG msglen;
388 char outbuf[100];
389 int myerrorcode = sock_errno();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000390
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000391 /* Retrieve socket-related error message from MPTN.MSG file */
392 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
393 myerrorcode - SOCBASEERR + 26,
394 "mptn.msg",
395 &msglen);
396 if (rc == NO_ERROR) {
397 PyObject *v;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000398
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000399 /* OS/2 doesn't guarantee a terminator */
400 outbuf[msglen] = '\0';
401 if (strlen(outbuf) > 0) {
402 /* If non-empty msg, trim CRLF */
403 char *lastc = &outbuf[ strlen(outbuf)-1 ];
404 while (lastc > outbuf && isspace(*lastc)) {
405 /* Trim trailing whitespace (CRLF) */
406 *lastc-- = '\0';
407 }
408 }
409 v = Py_BuildValue("(is)", myerrorcode, outbuf);
410 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000411 PyErr_SetObject(socket_error, v);
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000412 Py_DECREF(v);
413 }
414 return NULL;
415 }
416 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000417#endif
418
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000419 return PyErr_SetFromErrno(socket_error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000420}
421
Guido van Rossum30a685f1991-06-27 15:51:29 +0000422
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000423static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000424set_herror(int h_error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000425{
426 PyObject *v;
427
428#ifdef HAVE_HSTRERROR
429 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
430#else
431 v = Py_BuildValue("(is)", h_error, "host not found");
432#endif
433 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000434 PyErr_SetObject(socket_herror, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000435 Py_DECREF(v);
436 }
437
438 return NULL;
439}
440
441
442static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000443set_gaierror(int error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000444{
445 PyObject *v;
446
Martin v. Löwis272cb402002-03-01 08:31:07 +0000447#ifdef EAI_SYSTEM
448 /* EAI_SYSTEM is not available on Windows XP. */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000449 if (error == EAI_SYSTEM)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000450 return set_error();
Martin v. Löwis272cb402002-03-01 08:31:07 +0000451#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000452
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000453#ifdef HAVE_GAI_STRERROR
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000454 v = Py_BuildValue("(is)", error, gai_strerror(error));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000455#else
456 v = Py_BuildValue("(is)", error, "getaddrinfo failed");
457#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000458 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000459 PyErr_SetObject(socket_gaierror, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000460 Py_DECREF(v);
461 }
462
463 return NULL;
464}
465
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000466/* Function to perform the setting of socket blocking mode
467 internally. block = (1 | 0). */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000468static int
469internal_setblocking(PySocketSockObject *s, int block)
470{
471#ifndef RISCOS
472#ifndef MS_WINDOWS
473 int delay_flag;
474#endif
475#endif
476
477 Py_BEGIN_ALLOW_THREADS
478#ifdef __BEOS__
479 block = !block;
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000480 setsockopt(s->sock_fd, SOL_SOCKET, SO_NONBLOCK,
481 (void *)(&block), sizeof(int));
Guido van Rossum67f7a382002-06-06 21:08:16 +0000482#else
483#ifndef RISCOS
484#ifndef MS_WINDOWS
485#if defined(PYOS_OS2) && !defined(PYCC_GCC)
486 block = !block;
487 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
488#else /* !PYOS_OS2 */
489 delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
490 if (block)
491 delay_flag &= (~O_NDELAY);
492 else
493 delay_flag |= O_NDELAY;
494 fcntl(s->sock_fd, F_SETFL, delay_flag);
495#endif /* !PYOS_OS2 */
496#else /* MS_WINDOWS */
497 block = !block;
498 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
499#endif /* MS_WINDOWS */
500#endif /* __BEOS__ */
501#endif /* RISCOS */
502 Py_END_ALLOW_THREADS
503
504 /* Since these don't return anything */
505 return 1;
506}
507
Guido van Rossum11ba0942002-06-13 15:07:44 +0000508/* Do a select() on the socket, if necessary (sock_timeout > 0).
509 The argument writing indicates the direction.
510 This does not raise an exception or return a success indicator;
511 we'll let the actual socket call do that. */
512static void
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000513internal_select(PySocketSockObject *s, int writing)
Guido van Rossum67f7a382002-06-06 21:08:16 +0000514{
515 fd_set fds;
516 struct timeval tv;
Guido van Rossum11ba0942002-06-13 15:07:44 +0000517
Guido van Rossumad654902002-07-19 12:44:59 +0000518 /* Nothing to do unless we're in timeout mode (not non-blocking) */
Guido van Rossum11ba0942002-06-13 15:07:44 +0000519 if (s->sock_timeout <= 0.0)
520 return;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000521
Guido van Rossumad654902002-07-19 12:44:59 +0000522 /* Guard against closed socket */
523 if (s->sock_fd < 0)
524 return;
525
Guido van Rossum67f7a382002-06-06 21:08:16 +0000526 /* Construct the arguments to select */
527 tv.tv_sec = (int)s->sock_timeout;
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000528 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000529 FD_ZERO(&fds);
530 FD_SET(s->sock_fd, &fds);
531
532 /* See if the socket is ready */
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000533 if (writing)
Guido van Rossum11ba0942002-06-13 15:07:44 +0000534 select(s->sock_fd+1, NULL, &fds, NULL, &tv);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000535 else
Guido van Rossum11ba0942002-06-13 15:07:44 +0000536 select(s->sock_fd+1, &fds, NULL, NULL, &tv);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000537}
538
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000539/* Initialize a new socket object. */
540
Tim Petersa12b4cf2002-07-18 22:38:44 +0000541static double defaulttimeout = -1.0; /* Default timeout for new sockets */
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000542
Mark Hammond62b1ab12002-07-23 06:31:15 +0000543PyMODINIT_FUNC
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000544init_sockobject(PySocketSockObject *s,
545 SOCKET_T fd, int family, int type, int proto)
546{
547#ifdef RISCOS
548 int block = 1;
549#endif
550 s->sock_fd = fd;
551 s->sock_family = family;
552 s->sock_type = type;
553 s->sock_proto = proto;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000554 s->sock_timeout = defaulttimeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000555
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000556 s->errorhandler = &set_error;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000557
558 if (defaulttimeout >= 0.0)
559 internal_setblocking(s, 0);
560
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000561#ifdef RISCOS
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000562 if (taskwindow)
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000563 socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000564#endif
565}
566
567
Guido van Rossum30a685f1991-06-27 15:51:29 +0000568/* Create a new socket object.
569 This just creates the object and initializes it.
570 If the creation fails, return NULL and set an exception (implicit
571 in NEWOBJ()). */
572
Guido van Rossum73624e91994-10-10 17:59:00 +0000573static PySocketSockObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000574new_sockobject(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000575{
Guido van Rossum73624e91994-10-10 17:59:00 +0000576 PySocketSockObject *s;
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000577 s = (PySocketSockObject *)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000578 PyType_GenericNew(&sock_type, NULL, NULL);
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000579 if (s != NULL)
580 init_sockobject(s, fd, family, type, proto);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000581 return s;
582}
583
Guido van Rossum30a685f1991-06-27 15:51:29 +0000584
Guido van Rossum48a680c2001-03-02 06:34:14 +0000585/* Lock to allow python interpreter to continue, but only allow one
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000586 thread to be in gethostbyname */
Guido van Rossum3baaa131999-03-22 21:44:51 +0000587#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum65d5b571998-12-21 19:32:43 +0000588PyThread_type_lock gethostbyname_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000589#endif
590
591
Guido van Rossum30a685f1991-06-27 15:51:29 +0000592/* Convert a string specifying a host name or one of a few symbolic
593 names to a numeric IP address. This usually calls gethostbyname()
594 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000595 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000596 an error occurred; then an exception is raised. */
597
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000598static int
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000599setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000600{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000601 struct addrinfo hints, *res;
602 int error;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000603
Guido van Rossuma376cc51996-12-05 23:43:35 +0000604 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
Guido van Rossum30a685f1991-06-27 15:51:29 +0000605 if (name[0] == '\0') {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000606 int siz;
607 memset(&hints, 0, sizeof(hints));
608 hints.ai_family = af;
609 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
610 hints.ai_flags = AI_PASSIVE;
611 error = getaddrinfo(NULL, "0", &hints, &res);
612 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000613 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000614 return -1;
615 }
616 switch (res->ai_family) {
617 case AF_INET:
618 siz = 4;
619 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000620#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000621 case AF_INET6:
622 siz = 16;
623 break;
624#endif
625 default:
626 freeaddrinfo(res);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000627 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000628 "unsupported address family");
629 return -1;
630 }
631 if (res->ai_next) {
Martin v. Löwisf0b11d22001-11-07 08:31:03 +0000632 freeaddrinfo(res);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000633 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000634 "wildcard resolved to multiple address");
635 return -1;
636 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000637 if (res->ai_addrlen < addr_ret_size)
638 addr_ret_size = res->ai_addrlen;
639 memcpy(addr_ret, res->ai_addr, addr_ret_size);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000640 freeaddrinfo(res);
641 return siz;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000642 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000643 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000644 struct sockaddr_in *sin;
645 if (af != PF_INET && af != PF_UNSPEC) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000646 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000647 "address family mismatched");
648 return -1;
649 }
650 sin = (struct sockaddr_in *)addr_ret;
651 memset((void *) sin, '\0', sizeof(*sin));
652 sin->sin_family = AF_INET;
653#ifdef HAVE_SOCKADDR_SA_LEN
654 sin->sin_len = sizeof(*sin);
655#endif
656 sin->sin_addr.s_addr = INADDR_BROADCAST;
657 return sizeof(sin->sin_addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000658 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000659 memset(&hints, 0, sizeof(hints));
660 hints.ai_family = af;
661 error = getaddrinfo(name, NULL, &hints, &res);
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000662#if defined(__digital__) && defined(__unix__)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000663 if (error == EAI_NONAME && af == AF_UNSPEC) {
664 /* On Tru64 V5.1, numeric-to-addr conversion fails
665 if no address family is given. Assume IPv4 for now.*/
666 hints.ai_family = AF_INET;
667 error = getaddrinfo(name, NULL, &hints, &res);
668 }
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000669#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000670 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000671 set_gaierror(error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000672 return -1;
673 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000674 if (res->ai_addrlen < addr_ret_size)
675 addr_ret_size = res->ai_addrlen;
676 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000677 freeaddrinfo(res);
678 switch (addr_ret->sa_family) {
679 case AF_INET:
680 return 4;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000681#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000682 case AF_INET6:
683 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +0000684#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000685 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000686 PyErr_SetString(socket_error, "unknown address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000687 return -1;
688 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000689}
690
Guido van Rossum30a685f1991-06-27 15:51:29 +0000691
Guido van Rossum30a685f1991-06-27 15:51:29 +0000692/* Create a string object representing an IP address.
693 This is always a string of the form 'dd.dd.dd.dd' (with variable
694 size numbers). */
695
Guido van Rossum73624e91994-10-10 17:59:00 +0000696static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000697makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000698{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000699 char buf[NI_MAXHOST];
700 int error;
701
702 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
703 NI_NUMERICHOST);
704 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000705 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000706 return NULL;
707 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000708 return PyString_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000709}
710
711
712/* Create an object representing the given socket address,
713 suitable for passing it back to bind(), connect() etc.
714 The family field of the sockaddr structure is inspected
715 to determine what kind of address it really is. */
716
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000717/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000718static PyObject *
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000719makesockaddr(int sockfd, struct sockaddr *addr, int addrlen)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000720{
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000721 if (addrlen == 0) {
722 /* No address -- may be recvfrom() from known socket */
Guido van Rossum73624e91994-10-10 17:59:00 +0000723 Py_INCREF(Py_None);
724 return Py_None;
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000725 }
726
Guido van Rossumbcc20741998-08-04 22:53:56 +0000727#ifdef __BEOS__
Guido van Rossum2c8bcb82000-04-25 21:34:53 +0000728 /* XXX: BeOS version of accept() doesn't set family correctly */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000729 addr->sa_family = AF_INET;
730#endif
731
Guido van Rossum30a685f1991-06-27 15:51:29 +0000732 switch (addr->sa_family) {
733
734 case AF_INET:
735 {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000736 struct sockaddr_in *a;
737 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
Barry Warsaw752300b1997-01-03 17:18:10 +0000738 PyObject *ret = NULL;
739 if (addrobj) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000740 a = (struct sockaddr_in *)addr;
Barry Warsaw752300b1997-01-03 17:18:10 +0000741 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
742 Py_DECREF(addrobj);
743 }
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000744 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000745 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000746
Guido van Rossumb6775db1994-08-01 11:34:53 +0000747#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000748 case AF_UNIX:
749 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000750 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000751 return PyString_FromString(a->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000752 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000753#endif /* AF_UNIX */
754
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000755#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000756 case AF_INET6:
757 {
758 struct sockaddr_in6 *a;
759 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
760 PyObject *ret = NULL;
761 if (addrobj) {
762 a = (struct sockaddr_in6 *)addr;
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000763 ret = Py_BuildValue("Oiii",
764 addrobj,
765 ntohs(a->sin6_port),
766 a->sin6_flowinfo,
767 a->sin6_scope_id);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000768 Py_DECREF(addrobj);
769 }
770 return ret;
771 }
Jeremy Hylton22308652001-02-02 03:23:09 +0000772#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +0000773
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +0000774#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +0000775 case AF_PACKET:
776 {
777 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
778 char *ifname = "";
779 struct ifreq ifr;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000780 /* need to look up interface name give index */
781 if (a->sll_ifindex) {
Jeremy Hylton22308652001-02-02 03:23:09 +0000782 ifr.ifr_ifindex = a->sll_ifindex;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000783 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
Jeremy Hylton22308652001-02-02 03:23:09 +0000784 ifname = ifr.ifr_name;
Jeremy Hylton22308652001-02-02 03:23:09 +0000785 }
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000786 return Py_BuildValue("shbhs#",
787 ifname,
788 ntohs(a->sll_protocol),
789 a->sll_pkttype,
790 a->sll_hatype,
791 a->sll_addr,
792 a->sll_halen);
Jeremy Hylton22308652001-02-02 03:23:09 +0000793 }
794#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000795
Guido van Rossum30a685f1991-06-27 15:51:29 +0000796 /* More cases here... */
797
798 default:
Guido van Rossumaa948df1997-05-07 17:41:48 +0000799 /* If we don't know the address family, don't raise an
800 exception -- return it as a tuple. */
801 return Py_BuildValue("is#",
802 addr->sa_family,
803 addr->sa_data,
804 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000805
Guido van Rossum30a685f1991-06-27 15:51:29 +0000806 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000807}
808
Guido van Rossum30a685f1991-06-27 15:51:29 +0000809
810/* Parse a socket address argument according to the socket object's
811 address family. Return 1 if the address was in the proper format,
812 0 of not. The address is returned through addr_ret, its length
813 through len_ret. */
814
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000815static int
Guido van Rossum48a680c2001-03-02 06:34:14 +0000816getsockaddrarg(PySocketSockObject *s, PyObject *args,
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000817 struct sockaddr **addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000818{
Guido van Rossum30a685f1991-06-27 15:51:29 +0000819 switch (s->sock_family) {
820
Guido van Rossumb6775db1994-08-01 11:34:53 +0000821#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000822 case AF_UNIX:
823 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000824 struct sockaddr_un* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000825 char *path;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000826 int len;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000827 addr = (struct sockaddr_un*)&(s->sock_addr).un;
Guido van Rossum7e488981998-10-08 02:25:24 +0000828 if (!PyArg_Parse(args, "t#", &path, &len))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000829 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000830 if (len > sizeof addr->sun_path) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000831 PyErr_SetString(socket_error,
Barry Warsaw752300b1997-01-03 17:18:10 +0000832 "AF_UNIX path too long");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000833 return 0;
834 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000835 addr->sun_family = s->sock_family;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000836 memcpy(addr->sun_path, path, len);
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000837 addr->sun_path[len] = 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000838 *addr_ret = (struct sockaddr *) addr;
Guido van Rossum65af28a1996-06-11 18:36:33 +0000839 *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000840 return 1;
841 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000842#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000843
Guido van Rossum30a685f1991-06-27 15:51:29 +0000844 case AF_INET:
845 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000846 struct sockaddr_in* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000847 char *host;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000848 int port;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000849 addr=(struct sockaddr_in*)&(s->sock_addr).in;
Guido van Rossume4dad902000-12-01 13:13:11 +0000850 if (!PyTuple_Check(args)) {
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000851 PyErr_Format(
852 PyExc_TypeError,
853 "getsockaddrarg: "
854 "AF_INET address must be tuple, not %.500s",
855 args->ob_type->tp_name);
Guido van Rossume4dad902000-12-01 13:13:11 +0000856 return 0;
857 }
858 if (!PyArg_ParseTuple(args, "si:getsockaddrarg", &host, &port))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000859 return 0;
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000860 if (setipaddr(host, (struct sockaddr *)addr, sizeof(*addr), AF_INET) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000861 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000862 addr->sin_family = AF_INET;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000863 addr->sin_port = htons((short)port);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000864 *addr_ret = (struct sockaddr *) addr;
865 *len_ret = sizeof *addr;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000866 return 1;
867 }
868
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000869#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000870 case AF_INET6:
871 {
872 struct sockaddr_in6* addr;
873 char *host;
874 int port, flowinfo, scope_id;
875 addr = (struct sockaddr_in6*)&(s->sock_addr).in6;
876 flowinfo = scope_id = 0;
877 if (!PyArg_ParseTuple(args, "si|ii", &host, &port, &flowinfo,
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000878 &scope_id)) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000879 return 0;
880 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000881 if (setipaddr(host, (struct sockaddr *)addr, sizeof(*addr), AF_INET6) < 0)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000882 return 0;
883 addr->sin6_family = s->sock_family;
884 addr->sin6_port = htons((short)port);
885 addr->sin6_flowinfo = flowinfo;
886 addr->sin6_scope_id = scope_id;
887 *addr_ret = (struct sockaddr *) addr;
888 *len_ret = sizeof *addr;
889 return 1;
890 }
891#endif
892
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +0000893#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +0000894 case AF_PACKET:
895 {
896 struct sockaddr_ll* addr;
897 struct ifreq ifr;
898 char *interfaceName;
899 int protoNumber;
900 int hatype = 0;
901 int pkttype = 0;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000902 char *haddr;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000903
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000904 if (!PyArg_ParseTuple(args, "si|iis", &interfaceName,
905 &protoNumber, &pkttype, &hatype, &haddr))
Jeremy Hylton22308652001-02-02 03:23:09 +0000906 return 0;
907 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
908 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000909 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000910 s->errorhandler();
Jeremy Hylton22308652001-02-02 03:23:09 +0000911 return 0;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000912 }
Jeremy Hylton22308652001-02-02 03:23:09 +0000913 addr = &(s->sock_addr.ll);
914 addr->sll_family = AF_PACKET;
915 addr->sll_protocol = htons((short)protoNumber);
916 addr->sll_ifindex = ifr.ifr_ifindex;
917 addr->sll_pkttype = pkttype;
918 addr->sll_hatype = hatype;
919 *addr_ret = (struct sockaddr *) addr;
920 *len_ret = sizeof *addr;
921 return 1;
922 }
Guido van Rossum48a680c2001-03-02 06:34:14 +0000923#endif
924
Guido van Rossum30a685f1991-06-27 15:51:29 +0000925 /* More cases here... */
926
927 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000928 PyErr_SetString(socket_error, "getsockaddrarg: bad family");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000929 return 0;
930
931 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000932}
933
Guido van Rossum30a685f1991-06-27 15:51:29 +0000934
Guido van Rossum48a680c2001-03-02 06:34:14 +0000935/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +0000936 Return 1 if the family is known, 0 otherwise. The length is returned
937 through len_ret. */
938
939static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +0000940getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +0000941{
942 switch (s->sock_family) {
943
Guido van Rossumb6775db1994-08-01 11:34:53 +0000944#ifdef AF_UNIX
Guido van Rossum710e1df1992-06-12 10:39:36 +0000945 case AF_UNIX:
946 {
947 *len_ret = sizeof (struct sockaddr_un);
948 return 1;
949 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000950#endif /* AF_UNIX */
Guido van Rossum710e1df1992-06-12 10:39:36 +0000951
952 case AF_INET:
953 {
954 *len_ret = sizeof (struct sockaddr_in);
955 return 1;
956 }
957
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000958#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000959 case AF_INET6:
960 {
961 *len_ret = sizeof (struct sockaddr_in6);
962 return 1;
963 }
964#endif
965
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +0000966#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +0000967 case AF_PACKET:
968 {
969 *len_ret = sizeof (struct sockaddr_ll);
970 return 1;
971 }
972#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000973
Guido van Rossum710e1df1992-06-12 10:39:36 +0000974 /* More cases here... */
975
976 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000977 PyErr_SetString(socket_error, "getsockaddrlen: bad family");
Guido van Rossum710e1df1992-06-12 10:39:36 +0000978 return 0;
979
980 }
981}
982
983
Guido van Rossum30a685f1991-06-27 15:51:29 +0000984/* s.accept() method */
985
Guido van Rossum73624e91994-10-10 17:59:00 +0000986static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000987sock_accept(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000988{
989 char addrbuf[256];
Fred Drakea04eaad2000-06-30 02:46:07 +0000990 SOCKET_T newfd;
Guido van Rossumff3ab422000-04-24 15:16:03 +0000991 socklen_t addrlen;
Barry Warsaw752300b1997-01-03 17:18:10 +0000992 PyObject *sock = NULL;
993 PyObject *addr = NULL;
994 PyObject *res = NULL;
995
Guido van Rossum710e1df1992-06-12 10:39:36 +0000996 if (!getsockaddrlen(s, &addrlen))
997 return NULL;
Fred Drakefd168342001-05-09 19:11:33 +0000998 memset(addrbuf, 0, addrlen);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000999
Guido van Rossum73624e91994-10-10 17:59:00 +00001000 Py_BEGIN_ALLOW_THREADS
Guido van Rossum11ba0942002-06-13 15:07:44 +00001001 internal_select(s, 0);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001002 newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001003 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001004
Fred Drakea04eaad2000-06-30 02:46:07 +00001005#ifdef MS_WINDOWS
1006 if (newfd == INVALID_SOCKET)
1007#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001008 if (newfd < 0)
Fred Drakea04eaad2000-06-30 02:46:07 +00001009#endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001010 return s->errorhandler();
Barry Warsaw752300b1997-01-03 17:18:10 +00001011
Guido van Rossum30a685f1991-06-27 15:51:29 +00001012 /* Create the new object with unspecified family,
1013 to avoid calls to bind() etc. on it. */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001014 sock = (PyObject *) new_sockobject(newfd,
1015 s->sock_family,
1016 s->sock_type,
1017 s->sock_proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001018
Barry Warsaw752300b1997-01-03 17:18:10 +00001019 if (sock == NULL) {
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001020 SOCKETCLOSE(newfd);
Barry Warsaw752300b1997-01-03 17:18:10 +00001021 goto finally;
1022 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001023 addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001024 addrlen);
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001025 if (addr == NULL)
Barry Warsaw752300b1997-01-03 17:18:10 +00001026 goto finally;
1027
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001028 res = Py_BuildValue("OO", sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00001029
Guido van Rossum67f7a382002-06-06 21:08:16 +00001030finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001031 Py_XDECREF(sock);
1032 Py_XDECREF(addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001033 return res;
1034}
1035
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001036PyDoc_STRVAR(accept_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001037"accept() -> (socket object, address info)\n\
1038\n\
1039Wait for an incoming connection. Return a new socket representing the\n\
1040connection, and the address of the client. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001041info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001042
Guido van Rossum11ba0942002-06-13 15:07:44 +00001043/* s.setblocking(flag) method. Argument:
1044 False -- non-blocking mode; same as settimeout(0)
1045 True -- blocking mode; same as settimeout(None)
1046*/
Guido van Rossume4485b01994-09-07 14:32:49 +00001047
Guido van Rossum73624e91994-10-10 17:59:00 +00001048static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001049sock_setblocking(PySocketSockObject *s, PyObject *arg)
Guido van Rossume4485b01994-09-07 14:32:49 +00001050{
1051 int block;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001052
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001053 block = PyInt_AsLong(arg);
1054 if (block == -1 && PyErr_Occurred())
Guido van Rossume4485b01994-09-07 14:32:49 +00001055 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001056
Guido van Rossum11ba0942002-06-13 15:07:44 +00001057 s->sock_timeout = block ? -1.0 : 0.0;
Guido van Rossume8008f02002-06-07 03:36:20 +00001058 internal_setblocking(s, block);
Guido van Rossume4485b01994-09-07 14:32:49 +00001059
Guido van Rossum73624e91994-10-10 17:59:00 +00001060 Py_INCREF(Py_None);
1061 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +00001062}
Guido van Rossume4485b01994-09-07 14:32:49 +00001063
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001064PyDoc_STRVAR(setblocking_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001065"setblocking(flag)\n\
1066\n\
1067Set the socket to blocking (flag is true) or non-blocking (false).\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001068setblocking(True) is equivalent to settimeout(None);\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001069setblocking(False) is equivalent to settimeout(0.0).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001070
Guido van Rossum11ba0942002-06-13 15:07:44 +00001071/* s.settimeout(timeout) method. Argument:
1072 None -- no timeout, blocking mode; same as setblocking(True)
1073 0.0 -- non-blocking mode; same as setblocking(False)
1074 > 0 -- timeout mode; operations time out after timeout seconds
1075 < 0 -- illegal; raises an exception
1076*/
Guido van Rossum67f7a382002-06-06 21:08:16 +00001077static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001078sock_settimeout(PySocketSockObject *s, PyObject *arg)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001079{
Guido van Rossum11ba0942002-06-13 15:07:44 +00001080 double timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001081
1082 if (arg == Py_None)
Guido van Rossum11ba0942002-06-13 15:07:44 +00001083 timeout = -1.0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001084 else {
Guido van Rossum11ba0942002-06-13 15:07:44 +00001085 timeout = PyFloat_AsDouble(arg);
1086 if (timeout < 0.0) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00001087 if (!PyErr_Occurred())
1088 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00001089 "Timeout value out of range");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001090 return NULL;
1091 }
1092 }
1093
Guido van Rossum11ba0942002-06-13 15:07:44 +00001094 s->sock_timeout = timeout;
1095 internal_setblocking(s, timeout < 0.0);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001096
1097 Py_INCREF(Py_None);
1098 return Py_None;
1099}
1100
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001101PyDoc_STRVAR(settimeout_doc,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001102"settimeout(timeout)\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001103\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001104Set a timeout on socket operations. 'timeout' can be a float,\n\
1105giving in seconds, or None. Setting a timeout of None disables\n\
1106the timeout feature and is equivalent to setblocking(1).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001107Setting a timeout of zero is the same as setblocking(0).");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001108
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001109/* s.gettimeout() method.
1110 Returns the timeout associated with a socket. */
Guido van Rossum67f7a382002-06-06 21:08:16 +00001111static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001112sock_gettimeout(PySocketSockObject *s)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001113{
1114 if (s->sock_timeout < 0.0) {
1115 Py_INCREF(Py_None);
1116 return Py_None;
1117 }
1118 else
1119 return PyFloat_FromDouble(s->sock_timeout);
1120}
1121
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001122PyDoc_STRVAR(gettimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001123"gettimeout() -> timeout\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001124\n\
1125Returns the timeout in floating seconds associated with socket \n\
1126operations. A timeout of None indicates that timeouts on socket \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001127operations are disabled.");
Guido van Rossume4485b01994-09-07 14:32:49 +00001128
Guido van Rossum48a680c2001-03-02 06:34:14 +00001129#ifdef RISCOS
1130/* s.sleeptaskw(1 | 0) method */
1131
1132static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001133sock_sleeptaskw(PySocketSockObject *s,PyObject *args)
Guido van Rossum48a680c2001-03-02 06:34:14 +00001134{
Guido van Rossum67f7a382002-06-06 21:08:16 +00001135 int block;
1136 int delay_flag;
1137 if (!PyArg_Parse(args, "i", &block))
1138 return NULL;
1139 Py_BEGIN_ALLOW_THREADS
1140 socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
1141 Py_END_ALLOW_THREADS
Guido van Rossum48a680c2001-03-02 06:34:14 +00001142
Guido van Rossum67f7a382002-06-06 21:08:16 +00001143 Py_INCREF(Py_None);
1144 return Py_None;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001145}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001146PyDoc_STRVAR(sleeptaskw_doc,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001147"sleeptaskw(flag)\n\
1148\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001149Allow sleeps in taskwindows.");
Guido van Rossum48a680c2001-03-02 06:34:14 +00001150#endif
1151
1152
Guido van Rossumaee08791992-09-08 09:05:33 +00001153/* s.setsockopt() method.
1154 With an integer third argument, sets an integer option.
1155 With a string third argument, sets an option from a buffer;
1156 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001157
Guido van Rossum73624e91994-10-10 17:59:00 +00001158static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001159sock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001160{
1161 int level;
1162 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001163 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +00001164 char *buf;
1165 int buflen;
1166 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001167
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001168 if (PyArg_ParseTuple(args, "iii:setsockopt",
1169 &level, &optname, &flag)) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001170 buf = (char *) &flag;
1171 buflen = sizeof flag;
1172 }
1173 else {
Guido van Rossum73624e91994-10-10 17:59:00 +00001174 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001175 if (!PyArg_ParseTuple(args, "iis#:setsockopt",
1176 &level, &optname, &buf, &buflen))
Guido van Rossumaee08791992-09-08 09:05:33 +00001177 return NULL;
1178 }
Thomas Wouters334fb892000-07-25 12:56:38 +00001179 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001180 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001181 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001182 Py_INCREF(Py_None);
1183 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001184}
1185
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001186PyDoc_STRVAR(setsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001187"setsockopt(level, option, value)\n\
1188\n\
1189Set a socket option. See the Unix manual for level and option.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001190The value argument can either be an integer or a string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001191
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001192
Guido van Rossumaee08791992-09-08 09:05:33 +00001193/* s.getsockopt() method.
1194 With two arguments, retrieves an integer option.
1195 With a third integer argument, retrieves a string buffer of that size;
1196 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001197
Guido van Rossum73624e91994-10-10 17:59:00 +00001198static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001199sock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001200{
1201 int level;
1202 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001203 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +00001204 PyObject *buf;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001205 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001206
Guido van Rossumbcc20741998-08-04 22:53:56 +00001207#ifdef __BEOS__
Fred Drake564a6cc2001-05-11 20:12:26 +00001208 /* We have incomplete socket support. */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001209 PyErr_SetString(socket_error, "getsockopt not supported");
Guido van Rossumbcc20741998-08-04 22:53:56 +00001210 return NULL;
1211#else
1212
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001213 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
1214 &level, &optname, &buflen))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001215 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001216
Guido van Rossumbe32c891996-06-20 16:25:29 +00001217 if (buflen == 0) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001218 int flag = 0;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001219 socklen_t flagsize = sizeof flag;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001220 res = getsockopt(s->sock_fd, level, optname,
Thomas Wouters334fb892000-07-25 12:56:38 +00001221 (void *)&flag, &flagsize);
Guido van Rossumaee08791992-09-08 09:05:33 +00001222 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001223 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001224 return PyInt_FromLong(flag);
Guido van Rossumaee08791992-09-08 09:05:33 +00001225 }
Guido van Rossumaee08791992-09-08 09:05:33 +00001226 if (buflen <= 0 || buflen > 1024) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001227 PyErr_SetString(socket_error,
Barry Warsaw752300b1997-01-03 17:18:10 +00001228 "getsockopt buflen out of range");
Guido van Rossumaee08791992-09-08 09:05:33 +00001229 return NULL;
1230 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001231 buf = PyString_FromStringAndSize((char *)NULL, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001232 if (buf == NULL)
1233 return NULL;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001234 res = getsockopt(s->sock_fd, level, optname,
Fred Drake564a6cc2001-05-11 20:12:26 +00001235 (void *)PyString_AS_STRING(buf), &buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001236 if (res < 0) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001237 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001238 return s->errorhandler();
Guido van Rossumaee08791992-09-08 09:05:33 +00001239 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001240 _PyString_Resize(&buf, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001241 return buf;
Guido van Rossumbcc20741998-08-04 22:53:56 +00001242#endif /* __BEOS__ */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001243}
1244
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001245PyDoc_STRVAR(getsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001246"getsockopt(level, option[, buffersize]) -> value\n\
1247\n\
1248Get a socket option. See the Unix manual for level and option.\n\
1249If a nonzero buffersize argument is given, the return value is a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001250string of that length; otherwise it is an integer.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001251
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001252
Fred Drake728819a2000-07-01 03:40:12 +00001253/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001254
Guido van Rossum73624e91994-10-10 17:59:00 +00001255static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001256sock_bind(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001257{
1258 struct sockaddr *addr;
1259 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001260 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001261
Fred Drake728819a2000-07-01 03:40:12 +00001262 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001263 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001264 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001265 res = bind(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001266 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001267 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001268 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001269 Py_INCREF(Py_None);
1270 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001271}
1272
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001273PyDoc_STRVAR(bind_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001274"bind(address)\n\
1275\n\
1276Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00001277pair (host, port); the host must refer to the local host. For raw packet\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001278sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001279
Guido van Rossum30a685f1991-06-27 15:51:29 +00001280
1281/* s.close() method.
1282 Set the file descriptor to -1 so operations tried subsequently
1283 will surely fail. */
1284
Guido van Rossum73624e91994-10-10 17:59:00 +00001285static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001286sock_close(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001287{
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001288 SOCKET_T fd;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001289
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001290 if ((fd = s->sock_fd) != -1) {
1291 s->sock_fd = -1;
Guido van Rossum08481461996-10-12 14:07:22 +00001292 Py_BEGIN_ALLOW_THREADS
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001293 (void) SOCKETCLOSE(fd);
Guido van Rossum08481461996-10-12 14:07:22 +00001294 Py_END_ALLOW_THREADS
1295 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001296 Py_INCREF(Py_None);
1297 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001298}
1299
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001300PyDoc_STRVAR(close_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001301"close()\n\
1302\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001303Close the socket. It cannot be used after this call.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001304
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001305static int
1306internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen)
1307{
1308 int res;
1309
1310 res = connect(s->sock_fd, addr, addrlen);
1311
1312#ifdef MS_WINDOWS
1313
1314 if (s->sock_timeout > 0.0) {
1315 if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK) {
1316 internal_select(s, 1);
1317 res = connect(s->sock_fd, addr, addrlen);
Tim Peters58141872002-08-06 22:25:02 +00001318 if (res < 0) {
1319 /* On Win98, WSAEISCONN was seen here. But
1320 * on Win2K, WSAEINVAL. So accept both as
1321 * meaning "fine".
1322 */
1323 int code = WSAGetLastError();
1324 if (code == WSAEISCONN ||
1325 code == WSAEINVAL)
1326 res = 0;
1327 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001328 }
1329 }
1330
1331 if (res < 0)
1332 res = WSAGetLastError();
1333
1334#else
1335
1336 if (s->sock_timeout > 0.0) {
1337 if (res < 0 && errno == EINPROGRESS) {
1338 internal_select(s, 1);
1339 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum12e3c712002-08-08 20:39:30 +00001340 if (res < 0 && errno == EISCONN)
1341 res = 0;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001342 }
1343 }
1344
1345 if (res < 0)
1346 res = errno;
1347
1348#endif
1349
1350 return res;
1351}
Guido van Rossum30a685f1991-06-27 15:51:29 +00001352
Fred Drake728819a2000-07-01 03:40:12 +00001353/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001354
Guido van Rossum73624e91994-10-10 17:59:00 +00001355static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001356sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001357{
1358 struct sockaddr *addr;
1359 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001360 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001361
Fred Drake728819a2000-07-01 03:40:12 +00001362 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001363 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001364
Guido van Rossum73624e91994-10-10 17:59:00 +00001365 Py_BEGIN_ALLOW_THREADS
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001366 res = internal_connect(s, addr, addrlen);
Guido van Rossum11ba0942002-06-13 15:07:44 +00001367 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001368
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001369 if (res != 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001370 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001371 Py_INCREF(Py_None);
1372 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001373}
1374
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001375PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001376"connect(address)\n\
1377\n\
1378Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001379is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001380
Guido van Rossum30a685f1991-06-27 15:51:29 +00001381
Fred Drake728819a2000-07-01 03:40:12 +00001382/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001383
1384static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001385sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001386{
1387 struct sockaddr *addr;
1388 int addrlen;
1389 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001390
Fred Drake728819a2000-07-01 03:40:12 +00001391 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001392 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001393
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001394 Py_BEGIN_ALLOW_THREADS
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001395 res = internal_connect(s, addr, addrlen);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001396 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001397
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001398 return PyInt_FromLong((long) res);
1399}
1400
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001401PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001402"connect_ex(address) -> errno\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001403\n\
1404This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001405instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001406
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001407
Guido van Rossumed233a51992-06-23 09:07:03 +00001408/* s.fileno() method */
1409
Guido van Rossum73624e91994-10-10 17:59:00 +00001410static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001411sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00001412{
Fred Drakea04eaad2000-06-30 02:46:07 +00001413#if SIZEOF_SOCKET_T <= SIZEOF_LONG
Guido van Rossum73624e91994-10-10 17:59:00 +00001414 return PyInt_FromLong((long) s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +00001415#else
1416 return PyLong_FromLongLong((LONG_LONG)s->sock_fd);
1417#endif
Guido van Rossumed233a51992-06-23 09:07:03 +00001418}
1419
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001420PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001421"fileno() -> integer\n\
1422\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001423Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001424
Guido van Rossumed233a51992-06-23 09:07:03 +00001425
Guido van Rossumbe32c891996-06-20 16:25:29 +00001426#ifndef NO_DUP
1427/* s.dup() method */
1428
1429static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001430sock_dup(PySocketSockObject *s)
Guido van Rossumbe32c891996-06-20 16:25:29 +00001431{
Fred Drakea04eaad2000-06-30 02:46:07 +00001432 SOCKET_T newfd;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001433 PyObject *sock;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001434
Guido van Rossumbe32c891996-06-20 16:25:29 +00001435 newfd = dup(s->sock_fd);
1436 if (newfd < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001437 return s->errorhandler();
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001438 sock = (PyObject *) new_sockobject(newfd,
1439 s->sock_family,
1440 s->sock_type,
1441 s->sock_proto);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001442 if (sock == NULL)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001443 SOCKETCLOSE(newfd);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001444 return sock;
1445}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001446
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001447PyDoc_STRVAR(dup_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001448"dup() -> socket object\n\
1449\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001450Return a new socket object connected to the same system resource.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001451
Guido van Rossumbe32c891996-06-20 16:25:29 +00001452#endif
1453
1454
Guido van Rossumc89705d1992-11-26 08:54:07 +00001455/* s.getsockname() method */
1456
Guido van Rossum73624e91994-10-10 17:59:00 +00001457static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001458sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001459{
1460 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001461 int res;
1462 socklen_t addrlen;
1463
Guido van Rossumc89705d1992-11-26 08:54:07 +00001464 if (!getsockaddrlen(s, &addrlen))
1465 return NULL;
Guido van Rossumca42b161996-01-12 01:36:05 +00001466 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001467 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001468 res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001469 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001470 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001471 return s->errorhandler();
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001472 return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001473}
1474
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001475PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001476"getsockname() -> address info\n\
1477\n\
1478Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001479info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001480
Guido van Rossumc89705d1992-11-26 08:54:07 +00001481
Guido van Rossumb6775db1994-08-01 11:34:53 +00001482#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001483/* s.getpeername() method */
1484
Guido van Rossum73624e91994-10-10 17:59:00 +00001485static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001486sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001487{
1488 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001489 int res;
1490 socklen_t addrlen;
1491
Guido van Rossumc89705d1992-11-26 08:54:07 +00001492 if (!getsockaddrlen(s, &addrlen))
1493 return NULL;
Fred Drakefd168342001-05-09 19:11:33 +00001494 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001495 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001496 res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001497 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001498 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001499 return s->errorhandler();
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001500 return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001501}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001502
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001503PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001504"getpeername() -> address info\n\
1505\n\
1506Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001507info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001508
Guido van Rossumb6775db1994-08-01 11:34:53 +00001509#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001510
1511
Guido van Rossum30a685f1991-06-27 15:51:29 +00001512/* s.listen(n) method */
1513
Guido van Rossum73624e91994-10-10 17:59:00 +00001514static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001515sock_listen(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001516{
1517 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001518 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001519
1520 backlog = PyInt_AsLong(arg);
1521 if (backlog == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001522 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001523 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +00001524 if (backlog < 1)
1525 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001526 res = listen(s->sock_fd, backlog);
Guido van Rossum73624e91994-10-10 17:59:00 +00001527 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001528 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001529 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001530 Py_INCREF(Py_None);
1531 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001532}
1533
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001534PyDoc_STRVAR(listen_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001535"listen(backlog)\n\
1536\n\
1537Enable a server to accept connections. The backlog argument must be at\n\
1538least 1; it specifies the number of unaccepted connection that the system\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001539will allow before refusing new connections.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001540
1541
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001542#ifndef NO_DUP
Guido van Rossum30a685f1991-06-27 15:51:29 +00001543/* s.makefile(mode) method.
1544 Create a new open file object referring to a dupped version of
1545 the socket's file descriptor. (The dup() call is necessary so
1546 that the open file and socket objects may be closed independent
1547 of each other.)
1548 The mode argument specifies 'r' or 'w' passed to fdopen(). */
1549
Guido van Rossum73624e91994-10-10 17:59:00 +00001550static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001551sock_makefile(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001552{
Tim Petersdbd9ba62000-07-09 03:09:57 +00001553 extern int fclose(FILE *);
Guido van Rossum6b144911995-03-14 15:05:13 +00001554 char *mode = "r";
1555 int bufsize = -1;
Fred Drakea04eaad2000-06-30 02:46:07 +00001556#ifdef MS_WIN32
Tim Peters79248aa2001-08-29 21:37:10 +00001557 Py_intptr_t fd;
Fred Drakea04eaad2000-06-30 02:46:07 +00001558#else
Guido van Rossum30a685f1991-06-27 15:51:29 +00001559 int fd;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001560#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001561 FILE *fp;
Guido van Rossum6b144911995-03-14 15:05:13 +00001562 PyObject *f;
1563
Guido van Rossum43713e52000-02-29 13:59:29 +00001564 if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001565 return NULL;
Guido van Rossum8d665e61996-06-26 18:22:49 +00001566#ifdef MS_WIN32
Guido van Rossum82a5c661998-07-07 20:45:43 +00001567 if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) ||
1568 ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL))
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001569#else
Guido van Rossum82a5c661998-07-07 20:45:43 +00001570 if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL)
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001571#endif
Guido van Rossum82a5c661998-07-07 20:45:43 +00001572 {
Guido van Rossum6b144911995-03-14 15:05:13 +00001573 if (fd >= 0)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001574 SOCKETCLOSE(fd);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001575 return s->errorhandler();
Guido van Rossum6b144911995-03-14 15:05:13 +00001576 }
Jack Jansen6d8898b2002-03-25 15:32:01 +00001577#ifdef USE_GUSI2
1578 /* Workaround for bug in Metrowerks MSL vs. GUSI I/O library */
Guido van Rossum67f7a382002-06-06 21:08:16 +00001579 if (strchr(mode, 'b') != NULL)
Jack Jansen6d8898b2002-03-25 15:32:01 +00001580 bufsize = 0;
1581#endif
Guido van Rossum6b144911995-03-14 15:05:13 +00001582 f = PyFile_FromFile(fp, "<socket>", mode, fclose);
1583 if (f != NULL)
1584 PyFile_SetBufSize(f, bufsize);
1585 return f;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001586}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001587
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001588PyDoc_STRVAR(makefile_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001589"makefile([mode[, buffersize]]) -> file object\n\
1590\n\
1591Return a regular file object corresponding to the socket.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001592The mode and buffersize arguments are as for the built-in open() function.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001593
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001594#endif /* NO_DUP */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001595
Guido van Rossum48a680c2001-03-02 06:34:14 +00001596
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001597/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001598
Guido van Rossum73624e91994-10-10 17:59:00 +00001599static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001600sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001601{
Guido van Rossumbe32c891996-06-20 16:25:29 +00001602 int len, n, flags = 0;
Guido van Rossum73624e91994-10-10 17:59:00 +00001603 PyObject *buf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001604
Guido van Rossum43713e52000-02-29 13:59:29 +00001605 if (!PyArg_ParseTuple(args, "i|i:recv", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001606 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001607
1608 if (len < 0) {
Martin v. Löwisfba64e12001-11-19 10:41:26 +00001609 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00001610 "negative buffersize in recv");
Martin v. Löwisfba64e12001-11-19 10:41:26 +00001611 return NULL;
1612 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00001613
Guido van Rossum73624e91994-10-10 17:59:00 +00001614 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001615 if (buf == NULL)
1616 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001617
Guido van Rossum73624e91994-10-10 17:59:00 +00001618 Py_BEGIN_ALLOW_THREADS
Guido van Rossum11ba0942002-06-13 15:07:44 +00001619 internal_select(s, 0);
Jeremy Hylton1ba6bad2001-08-20 19:06:36 +00001620 n = recv(s->sock_fd, PyString_AS_STRING(buf), len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001621 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001622
Guido van Rossum7c53b771995-09-13 18:39:47 +00001623 if (n < 0) {
1624 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001625 return s->errorhandler();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001626 }
Tim Peters5de98422002-04-27 18:44:32 +00001627 if (n != len)
1628 _PyString_Resize(&buf, n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001629 return buf;
1630}
1631
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001632PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001633"recv(buffersize[, flags]) -> data\n\
1634\n\
1635Receive up to buffersize bytes from the socket. For the optional flags\n\
1636argument, see the Unix manual. When no data is available, block until\n\
1637at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001638the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001639
Guido van Rossum30a685f1991-06-27 15:51:29 +00001640
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001641/* s.recvfrom(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001642
Guido van Rossum73624e91994-10-10 17:59:00 +00001643static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001644sock_recvfrom(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001645{
1646 char addrbuf[256];
Barry Warsaw752300b1997-01-03 17:18:10 +00001647 PyObject *buf = NULL;
1648 PyObject *addr = NULL;
1649 PyObject *ret = NULL;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001650 int len, n, flags = 0;
1651 socklen_t addrlen;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001652
Guido van Rossum43713e52000-02-29 13:59:29 +00001653 if (!PyArg_ParseTuple(args, "i|i:recvfrom", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001654 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001655
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00001656 if (!getsockaddrlen(s, &addrlen))
1657 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001658 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00001659 if (buf == NULL)
1660 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001661
Guido van Rossum73624e91994-10-10 17:59:00 +00001662 Py_BEGIN_ALLOW_THREADS
Fred Drakefd168342001-05-09 19:11:33 +00001663 memset(addrbuf, 0, addrlen);
Guido van Rossum11ba0942002-06-13 15:07:44 +00001664 internal_select(s, 0);
Fred Drakefd168342001-05-09 19:11:33 +00001665 n = recvfrom(s->sock_fd, PyString_AS_STRING(buf), len, flags,
Guido van Rossum8d665e61996-06-26 18:22:49 +00001666#ifndef MS_WINDOWS
Andrew MacIntyreba43e872002-03-03 03:03:52 +00001667#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001668 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00001669#else
Thomas Wouters334fb892000-07-25 12:56:38 +00001670 (void *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00001671#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001672#else
Barry Warsaw752300b1997-01-03 17:18:10 +00001673 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001674#endif
Barry Warsaw752300b1997-01-03 17:18:10 +00001675 );
Guido van Rossum73624e91994-10-10 17:59:00 +00001676 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001677
Guido van Rossum7c53b771995-09-13 18:39:47 +00001678 if (n < 0) {
1679 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001680 return s->errorhandler();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001681 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00001682
Guido van Rossum53a9bf81996-06-11 18:35:24 +00001683 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001684 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001685
Guido van Rossum67f7a382002-06-06 21:08:16 +00001686 if (!(addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001687 addrlen)))
Barry Warsaw752300b1997-01-03 17:18:10 +00001688 goto finally;
1689
Guido van Rossum73624e91994-10-10 17:59:00 +00001690 ret = Py_BuildValue("OO", buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001691
1692finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001693 Py_XDECREF(addr);
1694 Py_XDECREF(buf);
Guido van Rossum6f5afc91993-02-05 09:46:15 +00001695 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001696}
1697
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001698PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001699"recvfrom(buffersize[, flags]) -> (data, address info)\n\
1700\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001701Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001702
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001703/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001704
Guido van Rossum73624e91994-10-10 17:59:00 +00001705static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001706sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001707{
Guido van Rossumff4949e1992-08-05 19:58:53 +00001708 char *buf;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001709 int len, n, flags = 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001710
Guido van Rossum43713e52000-02-29 13:59:29 +00001711 if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001712 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001713
Guido van Rossum73624e91994-10-10 17:59:00 +00001714 Py_BEGIN_ALLOW_THREADS
Guido van Rossum11ba0942002-06-13 15:07:44 +00001715 internal_select(s, 1);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001716 n = send(s->sock_fd, buf, len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001717 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001718
Guido van Rossum30a685f1991-06-27 15:51:29 +00001719 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001720 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001721 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00001722}
1723
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001724PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001725"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001726\n\
1727Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001728argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001729sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001730
1731
1732/* s.sendall(data [,flags]) method */
1733
1734static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001735sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001736{
1737 char *buf;
Martin v. Löwise6cc5b62002-02-16 23:13:54 +00001738 int len, n, flags = 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001739
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001740 if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags))
1741 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001742
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001743 Py_BEGIN_ALLOW_THREADS
Guido van Rossum11ba0942002-06-13 15:07:44 +00001744 internal_select(s, 1);
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001745 do {
1746 n = send(s->sock_fd, buf, len, flags);
1747 if (n < 0)
1748 break;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001749 buf += n;
1750 len -= n;
1751 } while (len > 0);
1752 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001753
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001754 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001755 return s->errorhandler();
Guido van Rossum67f7a382002-06-06 21:08:16 +00001756
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001757 Py_INCREF(Py_None);
1758 return Py_None;
1759}
1760
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001761PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001762"sendall(data[, flags])\n\
1763\n\
1764Send a data string to the socket. For the optional flags\n\
1765argument, see the Unix manual. This calls send() repeatedly\n\
1766until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001767to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001768
Guido van Rossum30a685f1991-06-27 15:51:29 +00001769
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001770/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001771
Guido van Rossum73624e91994-10-10 17:59:00 +00001772static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001773sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001774{
Guido van Rossum73624e91994-10-10 17:59:00 +00001775 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001776 char *buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001777 struct sockaddr *addr;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001778 int addrlen, len, n, flags;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001779
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001780 flags = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001781 if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001782 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001783 if (!PyArg_ParseTuple(args, "s#iO:sendto",
1784 &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001785 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001786 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00001787
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001788 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001789 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001790
Guido van Rossum73624e91994-10-10 17:59:00 +00001791 Py_BEGIN_ALLOW_THREADS
Guido van Rossum11ba0942002-06-13 15:07:44 +00001792 internal_select(s, 1);
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001793 n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001794 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001795
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001796 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001797 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001798 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001799}
1800
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001801PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001802"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001803\n\
1804Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001805For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001806
Guido van Rossum30a685f1991-06-27 15:51:29 +00001807
1808/* s.shutdown(how) method */
1809
Guido van Rossum73624e91994-10-10 17:59:00 +00001810static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001811sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001812{
1813 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001814 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001815
1816 how = PyInt_AsLong(arg);
1817 if (how == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001818 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001819 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001820 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +00001821 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001822 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001823 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001824 Py_INCREF(Py_None);
1825 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001826}
1827
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001828PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001829"shutdown(flag)\n\
1830\n\
1831Shut down the reading side of the socket (flag == 0), the writing side\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001832of the socket (flag == 1), or both ends (flag == 2).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001833
Guido van Rossum30a685f1991-06-27 15:51:29 +00001834
1835/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001836
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001837static PyMethodDef sock_methods[] = {
1838 {"accept", (PyCFunction)sock_accept, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001839 accept_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001840 {"bind", (PyCFunction)sock_bind, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001841 bind_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001842 {"close", (PyCFunction)sock_close, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001843 close_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001844 {"connect", (PyCFunction)sock_connect, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001845 connect_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001846 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001847 connect_ex_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00001848#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001849 {"dup", (PyCFunction)sock_dup, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001850 dup_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00001851#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001852 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001853 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001854#ifdef HAVE_GETPEERNAME
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001855 {"getpeername", (PyCFunction)sock_getpeername,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001856 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00001857#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001858 {"getsockname", (PyCFunction)sock_getsockname,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001859 METH_NOARGS, getsockname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001860 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001861 getsockopt_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001862 {"listen", (PyCFunction)sock_listen, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001863 listen_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001864#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001865 {"makefile", (PyCFunction)sock_makefile, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001866 makefile_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001867#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001868 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001869 recv_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001870 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001871 recvfrom_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001872 {"send", (PyCFunction)sock_send, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001873 send_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001874 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00001875 sendall_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001876 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001877 sendto_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001878 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001879 setblocking_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001880 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
Guido van Rossum67f7a382002-06-06 21:08:16 +00001881 settimeout_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001882 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
Guido van Rossum67f7a382002-06-06 21:08:16 +00001883 gettimeout_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001884 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001885 setsockopt_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001886 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001887 shutdown_doc},
1888#ifdef RISCOS
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001889 {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001890 sleeptaskw_doc},
1891#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00001892 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001893};
1894
Guido van Rossum30a685f1991-06-27 15:51:29 +00001895
Guido van Rossum73624e91994-10-10 17:59:00 +00001896/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00001897 First close the file description. */
1898
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001899static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001900sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001901{
Guido van Rossumfa972c92000-04-10 12:45:45 +00001902 if (s->sock_fd != -1)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001903 (void) SOCKETCLOSE(s->sock_fd);
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001904 s->ob_type->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001905}
1906
Guido van Rossum30a685f1991-06-27 15:51:29 +00001907
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001908static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001909sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001910{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001911 char buf[512];
Fred Drakea04eaad2000-06-30 02:46:07 +00001912#if SIZEOF_SOCKET_T > SIZEOF_LONG
1913 if (s->sock_fd > LONG_MAX) {
1914 /* this can occur on Win64, and actually there is a special
1915 ugly printf formatter for decimal pointer length integer
1916 printing, only bother if necessary*/
1917 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001918 "no printf formatter to display "
1919 "the socket descriptor in decimal");
Fred Drakea04eaad2000-06-30 02:46:07 +00001920 return NULL;
1921 }
1922#endif
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001923 PyOS_snprintf(
1924 buf, sizeof(buf),
1925 "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>",
1926 (long)s->sock_fd, s->sock_family,
1927 s->sock_type,
1928 s->sock_proto);
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00001929 return PyString_FromString(buf);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001930}
1931
1932
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001933/* Create a new, uninitialized socket object. */
1934
1935static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001936sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001937{
1938 PyObject *new;
1939
1940 new = type->tp_alloc(type, 0);
Guido van Rossume1c478f2002-06-06 20:08:25 +00001941 if (new != NULL) {
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001942 ((PySocketSockObject *)new)->sock_fd = -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001943 ((PySocketSockObject *)new)->sock_timeout = -1.0;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001944 ((PySocketSockObject *)new)->errorhandler = &set_error;
Guido van Rossume1c478f2002-06-06 20:08:25 +00001945 }
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001946 return new;
1947}
1948
1949
1950/* Initialize a new socket object. */
1951
1952/*ARGSUSED*/
1953static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00001954sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001955{
1956 PySocketSockObject *s = (PySocketSockObject *)self;
1957 SOCKET_T fd;
1958 int family = AF_INET, type = SOCK_STREAM, proto = 0;
1959 static char *keywords[] = {"family", "type", "proto", 0};
1960
1961 if (!PyArg_ParseTupleAndKeywords(args, kwds,
1962 "|iii:socket", keywords,
1963 &family, &type, &proto))
1964 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001965
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001966 Py_BEGIN_ALLOW_THREADS
1967 fd = socket(family, type, proto);
1968 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001969
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001970#ifdef MS_WINDOWS
1971 if (fd == INVALID_SOCKET)
1972#else
1973 if (fd < 0)
1974#endif
1975 {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001976 set_error();
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001977 return -1;
1978 }
1979 init_sockobject(s, fd, family, type, proto);
1980 /* From now on, ignore SIGPIPE and let the error checking
1981 do the work. */
1982#ifdef SIGPIPE
1983 (void) signal(SIGPIPE, SIG_IGN);
1984#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00001985
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001986 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001987
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001988}
1989
1990
Guido van Rossumb6775db1994-08-01 11:34:53 +00001991/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001992
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001993static PyTypeObject sock_type = {
Guido van Rossum55558541996-05-23 22:54:50 +00001994 PyObject_HEAD_INIT(0) /* Must fill in type value later */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001995 0, /* ob_size */
Guido van Rossumca6dfa52001-10-28 12:31:33 +00001996 "_socket.socket", /* tp_name */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00001997 sizeof(PySocketSockObject), /* tp_basicsize */
1998 0, /* tp_itemsize */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001999 (destructor)sock_dealloc, /* tp_dealloc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002000 0, /* tp_print */
2001 0, /* tp_getattr */
2002 0, /* tp_setattr */
2003 0, /* tp_compare */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002004 (reprfunc)sock_repr, /* tp_repr */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002005 0, /* tp_as_number */
2006 0, /* tp_as_sequence */
2007 0, /* tp_as_mapping */
2008 0, /* tp_hash */
2009 0, /* tp_call */
2010 0, /* tp_str */
Michael W. Hudson03f96bd2001-11-09 10:06:23 +00002011 0, /* set below */ /* tp_getattro */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002012 0, /* tp_setattro */
2013 0, /* tp_as_buffer */
2014 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002015 sock_doc, /* tp_doc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002016 0, /* tp_traverse */
2017 0, /* tp_clear */
2018 0, /* tp_richcompare */
2019 0, /* tp_weaklistoffset */
2020 0, /* tp_iter */
2021 0, /* tp_iternext */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002022 sock_methods, /* tp_methods */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002023 0, /* tp_members */
2024 0, /* tp_getset */
2025 0, /* tp_base */
2026 0, /* tp_dict */
2027 0, /* tp_descr_get */
2028 0, /* tp_descr_set */
2029 0, /* tp_dictoffset */
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002030 sock_initobj, /* tp_init */
Michael W. Hudson03f96bd2001-11-09 10:06:23 +00002031 0, /* set below */ /* tp_alloc */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002032 sock_new, /* tp_new */
Michael W. Hudson03f96bd2001-11-09 10:06:23 +00002033 0, /* set below */ /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002034};
2035
Guido van Rossum30a685f1991-06-27 15:51:29 +00002036
Guido van Rossum81194471991-07-27 21:42:02 +00002037/* Python interface to gethostname(). */
2038
2039/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002040static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002041socket_gethostname(PyObject *self, PyObject *args)
Guido van Rossum81194471991-07-27 21:42:02 +00002042{
2043 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00002044 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002045 if (!PyArg_ParseTuple(args, ":gethostname"))
Guido van Rossum81194471991-07-27 21:42:02 +00002046 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002047 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002048 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00002049 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002050 if (res < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002051 return set_error();
Guido van Rossum81194471991-07-27 21:42:02 +00002052 buf[sizeof buf - 1] = '\0';
Guido van Rossum73624e91994-10-10 17:59:00 +00002053 return PyString_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00002054}
Guido van Rossumff4949e1992-08-05 19:58:53 +00002055
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002056PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002057"gethostname() -> string\n\
2058\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002059Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002060
Guido van Rossumff4949e1992-08-05 19:58:53 +00002061
Guido van Rossum30a685f1991-06-27 15:51:29 +00002062/* Python interface to gethostbyname(name). */
2063
2064/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002065static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002066socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002067{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002068 char *name;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002069 struct sockaddr_storage addrbuf;
2070
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002071 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002072 return NULL;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002073 if (setipaddr(name, (struct sockaddr *)&addrbuf, sizeof(addrbuf), AF_INET) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002074 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002075 return makeipaddr((struct sockaddr *)&addrbuf,
2076 sizeof(struct sockaddr_in));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002077}
2078
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002079PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002080"gethostbyname(host) -> address\n\
2081\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002082Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002083
2084
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002085/* Convenience function common to gethostbyname_ex and gethostbyaddr */
2086
2087static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002088gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002089{
2090 char **pch;
2091 PyObject *rtn_tuple = (PyObject *)NULL;
2092 PyObject *name_list = (PyObject *)NULL;
2093 PyObject *addr_list = (PyObject *)NULL;
2094 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002095
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002096 if (h == NULL) {
Guido van Rossum48a680c2001-03-02 06:34:14 +00002097 /* Let's get real error message to return */
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002098#ifndef RISCOS
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002099 set_herror(h_errno);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002100#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002101 PyErr_SetString(socket_error, "host not found");
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002102#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002103 return NULL;
2104 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002105
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002106 if (h->h_addrtype != af) {
2107#ifdef HAVE_STRERROR
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002108 /* Let's get real error message to return */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002109 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002110 (char *)strerror(EAFNOSUPPORT));
2111#else
2112 PyErr_SetString(
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002113 socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002114 "Address family not supported by protocol family");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002115#endif
2116 return NULL;
2117 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002118
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002119 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002120
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002121 case AF_INET:
2122 if (alen < sizeof(struct sockaddr_in))
2123 return NULL;
2124 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002125
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002126#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002127 case AF_INET6:
2128 if (alen < sizeof(struct sockaddr_in6))
2129 return NULL;
2130 break;
2131#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002132
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002133 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002134
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002135 if ((name_list = PyList_New(0)) == NULL)
2136 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002137
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002138 if ((addr_list = PyList_New(0)) == NULL)
2139 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002140
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002141 for (pch = h->h_aliases; *pch != NULL; pch++) {
2142 int status;
2143 tmp = PyString_FromString(*pch);
2144 if (tmp == NULL)
2145 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002146
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002147 status = PyList_Append(name_list, tmp);
2148 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002149
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002150 if (status)
2151 goto err;
2152 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002153
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002154 for (pch = h->h_addr_list; *pch != NULL; pch++) {
2155 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002156
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002157 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002158
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002159 case AF_INET:
2160 {
2161 struct sockaddr_in sin;
2162 memset(&sin, 0, sizeof(sin));
2163 sin.sin_family = af;
2164#ifdef HAVE_SOCKADDR_SA_LEN
2165 sin.sin_len = sizeof(sin);
2166#endif
2167 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
2168 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002169
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002170 if (pch == h->h_addr_list && alen >= sizeof(sin))
2171 memcpy((char *) addr, &sin, sizeof(sin));
2172 break;
2173 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002174
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002175#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002176 case AF_INET6:
2177 {
2178 struct sockaddr_in6 sin6;
2179 memset(&sin6, 0, sizeof(sin6));
2180 sin6.sin6_family = af;
2181#ifdef HAVE_SOCKADDR_SA_LEN
2182 sin6.sin6_len = sizeof(sin6);
2183#endif
2184 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
2185 tmp = makeipaddr((struct sockaddr *)&sin6,
2186 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002187
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002188 if (pch == h->h_addr_list && alen >= sizeof(sin6))
2189 memcpy((char *) addr, &sin6, sizeof(sin6));
2190 break;
2191 }
2192#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002193
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002194 default: /* can't happen */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002195 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002196 "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002197 return NULL;
2198 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002199
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002200 if (tmp == NULL)
2201 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002202
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002203 status = PyList_Append(addr_list, tmp);
2204 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002205
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002206 if (status)
2207 goto err;
2208 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002209
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002210 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002211
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002212 err:
2213 Py_XDECREF(name_list);
2214 Py_XDECREF(addr_list);
2215 return rtn_tuple;
2216}
2217
2218
2219/* Python interface to gethostbyname_ex(name). */
2220
2221/*ARGSUSED*/
2222static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002223socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002224{
2225 char *name;
2226 struct hostent *h;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002227 struct sockaddr_storage addr;
Martin v. Löwis9db2f572001-07-23 01:30:10 +00002228 struct sockaddr *sa;
Guido van Rossum955becc1999-03-22 20:14:53 +00002229 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002230#ifdef HAVE_GETHOSTBYNAME_R
2231 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002232#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
2233 struct hostent_data data;
2234#else
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002235 char buf[16384];
2236 int buf_len = (sizeof buf) - 1;
2237 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002238#endif
2239#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002240 int result;
2241#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002242#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002243
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002244 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002245 return NULL;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002246 if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), PF_INET) < 0)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002247 return NULL;
2248 Py_BEGIN_ALLOW_THREADS
2249#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002250#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002251 result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
2252 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002253#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002254 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002255#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00002256 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002257 result = gethostbyname_r(name, &hp_allocated, &data);
2258 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002259#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002260#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002261#ifdef USE_GETHOSTBYNAME_LOCK
2262 PyThread_acquire_lock(gethostbyname_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002263#endif
2264 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002265#endif /* HAVE_GETHOSTBYNAME_R */
2266 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002267 /* Some C libraries would require addr.__ss_family instead of
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002268 addr.ss_family.
2269 Therefore, we cast the sockaddr_storage into sockaddr to
2270 access sa_family. */
Martin v. Löwis9db2f572001-07-23 01:30:10 +00002271 sa = (struct sockaddr*)&addr;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002272 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002273 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002274#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum955becc1999-03-22 20:14:53 +00002275 PyThread_release_lock(gethostbyname_lock);
2276#endif
2277 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002278}
2279
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002280PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002281"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
2282\n\
2283Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002284for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002285
2286
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002287/* Python interface to gethostbyaddr(IP). */
2288
2289/*ARGSUSED*/
2290static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002291socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002292{
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002293#ifdef ENABLE_IPV6
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002294 struct sockaddr_storage addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002295#else
Guido van Rossum48a680c2001-03-02 06:34:14 +00002296 struct sockaddr_in addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002297#endif
2298 struct sockaddr *sa = (struct sockaddr *)&addr;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002299 char *ip_num;
2300 struct hostent *h;
Guido van Rossum3baaa131999-03-22 21:44:51 +00002301 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002302#ifdef HAVE_GETHOSTBYNAME_R
2303 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002304#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
2305 struct hostent_data data;
2306#else
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002307 char buf[16384];
2308 int buf_len = (sizeof buf) - 1;
2309 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002310#endif
2311#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002312 int result;
2313#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002314#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002315 char *ap;
2316 int al;
2317 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002318
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002319 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002320 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002321 af = PF_UNSPEC;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002322 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002323 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002324 af = sa->sa_family;
2325 ap = NULL;
2326 al = 0;
2327 switch (af) {
2328 case AF_INET:
2329 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
2330 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
2331 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002332#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002333 case AF_INET6:
2334 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
2335 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
2336 break;
2337#endif
2338 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002339 PyErr_SetString(socket_error, "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002340 return NULL;
2341 }
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002342 Py_BEGIN_ALLOW_THREADS
2343#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002344#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002345 result = gethostbyaddr_r(ap, al, af,
2346 &hp_allocated, buf, buf_len,
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002347 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002348#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002349 h = gethostbyaddr_r(ap, al, af,
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002350 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002351#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00002352 memset((void *) &data, '\0', sizeof(data));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002353 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002354 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002355#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002356#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002357#ifdef USE_GETHOSTBYNAME_LOCK
2358 PyThread_acquire_lock(gethostbyname_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002359#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002360 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002361#endif /* HAVE_GETHOSTBYNAME_R */
2362 Py_END_ALLOW_THREADS
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002363 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002364#ifdef USE_GETHOSTBYNAME_LOCK
2365 PyThread_release_lock(gethostbyname_lock);
2366#endif
2367 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002368}
2369
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002370PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002371"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
2372\n\
2373Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002374for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002375
Guido van Rossum30a685f1991-06-27 15:51:29 +00002376
2377/* Python interface to getservbyname(name).
2378 This only returns the port number, since the other info is already
2379 known or not useful (like the list of aliases). */
2380
2381/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002382static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002383socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002384{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002385 char *name, *proto;
Guido van Rossum30a685f1991-06-27 15:51:29 +00002386 struct servent *sp;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002387 if (!PyArg_ParseTuple(args, "ss:getservbyname", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002388 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002389 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002390 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00002391 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00002392 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002393 PyErr_SetString(socket_error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00002394 return NULL;
2395 }
Guido van Rossum73624e91994-10-10 17:59:00 +00002396 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002397}
2398
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002399PyDoc_STRVAR(getservbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002400"getservbyname(servicename, protocolname) -> integer\n\
2401\n\
2402Return a port number from a service name and protocol name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002403The protocol name should be 'tcp' or 'udp'.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002404
Guido van Rossum30a685f1991-06-27 15:51:29 +00002405
Guido van Rossum3901d851996-12-19 16:35:04 +00002406/* Python interface to getprotobyname(name).
2407 This only returns the protocol number, since the other info is
2408 already known or not useful (like the list of aliases). */
2409
2410/*ARGSUSED*/
2411static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002412socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00002413{
2414 char *name;
2415 struct protoent *sp;
Guido van Rossumbcc20741998-08-04 22:53:56 +00002416#ifdef __BEOS__
2417/* Not available in BeOS yet. - [cjh] */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002418 PyErr_SetString(socket_error, "getprotobyname not supported");
Guido van Rossumbcc20741998-08-04 22:53:56 +00002419 return NULL;
2420#else
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002421 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
Guido van Rossum3901d851996-12-19 16:35:04 +00002422 return NULL;
2423 Py_BEGIN_ALLOW_THREADS
2424 sp = getprotobyname(name);
2425 Py_END_ALLOW_THREADS
2426 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002427 PyErr_SetString(socket_error, "protocol not found");
Guido van Rossum3901d851996-12-19 16:35:04 +00002428 return NULL;
2429 }
2430 return PyInt_FromLong((long) sp->p_proto);
Guido van Rossumbcc20741998-08-04 22:53:56 +00002431#endif
Guido van Rossum3901d851996-12-19 16:35:04 +00002432}
2433
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002434PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002435"getprotobyname(name) -> integer\n\
2436\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002437Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002438
Guido van Rossum3901d851996-12-19 16:35:04 +00002439
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002440#ifndef NO_DUP
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002441/* Create a socket object from a numeric file description.
2442 Useful e.g. if stdin is a socket.
2443 Additional arguments as for socket(). */
2444
2445/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002446static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002447socket_fromfd(PyObject *self, PyObject *args)
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002448{
Guido van Rossum73624e91994-10-10 17:59:00 +00002449 PySocketSockObject *s;
Fred Drakea04eaad2000-06-30 02:46:07 +00002450 SOCKET_T fd;
2451 int family, type, proto = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002452 if (!PyArg_ParseTuple(args, "iii|i:fromfd",
2453 &fd, &family, &type, &proto))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002454 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +00002455 /* Dup the fd so it and the socket can be closed independently */
2456 fd = dup(fd);
2457 if (fd < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002458 return set_error();
2459 s = new_sockobject(fd, family, type, proto);
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002460 /* From now on, ignore SIGPIPE and let the error checking
2461 do the work. */
Guido van Rossum48a680c2001-03-02 06:34:14 +00002462#ifdef SIGPIPE
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002463 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum48a680c2001-03-02 06:34:14 +00002464#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00002465 return (PyObject *) s;
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002466}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002467
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002468PyDoc_STRVAR(fromfd_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002469"fromfd(fd, family, type[, proto]) -> socket object\n\
2470\n\
2471Create a socket object from the given file descriptor.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002472The remaining arguments are the same as for socket().");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002473
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002474#endif /* NO_DUP */
Guido van Rossum2a7178e1992-12-08 13:38:24 +00002475
Guido van Rossum82a5c661998-07-07 20:45:43 +00002476
Guido van Rossum006bf911996-06-12 04:04:55 +00002477static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002478socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00002479{
2480 int x1, x2;
2481
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002482 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00002483 return NULL;
2484 }
2485 x2 = (int)ntohs((short)x1);
2486 return PyInt_FromLong(x2);
2487}
2488
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002489PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002490"ntohs(integer) -> integer\n\
2491\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002492Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002493
2494
Guido van Rossum006bf911996-06-12 04:04:55 +00002495static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002496socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00002497{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002498 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00002499
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002500 if (PyInt_Check(arg)) {
2501 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002502 if (x == (unsigned long) -1 && PyErr_Occurred())
2503 return NULL;
Guido van Rossum006bf911996-06-12 04:04:55 +00002504 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002505 else if (PyLong_Check(arg)) {
2506 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002507 if (x == (unsigned long) -1 && PyErr_Occurred())
2508 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002509#if SIZEOF_LONG > 4
2510 {
2511 unsigned long y;
2512 /* only want the trailing 32 bits */
2513 y = x & 0xFFFFFFFFUL;
2514 if (y ^ x)
2515 return PyErr_Format(PyExc_OverflowError,
2516 "long int larger than 32 bits");
2517 x = y;
Tim Peters58141872002-08-06 22:25:02 +00002518 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002519#endif
2520 }
2521 else
Tim Peters58141872002-08-06 22:25:02 +00002522 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002523 "expected int/long, %s found",
2524 arg->ob_type->tp_name);
2525 if (x == (unsigned long) -1 && PyErr_Occurred())
2526 return NULL;
2527 return PyInt_FromLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00002528}
2529
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002530PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002531"ntohl(integer) -> integer\n\
2532\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002533Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002534
2535
Guido van Rossum006bf911996-06-12 04:04:55 +00002536static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002537socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00002538{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002539 unsigned long x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00002540
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002541 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00002542 return NULL;
2543 }
2544 x2 = (int)htons((short)x1);
2545 return PyInt_FromLong(x2);
2546}
2547
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002548PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002549"htons(integer) -> integer\n\
2550\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002551Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002552
2553
Guido van Rossum006bf911996-06-12 04:04:55 +00002554static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002555socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00002556{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002557 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00002558
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002559 if (PyInt_Check(arg)) {
2560 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002561 if (x == (unsigned long) -1 && PyErr_Occurred())
2562 return NULL;
Guido van Rossum006bf911996-06-12 04:04:55 +00002563 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002564 else if (PyLong_Check(arg)) {
2565 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00002566 if (x == (unsigned long) -1 && PyErr_Occurred())
2567 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002568#if SIZEOF_LONG > 4
2569 {
2570 unsigned long y;
2571 /* only want the trailing 32 bits */
2572 y = x & 0xFFFFFFFFUL;
2573 if (y ^ x)
2574 return PyErr_Format(PyExc_OverflowError,
2575 "long int larger than 32 bits");
2576 x = y;
Tim Peters58141872002-08-06 22:25:02 +00002577 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002578#endif
2579 }
2580 else
Tim Peters58141872002-08-06 22:25:02 +00002581 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002582 "expected int/long, %s found",
2583 arg->ob_type->tp_name);
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002584 return PyInt_FromLong(htonl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00002585}
2586
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002587PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002588"htonl(integer) -> integer\n\
2589\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002590Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002591
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002592/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002593
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002594PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002595"inet_aton(string) -> packed 32-bit IP representation\n\
2596\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002597Convert 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 +00002598binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002599
2600static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002601socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002602{
Guido van Rossuma2e48551999-09-09 15:42:59 +00002603#ifndef INADDR_NONE
2604#define INADDR_NONE (-1)
2605#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002606
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002607 /* Have to use inet_addr() instead */
2608 char *ip_addr;
Fred Drakee5065292001-07-19 21:16:41 +00002609 unsigned long packed_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002610
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002611 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) {
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002612 return NULL;
2613 }
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002614 packed_addr = inet_addr(ip_addr);
2615
2616 if (packed_addr == INADDR_NONE) { /* invalid address */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002617 PyErr_SetString(socket_error,
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002618 "illegal IP address string passed to inet_aton");
2619 return NULL;
2620 }
2621
2622 return PyString_FromStringAndSize((char *) &packed_addr,
2623 sizeof(packed_addr));
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002624}
2625
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002626PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00002627"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002628\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002629Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002630
2631static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002632socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002633{
2634 char *packed_str;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002635 int addr_len;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002636 struct in_addr packed_addr;
2637
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002638 if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) {
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002639 return NULL;
2640 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00002641
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002642 if (addr_len != sizeof(packed_addr)) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002643 PyErr_SetString(socket_error,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002644 "packed IP wrong length for inet_ntoa");
2645 return NULL;
2646 }
2647
2648 memcpy(&packed_addr, packed_str, addr_len);
2649
2650 return PyString_FromString(inet_ntoa(packed_addr));
2651}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002652
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002653/* Python interface to getaddrinfo(host, port). */
2654
2655/*ARGSUSED*/
2656static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002657socket_getaddrinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002658{
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00002659 struct addrinfo hints, *res;
2660 struct addrinfo *res0 = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002661 PyObject *pobj = (PyObject *)NULL;
Martin v. Löwisf65b1a12001-08-12 09:28:40 +00002662 char pbuf[30];
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002663 char *hptr, *pptr;
2664 int family, socktype, protocol, flags;
2665 int error;
2666 PyObject *all = (PyObject *)NULL;
2667 PyObject *single = (PyObject *)NULL;
2668
2669 family = socktype = protocol = flags = 0;
2670 family = PF_UNSPEC;
2671 if (!PyArg_ParseTuple(args, "zO|iiii:getaddrinfo",
2672 &hptr, &pobj, &family, &socktype,
2673 &protocol, &flags)) {
2674 return NULL;
2675 }
2676 if (PyInt_Check(pobj)) {
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00002677 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002678 pptr = pbuf;
2679 } else if (PyString_Check(pobj)) {
2680 pptr = PyString_AsString(pobj);
2681 } else if (pobj == Py_None) {
2682 pptr = (char *)NULL;
2683 } else {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002684 PyErr_SetString(socket_error, "Int or String expected");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002685 return NULL;
2686 }
2687 memset(&hints, 0, sizeof(hints));
2688 hints.ai_family = family;
2689 hints.ai_socktype = socktype;
2690 hints.ai_protocol = protocol;
2691 hints.ai_flags = flags;
2692 error = getaddrinfo(hptr, pptr, &hints, &res0);
2693 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002694 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002695 return NULL;
2696 }
2697
2698 if ((all = PyList_New(0)) == NULL)
2699 goto err;
2700 for (res = res0; res; res = res->ai_next) {
Guido van Rossum716aac02001-10-12 18:59:27 +00002701 PyObject *addr =
2702 makesockaddr(-1, res->ai_addr, res->ai_addrlen);
2703 if (addr == NULL)
2704 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002705 single = Py_BuildValue("iiisO", res->ai_family,
2706 res->ai_socktype, res->ai_protocol,
2707 res->ai_canonname ? res->ai_canonname : "",
Guido van Rossum716aac02001-10-12 18:59:27 +00002708 addr);
2709 Py_DECREF(addr);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002710 if (single == NULL)
2711 goto err;
2712
2713 if (PyList_Append(all, single))
2714 goto err;
2715 Py_XDECREF(single);
2716 }
Neal Norwitz90128ba2002-08-09 03:37:42 +00002717 if (res0)
2718 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002719 return all;
2720 err:
2721 Py_XDECREF(single);
2722 Py_XDECREF(all);
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00002723 if (res0)
2724 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002725 return (PyObject *)NULL;
2726}
2727
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002728PyDoc_STRVAR(getaddrinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002729"getaddrinfo(host, port [, family, socktype, proto, flags])\n\
2730 -> list of (family, socktype, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002731\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002732Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002733
2734/* Python interface to getnameinfo(sa, flags). */
2735
2736/*ARGSUSED*/
2737static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002738socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002739{
2740 PyObject *sa = (PyObject *)NULL;
2741 int flags;
2742 char *hostp;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00002743 int port, flowinfo, scope_id;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002744 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
2745 struct addrinfo hints, *res = NULL;
2746 int error;
2747 PyObject *ret = (PyObject *)NULL;
2748
2749 flags = flowinfo = scope_id = 0;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00002750 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002751 return NULL;
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002752 if (!PyArg_ParseTuple(sa, "si|ii",
2753 &hostp, &port, &flowinfo, &scope_id))
Martin v. Löwis06b1d212001-11-02 23:34:52 +00002754 return NULL;
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00002755 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002756 memset(&hints, 0, sizeof(hints));
2757 hints.ai_family = PF_UNSPEC;
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00002758 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002759 error = getaddrinfo(hostp, pbuf, &hints, &res);
2760 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002761 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002762 goto fail;
2763 }
2764 if (res->ai_next) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002765 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002766 "sockaddr resolved to multiple addresses");
2767 goto fail;
2768 }
2769 switch (res->ai_family) {
2770 case AF_INET:
2771 {
2772 char *t1;
2773 int t2;
2774 if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002775 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002776 "IPv4 sockaddr must be 2 tuple");
2777 goto fail;
2778 }
2779 break;
2780 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002781#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002782 case AF_INET6:
2783 {
2784 struct sockaddr_in6 *sin6;
2785 sin6 = (struct sockaddr_in6 *)res->ai_addr;
2786 sin6->sin6_flowinfo = flowinfo;
2787 sin6->sin6_scope_id = scope_id;
2788 break;
2789 }
2790#endif
2791 }
2792 error = getnameinfo(res->ai_addr, res->ai_addrlen,
2793 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
2794 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002795 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002796 goto fail;
2797 }
2798 ret = Py_BuildValue("ss", hbuf, pbuf);
2799
2800fail:
2801 if (res)
2802 freeaddrinfo(res);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002803 return ret;
2804}
2805
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002806PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002807"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002808\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002809Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002810
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00002811
2812/* Python API to getting and setting the default timeout value. */
2813
2814static PyObject *
2815socket_getdefaulttimeout(PyObject *self)
2816{
2817 if (defaulttimeout < 0.0) {
2818 Py_INCREF(Py_None);
2819 return Py_None;
2820 }
2821 else
2822 return PyFloat_FromDouble(defaulttimeout);
2823}
2824
2825PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002826"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00002827\n\
2828Returns the default timeout in floating seconds for new socket objects.\n\
2829A value of None indicates that new socket objects have no timeout.\n\
2830When the socket module is first imported, the default is None.");
2831
2832static PyObject *
2833socket_setdefaulttimeout(PyObject *self, PyObject *arg)
2834{
2835 double timeout;
2836
2837 if (arg == Py_None)
2838 timeout = -1.0;
2839 else {
2840 timeout = PyFloat_AsDouble(arg);
2841 if (timeout < 0.0) {
2842 if (!PyErr_Occurred())
2843 PyErr_SetString(PyExc_ValueError,
2844 "Timeout value out of range");
2845 return NULL;
2846 }
2847 }
2848
2849 defaulttimeout = timeout;
2850
2851 Py_INCREF(Py_None);
2852 return Py_None;
2853}
2854
2855PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002856"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00002857\n\
2858Set the default timeout in floating seconds for new socket objects.\n\
2859A value of None indicates that new socket objects have no timeout.\n\
2860When the socket module is first imported, the default is None.");
2861
2862
Guido van Rossum30a685f1991-06-27 15:51:29 +00002863/* List of functions exported by this module. */
2864
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002865static PyMethodDef socket_methods[] = {
2866 {"gethostbyname", socket_gethostbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002867 METH_VARARGS, gethostbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002868 {"gethostbyname_ex", socket_gethostbyname_ex,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002869 METH_VARARGS, ghbn_ex_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002870 {"gethostbyaddr", socket_gethostbyaddr,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002871 METH_VARARGS, gethostbyaddr_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002872 {"gethostname", socket_gethostname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002873 METH_VARARGS, gethostname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002874 {"getservbyname", socket_getservbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002875 METH_VARARGS, getservbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002876 {"getprotobyname", socket_getprotobyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002877 METH_VARARGS,getprotobyname_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002878#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002879 {"fromfd", socket_fromfd,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002880 METH_VARARGS, fromfd_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002881#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002882 {"ntohs", socket_ntohs,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002883 METH_VARARGS, ntohs_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002884 {"ntohl", socket_ntohl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002885 METH_O, ntohl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002886 {"htons", socket_htons,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002887 METH_VARARGS, htons_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002888 {"htonl", socket_htonl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00002889 METH_O, htonl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002890 {"inet_aton", socket_inet_aton,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002891 METH_VARARGS, inet_aton_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002892 {"inet_ntoa", socket_inet_ntoa,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002893 METH_VARARGS, inet_ntoa_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002894 {"getaddrinfo", socket_getaddrinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002895 METH_VARARGS, getaddrinfo_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002896 {"getnameinfo", socket_getnameinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002897 METH_VARARGS, getnameinfo_doc},
Guido van Rossum1693ba82002-07-18 21:11:26 +00002898 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00002899 METH_NOARGS, getdefaulttimeout_doc},
2900 {"setdefaulttimeout", socket_setdefaulttimeout,
2901 METH_O, setdefaulttimeout_doc},
Guido van Rossum30a685f1991-06-27 15:51:29 +00002902 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002903};
2904
Guido van Rossum30a685f1991-06-27 15:51:29 +00002905
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002906#ifdef RISCOS
2907#define OS_INIT_DEFINED
Guido van Rossumbe32c891996-06-20 16:25:29 +00002908
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002909static int
2910os_init(void)
2911{
2912 _kernel_swi_regs r;
2913
2914 r.r[0] = 0;
2915 _kernel_swi(0x43380, &r, &r);
2916 taskwindow = r.r[0];
2917
2918 return 0;
2919}
2920
2921#endif /* RISCOS */
2922
2923
2924#ifdef MS_WINDOWS
2925#define OS_INIT_DEFINED
2926
2927/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00002928
2929static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002930os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00002931{
2932 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00002933}
2934
2935static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002936os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00002937{
2938 WSADATA WSAData;
2939 int ret;
2940 char buf[100];
2941 ret = WSAStartup(0x0101, &WSAData);
2942 switch (ret) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002943 case 0: /* No error */
2944 atexit(os_cleanup);
2945 return 1; /* Success */
Guido van Rossumbe32c891996-06-20 16:25:29 +00002946 case WSASYSNOTREADY:
2947 PyErr_SetString(PyExc_ImportError,
2948 "WSAStartup failed: network not ready");
2949 break;
2950 case WSAVERNOTSUPPORTED:
2951 case WSAEINVAL:
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002952 PyErr_SetString(
2953 PyExc_ImportError,
2954 "WSAStartup failed: requested version not supported");
Guido van Rossumbe32c891996-06-20 16:25:29 +00002955 break;
2956 default:
Tim Peters885d4572001-11-28 20:27:42 +00002957 PyOS_snprintf(buf, sizeof(buf),
2958 "WSAStartup failed: error code %d", ret);
Guido van Rossumbe32c891996-06-20 16:25:29 +00002959 PyErr_SetString(PyExc_ImportError, buf);
2960 break;
2961 }
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002962 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00002963}
2964
Guido van Rossum8d665e61996-06-26 18:22:49 +00002965#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00002966
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002967
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002968#ifdef PYOS_OS2
2969#define OS_INIT_DEFINED
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002970
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002971/* Additional initialization for OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002972
2973static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002974os_init(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002975{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002976#ifndef PYCC_GCC
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002977 char reason[64];
2978 int rc = sock_init();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002979
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002980 if (rc == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002981 return 1; /* Success */
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002982 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002983
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002984 PyOS_snprintf(reason, sizeof(reason),
2985 "OS/2 TCP/IP Error# %d", sock_errno());
2986 PyErr_SetString(PyExc_ImportError, reason);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002987
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002988 return 0; /* Failure */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00002989#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002990 /* No need to initialise sockets with GCC/EMX */
2991 return 1; /* Success */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00002992#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002993}
2994
2995#endif /* PYOS_OS2 */
2996
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002997
2998#ifndef OS_INIT_DEFINED
2999static int
3000os_init(void)
3001{
3002 return 1; /* Success */
3003}
3004#endif
3005
3006
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003007/* C API table - always add new things to the end for binary
3008 compatibility. */
3009static
3010PySocketModule_APIObject PySocketModuleAPI =
3011{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003012 &sock_type,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003013};
3014
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003015
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003016/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003017
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003018 This module is actually called "_socket", and there's a wrapper
3019 "socket.py" which implements some additional functionality. On some
3020 platforms (e.g. Windows and OS/2), socket.py also implements a
3021 wrapper for the socket type that provides missing functionality such
3022 as makefile(), dup() and fromfd(). The import of "_socket" may fail
3023 with an ImportError exception if os-specific initialization fails.
3024 On Windows, this does WINSOCK initialization. When WINSOCK is
3025 initialized succesfully, a call to WSACleanup() is scheduled to be
3026 made at exit time.
3027*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003028
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003029PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003030"Implementation module for socket operations.\n\
3031\n\
3032See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003033
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003034PyMODINIT_FUNC
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003035init_socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003036{
Fred Drake4baedc12002-04-01 14:53:37 +00003037 PyObject *m;
Fred Drake4baedc12002-04-01 14:53:37 +00003038
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003039 if (!os_init())
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003040 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003041
3042 sock_type.ob_type = &PyType_Type;
3043 sock_type.tp_getattro = PyObject_GenericGetAttr;
3044 sock_type.tp_alloc = PyType_GenericAlloc;
3045 sock_type.tp_free = PyObject_Del;
3046 m = Py_InitModule3(PySocket_MODULE_NAME,
3047 socket_methods,
3048 socket_doc);
3049
3050 socket_error = PyErr_NewException("socket.error", NULL, NULL);
3051 if (socket_error == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003052 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003053 Py_INCREF(socket_error);
3054 PyModule_AddObject(m, "error", socket_error);
3055 socket_herror = PyErr_NewException("socket.herror",
3056 socket_error, NULL);
3057 if (socket_herror == NULL)
3058 return;
3059 Py_INCREF(socket_herror);
3060 PyModule_AddObject(m, "herror", socket_herror);
3061 socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003062 NULL);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003063 if (socket_gaierror == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003064 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003065 Py_INCREF(socket_gaierror);
3066 PyModule_AddObject(m, "gaierror", socket_gaierror);
3067 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00003068 if (PyModule_AddObject(m, "SocketType",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003069 (PyObject *)&sock_type) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003070 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003071 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00003072 if (PyModule_AddObject(m, "socket",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003073 (PyObject *)&sock_type) != 0)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003074 return;
Guido van Rossum09be4091999-08-09 14:40:40 +00003075
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003076 /* Export C API */
Fred Drake4baedc12002-04-01 14:53:37 +00003077 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003078 PyCObject_FromVoidPtr((void *)&PySocketModuleAPI, NULL)
3079 ) != 0)
3080 return;
3081
Guido van Rossum09be4091999-08-09 14:40:40 +00003082 /* Address families (we only support AF_INET and AF_UNIX) */
3083#ifdef AF_UNSPEC
Fred Drake4baedc12002-04-01 14:53:37 +00003084 PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00003085#endif
Fred Drake4baedc12002-04-01 14:53:37 +00003086 PyModule_AddIntConstant(m, "AF_INET", AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003087#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00003088 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003089#endif /* AF_INET6 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003090#ifdef AF_UNIX
Fred Drake4baedc12002-04-01 14:53:37 +00003091 PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003092#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00003093#ifdef AF_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00003094 /* Amateur Radio AX.25 */
3095 PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003096#endif
3097#ifdef AF_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00003098 PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00003099#endif
3100#ifdef AF_APPLETALK
Fred Drake4baedc12002-04-01 14:53:37 +00003101 /* Appletalk DDP */
3102 PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00003103#endif
3104#ifdef AF_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00003105 /* Amateur radio NetROM */
3106 PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00003107#endif
3108#ifdef AF_BRIDGE
Fred Drake4baedc12002-04-01 14:53:37 +00003109 /* Multiprotocol bridge */
3110 PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003111#endif
3112#ifdef AF_AAL5
Fred Drake4baedc12002-04-01 14:53:37 +00003113 /* Reserved for Werner's ATM */
3114 PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00003115#endif
3116#ifdef AF_X25
Fred Drake4baedc12002-04-01 14:53:37 +00003117 /* Reserved for X.25 project */
3118 PyModule_AddIntConstant(m, "AF_X25", AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003119#endif
3120#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00003121 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00003122#endif
3123#ifdef AF_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00003124 /* Amateur Radio X.25 PLP */
3125 PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003126#endif
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00003127#ifdef HAVE_NETPACKET_PACKET_H
Fred Drake4baedc12002-04-01 14:53:37 +00003128 PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET);
3129 PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET);
3130 PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST);
3131 PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST);
3132 PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST);
3133 PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST);
3134 PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING);
3135 PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK);
3136 PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00003137#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003138
3139 /* Socket types */
Fred Drake4baedc12002-04-01 14:53:37 +00003140 PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
3141 PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00003142#ifndef __BEOS__
3143/* We have incomplete socket support. */
Fred Drake4baedc12002-04-01 14:53:37 +00003144 PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
3145 PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
3146 PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00003147#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003148
3149#ifdef SO_DEBUG
Fred Drake4baedc12002-04-01 14:53:37 +00003150 PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003151#endif
3152#ifdef SO_ACCEPTCONN
Fred Drake4baedc12002-04-01 14:53:37 +00003153 PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003154#endif
3155#ifdef SO_REUSEADDR
Fred Drake4baedc12002-04-01 14:53:37 +00003156 PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003157#endif
3158#ifdef SO_KEEPALIVE
Fred Drake4baedc12002-04-01 14:53:37 +00003159 PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003160#endif
3161#ifdef SO_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00003162 PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003163#endif
3164#ifdef SO_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00003165 PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003166#endif
3167#ifdef SO_USELOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00003168 PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003169#endif
3170#ifdef SO_LINGER
Fred Drake4baedc12002-04-01 14:53:37 +00003171 PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003172#endif
3173#ifdef SO_OOBINLINE
Fred Drake4baedc12002-04-01 14:53:37 +00003174 PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003175#endif
3176#ifdef SO_REUSEPORT
Fred Drake4baedc12002-04-01 14:53:37 +00003177 PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003178#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003179#ifdef SO_SNDBUF
Fred Drake4baedc12002-04-01 14:53:37 +00003180 PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003181#endif
3182#ifdef SO_RCVBUF
Fred Drake4baedc12002-04-01 14:53:37 +00003183 PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003184#endif
3185#ifdef SO_SNDLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00003186 PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003187#endif
3188#ifdef SO_RCVLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00003189 PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003190#endif
3191#ifdef SO_SNDTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00003192 PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003193#endif
3194#ifdef SO_RCVTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00003195 PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003196#endif
3197#ifdef SO_ERROR
Fred Drake4baedc12002-04-01 14:53:37 +00003198 PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003199#endif
3200#ifdef SO_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00003201 PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003202#endif
3203
3204 /* Maximum number of connections for "listen" */
3205#ifdef SOMAXCONN
Fred Drake4baedc12002-04-01 14:53:37 +00003206 PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003207#else
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003208 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003209#endif
3210
3211 /* Flags for send, recv */
3212#ifdef MSG_OOB
Fred Drake4baedc12002-04-01 14:53:37 +00003213 PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003214#endif
3215#ifdef MSG_PEEK
Fred Drake4baedc12002-04-01 14:53:37 +00003216 PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003217#endif
3218#ifdef MSG_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00003219 PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003220#endif
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00003221#ifdef MSG_DONTWAIT
Fred Drake4baedc12002-04-01 14:53:37 +00003222 PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00003223#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003224#ifdef MSG_EOR
Fred Drake4baedc12002-04-01 14:53:37 +00003225 PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003226#endif
3227#ifdef MSG_TRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00003228 PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003229#endif
3230#ifdef MSG_CTRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00003231 PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003232#endif
3233#ifdef MSG_WAITALL
Fred Drake4baedc12002-04-01 14:53:37 +00003234 PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003235#endif
3236#ifdef MSG_BTAG
Fred Drake4baedc12002-04-01 14:53:37 +00003237 PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003238#endif
3239#ifdef MSG_ETAG
Fred Drake4baedc12002-04-01 14:53:37 +00003240 PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003241#endif
3242
3243 /* Protocol level and numbers, usable for [gs]etsockopt */
3244#ifdef SOL_SOCKET
Fred Drake4baedc12002-04-01 14:53:37 +00003245 PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003246#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003247#ifdef SOL_IP
Fred Drake4baedc12002-04-01 14:53:37 +00003248 PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003249#else
Fred Drake4baedc12002-04-01 14:53:37 +00003250 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00003251#endif
3252#ifdef SOL_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00003253 PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00003254#endif
3255#ifdef SOL_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00003256 PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003257#endif
3258#ifdef SOL_ATALK
Fred Drake4baedc12002-04-01 14:53:37 +00003259 PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00003260#endif
3261#ifdef SOL_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00003262 PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00003263#endif
3264#ifdef SOL_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00003265 PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003266#endif
3267#ifdef SOL_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00003268 PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003269#else
Fred Drake4baedc12002-04-01 14:53:37 +00003270 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00003271#endif
3272#ifdef SOL_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00003273 PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003274#else
Fred Drake4baedc12002-04-01 14:53:37 +00003275 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00003276#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003277#ifdef IPPROTO_IP
Fred Drake4baedc12002-04-01 14:53:37 +00003278 PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00003279#else
Fred Drake4baedc12002-04-01 14:53:37 +00003280 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003281#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003282#ifdef IPPROTO_HOPOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003283 PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003284#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003285#ifdef IPPROTO_ICMP
Fred Drake4baedc12002-04-01 14:53:37 +00003286 PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00003287#else
Fred Drake4baedc12002-04-01 14:53:37 +00003288 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003289#endif
3290#ifdef IPPROTO_IGMP
Fred Drake4baedc12002-04-01 14:53:37 +00003291 PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003292#endif
3293#ifdef IPPROTO_GGP
Fred Drake4baedc12002-04-01 14:53:37 +00003294 PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003295#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003296#ifdef IPPROTO_IPV4
Fred Drake4baedc12002-04-01 14:53:37 +00003297 PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003298#endif
3299#ifdef IPPROTO_IPIP
Fred Drake4baedc12002-04-01 14:53:37 +00003300 PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003301#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003302#ifdef IPPROTO_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00003303 PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00003304#else
Fred Drake4baedc12002-04-01 14:53:37 +00003305 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003306#endif
3307#ifdef IPPROTO_EGP
Fred Drake4baedc12002-04-01 14:53:37 +00003308 PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003309#endif
3310#ifdef IPPROTO_PUP
Fred Drake4baedc12002-04-01 14:53:37 +00003311 PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003312#endif
3313#ifdef IPPROTO_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00003314 PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00003315#else
Fred Drake4baedc12002-04-01 14:53:37 +00003316 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003317#endif
3318#ifdef IPPROTO_IDP
Fred Drake4baedc12002-04-01 14:53:37 +00003319 PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003320#endif
3321#ifdef IPPROTO_HELLO
Fred Drake4baedc12002-04-01 14:53:37 +00003322 PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003323#endif
3324#ifdef IPPROTO_ND
Fred Drake4baedc12002-04-01 14:53:37 +00003325 PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003326#endif
3327#ifdef IPPROTO_TP
Fred Drake4baedc12002-04-01 14:53:37 +00003328 PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003329#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003330#ifdef IPPROTO_IPV6
Fred Drake4baedc12002-04-01 14:53:37 +00003331 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003332#endif
3333#ifdef IPPROTO_ROUTING
Fred Drake4baedc12002-04-01 14:53:37 +00003334 PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003335#endif
3336#ifdef IPPROTO_FRAGMENT
Fred Drake4baedc12002-04-01 14:53:37 +00003337 PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003338#endif
3339#ifdef IPPROTO_RSVP
Fred Drake4baedc12002-04-01 14:53:37 +00003340 PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003341#endif
3342#ifdef IPPROTO_GRE
Fred Drake4baedc12002-04-01 14:53:37 +00003343 PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003344#endif
3345#ifdef IPPROTO_ESP
Fred Drake4baedc12002-04-01 14:53:37 +00003346 PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003347#endif
3348#ifdef IPPROTO_AH
Fred Drake4baedc12002-04-01 14:53:37 +00003349 PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003350#endif
3351#ifdef IPPROTO_MOBILE
Fred Drake4baedc12002-04-01 14:53:37 +00003352 PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003353#endif
3354#ifdef IPPROTO_ICMPV6
Fred Drake4baedc12002-04-01 14:53:37 +00003355 PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003356#endif
3357#ifdef IPPROTO_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00003358 PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003359#endif
3360#ifdef IPPROTO_DSTOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003361 PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003362#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003363#ifdef IPPROTO_XTP
Fred Drake4baedc12002-04-01 14:53:37 +00003364 PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003365#endif
3366#ifdef IPPROTO_EON
Fred Drake4baedc12002-04-01 14:53:37 +00003367 PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003368#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003369#ifdef IPPROTO_PIM
Fred Drake4baedc12002-04-01 14:53:37 +00003370 PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003371#endif
3372#ifdef IPPROTO_IPCOMP
Fred Drake4baedc12002-04-01 14:53:37 +00003373 PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003374#endif
3375#ifdef IPPROTO_VRRP
Fred Drake4baedc12002-04-01 14:53:37 +00003376 PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003377#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003378#ifdef IPPROTO_BIP
Fred Drake4baedc12002-04-01 14:53:37 +00003379 PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003380#endif
3381/**/
3382#ifdef IPPROTO_RAW
Fred Drake4baedc12002-04-01 14:53:37 +00003383 PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00003384#else
Fred Drake4baedc12002-04-01 14:53:37 +00003385 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003386#endif
3387#ifdef IPPROTO_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00003388 PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003389#endif
3390
3391 /* Some port configuration */
3392#ifdef IPPORT_RESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00003393 PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003394#else
Fred Drake4baedc12002-04-01 14:53:37 +00003395 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003396#endif
3397#ifdef IPPORT_USERRESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00003398 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003399#else
Fred Drake4baedc12002-04-01 14:53:37 +00003400 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003401#endif
3402
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003403 /* Some reserved IP v.4 addresses */
3404#ifdef INADDR_ANY
Fred Drake4baedc12002-04-01 14:53:37 +00003405 PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003406#else
Fred Drake4baedc12002-04-01 14:53:37 +00003407 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003408#endif
3409#ifdef INADDR_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00003410 PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003411#else
Fred Drake4baedc12002-04-01 14:53:37 +00003412 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003413#endif
3414#ifdef INADDR_LOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00003415 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003416#else
Fred Drake4baedc12002-04-01 14:53:37 +00003417 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003418#endif
3419#ifdef INADDR_UNSPEC_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003420 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003421#else
Fred Drake4baedc12002-04-01 14:53:37 +00003422 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003423#endif
3424#ifdef INADDR_ALLHOSTS_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003425 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
3426 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003427#else
Fred Drake4baedc12002-04-01 14:53:37 +00003428 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003429#endif
3430#ifdef INADDR_MAX_LOCAL_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003431 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
3432 INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003433#else
Fred Drake4baedc12002-04-01 14:53:37 +00003434 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003435#endif
3436#ifdef INADDR_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00003437 PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003438#else
Fred Drake4baedc12002-04-01 14:53:37 +00003439 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00003440#endif
3441
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003442 /* IPv4 [gs]etsockopt options */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003443#ifdef IP_OPTIONS
Fred Drake4baedc12002-04-01 14:53:37 +00003444 PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003445#endif
3446#ifdef IP_HDRINCL
Fred Drake4baedc12002-04-01 14:53:37 +00003447 PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003448#endif
3449#ifdef IP_TOS
Fred Drake4baedc12002-04-01 14:53:37 +00003450 PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003451#endif
3452#ifdef IP_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00003453 PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003454#endif
3455#ifdef IP_RECVOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003456 PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003457#endif
3458#ifdef IP_RECVRETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003459 PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003460#endif
3461#ifdef IP_RECVDSTADDR
Fred Drake4baedc12002-04-01 14:53:37 +00003462 PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003463#endif
3464#ifdef IP_RETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00003465 PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003466#endif
3467#ifdef IP_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00003468 PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003469#endif
3470#ifdef IP_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00003471 PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003472#endif
3473#ifdef IP_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00003474 PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003475#endif
3476#ifdef IP_ADD_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00003477 PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003478#endif
3479#ifdef IP_DROP_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00003480 PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00003481#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003482#ifdef IP_DEFAULT_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00003483 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
3484 IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00003485#endif
3486#ifdef IP_DEFAULT_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00003487 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
3488 IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00003489#endif
3490#ifdef IP_MAX_MEMBERSHIPS
Fred Drake4baedc12002-04-01 14:53:37 +00003491 PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00003492#endif
3493
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003494 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
3495#ifdef IPV6_JOIN_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003496 PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003497#endif
3498#ifdef IPV6_LEAVE_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00003499 PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003500#endif
3501#ifdef IPV6_MULTICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00003502 PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003503#endif
3504#ifdef IPV6_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00003505 PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003506#endif
3507#ifdef IPV6_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00003508 PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003509#endif
3510#ifdef IPV6_UNICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00003511 PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003512#endif
3513
Guido van Rossum09be4091999-08-09 14:40:40 +00003514 /* TCP options */
3515#ifdef TCP_NODELAY
Fred Drake4baedc12002-04-01 14:53:37 +00003516 PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00003517#endif
3518#ifdef TCP_MAXSEG
Fred Drake4baedc12002-04-01 14:53:37 +00003519 PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00003520#endif
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003521#ifdef TCP_CORK
Fred Drake4baedc12002-04-01 14:53:37 +00003522 PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003523#endif
3524#ifdef TCP_KEEPIDLE
Fred Drake4baedc12002-04-01 14:53:37 +00003525 PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003526#endif
3527#ifdef TCP_KEEPINTVL
Fred Drake4baedc12002-04-01 14:53:37 +00003528 PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003529#endif
3530#ifdef TCP_KEEPCNT
Fred Drake4baedc12002-04-01 14:53:37 +00003531 PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003532#endif
3533#ifdef TCP_SYNCNT
Fred Drake4baedc12002-04-01 14:53:37 +00003534 PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003535#endif
3536#ifdef TCP_LINGER2
Fred Drake4baedc12002-04-01 14:53:37 +00003537 PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003538#endif
3539#ifdef TCP_DEFER_ACCEPT
Fred Drake4baedc12002-04-01 14:53:37 +00003540 PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003541#endif
3542#ifdef TCP_WINDOW_CLAMP
Fred Drake4baedc12002-04-01 14:53:37 +00003543 PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003544#endif
3545#ifdef TCP_INFO
Fred Drake4baedc12002-04-01 14:53:37 +00003546 PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003547#endif
3548#ifdef TCP_QUICKACK
Fred Drake4baedc12002-04-01 14:53:37 +00003549 PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00003550#endif
3551
Guido van Rossum09be4091999-08-09 14:40:40 +00003552
3553 /* IPX options */
3554#ifdef IPX_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00003555 PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003556#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003557
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003558 /* get{addr,name}info parameters */
3559#ifdef EAI_ADDRFAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00003560 PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003561#endif
3562#ifdef EAI_AGAIN
Fred Drake4baedc12002-04-01 14:53:37 +00003563 PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003564#endif
3565#ifdef EAI_BADFLAGS
Fred Drake4baedc12002-04-01 14:53:37 +00003566 PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003567#endif
3568#ifdef EAI_FAIL
Fred Drake4baedc12002-04-01 14:53:37 +00003569 PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003570#endif
3571#ifdef EAI_FAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00003572 PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003573#endif
3574#ifdef EAI_MEMORY
Fred Drake4baedc12002-04-01 14:53:37 +00003575 PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003576#endif
3577#ifdef EAI_NODATA
Fred Drake4baedc12002-04-01 14:53:37 +00003578 PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003579#endif
3580#ifdef EAI_NONAME
Fred Drake4baedc12002-04-01 14:53:37 +00003581 PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003582#endif
3583#ifdef EAI_SERVICE
Fred Drake4baedc12002-04-01 14:53:37 +00003584 PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003585#endif
3586#ifdef EAI_SOCKTYPE
Fred Drake4baedc12002-04-01 14:53:37 +00003587 PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003588#endif
3589#ifdef EAI_SYSTEM
Fred Drake4baedc12002-04-01 14:53:37 +00003590 PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003591#endif
3592#ifdef EAI_BADHINTS
Fred Drake4baedc12002-04-01 14:53:37 +00003593 PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003594#endif
3595#ifdef EAI_PROTOCOL
Fred Drake4baedc12002-04-01 14:53:37 +00003596 PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003597#endif
3598#ifdef EAI_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00003599 PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003600#endif
3601#ifdef AI_PASSIVE
Fred Drake4baedc12002-04-01 14:53:37 +00003602 PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003603#endif
3604#ifdef AI_CANONNAME
Fred Drake4baedc12002-04-01 14:53:37 +00003605 PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003606#endif
3607#ifdef AI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00003608 PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003609#endif
3610#ifdef AI_MASK
Fred Drake4baedc12002-04-01 14:53:37 +00003611 PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003612#endif
3613#ifdef AI_ALL
Fred Drake4baedc12002-04-01 14:53:37 +00003614 PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003615#endif
3616#ifdef AI_V4MAPPED_CFG
Fred Drake4baedc12002-04-01 14:53:37 +00003617 PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003618#endif
3619#ifdef AI_ADDRCONFIG
Fred Drake4baedc12002-04-01 14:53:37 +00003620 PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003621#endif
3622#ifdef AI_V4MAPPED
Fred Drake4baedc12002-04-01 14:53:37 +00003623 PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003624#endif
3625#ifdef AI_DEFAULT
Fred Drake4baedc12002-04-01 14:53:37 +00003626 PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003627#endif
3628#ifdef NI_MAXHOST
Fred Drake4baedc12002-04-01 14:53:37 +00003629 PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003630#endif
3631#ifdef NI_MAXSERV
Fred Drake4baedc12002-04-01 14:53:37 +00003632 PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003633#endif
3634#ifdef NI_NOFQDN
Fred Drake4baedc12002-04-01 14:53:37 +00003635 PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003636#endif
3637#ifdef NI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00003638 PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003639#endif
3640#ifdef NI_NAMEREQD
Fred Drake4baedc12002-04-01 14:53:37 +00003641 PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003642#endif
3643#ifdef NI_NUMERICSERV
Fred Drake4baedc12002-04-01 14:53:37 +00003644 PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003645#endif
3646#ifdef NI_DGRAM
Fred Drake4baedc12002-04-01 14:53:37 +00003647 PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003648#endif
3649
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003650 /* Initialize gethostbyname lock */
Guido van Rossum3baaa131999-03-22 21:44:51 +00003651#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum65d5b571998-12-21 19:32:43 +00003652 gethostbyname_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003653#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003654}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003655
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003656
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003657#ifndef HAVE_INET_PTON
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003658
3659/* Simplistic emulation code for inet_pton that only works for IPv4 */
3660
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003661int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003662inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003663{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003664 if (af == AF_INET) {
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003665 long packed_addr;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003666 packed_addr = inet_addr(src);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003667 if (packed_addr == INADDR_NONE)
3668 return 0;
3669 memcpy(dst, &packed_addr, 4);
3670 return 1;
3671 }
3672 /* Should set errno to EAFNOSUPPORT */
3673 return -1;
3674}
3675
Martin v. Löwisc925b1532001-07-21 09:42:15 +00003676const char *
3677inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003678{
3679 if (af == AF_INET) {
3680 struct in_addr packed_addr;
3681 if (size < 16)
3682 /* Should set errno to ENOSPC. */
3683 return NULL;
3684 memcpy(&packed_addr, src, sizeof(packed_addr));
3685 return strncpy(dst, inet_ntoa(packed_addr), size);
3686 }
3687 /* Should set errno to EAFNOSUPPORT */
3688 return NULL;
3689}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003690
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00003691#endif