blob: 2c0fc56d747b5e0be625acfac54784d26220a83f [file] [log] [blame]
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum6574b3e1991-06-25 21:36:08 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum6574b3e1991-06-25 21:36:08 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum6574b3e1991-06-25 21:36:08 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum6574b3e1991-06-25 21:36:08 +000029
30******************************************************************/
31
32/* Socket module */
33
Guido van Rossum09c8b6c1999-12-07 21:37:17 +000034/* SSL support based on patches by Brian E Gallew and Laszlo Kovacs */
35
Guido van Rossum6574b3e1991-06-25 21:36:08 +000036/*
37This module provides an interface to Berkeley socket IPC.
38
39Limitations:
40
Guido van Rossum30a685f1991-06-27 15:51:29 +000041- only AF_INET and AF_UNIX address families are supported
Guido van Rossum81194471991-07-27 21:42:02 +000042- no read/write operations (use send/recv or makefile instead)
Guido van Rossumbe32c891996-06-20 16:25:29 +000043- additional restrictions apply on Windows
Guido van Rossum6574b3e1991-06-25 21:36:08 +000044
Guido van Rossum27e177d1995-03-16 15:43:47 +000045Module interface:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000046
Guido van Rossum27e177d1995-03-16 15:43:47 +000047- socket.error: exception raised for socket specific errors
Guido van Rossum30a685f1991-06-27 15:51:29 +000048- socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd')
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000049- socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...])
Guido van Rossum27e177d1995-03-16 15:43:47 +000050- socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com')
Guido van Rossum25405c71996-12-19 16:42:52 +000051- socket.getprotobyname(protocolname) --> protocol number
Guido van Rossum27e177d1995-03-16 15:43:47 +000052- socket.getservbyname(servicename, protocolname) --> port number
Guido van Rossum4dd2a7e1991-07-01 18:51:33 +000053- socket.socket(family, type [, proto]) --> new socket object
Guido van Rossum006bf911996-06-12 04:04:55 +000054- socket.ntohs(16 bit value) --> new int object
55- socket.ntohl(32 bit value) --> new int object
56- socket.htons(16 bit value) --> new int object
57- socket.htonl(32 bit value) --> new int object
Guido van Rossum27e177d1995-03-16 15:43:47 +000058- socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +000059- socket.inet_aton(IP address) -> 32-bit packed IP representation
60- socket.inet_ntoa(packed IP) -> IP address string
Guido van Rossum09c8b6c1999-12-07 21:37:17 +000061- socket.ssl(socket, keyfile, certfile) -> new ssl object
Guido van Rossum6574b3e1991-06-25 21:36:08 +000062- an Internet socket address is a pair (hostname, port)
63 where hostname can be anything recognized by gethostbyname()
64 (including the dd.dd.dd.dd notation) and port is in host byte order
65- where a hostname is returned, the dd.dd.dd.dd notation is used
Guido van Rossum27e177d1995-03-16 15:43:47 +000066- a UNIX domain socket address is a string specifying the pathname
Guido van Rossum6574b3e1991-06-25 21:36:08 +000067
Guido van Rossum30a685f1991-06-27 15:51:29 +000068Socket methods:
Guido van Rossum527c9bd2000-04-04 14:10:20 +000069(NB: an argument list of the form (sockaddr...) means that multiple
70arguments are treated the same as a single tuple argument, for backwards
Guido van Rossumb49dc752000-04-04 15:09:07 +000071compatibility. This is deprecated, and will be removed in future versions.)
Guido van Rossum6574b3e1991-06-25 21:36:08 +000072
Guido van Rossum81194471991-07-27 21:42:02 +000073- s.accept() --> new socket object, sockaddr
Guido van Rossum527c9bd2000-04-04 14:10:20 +000074- s.bind(sockaddr...) --> None
Guido van Rossum27e177d1995-03-16 15:43:47 +000075- s.close() --> None
Guido van Rossum527c9bd2000-04-04 14:10:20 +000076- s.connect(sockaddr...) --> None
77- s.connect_ex(sockaddr...) --> 0 or errno (handy for e.g. async connect)
Guido van Rossum27e177d1995-03-16 15:43:47 +000078- s.fileno() --> file descriptor
Guido van Rossumbe32c891996-06-20 16:25:29 +000079- s.dup() --> same as socket.fromfd(os.dup(s.fileno(), ...)
Guido van Rossumc89705d1992-11-26 08:54:07 +000080- s.getpeername() --> sockaddr
Guido van Rossum27e177d1995-03-16 15:43:47 +000081- s.getsockname() --> sockaddr
82- s.getsockopt(level, optname[, buflen]) --> int or string
83- s.listen(backlog) --> None
Guido van Rossum6b144911995-03-14 15:05:13 +000084- s.makefile([mode[, bufsize]]) --> file object
Guido van Rossum27e177d1995-03-16 15:43:47 +000085- s.recv(buflen [,flags]) --> string
86- s.recvfrom(buflen [,flags]) --> string, sockaddr
Guido van Rossumb6775db1994-08-01 11:34:53 +000087- s.send(string [,flags]) --> nbytes
88- s.sendto(string, [flags,] sockaddr) --> nbytes
Guido van Rossum27e177d1995-03-16 15:43:47 +000089- s.setblocking(0 | 1) --> None
90- s.setsockopt(level, optname, value) --> None
91- s.shutdown(how) --> None
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000092- repr(s) --> "<socket object, fd=%d, family=%d, type=%d, protocol=%d>"
Guido van Rossum30a685f1991-06-27 15:51:29 +000093
Guido van Rossum6574b3e1991-06-25 21:36:08 +000094*/
95
Guido van Rossum73624e91994-10-10 17:59:00 +000096#include "Python.h"
Guido van Rossum3baaa131999-03-22 21:44:51 +000097
Guido van Rossum9376b741999-09-15 22:01:40 +000098/* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure
99 script doesn't get this right, so we hardcode some platform checks below.
100 On the other hand, not all Linux versions agree, so there the settings
101 computed by the configure script are needed! */
102
103#ifndef linux
Guido van Rossume7de2061999-03-24 17:24:33 +0000104#undef HAVE_GETHOSTBYNAME_R_3_ARG
105#undef HAVE_GETHOSTBYNAME_R_5_ARG
106#undef HAVE_GETHOSTBYNAME_R_6_ARG
Guido van Rossum9376b741999-09-15 22:01:40 +0000107#endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000108
Guido van Rossum7a122991999-04-13 04:07:32 +0000109#ifndef WITH_THREAD
110#undef HAVE_GETHOSTBYNAME_R
111#endif
112
Guido van Rossume7de2061999-03-24 17:24:33 +0000113#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7a122991999-04-13 04:07:32 +0000114#if defined(_AIX) || defined(__osf__)
Guido van Rossume7de2061999-03-24 17:24:33 +0000115#define HAVE_GETHOSTBYNAME_R_3_ARG
116#elif defined(__sun__) || defined(__sgi)
117#define HAVE_GETHOSTBYNAME_R_5_ARG
118#elif defined(linux)
Guido van Rossum9376b741999-09-15 22:01:40 +0000119/* Rely on the configure script */
Guido van Rossume7de2061999-03-24 17:24:33 +0000120#else
121#undef HAVE_GETHOSTBYNAME_R
122#endif
123#endif
124
Guido van Rossum3baaa131999-03-22 21:44:51 +0000125#if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && !defined(MS_WINDOWS)
126#define USE_GETHOSTBYNAME_LOCK
127#endif
128
129#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum49b56061998-10-01 20:42:43 +0000130#include "pythread.h"
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000131#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000132
Guido van Rossuma376cc51996-12-05 23:43:35 +0000133#ifdef HAVE_UNISTD_H
134#include <unistd.h>
135#endif
136
Guido van Rossumbcc20741998-08-04 22:53:56 +0000137#if !defined(MS_WINDOWS) && !defined(PYOS_OS2) && !defined(__BEOS__)
Guido van Rossumfb4130d1996-12-10 15:17:08 +0000138extern int gethostname(); /* For Solaris, at least */
139#endif
140
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000141#if defined(PYCC_VACPP)
142#include <types.h>
143#include <io.h>
144#include <sys/ioctl.h>
145#include <utils.h>
146#include <ctype.h>
147#endif
148
149#if defined(PYOS_OS2)
150#define INCL_DOS
151#define INCL_DOSERRORS
152#define INCL_NOPMAPI
153#include <os2.h>
154#endif
155
Guido van Rossumbcc20741998-08-04 22:53:56 +0000156#if defined(__BEOS__)
157/* It's in the libs, but not the headers... - [cjh] */
158int shutdown( int, int );
159#endif
160
Guido van Rossumb6775db1994-08-01 11:34:53 +0000161#include <sys/types.h>
162#include "mytime.h"
Guido van Rossumed233a51992-06-23 09:07:03 +0000163
Guido van Rossum81194471991-07-27 21:42:02 +0000164#include <signal.h>
Guido van Rossum6f489d91996-06-28 20:15:15 +0000165#ifndef MS_WINDOWS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000166#include <netdb.h>
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000167#include <sys/socket.h>
168#include <netinet/in.h>
Fred Drake11b09362000-05-16 13:30:12 +0000169#ifndef __BEOS__
Andrew M. Kuchling52a644c2000-03-31 17:24:30 +0000170#include <netinet/tcp.h>
Fred Drake11b09362000-05-16 13:30:12 +0000171#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000172
Guido van Rossum9376b741999-09-15 22:01:40 +0000173/* Headers needed for inet_ntoa() and inet_addr() */
174#ifdef __BEOS__
175#include <net/netdb.h>
176#else
Guido van Rossumff3ab422000-04-24 15:16:03 +0000177#ifndef USE_GUSI1
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000178#include <arpa/inet.h>
Guido van Rossum9376b741999-09-15 22:01:40 +0000179#endif
Guido van Rossum1b6e4631999-11-04 18:22:29 +0000180#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000181
Guido van Rossume4485b01994-09-07 14:32:49 +0000182#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000183#else
184#include <winsock.h>
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000185#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000186#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000187#ifdef HAVE_SYS_UN_H
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000188#include <sys/un.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000189#else
190#undef AF_UNIX
Guido van Rossum9575a441993-04-07 14:06:14 +0000191#endif
192
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000193#ifndef O_NDELAY
194#define O_NDELAY O_NONBLOCK /* For QNX only? */
195#endif
196
Guido van Rossumff3ab422000-04-24 15:16:03 +0000197#ifdef USE_GUSI1
Jack Jansen508537b1996-02-14 15:57:45 +0000198/* fdopen() isn't declared in stdio.h (sigh) */
199#include <GUSI.h>
200#endif
201
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000202#ifdef USE_SSL
203#include "rsa.h"
204#include "crypto.h"
205#include "x509.h"
206#include "pem.h"
207#include "ssl.h"
208#include "err.h"
209#endif /* USE_SSL */
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000210
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000211/* Here we have some hacks to choose between K&R or ANSI style function
212 definitions. For NT to build this as an extension module (ie, DLL)
213 it must be compiled by the C++ compiler, as it takes the address of
214 a static data item exported from the main Python DLL.
215*/
Guido van Rossumbcc20741998-08-04 22:53:56 +0000216#if defined(MS_WINDOWS) || defined(__BEOS__)
217/* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000218/* seem to be a few differences in the API */
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000219#define SOCKETCLOSE closesocket
Guido van Rossumbe32c891996-06-20 16:25:29 +0000220#define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000221#define FORCE_ANSI_FUNC_DEFS
222#endif
223
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000224#if defined(PYOS_OS2)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000225#define SOCKETCLOSE soclose
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000226#define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
227#define FORCE_ANSI_FUNC_DEFS
228#endif
229
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000230#ifndef SOCKETCLOSE
231#define SOCKETCLOSE close
232#endif
233
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000234#ifdef FORCE_ANSI_FUNC_DEFS
235#define BUILD_FUNC_DEF_1( fnname, arg1type, arg1name ) \
236fnname( arg1type arg1name )
237
238#define BUILD_FUNC_DEF_2( fnname, arg1type, arg1name, arg2type, arg2name ) \
239fnname( arg1type arg1name, arg2type arg2name )
240
241#define BUILD_FUNC_DEF_3( fnname, arg1type, arg1name, arg2type, arg2name , arg3type, arg3name ) \
242fnname( arg1type arg1name, arg2type arg2name, arg3type arg3name )
243
244#define BUILD_FUNC_DEF_4( fnname, arg1type, arg1name, arg2type, arg2name , arg3type, arg3name, arg4type, arg4name ) \
245fnname( arg1type arg1name, arg2type arg2name, arg3type arg3name, arg4type arg4name )
246
247#else /* !FORCE_ANSI_FN_DEFS */
248#define BUILD_FUNC_DEF_1( fnname, arg1type, arg1name ) \
249fnname( arg1name ) \
250 arg1type arg1name;
251
252#define BUILD_FUNC_DEF_2( fnname, arg1type, arg1name, arg2type, arg2name ) \
253fnname( arg1name, arg2name ) \
254 arg1type arg1name; \
255 arg2type arg2name;
256
257#define BUILD_FUNC_DEF_3( fnname, arg1type, arg1name, arg2type, arg2name, arg3type, arg3name ) \
258fnname( arg1name, arg2name, arg3name ) \
259 arg1type arg1name; \
260 arg2type arg2name; \
261 arg3type arg3name;
262
263#define BUILD_FUNC_DEF_4( fnname, arg1type, arg1name, arg2type, arg2name, arg3type, arg3name, arg4type, arg4name ) \
264fnname( arg1name, arg2name, arg3name, arg4name ) \
265 arg1type arg1name; \
266 arg2type arg2name; \
267 arg3type arg3name; \
268 arg4type arg4name;
269
270#endif /* !FORCE_ANSI_FN_DEFS */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000271
272/* Global variable holding the exception type for errors detected
273 by this module (but not argument type or memory errors, etc.). */
274
Guido van Rossum73624e91994-10-10 17:59:00 +0000275static PyObject *PySocket_Error;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000276
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000277#ifdef USE_SSL
278static PyObject *SSLErrorObject;
279#endif /* USE_SSL */
280
Guido van Rossum30a685f1991-06-27 15:51:29 +0000281
282/* Convenience function to raise an error according to errno
283 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000284
Guido van Rossum73624e91994-10-10 17:59:00 +0000285static PyObject *
286PySocket_Err()
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000287{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000288#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000289 if (WSAGetLastError()) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000290 PyObject *v;
291 v = Py_BuildValue("(is)", WSAGetLastError(), "winsock error");
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000292 if (v != NULL) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000293 PyErr_SetObject(PySocket_Error, v);
294 Py_DECREF(v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000295 }
296 return NULL;
297 }
298 else
299#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000300
301#if defined(PYOS_OS2)
302 if (sock_errno() != NO_ERROR) {
303 APIRET rc;
304 ULONG msglen;
305 char outbuf[100];
306 int myerrorcode = sock_errno();
307
308 /* Retrieve Socket-Related Error Message from MPTN.MSG File */
309 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
310 myerrorcode - SOCBASEERR + 26, "mptn.msg", &msglen);
311 if (rc == NO_ERROR) {
312 PyObject *v;
313
314 outbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
315 if (strlen(outbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
316 char *lastc = &outbuf[ strlen(outbuf)-1 ];
317 while (lastc > outbuf && isspace(*lastc))
318 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
319 }
320 v = Py_BuildValue("(is)", myerrorcode, outbuf);
321 if (v != NULL) {
322 PyErr_SetObject(PySocket_Error, v);
323 Py_DECREF(v);
324 }
325 return NULL;
326 }
327 }
328#endif
329
Guido van Rossum73624e91994-10-10 17:59:00 +0000330 return PyErr_SetFromErrno(PySocket_Error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000331}
332
Guido van Rossum30a685f1991-06-27 15:51:29 +0000333
334/* The object holding a socket. It holds some extra information,
335 like the address family, which is used to decode socket address
336 arguments properly. */
337
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000338typedef struct {
Guido van Rossum73624e91994-10-10 17:59:00 +0000339 PyObject_HEAD
Guido van Rossum30a685f1991-06-27 15:51:29 +0000340 int sock_fd; /* Socket file descriptor */
341 int sock_family; /* Address family, e.g., AF_INET */
342 int sock_type; /* Socket type, e.g., SOCK_STREAM */
343 int sock_proto; /* Protocol type, usually 0 */
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000344 union sock_addr {
345 struct sockaddr_in in;
346#ifdef AF_UNIX
347 struct sockaddr_un un;
348#endif
349 } sock_addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000350} PySocketSockObject;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000351
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000352#ifdef USE_SSL
Guido van Rossum30a685f1991-06-27 15:51:29 +0000353
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000354typedef struct {
355 PyObject_HEAD
356 PySocketSockObject *Socket; /* Socket on which we're layered */
357 PyObject *x_attr; /* Attributes dictionary */
358 SSL_CTX* ctx;
359 SSL* ssl;
360 X509* server_cert;
361 BIO* sbio;
362 char server[256];
363 char issuer[256];
364
365} SSLObject;
366
367staticforward PyTypeObject SSL_Type;
368staticforward int SSL_setattr(SSLObject *self, char *name, PyObject *v);
369staticforward PyObject *SSL_SSLwrite(SSLObject *self, PyObject *args);
370staticforward PyObject *SSL_SSLread(SSLObject *self, PyObject *args);
371
372#define SSLObject_Check(v) ((v)->ob_type == &SSL_Type)
373
374#endif /* USE_SSL */
375
Guido van Rossum30a685f1991-06-27 15:51:29 +0000376/* A forward reference to the Socktype type object.
377 The Socktype variable contains pointers to various functions,
Guido van Rossum73624e91994-10-10 17:59:00 +0000378 some of which call newsockobject(), which uses Socktype, so
Guido van Rossum54ba21b1991-09-10 14:57:12 +0000379 there has to be a circular reference. */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000380
Guido van Rossum73624e91994-10-10 17:59:00 +0000381staticforward PyTypeObject PySocketSock_Type;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000382
Guido van Rossum30a685f1991-06-27 15:51:29 +0000383
384/* Create a new socket object.
385 This just creates the object and initializes it.
386 If the creation fails, return NULL and set an exception (implicit
387 in NEWOBJ()). */
388
Guido van Rossum73624e91994-10-10 17:59:00 +0000389static PySocketSockObject *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000390BUILD_FUNC_DEF_4(PySocketSock_New,int,fd, int,family, int,type, int,proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000391{
Guido van Rossum73624e91994-10-10 17:59:00 +0000392 PySocketSockObject *s;
Guido van Rossum55558541996-05-23 22:54:50 +0000393 PySocketSock_Type.ob_type = &PyType_Type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000394 s = PyObject_New(PySocketSockObject, &PySocketSock_Type);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000395 if (s != NULL) {
396 s->sock_fd = fd;
397 s->sock_family = family;
398 s->sock_type = type;
399 s->sock_proto = proto;
400 }
401 return s;
402}
403
Guido van Rossum30a685f1991-06-27 15:51:29 +0000404
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000405/* Lock to allow python interpreter to continue, but only allow one
406 thread to be in gethostbyname */
Guido van Rossum3baaa131999-03-22 21:44:51 +0000407#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum65d5b571998-12-21 19:32:43 +0000408PyThread_type_lock gethostbyname_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000409#endif
410
411
Guido van Rossum30a685f1991-06-27 15:51:29 +0000412/* Convert a string specifying a host name or one of a few symbolic
413 names to a numeric IP address. This usually calls gethostbyname()
414 to do the work; the names "" and "<broadcast>" are special.
415 Return the length (should always be 4 bytes), or negative if
416 an error occurred; then an exception is raised. */
417
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000418static int
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000419BUILD_FUNC_DEF_2(setipaddr, char*,name, struct sockaddr_in *,addr_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000420{
421 struct hostent *hp;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000422 int d1, d2, d3, d4;
Guido van Rossum955becc1999-03-22 20:14:53 +0000423 int h_length;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000424 char ch;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000425#ifdef HAVE_GETHOSTBYNAME_R
426 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +0000427#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
428 struct hostent_data data;
429#else
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000430 char buf[1001];
431 int buf_len = (sizeof buf) - 1;
432 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +0000433#endif
434#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +0000435 int result;
436#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000437#endif /* HAVE_GETHOSTBYNAME_R */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000438
Guido van Rossuma376cc51996-12-05 23:43:35 +0000439 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
Guido van Rossum30a685f1991-06-27 15:51:29 +0000440 if (name[0] == '\0') {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000441 addr_ret->sin_addr.s_addr = INADDR_ANY;
442 return 4;
443 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000444 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000445 addr_ret->sin_addr.s_addr = INADDR_BROADCAST;
446 return 4;
447 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000448 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
449 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
450 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
451 addr_ret->sin_addr.s_addr = htonl(
452 ((long) d1 << 24) | ((long) d2 << 16) |
453 ((long) d3 << 8) | ((long) d4 << 0));
454 return 4;
455 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000456 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000457#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +0000458#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +0000459 result = gethostbyname_r(name, &hp_allocated, buf, buf_len, &hp, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +0000460#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000461 hp = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +0000462#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +0000463 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +0000464 result = gethostbyname_r(name, &hp_allocated, &data);
465 hp = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +0000466#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000467#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +0000468#ifdef USE_GETHOSTBYNAME_LOCK
469 PyThread_acquire_lock(gethostbyname_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000470#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000471 hp = gethostbyname(name);
472#endif /* HAVE_GETHOSTBYNAME_R */
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000473 Py_END_ALLOW_THREADS
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000474
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000475 if (hp == NULL) {
Guido van Rossumca9b3231995-01-10 16:30:54 +0000476#ifdef HAVE_HSTRERROR
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000477 /* Let's get real error message to return */
478 extern int h_errno;
479 PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno));
480#else
Guido van Rossum73624e91994-10-10 17:59:00 +0000481 PyErr_SetString(PySocket_Error, "host not found");
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000482#endif
Guido van Rossum3baaa131999-03-22 21:44:51 +0000483#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum955becc1999-03-22 20:14:53 +0000484 PyThread_release_lock(gethostbyname_lock);
485#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000486 return -1;
487 }
488 memcpy((char *) &addr_ret->sin_addr, hp->h_addr, hp->h_length);
Guido van Rossum955becc1999-03-22 20:14:53 +0000489 h_length = hp->h_length;
Guido van Rossum3baaa131999-03-22 21:44:51 +0000490#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum955becc1999-03-22 20:14:53 +0000491 PyThread_release_lock(gethostbyname_lock);
492#endif
493 return h_length;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000494}
495
Guido van Rossum30a685f1991-06-27 15:51:29 +0000496
Guido van Rossum30a685f1991-06-27 15:51:29 +0000497/* Create a string object representing an IP address.
498 This is always a string of the form 'dd.dd.dd.dd' (with variable
499 size numbers). */
500
Guido van Rossum73624e91994-10-10 17:59:00 +0000501static PyObject *
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000502BUILD_FUNC_DEF_1(makeipaddr, struct sockaddr_in *,addr)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000503{
504 long x = ntohl(addr->sin_addr.s_addr);
505 char buf[100];
506 sprintf(buf, "%d.%d.%d.%d",
507 (int) (x>>24) & 0xff, (int) (x>>16) & 0xff,
508 (int) (x>> 8) & 0xff, (int) (x>> 0) & 0xff);
Guido van Rossum73624e91994-10-10 17:59:00 +0000509 return PyString_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000510}
511
512
513/* Create an object representing the given socket address,
514 suitable for passing it back to bind(), connect() etc.
515 The family field of the sockaddr structure is inspected
516 to determine what kind of address it really is. */
517
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000518/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000519static PyObject *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000520BUILD_FUNC_DEF_2(makesockaddr,struct sockaddr *,addr, int,addrlen)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000521{
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000522 if (addrlen == 0) {
523 /* No address -- may be recvfrom() from known socket */
Guido van Rossum73624e91994-10-10 17:59:00 +0000524 Py_INCREF(Py_None);
525 return Py_None;
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000526 }
527
Guido van Rossumbcc20741998-08-04 22:53:56 +0000528#ifdef __BEOS__
Guido van Rossum2c8bcb82000-04-25 21:34:53 +0000529 /* XXX: BeOS version of accept() doesn't set family correctly */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000530 addr->sa_family = AF_INET;
531#endif
532
Guido van Rossum30a685f1991-06-27 15:51:29 +0000533 switch (addr->sa_family) {
534
535 case AF_INET:
536 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000537 struct sockaddr_in *a = (struct sockaddr_in *) addr;
Barry Warsaw752300b1997-01-03 17:18:10 +0000538 PyObject *addrobj = makeipaddr(a);
539 PyObject *ret = NULL;
540 if (addrobj) {
541 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
542 Py_DECREF(addrobj);
543 }
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000544 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000545 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000546
Guido van Rossumb6775db1994-08-01 11:34:53 +0000547#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000548 case AF_UNIX:
549 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000550 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000551 return PyString_FromString(a->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000552 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000553#endif /* AF_UNIX */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000554
555 /* More cases here... */
556
557 default:
Guido van Rossumaa948df1997-05-07 17:41:48 +0000558 /* If we don't know the address family, don't raise an
559 exception -- return it as a tuple. */
560 return Py_BuildValue("is#",
561 addr->sa_family,
562 addr->sa_data,
563 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000564
Guido van Rossum30a685f1991-06-27 15:51:29 +0000565 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000566}
567
Guido van Rossum30a685f1991-06-27 15:51:29 +0000568
569/* Parse a socket address argument according to the socket object's
570 address family. Return 1 if the address was in the proper format,
571 0 of not. The address is returned through addr_ret, its length
572 through len_ret. */
573
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000574static int
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000575BUILD_FUNC_DEF_4(
Guido van Rossum73624e91994-10-10 17:59:00 +0000576getsockaddrarg,PySocketSockObject *,s, PyObject *,args, struct sockaddr **,addr_ret, int *,len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000577{
Guido van Rossum30a685f1991-06-27 15:51:29 +0000578 switch (s->sock_family) {
579
Guido van Rossumb6775db1994-08-01 11:34:53 +0000580#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000581 case AF_UNIX:
582 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000583 struct sockaddr_un* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000584 char *path;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000585 int len;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000586 addr = (struct sockaddr_un* )&(s->sock_addr).un;
Guido van Rossum7e488981998-10-08 02:25:24 +0000587 if (!PyArg_Parse(args, "t#", &path, &len))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000588 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000589 if (len > sizeof addr->sun_path) {
Barry Warsaw752300b1997-01-03 17:18:10 +0000590 PyErr_SetString(PySocket_Error,
591 "AF_UNIX path too long");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000592 return 0;
593 }
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000594 addr->sun_family = AF_UNIX;
595 memcpy(addr->sun_path, path, len);
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000596 addr->sun_path[len] = 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000597 *addr_ret = (struct sockaddr *) addr;
Guido van Rossum65af28a1996-06-11 18:36:33 +0000598 *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000599 return 1;
600 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000601#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000602
Guido van Rossum30a685f1991-06-27 15:51:29 +0000603 case AF_INET:
604 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000605 struct sockaddr_in* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000606 char *host;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000607 int port;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000608 addr=(struct sockaddr_in*)&(s->sock_addr).in;
Guido van Rossum73624e91994-10-10 17:59:00 +0000609 if (!PyArg_Parse(args, "(si)", &host, &port))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000610 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000611 if (setipaddr(host, addr) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000612 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000613 addr->sin_family = AF_INET;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000614 addr->sin_port = htons((short)port);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000615 *addr_ret = (struct sockaddr *) addr;
616 *len_ret = sizeof *addr;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000617 return 1;
618 }
619
Guido van Rossum30a685f1991-06-27 15:51:29 +0000620 /* More cases here... */
621
622 default:
Guido van Rossum73624e91994-10-10 17:59:00 +0000623 PyErr_SetString(PySocket_Error, "getsockaddrarg: bad family");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000624 return 0;
625
626 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000627}
628
Guido van Rossum30a685f1991-06-27 15:51:29 +0000629
Guido van Rossum710e1df1992-06-12 10:39:36 +0000630/* Get the address length according to the socket object's address family.
631 Return 1 if the family is known, 0 otherwise. The length is returned
632 through len_ret. */
633
634static int
Guido van Rossum73624e91994-10-10 17:59:00 +0000635BUILD_FUNC_DEF_2(getsockaddrlen,PySocketSockObject *,s, int *,len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +0000636{
637 switch (s->sock_family) {
638
Guido van Rossumb6775db1994-08-01 11:34:53 +0000639#ifdef AF_UNIX
Guido van Rossum710e1df1992-06-12 10:39:36 +0000640 case AF_UNIX:
641 {
642 *len_ret = sizeof (struct sockaddr_un);
643 return 1;
644 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000645#endif /* AF_UNIX */
Guido van Rossum710e1df1992-06-12 10:39:36 +0000646
647 case AF_INET:
648 {
649 *len_ret = sizeof (struct sockaddr_in);
650 return 1;
651 }
652
653 /* More cases here... */
654
655 default:
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000656 PyErr_SetString(PySocket_Error, "getsockaddrlen: bad family");
Guido van Rossum710e1df1992-06-12 10:39:36 +0000657 return 0;
658
659 }
660}
661
662
Guido van Rossum30a685f1991-06-27 15:51:29 +0000663/* s.accept() method */
664
Guido van Rossum73624e91994-10-10 17:59:00 +0000665static PyObject *
666BUILD_FUNC_DEF_2(PySocketSock_accept,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000667{
668 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +0000669 int newfd;
670 socklen_t addrlen;
Barry Warsaw752300b1997-01-03 17:18:10 +0000671 PyObject *sock = NULL;
672 PyObject *addr = NULL;
673 PyObject *res = NULL;
674
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000675 if (!PyArg_ParseTuple(args, ":accept"))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000676 return NULL;
Guido van Rossum710e1df1992-06-12 10:39:36 +0000677 if (!getsockaddrlen(s, &addrlen))
678 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000679 Py_BEGIN_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000680 newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000681 Py_END_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000682 if (newfd < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000683 return PySocket_Err();
Barry Warsaw752300b1997-01-03 17:18:10 +0000684
Guido van Rossum30a685f1991-06-27 15:51:29 +0000685 /* Create the new object with unspecified family,
686 to avoid calls to bind() etc. on it. */
Guido van Rossum73624e91994-10-10 17:59:00 +0000687 sock = (PyObject *) PySocketSock_New(newfd,
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000688 s->sock_family,
689 s->sock_type,
690 s->sock_proto);
Barry Warsaw752300b1997-01-03 17:18:10 +0000691 if (sock == NULL) {
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000692 SOCKETCLOSE(newfd);
Barry Warsaw752300b1997-01-03 17:18:10 +0000693 goto finally;
694 }
695 if (!(addr = makesockaddr((struct sockaddr *) addrbuf, addrlen)))
696 goto finally;
697
698 if (!(res = Py_BuildValue("OO", sock, addr)))
699 goto finally;
700
701 finally:
Guido van Rossum73624e91994-10-10 17:59:00 +0000702 Py_XDECREF(sock);
703 Py_XDECREF(addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000704 return res;
705}
706
Guido van Rossum82a5c661998-07-07 20:45:43 +0000707static char accept_doc[] =
708"accept() -> (socket object, address info)\n\
709\n\
710Wait for an incoming connection. Return a new socket representing the\n\
711connection, and the address of the client. For IP sockets, the address\n\
712info is a pair (hostaddr, port).";
713
Guido van Rossum30a685f1991-06-27 15:51:29 +0000714
Guido van Rossume4485b01994-09-07 14:32:49 +0000715/* s.setblocking(1 | 0) method */
716
Guido van Rossum73624e91994-10-10 17:59:00 +0000717static PyObject *
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000718BUILD_FUNC_DEF_2(PySocketSock_setblocking,PySocketSockObject*,s,PyObject*,args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000719{
720 int block;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000721#ifndef MS_WINDOWS
Guido van Rossume4485b01994-09-07 14:32:49 +0000722 int delay_flag;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000723#endif
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000724 if (!PyArg_ParseTuple(args, "i:setblocking", &block))
Guido van Rossume4485b01994-09-07 14:32:49 +0000725 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000726 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000727#ifdef __BEOS__
728 block = !block;
729 setsockopt( s->sock_fd, SOL_SOCKET, SO_NONBLOCK,
730 (void *)(&block), sizeof( int ) );
731#else
Guido van Rossum6f489d91996-06-28 20:15:15 +0000732#ifndef MS_WINDOWS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000733#ifdef PYOS_OS2
734 block = !block;
735 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
736#else /* !PYOS_OS2 */
Guido van Rossume4485b01994-09-07 14:32:49 +0000737 delay_flag = fcntl (s->sock_fd, F_GETFL, 0);
738 if (block)
739 delay_flag &= (~O_NDELAY);
740 else
741 delay_flag |= O_NDELAY;
742 fcntl (s->sock_fd, F_SETFL, delay_flag);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000743#endif /* !PYOS_OS2 */
744#else /* MS_WINDOWS */
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000745 block = !block;
746 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000747#endif /* MS_WINDOWS */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000748#endif /* __BEOS__ */
Guido van Rossum73624e91994-10-10 17:59:00 +0000749 Py_END_ALLOW_THREADS
Guido van Rossume4485b01994-09-07 14:32:49 +0000750
Guido van Rossum73624e91994-10-10 17:59:00 +0000751 Py_INCREF(Py_None);
752 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000753}
Guido van Rossume4485b01994-09-07 14:32:49 +0000754
Guido van Rossum82a5c661998-07-07 20:45:43 +0000755static char setblocking_doc[] =
756"setblocking(flag)\n\
757\n\
758Set the socket to blocking (flag is true) or non-blocking (false).\n\
759This uses the FIONBIO ioctl with the O_NDELAY flag.";
760
Guido van Rossume4485b01994-09-07 14:32:49 +0000761
Guido van Rossumaee08791992-09-08 09:05:33 +0000762/* s.setsockopt() method.
763 With an integer third argument, sets an integer option.
764 With a string third argument, sets an option from a buffer;
765 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000766
Guido van Rossum73624e91994-10-10 17:59:00 +0000767static PyObject *
768BUILD_FUNC_DEF_2(PySocketSock_setsockopt,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000769{
770 int level;
771 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000772 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +0000773 char *buf;
774 int buflen;
775 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000776
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000777 if (PyArg_ParseTuple(args, "iii:setsockopt",
778 &level, &optname, &flag)) {
Guido van Rossumaee08791992-09-08 09:05:33 +0000779 buf = (char *) &flag;
780 buflen = sizeof flag;
781 }
782 else {
Guido van Rossum73624e91994-10-10 17:59:00 +0000783 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000784 if (!PyArg_ParseTuple(args, "iis#:setsockopt",
785 &level, &optname, &buf, &buflen))
Guido van Rossumaee08791992-09-08 09:05:33 +0000786 return NULL;
787 }
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000788 res = setsockopt(s->sock_fd, level, optname, (ANY *)buf, buflen);
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000789 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000790 return PySocket_Err();
791 Py_INCREF(Py_None);
792 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000793}
794
Guido van Rossum82a5c661998-07-07 20:45:43 +0000795static char setsockopt_doc[] =
796"setsockopt(level, option, value)\n\
797\n\
798Set a socket option. See the Unix manual for level and option.\n\
799The value argument can either be an integer or a string.";
800
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000801
Guido van Rossumaee08791992-09-08 09:05:33 +0000802/* s.getsockopt() method.
803 With two arguments, retrieves an integer option.
804 With a third integer argument, retrieves a string buffer of that size;
805 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000806
Guido van Rossum73624e91994-10-10 17:59:00 +0000807static PyObject *
808BUILD_FUNC_DEF_2(PySocketSock_getsockopt,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000809{
810 int level;
811 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000812 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +0000813 PyObject *buf;
Guido van Rossumff3ab422000-04-24 15:16:03 +0000814 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000815
Guido van Rossumbcc20741998-08-04 22:53:56 +0000816#ifdef __BEOS__
817/* We have incomplete socket support. */
818 PyErr_SetString( PySocket_Error, "getsockopt not supported" );
819 return NULL;
820#else
821
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000822 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
823 &level, &optname, &buflen))
Guido van Rossumbe32c891996-06-20 16:25:29 +0000824 return NULL;
825
826 if (buflen == 0) {
Guido van Rossumaee08791992-09-08 09:05:33 +0000827 int flag = 0;
Guido van Rossumff3ab422000-04-24 15:16:03 +0000828 socklen_t flagsize = sizeof flag;
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000829 res = getsockopt(s->sock_fd, level, optname,
830 (ANY *)&flag, &flagsize);
Guido van Rossumaee08791992-09-08 09:05:33 +0000831 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000832 return PySocket_Err();
833 return PyInt_FromLong(flag);
Guido van Rossumaee08791992-09-08 09:05:33 +0000834 }
Guido van Rossumaee08791992-09-08 09:05:33 +0000835 if (buflen <= 0 || buflen > 1024) {
Barry Warsaw752300b1997-01-03 17:18:10 +0000836 PyErr_SetString(PySocket_Error,
837 "getsockopt buflen out of range");
Guido van Rossumaee08791992-09-08 09:05:33 +0000838 return NULL;
839 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000840 buf = PyString_FromStringAndSize((char *)NULL, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000841 if (buf == NULL)
842 return NULL;
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000843 res = getsockopt(s->sock_fd, level, optname,
Guido van Rossuma597dde1995-01-10 20:56:29 +0000844 (ANY *)PyString_AsString(buf), &buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000845 if (res < 0) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000846 Py_DECREF(buf);
847 return PySocket_Err();
Guido van Rossumaee08791992-09-08 09:05:33 +0000848 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000849 _PyString_Resize(&buf, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000850 return buf;
Guido van Rossumbcc20741998-08-04 22:53:56 +0000851#endif /* __BEOS__ */
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000852}
853
Guido van Rossum82a5c661998-07-07 20:45:43 +0000854static char getsockopt_doc[] =
855"getsockopt(level, option[, buffersize]) -> value\n\
856\n\
857Get a socket option. See the Unix manual for level and option.\n\
858If a nonzero buffersize argument is given, the return value is a\n\
859string of that length; otherwise it is an integer.";
860
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000861
Guido van Rossum527c9bd2000-04-04 14:10:20 +0000862/* s.bind(sockaddr...) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000863
Guido van Rossum73624e91994-10-10 17:59:00 +0000864static PyObject *
865BUILD_FUNC_DEF_2(PySocketSock_bind,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000866{
867 struct sockaddr *addr;
868 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000869 int res;
Guido van Rossum527c9bd2000-04-04 14:10:20 +0000870 if (!getsockaddrarg(s, args, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000871 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000872 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000873 res = bind(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000874 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000875 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000876 return PySocket_Err();
877 Py_INCREF(Py_None);
878 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000879}
880
Guido van Rossum82a5c661998-07-07 20:45:43 +0000881static char bind_doc[] =
882"bind(address)\n\
883\n\
884Bind the socket to a local address. For IP sockets, the address is a\n\
885pair (host, port); the host must refer to the local host.";
886
Guido van Rossum30a685f1991-06-27 15:51:29 +0000887
888/* s.close() method.
889 Set the file descriptor to -1 so operations tried subsequently
890 will surely fail. */
891
Guido van Rossum73624e91994-10-10 17:59:00 +0000892static PyObject *
893BUILD_FUNC_DEF_2(PySocketSock_close,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000894{
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000895 if (!PyArg_ParseTuple(args, ":close"))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000896 return NULL;
Guido van Rossum08481461996-10-12 14:07:22 +0000897 if (s->sock_fd != -1) {
898 Py_BEGIN_ALLOW_THREADS
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000899 (void) SOCKETCLOSE(s->sock_fd);
Guido van Rossum08481461996-10-12 14:07:22 +0000900 Py_END_ALLOW_THREADS
901 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000902 s->sock_fd = -1;
Guido van Rossum73624e91994-10-10 17:59:00 +0000903 Py_INCREF(Py_None);
904 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000905}
906
Guido van Rossum82a5c661998-07-07 20:45:43 +0000907static char close_doc[] =
908"close()\n\
909\n\
910Close the socket. It cannot be used after this call.";
911
Guido van Rossum30a685f1991-06-27 15:51:29 +0000912
Guido van Rossum527c9bd2000-04-04 14:10:20 +0000913/* s.connect(sockaddr...) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000914
Guido van Rossum73624e91994-10-10 17:59:00 +0000915static PyObject *
916BUILD_FUNC_DEF_2(PySocketSock_connect,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000917{
918 struct sockaddr *addr;
919 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000920 int res;
Guido van Rossum527c9bd2000-04-04 14:10:20 +0000921 if (!getsockaddrarg(s, args, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000922 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000923 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000924 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000925 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000926 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000927 return PySocket_Err();
928 Py_INCREF(Py_None);
929 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000930}
931
Guido van Rossum82a5c661998-07-07 20:45:43 +0000932static char connect_doc[] =
933"connect(address)\n\
934\n\
935Connect the socket to a remote address. For IP sockets, the address\n\
936is a pair (host, port).";
937
Guido van Rossum30a685f1991-06-27 15:51:29 +0000938
Guido van Rossum527c9bd2000-04-04 14:10:20 +0000939/* s.connect_ex(sockaddr...) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +0000940
941static PyObject *
942BUILD_FUNC_DEF_2(PySocketSock_connect_ex,PySocketSockObject *,s, PyObject *,args)
943{
944 struct sockaddr *addr;
945 int addrlen;
946 int res;
Guido van Rossum527c9bd2000-04-04 14:10:20 +0000947 if (!getsockaddrarg(s, args, &addr, &addrlen))
Guido van Rossumfc4255d1997-11-19 18:57:13 +0000948 return NULL;
949 Py_BEGIN_ALLOW_THREADS
950 res = connect(s->sock_fd, addr, addrlen);
951 Py_END_ALLOW_THREADS
952 if (res != 0)
953 res = errno;
954 return PyInt_FromLong((long) res);
955}
956
Guido van Rossum82a5c661998-07-07 20:45:43 +0000957static char connect_ex_doc[] =
958"connect_ex(address)\n\
959\n\
960This is like connect(address), but returns an error code (the errno value)\n\
961instead of raising an exception when an error occurs.";
962
Guido van Rossumfc4255d1997-11-19 18:57:13 +0000963
Guido van Rossumed233a51992-06-23 09:07:03 +0000964/* s.fileno() method */
965
Guido van Rossum73624e91994-10-10 17:59:00 +0000966static PyObject *
967BUILD_FUNC_DEF_2(PySocketSock_fileno,PySocketSockObject *,s, PyObject *,args)
Guido van Rossumed233a51992-06-23 09:07:03 +0000968{
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000969 if (!PyArg_ParseTuple(args, ":fileno"))
Guido van Rossumed233a51992-06-23 09:07:03 +0000970 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000971 return PyInt_FromLong((long) s->sock_fd);
Guido van Rossumed233a51992-06-23 09:07:03 +0000972}
973
Guido van Rossum82a5c661998-07-07 20:45:43 +0000974static char fileno_doc[] =
975"fileno() -> integer\n\
976\n\
977Return the integer file descriptor of the socket.";
978
Guido van Rossumed233a51992-06-23 09:07:03 +0000979
Guido van Rossumbe32c891996-06-20 16:25:29 +0000980#ifndef NO_DUP
981/* s.dup() method */
982
983static PyObject *
984BUILD_FUNC_DEF_2(PySocketSock_dup,PySocketSockObject *,s, PyObject *,args)
985{
986 int newfd;
987 PyObject *sock;
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000988 if (!PyArg_ParseTuple(args, ":dup"))
Guido van Rossumbe32c891996-06-20 16:25:29 +0000989 return NULL;
990 newfd = dup(s->sock_fd);
991 if (newfd < 0)
992 return PySocket_Err();
993 sock = (PyObject *) PySocketSock_New(newfd,
Barry Warsaw752300b1997-01-03 17:18:10 +0000994 s->sock_family,
995 s->sock_type,
996 s->sock_proto);
Guido van Rossumbe32c891996-06-20 16:25:29 +0000997 if (sock == NULL)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000998 SOCKETCLOSE(newfd);
Guido van Rossumbe32c891996-06-20 16:25:29 +0000999 return sock;
1000}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001001
1002static char dup_doc[] =
1003"dup() -> socket object\n\
1004\n\
1005Return a new socket object connected to the same system resource.";
1006
Guido van Rossumbe32c891996-06-20 16:25:29 +00001007#endif
1008
1009
Guido van Rossumc89705d1992-11-26 08:54:07 +00001010/* s.getsockname() method */
1011
Guido van Rossum73624e91994-10-10 17:59:00 +00001012static PyObject *
1013BUILD_FUNC_DEF_2(PySocketSock_getsockname,PySocketSockObject *,s, PyObject *,args)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001014{
1015 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001016 int res;
1017 socklen_t addrlen;
1018
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001019 if (!PyArg_ParseTuple(args, ":getsockname"))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001020 return NULL;
1021 if (!getsockaddrlen(s, &addrlen))
1022 return NULL;
Guido van Rossumca42b161996-01-12 01:36:05 +00001023 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001024 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001025 res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001026 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001027 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001028 return PySocket_Err();
Guido van Rossumc89705d1992-11-26 08:54:07 +00001029 return makesockaddr((struct sockaddr *) addrbuf, addrlen);
1030}
1031
Guido van Rossum82a5c661998-07-07 20:45:43 +00001032static char getsockname_doc[] =
1033"getsockname() -> address info\n\
1034\n\
1035Return the address of the local endpoint. For IP sockets, the address\n\
1036info is a pair (hostaddr, port).";
1037
Guido van Rossumc89705d1992-11-26 08:54:07 +00001038
Guido van Rossumb6775db1994-08-01 11:34:53 +00001039#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001040/* s.getpeername() method */
1041
Guido van Rossum73624e91994-10-10 17:59:00 +00001042static PyObject *
1043BUILD_FUNC_DEF_2(PySocketSock_getpeername,PySocketSockObject *,s, PyObject *,args)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001044{
1045 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001046 int res;
1047 socklen_t addrlen;
1048
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001049 if (!PyArg_ParseTuple(args, ":getpeername"))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001050 return NULL;
1051 if (!getsockaddrlen(s, &addrlen))
1052 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001053 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001054 res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001055 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001056 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001057 return PySocket_Err();
Guido van Rossumc89705d1992-11-26 08:54:07 +00001058 return makesockaddr((struct sockaddr *) addrbuf, addrlen);
1059}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001060
1061static char getpeername_doc[] =
1062"getpeername() -> address info\n\
1063\n\
1064Return the address of the remote endpoint. For IP sockets, the address\n\
1065info is a pair (hostaddr, port).";
1066
Guido van Rossumb6775db1994-08-01 11:34:53 +00001067#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001068
1069
Guido van Rossum30a685f1991-06-27 15:51:29 +00001070/* s.listen(n) method */
1071
Guido van Rossum73624e91994-10-10 17:59:00 +00001072static PyObject *
1073BUILD_FUNC_DEF_2(PySocketSock_listen,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001074{
1075 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001076 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001077 if (!PyArg_ParseTuple(args, "i:listen", &backlog))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001078 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001079 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +00001080 if (backlog < 1)
1081 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001082 res = listen(s->sock_fd, backlog);
Guido van Rossum73624e91994-10-10 17:59:00 +00001083 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001084 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001085 return PySocket_Err();
1086 Py_INCREF(Py_None);
1087 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001088}
1089
Guido van Rossum82a5c661998-07-07 20:45:43 +00001090static char listen_doc[] =
1091"listen(backlog)\n\
1092\n\
1093Enable a server to accept connections. The backlog argument must be at\n\
1094least 1; it specifies the number of unaccepted connection that the system\n\
1095will allow before refusing new connections.";
1096
1097
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001098#ifndef NO_DUP
Guido van Rossum30a685f1991-06-27 15:51:29 +00001099/* s.makefile(mode) method.
1100 Create a new open file object referring to a dupped version of
1101 the socket's file descriptor. (The dup() call is necessary so
1102 that the open file and socket objects may be closed independent
1103 of each other.)
1104 The mode argument specifies 'r' or 'w' passed to fdopen(). */
1105
Guido van Rossum73624e91994-10-10 17:59:00 +00001106static PyObject *
1107BUILD_FUNC_DEF_2(PySocketSock_makefile,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001108{
Guido van Rossuma597dde1995-01-10 20:56:29 +00001109 extern int fclose Py_PROTO((FILE *));
Guido van Rossum6b144911995-03-14 15:05:13 +00001110 char *mode = "r";
1111 int bufsize = -1;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001112 int fd;
1113 FILE *fp;
Guido van Rossum6b144911995-03-14 15:05:13 +00001114 PyObject *f;
1115
Guido van Rossum43713e52000-02-29 13:59:29 +00001116 if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001117 return NULL;
Guido van Rossum8d665e61996-06-26 18:22:49 +00001118#ifdef MS_WIN32
Guido van Rossum82a5c661998-07-07 20:45:43 +00001119 if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) ||
1120 ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL))
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001121#else
Guido van Rossum82a5c661998-07-07 20:45:43 +00001122 if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL)
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001123#endif
Guido van Rossum82a5c661998-07-07 20:45:43 +00001124 {
Guido van Rossum6b144911995-03-14 15:05:13 +00001125 if (fd >= 0)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001126 SOCKETCLOSE(fd);
Guido van Rossum73624e91994-10-10 17:59:00 +00001127 return PySocket_Err();
Guido van Rossum6b144911995-03-14 15:05:13 +00001128 }
1129 f = PyFile_FromFile(fp, "<socket>", mode, fclose);
1130 if (f != NULL)
1131 PyFile_SetBufSize(f, bufsize);
1132 return f;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001133}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001134
1135static char makefile_doc[] =
1136"makefile([mode[, buffersize]]) -> file object\n\
1137\n\
1138Return a regular file object corresponding to the socket.\n\
1139The mode and buffersize arguments are as for the built-in open() function.";
1140
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001141#endif /* NO_DUP */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001142
Guido van Rossum82a5c661998-07-07 20:45:43 +00001143
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001144/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001145
Guido van Rossum73624e91994-10-10 17:59:00 +00001146static PyObject *
1147BUILD_FUNC_DEF_2(PySocketSock_recv,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001148{
Guido van Rossumbe32c891996-06-20 16:25:29 +00001149 int len, n, flags = 0;
Guido van Rossum73624e91994-10-10 17:59:00 +00001150 PyObject *buf;
Guido van Rossum43713e52000-02-29 13:59:29 +00001151 if (!PyArg_ParseTuple(args, "i|i:recv", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001152 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001153 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001154 if (buf == NULL)
1155 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001156 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma597dde1995-01-10 20:56:29 +00001157 n = recv(s->sock_fd, PyString_AsString(buf), len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001158 Py_END_ALLOW_THREADS
Guido van Rossum7c53b771995-09-13 18:39:47 +00001159 if (n < 0) {
1160 Py_DECREF(buf);
Guido van Rossum73624e91994-10-10 17:59:00 +00001161 return PySocket_Err();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001162 }
Guido van Rossum53a9bf81996-06-11 18:35:24 +00001163 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001164 return NULL;
1165 return buf;
1166}
1167
Guido van Rossum82a5c661998-07-07 20:45:43 +00001168static char recv_doc[] =
1169"recv(buffersize[, flags]) -> data\n\
1170\n\
1171Receive up to buffersize bytes from the socket. For the optional flags\n\
1172argument, see the Unix manual. When no data is available, block until\n\
1173at least one byte is available or until the remote end is closed. When\n\
1174the remote end is closed and all data is read, return the empty string.";
1175
Guido van Rossum30a685f1991-06-27 15:51:29 +00001176
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001177/* s.recvfrom(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001178
Guido van Rossum73624e91994-10-10 17:59:00 +00001179static PyObject *
1180BUILD_FUNC_DEF_2(PySocketSock_recvfrom,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001181{
1182 char addrbuf[256];
Barry Warsaw752300b1997-01-03 17:18:10 +00001183 PyObject *buf = NULL;
1184 PyObject *addr = NULL;
1185 PyObject *ret = NULL;
1186
Guido van Rossumff3ab422000-04-24 15:16:03 +00001187 int len, n, flags = 0;
1188 socklen_t addrlen;
Guido van Rossum43713e52000-02-29 13:59:29 +00001189 if (!PyArg_ParseTuple(args, "i|i:recvfrom", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001190 return NULL;
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00001191 if (!getsockaddrlen(s, &addrlen))
1192 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001193 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00001194 if (buf == NULL)
1195 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001196 Py_BEGIN_ALLOW_THREADS
1197 n = recvfrom(s->sock_fd, PyString_AsString(buf), len, flags,
Guido van Rossum8d665e61996-06-26 18:22:49 +00001198#ifndef MS_WINDOWS
Guido van Rossum32c575d1997-12-02 20:37:32 +00001199#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001200 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00001201#else
Barry Warsaw752300b1997-01-03 17:18:10 +00001202 (ANY *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00001203#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001204#else
Barry Warsaw752300b1997-01-03 17:18:10 +00001205 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001206#endif
Barry Warsaw752300b1997-01-03 17:18:10 +00001207 );
Guido van Rossum73624e91994-10-10 17:59:00 +00001208 Py_END_ALLOW_THREADS
Guido van Rossum7c53b771995-09-13 18:39:47 +00001209 if (n < 0) {
1210 Py_DECREF(buf);
Guido van Rossum73624e91994-10-10 17:59:00 +00001211 return PySocket_Err();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001212 }
Guido van Rossum53a9bf81996-06-11 18:35:24 +00001213 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001214 return NULL;
Barry Warsaw752300b1997-01-03 17:18:10 +00001215
1216 if (!(addr = makesockaddr((struct sockaddr *)addrbuf, addrlen)))
1217 goto finally;
1218
Guido van Rossum73624e91994-10-10 17:59:00 +00001219 ret = Py_BuildValue("OO", buf, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00001220 finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001221 Py_XDECREF(addr);
1222 Py_XDECREF(buf);
Guido van Rossum6f5afc91993-02-05 09:46:15 +00001223 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001224}
1225
Guido van Rossum82a5c661998-07-07 20:45:43 +00001226static char recvfrom_doc[] =
1227"recvfrom(buffersize[, flags]) -> (data, address info)\n\
1228\n\
1229Like recv(buffersize, flags) but also return the sender's address info.";
1230
Guido van Rossum30a685f1991-06-27 15:51:29 +00001231
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001232/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001233
Guido van Rossum73624e91994-10-10 17:59:00 +00001234static PyObject *
1235BUILD_FUNC_DEF_2(PySocketSock_send,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001236{
Guido van Rossumff4949e1992-08-05 19:58:53 +00001237 char *buf;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001238 int len, n, flags = 0;
Guido van Rossum43713e52000-02-29 13:59:29 +00001239 if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001240 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001241 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001242 n = send(s->sock_fd, buf, len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001243 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00001244 if (n < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001245 return PySocket_Err();
1246 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00001247}
1248
Guido van Rossum82a5c661998-07-07 20:45:43 +00001249static char send_doc[] =
1250"send(data[, flags])\n\
1251\n\
1252Send a data string to the socket. For the optional flags\n\
1253argument, see the Unix manual.";
1254
Guido van Rossum30a685f1991-06-27 15:51:29 +00001255
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001256/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001257
Guido van Rossum73624e91994-10-10 17:59:00 +00001258static PyObject *
1259BUILD_FUNC_DEF_2(PySocketSock_sendto,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001260{
Guido van Rossum73624e91994-10-10 17:59:00 +00001261 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001262 char *buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001263 struct sockaddr *addr;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001264 int addrlen, len, n, flags;
1265 flags = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001266 if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001267 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001268 if (!PyArg_ParseTuple(args, "s#iO:sendto",
1269 &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001270 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001271 }
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001272 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001273 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001274 Py_BEGIN_ALLOW_THREADS
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001275 n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001276 Py_END_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001277 if (n < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001278 return PySocket_Err();
1279 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001280}
1281
Guido van Rossum82a5c661998-07-07 20:45:43 +00001282static char sendto_doc[] =
1283"sendto(data[, flags], address)\n\
1284\n\
1285Like send(data, flags) but allows specifying the destination address.\n\
1286For IP sockets, the address is a pair (hostaddr, port).";
1287
Guido van Rossum30a685f1991-06-27 15:51:29 +00001288
1289/* s.shutdown(how) method */
1290
Guido van Rossum73624e91994-10-10 17:59:00 +00001291static PyObject *
1292BUILD_FUNC_DEF_2(PySocketSock_shutdown,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001293{
1294 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001295 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001296 if (!PyArg_ParseTuple(args, "i:shutdown", &how))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001297 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001298 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001299 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +00001300 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001301 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001302 return PySocket_Err();
1303 Py_INCREF(Py_None);
1304 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001305}
1306
Guido van Rossum82a5c661998-07-07 20:45:43 +00001307static char shutdown_doc[] =
1308"shutdown(flag)\n\
1309\n\
1310Shut down the reading side of the socket (flag == 0), the writing side\n\
1311of the socket (flag == 1), or both ends (flag == 2).";
1312
Guido van Rossum30a685f1991-06-27 15:51:29 +00001313
1314/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001315
Guido van Rossum73624e91994-10-10 17:59:00 +00001316static PyMethodDef PySocketSock_methods[] = {
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001317 {"accept", (PyCFunction)PySocketSock_accept, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001318 accept_doc},
Guido van Rossum527c9bd2000-04-04 14:10:20 +00001319 {"bind", (PyCFunction)PySocketSock_bind, 0,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001320 bind_doc},
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001321 {"close", (PyCFunction)PySocketSock_close, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001322 close_doc},
Guido van Rossum527c9bd2000-04-04 14:10:20 +00001323 {"connect", (PyCFunction)PySocketSock_connect, 0,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001324 connect_doc},
Guido van Rossum527c9bd2000-04-04 14:10:20 +00001325 {"connect_ex", (PyCFunction)PySocketSock_connect_ex, 0,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001326 connect_ex_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00001327#ifndef NO_DUP
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001328 {"dup", (PyCFunction)PySocketSock_dup, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001329 dup_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00001330#endif
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001331 {"fileno", (PyCFunction)PySocketSock_fileno, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001332 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001333#ifdef HAVE_GETPEERNAME
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001334 {"getpeername", (PyCFunction)PySocketSock_getpeername, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001335 getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00001336#endif
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001337 {"getsockname", (PyCFunction)PySocketSock_getsockname, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001338 getsockname_doc},
1339 {"getsockopt", (PyCFunction)PySocketSock_getsockopt, 1,
1340 getsockopt_doc},
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001341 {"listen", (PyCFunction)PySocketSock_listen, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001342 listen_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001343#ifndef NO_DUP
Guido van Rossum82a5c661998-07-07 20:45:43 +00001344 {"makefile", (PyCFunction)PySocketSock_makefile, 1,
1345 makefile_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001346#endif
Guido van Rossum82a5c661998-07-07 20:45:43 +00001347 {"recv", (PyCFunction)PySocketSock_recv, 1,
1348 recv_doc},
1349 {"recvfrom", (PyCFunction)PySocketSock_recvfrom, 1,
1350 recvfrom_doc},
1351 {"send", (PyCFunction)PySocketSock_send, 1,
1352 send_doc},
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001353 {"sendto", (PyCFunction)PySocketSock_sendto, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001354 sendto_doc},
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001355 {"setblocking", (PyCFunction)PySocketSock_setblocking, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001356 setblocking_doc},
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001357 {"setsockopt", (PyCFunction)PySocketSock_setsockopt, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001358 setsockopt_doc},
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001359 {"shutdown", (PyCFunction)PySocketSock_shutdown, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001360 shutdown_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001361 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001362};
1363
Guido van Rossum30a685f1991-06-27 15:51:29 +00001364
Guido van Rossum73624e91994-10-10 17:59:00 +00001365/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00001366 First close the file description. */
1367
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001368static void
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001369BUILD_FUNC_DEF_1(PySocketSock_dealloc,PySocketSockObject *,s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001370{
Guido van Rossumfa972c92000-04-10 12:45:45 +00001371 if (s->sock_fd != -1)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001372 (void) SOCKETCLOSE(s->sock_fd);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001373 PyObject_Del(s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001374}
1375
Guido van Rossum30a685f1991-06-27 15:51:29 +00001376
1377/* Return a socket object's named attribute. */
1378
Guido van Rossum73624e91994-10-10 17:59:00 +00001379static PyObject *
1380BUILD_FUNC_DEF_2(PySocketSock_getattr,PySocketSockObject *,s, char *,name)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001381{
Guido van Rossum73624e91994-10-10 17:59:00 +00001382 return Py_FindMethod(PySocketSock_methods, (PyObject *) s, name);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001383}
1384
Guido van Rossum30a685f1991-06-27 15:51:29 +00001385
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001386static PyObject *
1387BUILD_FUNC_DEF_1(PySocketSock_repr,PySocketSockObject *,s)
1388{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001389 char buf[512];
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001390 sprintf(buf,
1391 "<socket object, fd=%d, family=%d, type=%d, protocol=%d>",
1392 s->sock_fd, s->sock_family, s->sock_type, s->sock_proto);
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00001393 return PyString_FromString(buf);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001394}
1395
1396
Guido van Rossumb6775db1994-08-01 11:34:53 +00001397/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001398
Guido van Rossum73624e91994-10-10 17:59:00 +00001399static PyTypeObject PySocketSock_Type = {
Guido van Rossum55558541996-05-23 22:54:50 +00001400 PyObject_HEAD_INIT(0) /* Must fill in type value later */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001401 0,
1402 "socket",
Guido van Rossum73624e91994-10-10 17:59:00 +00001403 sizeof(PySocketSockObject),
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001404 0,
Guido van Rossum73624e91994-10-10 17:59:00 +00001405 (destructor)PySocketSock_dealloc, /*tp_dealloc*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001406 0, /*tp_print*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001407 (getattrfunc)PySocketSock_getattr, /*tp_getattr*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001408 0, /*tp_setattr*/
1409 0, /*tp_compare*/
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001410 (reprfunc)PySocketSock_repr, /*tp_repr*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001411 0, /*tp_as_number*/
1412 0, /*tp_as_sequence*/
1413 0, /*tp_as_mapping*/
1414};
1415
Guido van Rossum30a685f1991-06-27 15:51:29 +00001416
Guido van Rossum81194471991-07-27 21:42:02 +00001417/* Python interface to gethostname(). */
1418
1419/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001420static PyObject *
1421BUILD_FUNC_DEF_2(PySocket_gethostname,PyObject *,self, PyObject *,args)
Guido van Rossum81194471991-07-27 21:42:02 +00001422{
1423 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001424 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001425 if (!PyArg_ParseTuple(args, ":gethostname"))
Guido van Rossum81194471991-07-27 21:42:02 +00001426 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001427 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001428 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00001429 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001430 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001431 return PySocket_Err();
Guido van Rossum81194471991-07-27 21:42:02 +00001432 buf[sizeof buf - 1] = '\0';
Guido van Rossum73624e91994-10-10 17:59:00 +00001433 return PyString_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00001434}
Guido van Rossumff4949e1992-08-05 19:58:53 +00001435
Guido van Rossum82a5c661998-07-07 20:45:43 +00001436static char gethostname_doc[] =
1437"gethostname() -> string\n\
1438\n\
1439Return the current host name.";
1440
Guido van Rossumff4949e1992-08-05 19:58:53 +00001441
Guido van Rossum30a685f1991-06-27 15:51:29 +00001442/* Python interface to gethostbyname(name). */
1443
1444/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001445static PyObject *
1446BUILD_FUNC_DEF_2(PySocket_gethostbyname,PyObject *,self, PyObject *,args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001447{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001448 char *name;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001449 struct sockaddr_in addrbuf;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001450 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00001451 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001452 if (setipaddr(name, &addrbuf) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001453 return NULL;
1454 return makeipaddr(&addrbuf);
1455}
1456
Guido van Rossum82a5c661998-07-07 20:45:43 +00001457static char gethostbyname_doc[] =
1458"gethostbyname(host) -> address\n\
1459\n\
1460Return the IP address (a string of the form '255.255.255.255') for a host.";
1461
1462
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001463/* Convenience function common to gethostbyname_ex and gethostbyaddr */
1464
1465static PyObject *
1466gethost_common(h, addr)
1467 struct hostent *h;
1468 struct sockaddr_in *addr;
1469{
1470 char **pch;
1471 PyObject *rtn_tuple = (PyObject *)NULL;
1472 PyObject *name_list = (PyObject *)NULL;
1473 PyObject *addr_list = (PyObject *)NULL;
1474 PyObject *tmp;
1475 if (h == NULL) {
1476#ifdef HAVE_HSTRERROR
1477 /* Let's get real error message to return */
1478 extern int h_errno;
1479 PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno));
1480#else
1481 PyErr_SetString(PySocket_Error, "host not found");
1482#endif
1483 return NULL;
1484 }
1485 if ((name_list = PyList_New(0)) == NULL)
1486 goto err;
1487 if ((addr_list = PyList_New(0)) == NULL)
1488 goto err;
1489 for (pch = h->h_aliases; *pch != NULL; pch++) {
1490 int status;
1491 tmp = PyString_FromString(*pch);
1492 if (tmp == NULL)
1493 goto err;
1494 status = PyList_Append(name_list, tmp);
1495 Py_DECREF(tmp);
1496 if (status)
1497 goto err;
1498 }
1499 for (pch = h->h_addr_list; *pch != NULL; pch++) {
1500 int status;
1501 memcpy((char *) &addr->sin_addr, *pch, h->h_length);
1502 tmp = makeipaddr(addr);
1503 if (tmp == NULL)
1504 goto err;
1505 status = PyList_Append(addr_list, tmp);
1506 Py_DECREF(tmp);
1507 if (status)
1508 goto err;
1509 }
1510 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
1511 err:
1512 Py_XDECREF(name_list);
1513 Py_XDECREF(addr_list);
1514 return rtn_tuple;
1515}
1516
1517
1518/* Python interface to gethostbyname_ex(name). */
1519
1520/*ARGSUSED*/
1521static PyObject *
1522BUILD_FUNC_DEF_2(PySocket_gethostbyname_ex,PyObject *,self, PyObject *,args)
1523{
1524 char *name;
1525 struct hostent *h;
1526 struct sockaddr_in addr;
Guido van Rossum955becc1999-03-22 20:14:53 +00001527 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001528#ifdef HAVE_GETHOSTBYNAME_R
1529 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001530#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
1531 struct hostent_data data;
1532#else
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001533 char buf[16384];
1534 int buf_len = (sizeof buf) - 1;
1535 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001536#endif
1537#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001538 int result;
1539#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001540#endif /* HAVE_GETHOSTBYNAME_R */
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001541 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001542 return NULL;
1543 if (setipaddr(name, &addr) < 0)
1544 return NULL;
1545 Py_BEGIN_ALLOW_THREADS
1546#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001547#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001548 result = gethostbyname_r(name, &hp_allocated, buf, buf_len, &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001549#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001550 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001551#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00001552 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001553 result = gethostbyname_r(name, &hp_allocated, &data);
1554 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001555#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001556#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00001557#ifdef USE_GETHOSTBYNAME_LOCK
1558 PyThread_acquire_lock(gethostbyname_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001559#endif
1560 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001561#endif /* HAVE_GETHOSTBYNAME_R */
1562 Py_END_ALLOW_THREADS
Guido van Rossum3baaa131999-03-22 21:44:51 +00001563 ret = gethost_common(h, &addr);
1564#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum955becc1999-03-22 20:14:53 +00001565 PyThread_release_lock(gethostbyname_lock);
1566#endif
1567 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001568}
1569
1570static char ghbn_ex_doc[] =
1571"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
1572\n\
1573Return the true host name, a list of aliases, and a list of IP addresses,\n\
1574for a host. The host argument is a string giving a host name or IP number.";
1575
1576
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001577/* Python interface to gethostbyaddr(IP). */
1578
1579/*ARGSUSED*/
1580static PyObject *
1581BUILD_FUNC_DEF_2(PySocket_gethostbyaddr,PyObject *,self, PyObject *, args)
1582{
1583 struct sockaddr_in addr;
1584 char *ip_num;
1585 struct hostent *h;
Guido van Rossum3baaa131999-03-22 21:44:51 +00001586 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001587#ifdef HAVE_GETHOSTBYNAME_R
1588 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001589#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
1590 struct hostent_data data;
1591#else
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001592 char buf[16384];
1593 int buf_len = (sizeof buf) - 1;
1594 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001595#endif
1596#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001597 int result;
1598#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001599#endif /* HAVE_GETHOSTBYNAME_R */
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001600
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001601 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001602 return NULL;
1603 if (setipaddr(ip_num, &addr) < 0)
1604 return NULL;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001605 Py_BEGIN_ALLOW_THREADS
1606#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001607#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001608 result = gethostbyaddr_r((char *)&addr.sin_addr,
1609 sizeof(addr.sin_addr),
1610 AF_INET, &hp_allocated, buf, buf_len,
1611 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001612#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001613 h = gethostbyaddr_r((char *)&addr.sin_addr,
1614 sizeof(addr.sin_addr),
1615 AF_INET,
1616 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001617#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00001618 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001619 result = gethostbyaddr_r((char *)&addr.sin_addr,
1620 sizeof(addr.sin_addr),
1621 AF_INET, &hp_allocated, &data);
1622 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001623#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001624#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00001625#ifdef USE_GETHOSTBYNAME_LOCK
1626 PyThread_acquire_lock(gethostbyname_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001627#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001628 h = gethostbyaddr((char *)&addr.sin_addr,
1629 sizeof(addr.sin_addr),
1630 AF_INET);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001631#endif /* HAVE_GETHOSTBYNAME_R */
1632 Py_END_ALLOW_THREADS
Guido van Rossum3baaa131999-03-22 21:44:51 +00001633 ret = gethost_common(h, &addr);
1634#ifdef USE_GETHOSTBYNAME_LOCK
1635 PyThread_release_lock(gethostbyname_lock);
1636#endif
1637 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001638}
1639
Guido van Rossum82a5c661998-07-07 20:45:43 +00001640static char gethostbyaddr_doc[] =
1641"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
1642\n\
1643Return the true host name, a list of aliases, and a list of IP addresses,\n\
1644for a host. The host argument is a string giving a host name or IP number.";
1645
Guido van Rossum30a685f1991-06-27 15:51:29 +00001646
1647/* Python interface to getservbyname(name).
1648 This only returns the port number, since the other info is already
1649 known or not useful (like the list of aliases). */
1650
1651/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001652static PyObject *
1653BUILD_FUNC_DEF_2(PySocket_getservbyname,PyObject *,self, PyObject *,args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001654{
Guido van Rossumff4949e1992-08-05 19:58:53 +00001655 char *name, *proto;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001656 struct servent *sp;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001657 if (!PyArg_ParseTuple(args, "ss:getservbyname", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00001658 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001659 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001660 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00001661 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00001662 if (sp == NULL) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001663 PyErr_SetString(PySocket_Error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00001664 return NULL;
1665 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001666 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00001667}
1668
Guido van Rossum82a5c661998-07-07 20:45:43 +00001669static char getservbyname_doc[] =
1670"getservbyname(servicename, protocolname) -> integer\n\
1671\n\
1672Return a port number from a service name and protocol name.\n\
1673The protocol name should be 'tcp' or 'udp'.";
1674
Guido van Rossum30a685f1991-06-27 15:51:29 +00001675
Guido van Rossum3901d851996-12-19 16:35:04 +00001676/* Python interface to getprotobyname(name).
1677 This only returns the protocol number, since the other info is
1678 already known or not useful (like the list of aliases). */
1679
1680/*ARGSUSED*/
1681static PyObject *
1682BUILD_FUNC_DEF_2(PySocket_getprotobyname,PyObject *,self, PyObject *,args)
1683{
1684 char *name;
1685 struct protoent *sp;
Guido van Rossumbcc20741998-08-04 22:53:56 +00001686#ifdef __BEOS__
1687/* Not available in BeOS yet. - [cjh] */
1688 PyErr_SetString( PySocket_Error, "getprotobyname not supported" );
1689 return NULL;
1690#else
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001691 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
Guido van Rossum3901d851996-12-19 16:35:04 +00001692 return NULL;
1693 Py_BEGIN_ALLOW_THREADS
1694 sp = getprotobyname(name);
1695 Py_END_ALLOW_THREADS
1696 if (sp == NULL) {
1697 PyErr_SetString(PySocket_Error, "protocol not found");
1698 return NULL;
1699 }
1700 return PyInt_FromLong((long) sp->p_proto);
Guido van Rossumbcc20741998-08-04 22:53:56 +00001701#endif
Guido van Rossum3901d851996-12-19 16:35:04 +00001702}
1703
Guido van Rossum82a5c661998-07-07 20:45:43 +00001704static char getprotobyname_doc[] =
1705"getprotobyname(name) -> integer\n\
1706\n\
1707Return the protocol number for the named protocol. (Rarely used.)";
1708
Guido van Rossum3901d851996-12-19 16:35:04 +00001709
Guido van Rossum30a685f1991-06-27 15:51:29 +00001710/* Python interface to socket(family, type, proto).
1711 The third (protocol) argument is optional.
1712 Return a new socket object. */
1713
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001714/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001715static PyObject *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001716BUILD_FUNC_DEF_2(PySocket_socket,PyObject *,self, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001717{
Guido van Rossum73624e91994-10-10 17:59:00 +00001718 PySocketSockObject *s;
Guido van Rossum6f489d91996-06-28 20:15:15 +00001719#ifdef MS_WINDOWS
Guido van Rossumd639d4d1996-06-12 04:04:22 +00001720 SOCKET fd;
1721#else
1722 int fd;
1723#endif
Guido van Rossumbe32c891996-06-20 16:25:29 +00001724 int family, type, proto = 0;
Guido van Rossum43713e52000-02-29 13:59:29 +00001725 if (!PyArg_ParseTuple(args, "ii|i:socket", &family, &type, &proto))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001726 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001727 Py_BEGIN_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001728 fd = socket(family, type, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00001729 Py_END_ALLOW_THREADS
Guido van Rossum6f489d91996-06-28 20:15:15 +00001730#ifdef MS_WINDOWS
Guido van Rossumd639d4d1996-06-12 04:04:22 +00001731 if (fd == INVALID_SOCKET)
1732#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001733 if (fd < 0)
Guido van Rossumd639d4d1996-06-12 04:04:22 +00001734#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001735 return PySocket_Err();
1736 s = PySocketSock_New(fd, family, type, proto);
Guido van Rossum30a685f1991-06-27 15:51:29 +00001737 /* If the object can't be created, don't forget to close the
1738 file descriptor again! */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001739 if (s == NULL)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001740 (void) SOCKETCLOSE(fd);
Guido van Rossum81194471991-07-27 21:42:02 +00001741 /* From now on, ignore SIGPIPE and let the error checking
1742 do the work. */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001743#ifdef SIGPIPE
Guido van Rossum81194471991-07-27 21:42:02 +00001744 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001745#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001746 return (PyObject *) s;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001747}
1748
Guido van Rossum82a5c661998-07-07 20:45:43 +00001749static char socket_doc[] =
1750"socket(family, type[, proto]) -> socket object\n\
1751\n\
1752Open a socket of the given type. The family argument specifies the\n\
1753address family; it is normally AF_INET, sometimes AF_UNIX.\n\
1754The type argument specifies whether this is a stream (SOCK_STREAM)\n\
1755or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\
1756specifying the default protocol.";
1757
1758
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001759#ifndef NO_DUP
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001760/* Create a socket object from a numeric file description.
1761 Useful e.g. if stdin is a socket.
1762 Additional arguments as for socket(). */
1763
1764/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001765static PyObject *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001766BUILD_FUNC_DEF_2(PySocket_fromfd,PyObject *,self, PyObject *,args)
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001767{
Guido van Rossum73624e91994-10-10 17:59:00 +00001768 PySocketSockObject *s;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001769 int fd, family, type, proto = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001770 if (!PyArg_ParseTuple(args, "iii|i:fromfd",
1771 &fd, &family, &type, &proto))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001772 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +00001773 /* Dup the fd so it and the socket can be closed independently */
1774 fd = dup(fd);
1775 if (fd < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001776 return PySocket_Err();
1777 s = PySocketSock_New(fd, family, type, proto);
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001778 /* From now on, ignore SIGPIPE and let the error checking
1779 do the work. */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001780#ifdef SIGPIPE
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001781 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001782#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001783 return (PyObject *) s;
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001784}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001785
1786static char fromfd_doc[] =
1787"fromfd(fd, family, type[, proto]) -> socket object\n\
1788\n\
1789Create a socket object from the given file descriptor.\n\
1790The remaining arguments are the same as for socket().";
1791
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001792#endif /* NO_DUP */
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001793
Guido van Rossum82a5c661998-07-07 20:45:43 +00001794
Guido van Rossum006bf911996-06-12 04:04:55 +00001795static PyObject *
1796BUILD_FUNC_DEF_2(PySocket_ntohs, PyObject *, self, PyObject *, args)
1797{
1798 int x1, x2;
1799
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001800 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00001801 return NULL;
1802 }
1803 x2 = (int)ntohs((short)x1);
1804 return PyInt_FromLong(x2);
1805}
1806
Guido van Rossum82a5c661998-07-07 20:45:43 +00001807static char ntohs_doc[] =
1808"ntohs(integer) -> integer\n\
1809\n\
1810Convert a 16-bit integer from network to host byte order.";
1811
1812
Guido van Rossum006bf911996-06-12 04:04:55 +00001813static PyObject *
1814BUILD_FUNC_DEF_2(PySocket_ntohl, PyObject *, self, PyObject *, args)
1815{
1816 int x1, x2;
1817
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001818 if (!PyArg_ParseTuple(args, "i:ntohl", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00001819 return NULL;
1820 }
1821 x2 = ntohl(x1);
1822 return PyInt_FromLong(x2);
1823}
1824
Guido van Rossum82a5c661998-07-07 20:45:43 +00001825static char ntohl_doc[] =
1826"ntohl(integer) -> integer\n\
1827\n\
1828Convert a 32-bit integer from network to host byte order.";
1829
1830
Guido van Rossum006bf911996-06-12 04:04:55 +00001831static PyObject *
1832BUILD_FUNC_DEF_2(PySocket_htons, PyObject *, self, PyObject *, args)
1833{
1834 int x1, x2;
1835
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001836 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00001837 return NULL;
1838 }
1839 x2 = (int)htons((short)x1);
1840 return PyInt_FromLong(x2);
1841}
1842
Guido van Rossum82a5c661998-07-07 20:45:43 +00001843static char htons_doc[] =
1844"htons(integer) -> integer\n\
1845\n\
1846Convert a 16-bit integer from host to network byte order.";
1847
1848
Guido van Rossum006bf911996-06-12 04:04:55 +00001849static PyObject *
1850BUILD_FUNC_DEF_2(PySocket_htonl, PyObject *, self, PyObject *, args)
1851{
1852 int x1, x2;
1853
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001854 if (!PyArg_ParseTuple(args, "i:htonl", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00001855 return NULL;
1856 }
1857 x2 = htonl(x1);
1858 return PyInt_FromLong(x2);
1859}
1860
Guido van Rossum82a5c661998-07-07 20:45:43 +00001861static char htonl_doc[] =
1862"htonl(integer) -> integer\n\
1863\n\
1864Convert a 32-bit integer from host to network byte order.";
1865
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001866/*
1867 * socket.inet_aton() and socket.inet_ntoa() functions
1868 *
1869 * written 20 Aug 1999 by Ben Gertzfield <che@debian.org> <- blame him!
1870 *
1871 */
1872
1873static char inet_aton_doc[] =
1874"inet_aton(string) -> packed 32-bit IP representation\n\
1875\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00001876Convert an IP address in string format (123.45.67.89) to the 32-bit packed\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001877binary format used in low-level network functions.";
1878
1879static PyObject*
1880BUILD_FUNC_DEF_2(PySocket_inet_aton, PyObject *, self, PyObject *, args)
1881{
Guido van Rossuma2e48551999-09-09 15:42:59 +00001882#ifndef INADDR_NONE
1883#define INADDR_NONE (-1)
1884#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001885
Guido van Rossumc6a164b1999-08-20 19:11:27 +00001886 /* Have to use inet_addr() instead */
1887 char *ip_addr;
1888 long packed_addr;
1889
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001890 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) {
Guido van Rossumc6a164b1999-08-20 19:11:27 +00001891 return NULL;
1892 }
Guido van Rossumff3ab422000-04-24 15:16:03 +00001893#ifdef USE_GUSI1
Guido van Rossum1b6e4631999-11-04 18:22:29 +00001894 packed_addr = (long)inet_addr(ip_addr).s_addr;
1895#else
Guido van Rossumc6a164b1999-08-20 19:11:27 +00001896 packed_addr = inet_addr(ip_addr);
Guido van Rossum1b6e4631999-11-04 18:22:29 +00001897#endif
Guido van Rossumc6a164b1999-08-20 19:11:27 +00001898
1899 if (packed_addr == INADDR_NONE) { /* invalid address */
1900 PyErr_SetString(PySocket_Error,
1901 "illegal IP address string passed to inet_aton");
1902 return NULL;
1903 }
1904
1905 return PyString_FromStringAndSize((char *) &packed_addr,
1906 sizeof(packed_addr));
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001907}
1908
1909static char inet_ntoa_doc[] =
Fred Drakee0661342000-03-07 14:05:16 +00001910"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001911\n\
1912Convert an IP address from 32-bit packed binary format to string format";
1913
1914static PyObject*
1915BUILD_FUNC_DEF_2(PySocket_inet_ntoa, PyObject *, self, PyObject *, args)
1916{
1917 char *packed_str;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00001918 int addr_len;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001919 struct in_addr packed_addr;
1920
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001921 if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) {
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001922 return NULL;
1923 }
1924
1925 if (addr_len != sizeof(packed_addr)) {
1926 PyErr_SetString(PySocket_Error,
1927 "packed IP wrong length for inet_ntoa");
1928 return NULL;
1929 }
1930
1931 memcpy(&packed_addr, packed_str, addr_len);
1932
1933 return PyString_FromString(inet_ntoa(packed_addr));
1934}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001935
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001936
1937#ifdef USE_SSL
1938
1939/* This is a C function to be called for new object initialization */
1940static SSLObject *
1941BUILD_FUNC_DEF_3(newSSLObject,
1942 PySocketSockObject *,Sock, char*,key_file, char*,cert_file)
1943{
1944 SSLObject *self;
1945 char *str;
1946
1947#if 0
1948 meth=SSLv23_client_method();
1949 meth=SSLv3_client_method();
1950 meth=SSLv2_client_method();
1951#endif
1952
Guido van Rossumb18618d2000-05-03 23:44:39 +00001953 self = PyObject_New(SSLObject, &SSL_Type); /* Create new object */
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001954 if (self == NULL){
1955 PyErr_SetObject(SSLErrorObject,
1956 PyString_FromString("newSSLObject error"));
1957 return NULL;
1958 }
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001959 memset(self->server, '\0', sizeof(char) * 256);
1960 memset(self->issuer, '\0', sizeof(char) * 256);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001961
1962 self->x_attr = PyDict_New();
1963 self->ctx = SSL_CTX_new(SSLv23_method()); /* Set up context */
1964 if (self->ctx == NULL) {
1965 PyErr_SetObject(SSLErrorObject,
1966 PyString_FromString("SSL_CTX_new error"));
Guido van Rossumb18618d2000-05-03 23:44:39 +00001967 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001968 return NULL;
1969 }
1970
1971 if ( (key_file && !cert_file) || (!key_file && cert_file) )
1972 {
1973 PyErr_SetObject(SSLErrorObject,
1974 PyString_FromString(
1975 "Both the key & certificate files must be specified"));
Guido van Rossumb18618d2000-05-03 23:44:39 +00001976 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001977 return NULL;
1978 }
1979
1980 if (key_file && cert_file)
1981 {
1982 if (SSL_CTX_use_PrivateKey_file(self->ctx, key_file,
1983 SSL_FILETYPE_PEM) < 1)
1984 {
1985 PyErr_SetObject(SSLErrorObject,
1986 PyString_FromString(
1987 "SSL_CTX_use_PrivateKey_file error"));
Guido van Rossumb18618d2000-05-03 23:44:39 +00001988 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001989 return NULL;
1990 }
1991
1992 if (SSL_CTX_use_certificate_chain_file(self->ctx,
1993 cert_file) < 1)
1994 {
1995 PyErr_SetObject(SSLErrorObject,
1996 PyString_FromString(
1997 "SSL_CTX_use_certificate_chain_file error"));
Guido van Rossumb18618d2000-05-03 23:44:39 +00001998 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001999 return NULL;
2000 }
2001 }
2002
2003 SSL_CTX_set_verify(self->ctx,
2004 SSL_VERIFY_NONE, NULL); /* set verify lvl */
2005 self->ssl = SSL_new(self->ctx); /* New ssl struct */
2006 SSL_set_fd(self->ssl, Sock->sock_fd); /* Set the socket for SSL */
2007 SSL_set_connect_state(self->ssl);
2008
2009 if ((SSL_connect(self->ssl)) == -1) {
2010 /* Actually negotiate SSL connection */
2011 PyErr_SetObject(SSLErrorObject,
2012 PyString_FromString("SSL_connect error"));
Guido van Rossumb18618d2000-05-03 23:44:39 +00002013 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002014 return NULL;
2015 }
2016 self->ssl->debug = 1;
2017
2018 if ((self->server_cert = SSL_get_peer_certificate(self->ssl))) {
2019 X509_NAME_oneline(X509_get_subject_name(self->server_cert),
2020 self->server, 256);
2021 X509_NAME_oneline(X509_get_issuer_name(self->server_cert),
2022 self->issuer, 256);
2023 }
2024 self->x_attr = NULL;
2025 self->Socket = Sock;
2026 Py_INCREF(self->Socket);
2027 return self;
2028}
2029
2030/* This is the Python function called for new object initialization */
2031static PyObject *
2032BUILD_FUNC_DEF_2(PySocket_ssl, PyObject *, self, PyObject *, args)
2033{
2034 SSLObject *rv;
2035 PySocketSockObject *Sock;
2036 char *key_file;
2037 char *cert_file;
2038
Guido van Rossum43713e52000-02-29 13:59:29 +00002039 if (!PyArg_ParseTuple(args, "O!zz:ssl",
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002040 &PySocketSock_Type, (PyObject*)&Sock,
2041 &key_file, &cert_file) )
2042 return NULL;
2043
2044 rv = newSSLObject(Sock, key_file, cert_file);
2045 if ( rv == NULL )
2046 return NULL;
2047 return (PyObject *)rv;
2048}
2049
2050static char ssl_doc[] =
2051"ssl(socket, keyfile, certfile) -> sslobject";
2052
2053static PyObject *
2054BUILD_FUNC_DEF_2(SSL_server, SSLObject *, self, PyObject *, args)
2055{
2056 return PyString_FromString(self->server);
2057}
2058
2059static PyObject *
2060BUILD_FUNC_DEF_2(SSL_issuer, SSLObject *, self, PyObject *, args)
2061{
2062 return PyString_FromString(self->issuer);
2063}
2064
2065
2066/* SSL object methods */
2067
2068static PyMethodDef SSLMethods[] = {
2069 { "write", (PyCFunction)SSL_SSLwrite, 1 },
2070 { "read", (PyCFunction)SSL_SSLread, 1 },
2071 { "server", (PyCFunction)SSL_server, 1 },
2072 { "issuer", (PyCFunction)SSL_issuer, 1 },
2073 { NULL, NULL}
2074};
2075
2076static void SSL_dealloc(SSLObject *self)
2077{
2078 if (self->server_cert) /* Possible not to have one? */
2079 X509_free (self->server_cert);
2080 SSL_CTX_free(self->ctx);
2081 SSL_free(self->ssl);
2082 Py_XDECREF(self->x_attr);
2083 Py_XDECREF(self->Socket);
Guido van Rossumb18618d2000-05-03 23:44:39 +00002084 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002085}
2086
2087static PyObject *SSL_getattr(SSLObject *self, char *name)
2088{
2089 return Py_FindMethod(SSLMethods, (PyObject *)self, name);
2090}
2091
2092staticforward PyTypeObject SSL_Type = {
2093 PyObject_HEAD_INIT(&PyType_Type)
2094 0, /*ob_size*/
2095 "SSL", /*tp_name*/
2096 sizeof(SSLObject), /*tp_basicsize*/
2097 0, /*tp_itemsize*/
2098 /* methods */
2099 (destructor)SSL_dealloc, /*tp_dealloc*/
2100 0, /*tp_print*/
2101 (getattrfunc)SSL_getattr, /*tp_getattr*/
2102 0, /*tp_setattr*/
2103 0, /*tp_compare*/
2104 0, /*tp_repr*/
2105 0, /*tp_as_number*/
2106 0, /*tp_as_sequence*/
2107 0, /*tp_as_mapping*/
2108 0, /*tp_hash*/
2109};
2110
2111
2112
2113static PyObject *SSL_SSLwrite(SSLObject *self, PyObject *args)
2114{
2115 char *data;
2116 int len = 0;
2117
Guido van Rossum43713e52000-02-29 13:59:29 +00002118 if (!PyArg_ParseTuple(args, "s|i:write", &data, &len))
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002119 return NULL;
2120
2121 if (!len)
2122 len = strlen(data);
2123
2124 len = SSL_write(self->ssl, data, len);
2125 return PyInt_FromLong((long)len);
2126}
2127
2128static PyObject *SSL_SSLread(SSLObject *self, PyObject *args)
2129{
2130 PyObject *buf;
2131 int count = 0;
2132 int len = 1024;
2133 int res;
2134
Guido van Rossum43713e52000-02-29 13:59:29 +00002135 PyArg_ParseTuple(args, "|i:read", &len);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002136
2137 if (!(buf = PyString_FromStringAndSize((char *) 0, len)))
2138 return NULL; /* Error object should already be set */
2139
2140 count = SSL_read(self->ssl, PyString_AsString(buf), len);
2141 res = SSL_get_error(self->ssl, count);
2142
2143 switch (res) {
2144 case 0: /* Good return value! */
2145 break;
2146 case 6:
2147 PyErr_SetString(SSLErrorObject, "EOF");
2148 Py_DECREF(buf);
2149 return NULL;
2150 break;
2151 case 5:
2152 default:
2153 return PyErr_SetFromErrno(SSLErrorObject);
2154 break;
2155 }
2156
2157 fflush(stderr);
2158
2159 if (count < 0) {
2160 Py_DECREF(buf);
2161 return PyErr_SetFromErrno(SSLErrorObject);
2162 }
2163
2164 if (count != len && _PyString_Resize(&buf, count) < 0)
2165 return NULL;
2166 return buf;
2167}
2168
2169#endif /* USE_SSL */
2170
2171
Guido van Rossum30a685f1991-06-27 15:51:29 +00002172/* List of functions exported by this module. */
2173
Guido van Rossum73624e91994-10-10 17:59:00 +00002174static PyMethodDef PySocket_methods[] = {
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002175 {"gethostbyname", PySocket_gethostbyname, 1, gethostbyname_doc},
2176 {"gethostbyname_ex", PySocket_gethostbyname_ex, 1, ghbn_ex_doc},
2177 {"gethostbyaddr", PySocket_gethostbyaddr, 1, gethostbyaddr_doc},
2178 {"gethostname", PySocket_gethostname, 1, gethostname_doc},
2179 {"getservbyname", PySocket_getservbyname, 1, getservbyname_doc},
2180 {"getprotobyname", PySocket_getprotobyname, 1,getprotobyname_doc},
Guido van Rossum82a5c661998-07-07 20:45:43 +00002181 {"socket", PySocket_socket, 1, socket_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002182#ifndef NO_DUP
Guido van Rossum82a5c661998-07-07 20:45:43 +00002183 {"fromfd", PySocket_fromfd, 1, fromfd_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002184#endif
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002185 {"ntohs", PySocket_ntohs, 1, ntohs_doc},
2186 {"ntohl", PySocket_ntohl, 1, ntohl_doc},
2187 {"htons", PySocket_htons, 1, htons_doc},
2188 {"htonl", PySocket_htonl, 1, htonl_doc},
2189 {"inet_aton", PySocket_inet_aton, 1, inet_aton_doc},
2190 {"inet_ntoa", PySocket_inet_ntoa, 1, inet_ntoa_doc},
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002191#ifdef USE_SSL
2192 {"ssl", PySocket_ssl, 1, ssl_doc},
2193#endif /* USE_SSL */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002194 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002195};
2196
Guido van Rossum30a685f1991-06-27 15:51:29 +00002197
2198/* Convenience routine to export an integer value.
Barry Warsaw752300b1997-01-03 17:18:10 +00002199 *
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002200 * Errors are silently ignored, for better or for worse...
Barry Warsaw752300b1997-01-03 17:18:10 +00002201 */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002202static void
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002203BUILD_FUNC_DEF_3(insint,PyObject *,d, char *,name, int,value)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002204{
Guido van Rossum73624e91994-10-10 17:59:00 +00002205 PyObject *v = PyInt_FromLong((long) value);
Barry Warsaw752300b1997-01-03 17:18:10 +00002206 if (!v || PyDict_SetItemString(d, name, v))
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002207 PyErr_Clear();
Barry Warsaw752300b1997-01-03 17:18:10 +00002208
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002209 Py_XDECREF(v);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002210}
2211
Guido van Rossum30a685f1991-06-27 15:51:29 +00002212
Guido van Rossum8d665e61996-06-26 18:22:49 +00002213#ifdef MS_WINDOWS
Guido van Rossumbe32c891996-06-20 16:25:29 +00002214
2215/* Additional initialization and cleanup for NT/Windows */
2216
2217static void
2218NTcleanup()
2219{
2220 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00002221}
2222
2223static int
2224NTinit()
2225{
2226 WSADATA WSAData;
2227 int ret;
2228 char buf[100];
2229 ret = WSAStartup(0x0101, &WSAData);
2230 switch (ret) {
2231 case 0: /* no error */
2232 atexit(NTcleanup);
2233 return 1;
2234 case WSASYSNOTREADY:
2235 PyErr_SetString(PyExc_ImportError,
2236 "WSAStartup failed: network not ready");
2237 break;
2238 case WSAVERNOTSUPPORTED:
2239 case WSAEINVAL:
2240 PyErr_SetString(PyExc_ImportError,
2241 "WSAStartup failed: requested version not supported");
2242 break;
2243 default:
2244 sprintf(buf, "WSAStartup failed: error code %d", ret);
2245 PyErr_SetString(PyExc_ImportError, buf);
2246 break;
2247 }
2248 return 0;
2249}
2250
Guido van Rossum8d665e61996-06-26 18:22:49 +00002251#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00002252
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002253#if defined(PYOS_OS2)
2254
2255/* Additional initialization and cleanup for OS/2 */
2256
2257static void
2258OS2cleanup()
2259{
2260 /* No cleanup is necessary for OS/2 Sockets */
2261}
2262
2263static int
2264OS2init()
2265{
2266 char reason[64];
2267 int rc = sock_init();
2268
2269 if (rc == 0) {
Guido van Rossum32c575d1997-12-02 20:37:32 +00002270 atexit(OS2cleanup);
2271 return 1; /* Indicate Success */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002272 }
2273
2274 sprintf(reason, "OS/2 TCP/IP Error# %d", sock_errno());
2275 PyErr_SetString(PyExc_ImportError, reason);
2276
Guido van Rossum32c575d1997-12-02 20:37:32 +00002277 return 0; /* Indicate Failure */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002278}
2279
2280#endif /* PYOS_OS2 */
2281
Guido van Rossum30a685f1991-06-27 15:51:29 +00002282/* Initialize this module.
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002283 * This is called when the first 'import socket' is done,
2284 * via a table in config.c, if config.c is compiled with USE_SOCKET
2285 * defined.
2286 *
2287 * For MS_WINDOWS (which means any Windows variant), this module
2288 * is actually called "_socket", and there's a wrapper "socket.py"
2289 * which implements some missing functionality (such as makefile(),
2290 * dup() and fromfd()). The import of "_socket" may fail with an
2291 * ImportError exception if initialization of WINSOCK fails. When
2292 * WINSOCK is initialized succesfully, a call to WSACleanup() is
2293 * scheduled to be made at exit time.
2294 *
2295 * For OS/2, this module is also called "_socket" and uses a wrapper
2296 * "socket.py" which implements that functionality that is missing
2297 * when PC operating systems don't put socket descriptors in the
2298 * operating system's filesystem layer.
2299 */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002300
Guido van Rossum82a5c661998-07-07 20:45:43 +00002301static char module_doc[] =
2302"This module provides socket operations and some related functions.\n\
2303On Unix, it supports IP (Internet Protocol) and Unix domain sockets.\n\
2304On other systems, it only supports IP.\n\
2305\n\
2306Functions:\n\
2307\n\
2308socket() -- create a new socket object\n\
2309fromfd() -- create a socket object from an open file descriptor (*)\n\
2310gethostname() -- return the current hostname\n\
2311gethostbyname() -- map a hostname to its IP number\n\
2312gethostbyaddr() -- map an IP number or hostname to DNS info\n\
2313getservbyname() -- map a service name and a protocol name to a port number\n\
2314getprotobyname() -- mape a protocol name (e.g. 'tcp') to a number\n\
2315ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order\n\
2316htons(), htonl() -- convert 16, 32 bit int from host to network byte order\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002317inet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format\n\
2318inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89)\n\
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002319ssl() -- secure socket layer support (only available if configured)\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002320\n\
2321(*) not available on all platforms!)\n\
2322\n\
2323Special objects:\n\
2324\n\
2325SocketType -- type object for socket objects\n\
2326error -- exception raised for I/O errors\n\
2327\n\
2328Integer constants:\n\
2329\n\
2330AF_INET, AF_UNIX -- socket domains (first argument to socket() call)\n\
2331SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)\n\
2332\n\
2333Many other constants may be defined; these may be used in calls to\n\
2334the setsockopt() and getsockopt() methods.\n\
2335";
2336
2337static char sockettype_doc[] =
2338"A socket represents one endpoint of a network connection.\n\
2339\n\
2340Methods:\n\
2341\n\
2342accept() -- accept a connection, returning new socket and client address\n\
2343bind() -- bind the socket to a local address\n\
2344close() -- close the socket\n\
2345connect() -- connect the socket to a remote address\n\
2346connect_ex() -- connect, return an error code instead of an exception \n\
2347dup() -- return a new socket object identical to the current one (*)\n\
2348fileno() -- return underlying file descriptor\n\
2349getpeername() -- return remote address (*)\n\
2350getsockname() -- return local address\n\
2351getsockopt() -- get socket options\n\
2352listen() -- start listening for incoming connections\n\
2353makefile() -- return a file object corresponding tot the socket (*)\n\
2354recv() -- receive data\n\
2355recvfrom() -- receive data and sender's address\n\
2356send() -- send data\n\
2357sendto() -- send data to a given address\n\
2358setblocking() -- set or clear the blocking I/O flag\n\
2359setsockopt() -- set socket options\n\
2360shutdown() -- shut down traffic in one or both directions\n\
2361\n\
2362(*) not available on all platforms!)";
2363
Guido van Rossum3886bb61998-12-04 18:50:17 +00002364DL_EXPORT(void)
Guido van Rossumbcc20741998-08-04 22:53:56 +00002365#if defined(MS_WINDOWS) || defined(PYOS_OS2) || defined(__BEOS__)
Guido van Rossumbe32c891996-06-20 16:25:29 +00002366init_socket()
2367#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002368initsocket()
Guido van Rossumbe32c891996-06-20 16:25:29 +00002369#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002370{
Guido van Rossum73624e91994-10-10 17:59:00 +00002371 PyObject *m, *d;
Guido van Rossum8d665e61996-06-26 18:22:49 +00002372#ifdef MS_WINDOWS
Guido van Rossumbe32c891996-06-20 16:25:29 +00002373 if (!NTinit())
2374 return;
Guido van Rossum82a5c661998-07-07 20:45:43 +00002375 m = Py_InitModule3("_socket", PySocket_methods, module_doc);
Guido van Rossumbe32c891996-06-20 16:25:29 +00002376#else
Guido van Rossum32c575d1997-12-02 20:37:32 +00002377#if defined(__TOS_OS2__)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002378 if (!OS2init())
2379 return;
Guido van Rossum82a5c661998-07-07 20:45:43 +00002380 m = Py_InitModule3("_socket", PySocket_methods, module_doc);
Guido van Rossum32c575d1997-12-02 20:37:32 +00002381#else
Guido van Rossumbcc20741998-08-04 22:53:56 +00002382#if defined(__BEOS__)
2383 m = Py_InitModule3("_socket", PySocket_methods, module_doc);
2384#else
Guido van Rossum82a5c661998-07-07 20:45:43 +00002385 m = Py_InitModule3("socket", PySocket_methods, module_doc);
Guido van Rossumbcc20741998-08-04 22:53:56 +00002386#endif /* __BEOS__ */
Guido van Rossum32c575d1997-12-02 20:37:32 +00002387#endif
Guido van Rossumbe32c891996-06-20 16:25:29 +00002388#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00002389 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002390 PySocket_Error = PyErr_NewException("socket.error", NULL, NULL);
2391 if (PySocket_Error == NULL)
2392 return;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002393#ifdef USE_SSL
2394 SSL_load_error_strings();
2395 SSLeay_add_ssl_algorithms();
2396 SSLErrorObject = PyErr_NewException("socket.sslerror", NULL, NULL);
2397 if (SSLErrorObject == NULL)
2398 return;
2399 PyDict_SetItemString(d, "sslerror", SSLErrorObject);
2400 Py_INCREF(&SSL_Type);
2401 if (PyDict_SetItemString(d, "SSLType",
2402 (PyObject *)&SSL_Type) != 0)
2403 return;
2404#endif /* USE_SSL */
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002405 PyDict_SetItemString(d, "error", PySocket_Error);
Guido van Rossum3b4b6fc1997-05-21 14:37:37 +00002406 PySocketSock_Type.ob_type = &PyType_Type;
Guido van Rossum82a5c661998-07-07 20:45:43 +00002407 PySocketSock_Type.tp_doc = sockettype_doc;
Guido van Rossum3b4b6fc1997-05-21 14:37:37 +00002408 Py_INCREF(&PySocketSock_Type);
Guido van Rossum91ba64d1997-06-02 22:18:09 +00002409 if (PyDict_SetItemString(d, "SocketType",
Guido van Rossum3b4b6fc1997-05-21 14:37:37 +00002410 (PyObject *)&PySocketSock_Type) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002411 return;
Guido van Rossum09be4091999-08-09 14:40:40 +00002412
2413 /* Address families (we only support AF_INET and AF_UNIX) */
2414#ifdef AF_UNSPEC
2415 insint(d, "AF_UNSPEC", AF_UNSPEC);
2416#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002417 insint(d, "AF_INET", AF_INET);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002418#ifdef AF_UNIX
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002419 insint(d, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002420#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00002421#ifdef AF_AX25
2422 insint(d, "AF_AX25", AF_AX25); /* Amateur Radio AX.25 */
2423#endif
2424#ifdef AF_IPX
2425 insint(d, "AF_IPX", AF_IPX); /* Novell IPX */
2426#endif
2427#ifdef AF_APPLETALK
2428 insint(d, "AF_APPLETALK", AF_APPLETALK); /* Appletalk DDP */
2429#endif
2430#ifdef AF_NETROM
2431 insint(d, "AF_NETROM", AF_NETROM); /* Amateur radio NetROM */
2432#endif
2433#ifdef AF_BRIDGE
2434 insint(d, "AF_BRIDGE", AF_BRIDGE); /* Multiprotocol bridge */
2435#endif
2436#ifdef AF_AAL5
2437 insint(d, "AF_AAL5", AF_AAL5); /* Reserved for Werner's ATM */
2438#endif
2439#ifdef AF_X25
2440 insint(d, "AF_X25", AF_X25); /* Reserved for X.25 project */
2441#endif
2442#ifdef AF_INET6
2443 insint(d, "AF_INET6", AF_INET6); /* IP version 6 */
2444#endif
2445#ifdef AF_ROSE
2446 insint(d, "AF_ROSE", AF_ROSE); /* Amateur Radio X.25 PLP */
2447#endif
2448
2449 /* Socket types */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002450 insint(d, "SOCK_STREAM", SOCK_STREAM);
2451 insint(d, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00002452#ifndef __BEOS__
2453/* We have incomplete socket support. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002454 insint(d, "SOCK_RAW", SOCK_RAW);
2455 insint(d, "SOCK_SEQPACKET", SOCK_SEQPACKET);
2456 insint(d, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00002457#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002458
2459#ifdef SO_DEBUG
2460 insint(d, "SO_DEBUG", SO_DEBUG);
2461#endif
2462#ifdef SO_ACCEPTCONN
2463 insint(d, "SO_ACCEPTCONN", SO_ACCEPTCONN);
2464#endif
2465#ifdef SO_REUSEADDR
2466 insint(d, "SO_REUSEADDR", SO_REUSEADDR);
2467#endif
2468#ifdef SO_KEEPALIVE
2469 insint(d, "SO_KEEPALIVE", SO_KEEPALIVE);
2470#endif
2471#ifdef SO_DONTROUTE
2472 insint(d, "SO_DONTROUTE", SO_DONTROUTE);
2473#endif
2474#ifdef SO_BROADCAST
2475 insint(d, "SO_BROADCAST", SO_BROADCAST);
2476#endif
2477#ifdef SO_USELOOPBACK
2478 insint(d, "SO_USELOOPBACK", SO_USELOOPBACK);
2479#endif
2480#ifdef SO_LINGER
2481 insint(d, "SO_LINGER", SO_LINGER);
2482#endif
2483#ifdef SO_OOBINLINE
2484 insint(d, "SO_OOBINLINE", SO_OOBINLINE);
2485#endif
2486#ifdef SO_REUSEPORT
2487 insint(d, "SO_REUSEPORT", SO_REUSEPORT);
2488#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002489#ifdef SO_SNDBUF
2490 insint(d, "SO_SNDBUF", SO_SNDBUF);
2491#endif
2492#ifdef SO_RCVBUF
2493 insint(d, "SO_RCVBUF", SO_RCVBUF);
2494#endif
2495#ifdef SO_SNDLOWAT
2496 insint(d, "SO_SNDLOWAT", SO_SNDLOWAT);
2497#endif
2498#ifdef SO_RCVLOWAT
2499 insint(d, "SO_RCVLOWAT", SO_RCVLOWAT);
2500#endif
2501#ifdef SO_SNDTIMEO
2502 insint(d, "SO_SNDTIMEO", SO_SNDTIMEO);
2503#endif
2504#ifdef SO_RCVTIMEO
2505 insint(d, "SO_RCVTIMEO", SO_RCVTIMEO);
2506#endif
2507#ifdef SO_ERROR
2508 insint(d, "SO_ERROR", SO_ERROR);
2509#endif
2510#ifdef SO_TYPE
2511 insint(d, "SO_TYPE", SO_TYPE);
2512#endif
2513
2514 /* Maximum number of connections for "listen" */
2515#ifdef SOMAXCONN
2516 insint(d, "SOMAXCONN", SOMAXCONN);
2517#else
2518 insint(d, "SOMAXCONN", 5); /* Common value */
2519#endif
2520
2521 /* Flags for send, recv */
2522#ifdef MSG_OOB
2523 insint(d, "MSG_OOB", MSG_OOB);
2524#endif
2525#ifdef MSG_PEEK
2526 insint(d, "MSG_PEEK", MSG_PEEK);
2527#endif
2528#ifdef MSG_DONTROUTE
2529 insint(d, "MSG_DONTROUTE", MSG_DONTROUTE);
2530#endif
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00002531#ifdef MSG_DONTWAIT
Guido van Rossum14f3f592000-04-25 21:53:58 +00002532 insint(d, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00002533#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002534#ifdef MSG_EOR
2535 insint(d, "MSG_EOR", MSG_EOR);
2536#endif
2537#ifdef MSG_TRUNC
2538 insint(d, "MSG_TRUNC", MSG_TRUNC);
2539#endif
2540#ifdef MSG_CTRUNC
2541 insint(d, "MSG_CTRUNC", MSG_CTRUNC);
2542#endif
2543#ifdef MSG_WAITALL
2544 insint(d, "MSG_WAITALL", MSG_WAITALL);
2545#endif
2546#ifdef MSG_BTAG
2547 insint(d, "MSG_BTAG", MSG_BTAG);
2548#endif
2549#ifdef MSG_ETAG
2550 insint(d, "MSG_ETAG", MSG_ETAG);
2551#endif
2552
2553 /* Protocol level and numbers, usable for [gs]etsockopt */
2554#ifdef SOL_SOCKET
2555 insint(d, "SOL_SOCKET", SOL_SOCKET);
2556#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00002557#ifdef SOL_IP
2558 insint(d, "SOL_IP", SOL_IP);
2559#else
2560 insint(d, "SOL_IP", 0);
2561#endif
2562#ifdef SOL_IPX
2563 insint(d, "SOL_IPX", SOL_IPX);
2564#endif
2565#ifdef SOL_AX25
2566 insint(d, "SOL_AX25", SOL_AX25);
2567#endif
2568#ifdef SOL_ATALK
2569 insint(d, "SOL_ATALK", SOL_ATALK);
2570#endif
2571#ifdef SOL_NETROM
2572 insint(d, "SOL_NETROM", SOL_NETROM);
2573#endif
2574#ifdef SOL_ROSE
2575 insint(d, "SOL_ROSE", SOL_ROSE);
2576#endif
2577#ifdef SOL_TCP
2578 insint(d, "SOL_TCP", SOL_TCP);
2579#else
2580 insint(d, "SOL_TCP", 6);
2581#endif
2582#ifdef SOL_UDP
2583 insint(d, "SOL_UDP", SOL_UDP);
2584#else
2585 insint(d, "SOL_UDP", 17);
2586#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002587#ifdef IPPROTO_IP
2588 insint(d, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00002589#else
2590 insint(d, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002591#endif
2592#ifdef IPPROTO_ICMP
2593 insint(d, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00002594#else
2595 insint(d, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002596#endif
2597#ifdef IPPROTO_IGMP
2598 insint(d, "IPPROTO_IGMP", IPPROTO_IGMP);
2599#endif
2600#ifdef IPPROTO_GGP
2601 insint(d, "IPPROTO_GGP", IPPROTO_GGP);
2602#endif
2603#ifdef IPPROTO_TCP
2604 insint(d, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00002605#else
2606 insint(d, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002607#endif
2608#ifdef IPPROTO_EGP
2609 insint(d, "IPPROTO_EGP", IPPROTO_EGP);
2610#endif
2611#ifdef IPPROTO_PUP
2612 insint(d, "IPPROTO_PUP", IPPROTO_PUP);
2613#endif
2614#ifdef IPPROTO_UDP
2615 insint(d, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00002616#else
2617 insint(d, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002618#endif
2619#ifdef IPPROTO_IDP
2620 insint(d, "IPPROTO_IDP", IPPROTO_IDP);
2621#endif
2622#ifdef IPPROTO_HELLO
2623 insint(d, "IPPROTO_HELLO", IPPROTO_HELLO);
2624#endif
2625#ifdef IPPROTO_ND
2626 insint(d, "IPPROTO_ND", IPPROTO_ND);
2627#endif
2628#ifdef IPPROTO_TP
2629 insint(d, "IPPROTO_TP", IPPROTO_TP);
2630#endif
2631#ifdef IPPROTO_XTP
2632 insint(d, "IPPROTO_XTP", IPPROTO_XTP);
2633#endif
2634#ifdef IPPROTO_EON
2635 insint(d, "IPPROTO_EON", IPPROTO_EON);
2636#endif
2637#ifdef IPPROTO_BIP
2638 insint(d, "IPPROTO_BIP", IPPROTO_BIP);
2639#endif
2640/**/
2641#ifdef IPPROTO_RAW
2642 insint(d, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00002643#else
2644 insint(d, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002645#endif
2646#ifdef IPPROTO_MAX
2647 insint(d, "IPPROTO_MAX", IPPROTO_MAX);
2648#endif
2649
2650 /* Some port configuration */
2651#ifdef IPPORT_RESERVED
2652 insint(d, "IPPORT_RESERVED", IPPORT_RESERVED);
2653#else
2654 insint(d, "IPPORT_RESERVED", 1024);
2655#endif
2656#ifdef IPPORT_USERRESERVED
2657 insint(d, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
2658#else
2659 insint(d, "IPPORT_USERRESERVED", 5000);
2660#endif
2661
Guido van Rossum5f05eb41995-02-17 15:11:07 +00002662 /* Some reserved IP v.4 addresses */
2663#ifdef INADDR_ANY
2664 insint(d, "INADDR_ANY", INADDR_ANY);
2665#else
2666 insint(d, "INADDR_ANY", 0x00000000);
2667#endif
2668#ifdef INADDR_BROADCAST
2669 insint(d, "INADDR_BROADCAST", INADDR_BROADCAST);
2670#else
2671 insint(d, "INADDR_BROADCAST", 0xffffffff);
2672#endif
2673#ifdef INADDR_LOOPBACK
2674 insint(d, "INADDR_LOOPBACK", INADDR_LOOPBACK);
2675#else
2676 insint(d, "INADDR_LOOPBACK", 0x7F000001);
2677#endif
2678#ifdef INADDR_UNSPEC_GROUP
2679 insint(d, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
2680#else
2681 insint(d, "INADDR_UNSPEC_GROUP", 0xe0000000);
2682#endif
2683#ifdef INADDR_ALLHOSTS_GROUP
2684 insint(d, "INADDR_ALLHOSTS_GROUP", INADDR_ALLHOSTS_GROUP);
2685#else
2686 insint(d, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
2687#endif
2688#ifdef INADDR_MAX_LOCAL_GROUP
2689 insint(d, "INADDR_MAX_LOCAL_GROUP", INADDR_MAX_LOCAL_GROUP);
2690#else
2691 insint(d, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
2692#endif
2693#ifdef INADDR_NONE
2694 insint(d, "INADDR_NONE", INADDR_NONE);
2695#else
2696 insint(d, "INADDR_NONE", 0xffffffff);
2697#endif
2698
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002699 /* IP [gs]etsockopt options */
2700#ifdef IP_OPTIONS
2701 insint(d, "IP_OPTIONS", IP_OPTIONS);
2702#endif
2703#ifdef IP_HDRINCL
2704 insint(d, "IP_HDRINCL", IP_HDRINCL);
2705#endif
2706#ifdef IP_TOS
2707 insint(d, "IP_TOS", IP_TOS);
2708#endif
2709#ifdef IP_TTL
2710 insint(d, "IP_TTL", IP_TTL);
2711#endif
2712#ifdef IP_RECVOPTS
2713 insint(d, "IP_RECVOPTS", IP_RECVOPTS);
2714#endif
2715#ifdef IP_RECVRETOPTS
2716 insint(d, "IP_RECVRETOPTS", IP_RECVRETOPTS);
2717#endif
2718#ifdef IP_RECVDSTADDR
2719 insint(d, "IP_RECVDSTADDR", IP_RECVDSTADDR);
2720#endif
2721#ifdef IP_RETOPTS
2722 insint(d, "IP_RETOPTS", IP_RETOPTS);
2723#endif
2724#ifdef IP_MULTICAST_IF
2725 insint(d, "IP_MULTICAST_IF", IP_MULTICAST_IF);
2726#endif
2727#ifdef IP_MULTICAST_TTL
2728 insint(d, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
2729#endif
2730#ifdef IP_MULTICAST_LOOP
2731 insint(d, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
2732#endif
2733#ifdef IP_ADD_MEMBERSHIP
2734 insint(d, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
2735#endif
2736#ifdef IP_DROP_MEMBERSHIP
2737 insint(d, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
2738#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00002739#ifdef IP_DEFAULT_MULTICAST_TTL
2740 insint(d, "IP_DEFAULT_MULTICAST_TTL", IP_DEFAULT_MULTICAST_TTL);
2741#endif
2742#ifdef IP_DEFAULT_MULTICAST_LOOP
2743 insint(d, "IP_DEFAULT_MULTICAST_LOOP", IP_DEFAULT_MULTICAST_LOOP);
2744#endif
2745#ifdef IP_MAX_MEMBERSHIPS
2746 insint(d, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
2747#endif
2748
2749 /* TCP options */
2750#ifdef TCP_NODELAY
2751 insint(d, "TCP_NODELAY", TCP_NODELAY);
2752#endif
2753#ifdef TCP_MAXSEG
2754 insint(d, "TCP_MAXSEG", TCP_MAXSEG);
2755#endif
2756
2757 /* IPX options */
2758#ifdef IPX_TYPE
2759 insint(d, "IPX_TYPE", IPX_TYPE);
2760#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002761
2762 /* Initialize gethostbyname lock */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002763#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum65d5b571998-12-21 19:32:43 +00002764 gethostbyname_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002765#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002766}