blob: 284e5b78b2b2b272337e43b7edbb7de9c6a940a4 [file] [log] [blame]
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001/* Socket module */
2
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00003/* SSL support based on patches by Brian E Gallew and Laszlo Kovacs */
4
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005/*
6This module provides an interface to Berkeley socket IPC.
7
8Limitations:
9
Guido van Rossum30a685f1991-06-27 15:51:29 +000010- only AF_INET and AF_UNIX address families are supported
Guido van Rossum81194471991-07-27 21:42:02 +000011- no read/write operations (use send/recv or makefile instead)
Guido van Rossumbe32c891996-06-20 16:25:29 +000012- additional restrictions apply on Windows
Guido van Rossum6574b3e1991-06-25 21:36:08 +000013
Guido van Rossum27e177d1995-03-16 15:43:47 +000014Module interface:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000015
Guido van Rossum27e177d1995-03-16 15:43:47 +000016- socket.error: exception raised for socket specific errors
Guido van Rossum30a685f1991-06-27 15:51:29 +000017- socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd')
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000018- socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...])
Guido van Rossum27e177d1995-03-16 15:43:47 +000019- socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com')
Guido van Rossum25405c71996-12-19 16:42:52 +000020- socket.getprotobyname(protocolname) --> protocol number
Guido van Rossum27e177d1995-03-16 15:43:47 +000021- socket.getservbyname(servicename, protocolname) --> port number
Guido van Rossum4dd2a7e1991-07-01 18:51:33 +000022- socket.socket(family, type [, proto]) --> new socket object
Guido van Rossum006bf911996-06-12 04:04:55 +000023- socket.ntohs(16 bit value) --> new int object
24- socket.ntohl(32 bit value) --> new int object
25- socket.htons(16 bit value) --> new int object
26- socket.htonl(32 bit value) --> new int object
Guido van Rossum27e177d1995-03-16 15:43:47 +000027- socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +000028- socket.inet_aton(IP address) -> 32-bit packed IP representation
29- socket.inet_ntoa(packed IP) -> IP address string
Guido van Rossum09c8b6c1999-12-07 21:37:17 +000030- socket.ssl(socket, keyfile, certfile) -> new ssl object
Guido van Rossum6574b3e1991-06-25 21:36:08 +000031- an Internet socket address is a pair (hostname, port)
32 where hostname can be anything recognized by gethostbyname()
33 (including the dd.dd.dd.dd notation) and port is in host byte order
34- where a hostname is returned, the dd.dd.dd.dd notation is used
Guido van Rossum27e177d1995-03-16 15:43:47 +000035- a UNIX domain socket address is a string specifying the pathname
Guido van Rossum6574b3e1991-06-25 21:36:08 +000036
Guido van Rossum30a685f1991-06-27 15:51:29 +000037Socket methods:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000038
Guido van Rossum81194471991-07-27 21:42:02 +000039- s.accept() --> new socket object, sockaddr
Fred Drake728819a2000-07-01 03:40:12 +000040- s.bind(sockaddr) --> None
Guido van Rossum27e177d1995-03-16 15:43:47 +000041- s.close() --> None
Fred Drake728819a2000-07-01 03:40:12 +000042- s.connect(sockaddr) --> None
43- s.connect_ex(sockaddr) --> 0 or errno (handy for e.g. async connect)
Guido van Rossum27e177d1995-03-16 15:43:47 +000044- s.fileno() --> file descriptor
Guido van Rossumbe32c891996-06-20 16:25:29 +000045- s.dup() --> same as socket.fromfd(os.dup(s.fileno(), ...)
Guido van Rossumc89705d1992-11-26 08:54:07 +000046- s.getpeername() --> sockaddr
Guido van Rossum27e177d1995-03-16 15:43:47 +000047- s.getsockname() --> sockaddr
48- s.getsockopt(level, optname[, buflen]) --> int or string
49- s.listen(backlog) --> None
Guido van Rossum6b144911995-03-14 15:05:13 +000050- s.makefile([mode[, bufsize]]) --> file object
Guido van Rossum27e177d1995-03-16 15:43:47 +000051- s.recv(buflen [,flags]) --> string
52- s.recvfrom(buflen [,flags]) --> string, sockaddr
Guido van Rossumb6775db1994-08-01 11:34:53 +000053- s.send(string [,flags]) --> nbytes
54- s.sendto(string, [flags,] sockaddr) --> nbytes
Guido van Rossum27e177d1995-03-16 15:43:47 +000055- s.setblocking(0 | 1) --> None
56- s.setsockopt(level, optname, value) --> None
57- s.shutdown(how) --> None
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000058- repr(s) --> "<socket object, fd=%d, family=%d, type=%d, protocol=%d>"
Guido van Rossum30a685f1991-06-27 15:51:29 +000059
Guido van Rossum6574b3e1991-06-25 21:36:08 +000060*/
61
Guido van Rossum73624e91994-10-10 17:59:00 +000062#include "Python.h"
Guido van Rossum3baaa131999-03-22 21:44:51 +000063
Guido van Rossum9376b741999-09-15 22:01:40 +000064/* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure
65 script doesn't get this right, so we hardcode some platform checks below.
66 On the other hand, not all Linux versions agree, so there the settings
67 computed by the configure script are needed! */
68
69#ifndef linux
Guido van Rossume7de2061999-03-24 17:24:33 +000070#undef HAVE_GETHOSTBYNAME_R_3_ARG
71#undef HAVE_GETHOSTBYNAME_R_5_ARG
72#undef HAVE_GETHOSTBYNAME_R_6_ARG
Guido van Rossum9376b741999-09-15 22:01:40 +000073#endif
Guido van Rossume7de2061999-03-24 17:24:33 +000074
Guido van Rossum7a122991999-04-13 04:07:32 +000075#ifndef WITH_THREAD
76#undef HAVE_GETHOSTBYNAME_R
77#endif
78
Guido van Rossume7de2061999-03-24 17:24:33 +000079#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7a122991999-04-13 04:07:32 +000080#if defined(_AIX) || defined(__osf__)
Guido van Rossume7de2061999-03-24 17:24:33 +000081#define HAVE_GETHOSTBYNAME_R_3_ARG
82#elif defined(__sun__) || defined(__sgi)
83#define HAVE_GETHOSTBYNAME_R_5_ARG
84#elif defined(linux)
Guido van Rossum9376b741999-09-15 22:01:40 +000085/* Rely on the configure script */
Guido van Rossume7de2061999-03-24 17:24:33 +000086#else
87#undef HAVE_GETHOSTBYNAME_R
88#endif
89#endif
90
Guido van Rossum3baaa131999-03-22 21:44:51 +000091#if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && !defined(MS_WINDOWS)
92#define USE_GETHOSTBYNAME_LOCK
93#endif
94
95#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum49b56061998-10-01 20:42:43 +000096#include "pythread.h"
Guido van Rossum4f199ea1998-04-09 20:56:35 +000097#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +000098
Guido van Rossuma376cc51996-12-05 23:43:35 +000099#ifdef HAVE_UNISTD_H
100#include <unistd.h>
101#endif
102
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000103#if defined(PYCC_VACPP)
104#include <types.h>
105#include <io.h>
106#include <sys/ioctl.h>
107#include <utils.h>
108#include <ctype.h>
109#endif
110
111#if defined(PYOS_OS2)
112#define INCL_DOS
113#define INCL_DOSERRORS
114#define INCL_NOPMAPI
115#include <os2.h>
116#endif
117
Guido van Rossumb6775db1994-08-01 11:34:53 +0000118#include <sys/types.h>
Guido van Rossumed233a51992-06-23 09:07:03 +0000119
Guido van Rossum81194471991-07-27 21:42:02 +0000120#include <signal.h>
Guido van Rossum6f489d91996-06-28 20:15:15 +0000121#ifndef MS_WINDOWS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000122#include <netdb.h>
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000123#include <sys/socket.h>
124#include <netinet/in.h>
Fred Drake11b09362000-05-16 13:30:12 +0000125#ifndef __BEOS__
Andrew M. Kuchling52a644c2000-03-31 17:24:30 +0000126#include <netinet/tcp.h>
Fred Drake11b09362000-05-16 13:30:12 +0000127#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000128
Guido van Rossum9376b741999-09-15 22:01:40 +0000129/* Headers needed for inet_ntoa() and inet_addr() */
130#ifdef __BEOS__
131#include <net/netdb.h>
132#else
Guido van Rossumff3ab422000-04-24 15:16:03 +0000133#ifndef USE_GUSI1
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000134#include <arpa/inet.h>
Guido van Rossum9376b741999-09-15 22:01:40 +0000135#endif
Guido van Rossum1b6e4631999-11-04 18:22:29 +0000136#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000137
Guido van Rossume4485b01994-09-07 14:32:49 +0000138#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000139#else
140#include <winsock.h>
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000141#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000142#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000143#ifdef HAVE_SYS_UN_H
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000144#include <sys/un.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000145#else
146#undef AF_UNIX
Guido van Rossum9575a441993-04-07 14:06:14 +0000147#endif
148
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000149#ifndef O_NDELAY
150#define O_NDELAY O_NONBLOCK /* For QNX only? */
151#endif
152
Guido van Rossumff3ab422000-04-24 15:16:03 +0000153#ifdef USE_GUSI1
Jack Jansen508537b1996-02-14 15:57:45 +0000154/* fdopen() isn't declared in stdio.h (sigh) */
155#include <GUSI.h>
156#endif
157
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000158#ifdef USE_SSL
159#include "rsa.h"
160#include "crypto.h"
161#include "x509.h"
162#include "pem.h"
163#include "ssl.h"
164#include "err.h"
165#endif /* USE_SSL */
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000166
Guido van Rossumbcc20741998-08-04 22:53:56 +0000167#if defined(MS_WINDOWS) || defined(__BEOS__)
168/* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000169/* seem to be a few differences in the API */
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000170#define SOCKETCLOSE closesocket
Guido van Rossumbe32c891996-06-20 16:25:29 +0000171#define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000172#endif
173
Fred Drakea04eaad2000-06-30 02:46:07 +0000174/* abstract the socket file descriptor type */
175#ifdef MS_WINDOWS
176typedef SOCKET SOCKET_T;
177# ifdef MS_WIN64
178# define SIZEOF_SOCKET_T 8
179# else
180# define SIZEOF_SOCKET_T 4
181# endif
182#else
183typedef int SOCKET_T;
184# define SIZEOF_SOCKET_T SIZEOF_INT
185#endif
186
187
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000188#if defined(PYOS_OS2)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000189#define SOCKETCLOSE soclose
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000190#define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000191#endif
192
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000193#ifndef SOCKETCLOSE
194#define SOCKETCLOSE close
195#endif
196
Guido van Rossum30a685f1991-06-27 15:51:29 +0000197/* Global variable holding the exception type for errors detected
198 by this module (but not argument type or memory errors, etc.). */
199
Guido van Rossum73624e91994-10-10 17:59:00 +0000200static PyObject *PySocket_Error;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000201
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000202#ifdef USE_SSL
203static PyObject *SSLErrorObject;
204#endif /* USE_SSL */
205
Guido van Rossum30a685f1991-06-27 15:51:29 +0000206
207/* Convenience function to raise an error according to errno
208 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000209
Guido van Rossum73624e91994-10-10 17:59:00 +0000210static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000211PySocket_Err(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000212{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000213#ifdef MS_WINDOWS
Mark Hammond46a733d2000-07-24 01:45:11 +0000214 int err_no = WSAGetLastError();
215 if (err_no) {
216 static struct { int no; const char *msg; } *msgp, msgs[] = {
217 { WSAEINTR, "Interrupted system call" },
218 { WSAEBADF, "Bad file descriptor" },
219 { WSAEACCES, "Permission denied" },
220 { WSAEFAULT, "Bad address" },
221 { WSAEINVAL, "Invalid argument" },
222 { WSAEMFILE, "Too many open files" },
223 { WSAEWOULDBLOCK,
224 "The socket operation could not complete "
225 "without blocking" },
226 { WSAEINPROGRESS, "Operation now in progress" },
227 { WSAEALREADY, "Operation already in progress" },
228 { WSAENOTSOCK, "Socket operation on non-socket" },
229 { WSAEDESTADDRREQ, "Destination address required" },
230 { WSAEMSGSIZE, "Message too long" },
231 { WSAEPROTOTYPE, "Protocol wrong type for socket" },
232 { WSAENOPROTOOPT, "Protocol not available" },
233 { WSAEPROTONOSUPPORT, "Protocol not supported" },
234 { WSAESOCKTNOSUPPORT, "Socket type not supported" },
235 { WSAEOPNOTSUPP, "Operation not supported" },
236 { WSAEPFNOSUPPORT, "Protocol family not supported" },
237 { WSAEAFNOSUPPORT, "Address family not supported" },
238 { WSAEADDRINUSE, "Address already in use" },
239 { WSAEADDRNOTAVAIL,
240 "Can't assign requested address" },
241 { WSAENETDOWN, "Network is down" },
242 { WSAENETUNREACH, "Network is unreachable" },
243 { WSAENETRESET,
244 "Network dropped connection on reset" },
245 { WSAECONNABORTED,
246 "Software caused connection abort" },
247 { WSAECONNRESET, "Connection reset by peer" },
248 { WSAENOBUFS, "No buffer space available" },
249 { WSAEISCONN, "Socket is already connected" },
250 { WSAENOTCONN, "Socket is not connected" },
251 { WSAESHUTDOWN, "Can't send after socket shutdown" },
252 { WSAETOOMANYREFS,
253 "Too many references: can't splice" },
254 { WSAETIMEDOUT, "Operation timed out" },
255 { WSAECONNREFUSED, "Connection refused" },
256 { WSAELOOP, "Too many levels of symbolic links" },
257 { WSAENAMETOOLONG, "File name too long" },
258 { WSAEHOSTDOWN, "Host is down" },
259 { WSAEHOSTUNREACH, "No route to host" },
260 { WSAENOTEMPTY, "Directory not empty" },
261 { WSAEPROCLIM, "Too many processes" },
262 { WSAEUSERS, "Too many users" },
263 { WSAEDQUOT, "Disc quota exceeded" },
264 { WSAESTALE, "Stale NFS file handle" },
265 { WSAEREMOTE, "Too many levels of remote in path" },
266 { WSASYSNOTREADY,
267 "Network subsystem is unvailable" },
268 { WSAVERNOTSUPPORTED,
269 "WinSock version is not supported" },
270 { WSANOTINITIALISED,
271 "Successful WSAStartup() not yet performed" },
272 { WSAEDISCON, "Graceful shutdown in progress" },
273 /* Resolver errors */
274 { WSAHOST_NOT_FOUND, "No such host is known" },
275 { WSATRY_AGAIN, "Host not found, or server failed" },
276 { WSANO_RECOVERY,
277 "Unexpected server error encountered" },
278 { WSANO_DATA, "Valid name without requested data" },
279 { WSANO_ADDRESS, "No address, look for MX record" },
280 { 0, NULL }
281 };
Guido van Rossum73624e91994-10-10 17:59:00 +0000282 PyObject *v;
Mark Hammond46a733d2000-07-24 01:45:11 +0000283 const char *msg = "winsock error";
284
285 for (msgp = msgs; msgp->msg; msgp++) {
286 if (err_no == msgp->no) {
287 msg = msgp->msg;
288 break;
289 }
290 }
291
292 v = Py_BuildValue("(is)", err_no, msg);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000293 if (v != NULL) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000294 PyErr_SetObject(PySocket_Error, v);
295 Py_DECREF(v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000296 }
297 return NULL;
298 }
299 else
300#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000301
302#if defined(PYOS_OS2)
303 if (sock_errno() != NO_ERROR) {
304 APIRET rc;
305 ULONG msglen;
306 char outbuf[100];
307 int myerrorcode = sock_errno();
308
309 /* Retrieve Socket-Related Error Message from MPTN.MSG File */
310 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
311 myerrorcode - SOCBASEERR + 26, "mptn.msg", &msglen);
312 if (rc == NO_ERROR) {
313 PyObject *v;
314
315 outbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
316 if (strlen(outbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
317 char *lastc = &outbuf[ strlen(outbuf)-1 ];
318 while (lastc > outbuf && isspace(*lastc))
319 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
320 }
321 v = Py_BuildValue("(is)", myerrorcode, outbuf);
322 if (v != NULL) {
323 PyErr_SetObject(PySocket_Error, v);
324 Py_DECREF(v);
325 }
326 return NULL;
327 }
328 }
329#endif
330
Guido van Rossum73624e91994-10-10 17:59:00 +0000331 return PyErr_SetFromErrno(PySocket_Error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000332}
333
Guido van Rossum30a685f1991-06-27 15:51:29 +0000334
335/* The object holding a socket. It holds some extra information,
336 like the address family, which is used to decode socket address
337 arguments properly. */
338
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000339typedef struct {
Guido van Rossum73624e91994-10-10 17:59:00 +0000340 PyObject_HEAD
Fred Drakea04eaad2000-06-30 02:46:07 +0000341 SOCKET_T sock_fd; /* Socket file descriptor */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000342 int sock_family; /* Address family, e.g., AF_INET */
343 int sock_type; /* Socket type, e.g., SOCK_STREAM */
344 int sock_proto; /* Protocol type, usually 0 */
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000345 union sock_addr {
346 struct sockaddr_in in;
347#ifdef AF_UNIX
348 struct sockaddr_un un;
349#endif
350 } sock_addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000351} PySocketSockObject;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000352
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000353#ifdef USE_SSL
Guido van Rossum30a685f1991-06-27 15:51:29 +0000354
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000355typedef struct {
356 PyObject_HEAD
357 PySocketSockObject *Socket; /* Socket on which we're layered */
358 PyObject *x_attr; /* Attributes dictionary */
359 SSL_CTX* ctx;
360 SSL* ssl;
361 X509* server_cert;
362 BIO* sbio;
363 char server[256];
364 char issuer[256];
365
366} SSLObject;
367
368staticforward PyTypeObject SSL_Type;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000369staticforward 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 *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000390PySocketSock_New(SOCKET_T 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
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000419setipaddr(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 *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000502makeipaddr(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 *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000520makesockaddr(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
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000575getsockaddrarg(PySocketSockObject *s, PyObject *args, struct sockaddr **addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000576{
Guido van Rossum30a685f1991-06-27 15:51:29 +0000577 switch (s->sock_family) {
578
Guido van Rossumb6775db1994-08-01 11:34:53 +0000579#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000580 case AF_UNIX:
581 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000582 struct sockaddr_un* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000583 char *path;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000584 int len;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000585 addr = (struct sockaddr_un* )&(s->sock_addr).un;
Guido van Rossum7e488981998-10-08 02:25:24 +0000586 if (!PyArg_Parse(args, "t#", &path, &len))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000587 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000588 if (len > sizeof addr->sun_path) {
Barry Warsaw752300b1997-01-03 17:18:10 +0000589 PyErr_SetString(PySocket_Error,
590 "AF_UNIX path too long");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000591 return 0;
592 }
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000593 addr->sun_family = AF_UNIX;
594 memcpy(addr->sun_path, path, len);
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000595 addr->sun_path[len] = 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000596 *addr_ret = (struct sockaddr *) addr;
Guido van Rossum65af28a1996-06-11 18:36:33 +0000597 *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000598 return 1;
599 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000600#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000601
Guido van Rossum30a685f1991-06-27 15:51:29 +0000602 case AF_INET:
603 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000604 struct sockaddr_in* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000605 char *host;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000606 int port;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000607 addr=(struct sockaddr_in*)&(s->sock_addr).in;
Guido van Rossum73624e91994-10-10 17:59:00 +0000608 if (!PyArg_Parse(args, "(si)", &host, &port))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000609 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000610 if (setipaddr(host, addr) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000611 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000612 addr->sin_family = AF_INET;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000613 addr->sin_port = htons((short)port);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000614 *addr_ret = (struct sockaddr *) addr;
615 *len_ret = sizeof *addr;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000616 return 1;
617 }
618
Guido van Rossum30a685f1991-06-27 15:51:29 +0000619 /* More cases here... */
620
621 default:
Guido van Rossum73624e91994-10-10 17:59:00 +0000622 PyErr_SetString(PySocket_Error, "getsockaddrarg: bad family");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000623 return 0;
624
625 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000626}
627
Guido van Rossum30a685f1991-06-27 15:51:29 +0000628
Guido van Rossum710e1df1992-06-12 10:39:36 +0000629/* Get the address length according to the socket object's address family.
630 Return 1 if the family is known, 0 otherwise. The length is returned
631 through len_ret. */
632
633static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +0000634getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +0000635{
636 switch (s->sock_family) {
637
Guido van Rossumb6775db1994-08-01 11:34:53 +0000638#ifdef AF_UNIX
Guido van Rossum710e1df1992-06-12 10:39:36 +0000639 case AF_UNIX:
640 {
641 *len_ret = sizeof (struct sockaddr_un);
642 return 1;
643 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000644#endif /* AF_UNIX */
Guido van Rossum710e1df1992-06-12 10:39:36 +0000645
646 case AF_INET:
647 {
648 *len_ret = sizeof (struct sockaddr_in);
649 return 1;
650 }
651
652 /* More cases here... */
653
654 default:
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000655 PyErr_SetString(PySocket_Error, "getsockaddrlen: bad family");
Guido van Rossum710e1df1992-06-12 10:39:36 +0000656 return 0;
657
658 }
659}
660
661
Guido van Rossum30a685f1991-06-27 15:51:29 +0000662/* s.accept() method */
663
Guido van Rossum73624e91994-10-10 17:59:00 +0000664static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000665PySocketSock_accept(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000666{
667 char addrbuf[256];
Fred Drakea04eaad2000-06-30 02:46:07 +0000668 SOCKET_T newfd;
Guido van Rossumff3ab422000-04-24 15:16:03 +0000669 socklen_t addrlen;
Barry Warsaw752300b1997-01-03 17:18:10 +0000670 PyObject *sock = NULL;
671 PyObject *addr = NULL;
672 PyObject *res = NULL;
673
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000674 if (!PyArg_ParseTuple(args, ":accept"))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000675 return NULL;
Guido van Rossum710e1df1992-06-12 10:39:36 +0000676 if (!getsockaddrlen(s, &addrlen))
677 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000678 Py_BEGIN_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000679 newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000680 Py_END_ALLOW_THREADS
Fred Drakea04eaad2000-06-30 02:46:07 +0000681#ifdef MS_WINDOWS
682 if (newfd == INVALID_SOCKET)
683#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000684 if (newfd < 0)
Fred Drakea04eaad2000-06-30 02:46:07 +0000685#endif
Guido van Rossum73624e91994-10-10 17:59:00 +0000686 return PySocket_Err();
Barry Warsaw752300b1997-01-03 17:18:10 +0000687
Guido van Rossum30a685f1991-06-27 15:51:29 +0000688 /* Create the new object with unspecified family,
689 to avoid calls to bind() etc. on it. */
Guido van Rossum73624e91994-10-10 17:59:00 +0000690 sock = (PyObject *) PySocketSock_New(newfd,
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000691 s->sock_family,
692 s->sock_type,
693 s->sock_proto);
Barry Warsaw752300b1997-01-03 17:18:10 +0000694 if (sock == NULL) {
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000695 SOCKETCLOSE(newfd);
Barry Warsaw752300b1997-01-03 17:18:10 +0000696 goto finally;
697 }
698 if (!(addr = makesockaddr((struct sockaddr *) addrbuf, addrlen)))
699 goto finally;
700
701 if (!(res = Py_BuildValue("OO", sock, addr)))
702 goto finally;
703
704 finally:
Guido van Rossum73624e91994-10-10 17:59:00 +0000705 Py_XDECREF(sock);
706 Py_XDECREF(addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000707 return res;
708}
709
Guido van Rossum82a5c661998-07-07 20:45:43 +0000710static char accept_doc[] =
711"accept() -> (socket object, address info)\n\
712\n\
713Wait for an incoming connection. Return a new socket representing the\n\
714connection, and the address of the client. For IP sockets, the address\n\
715info is a pair (hostaddr, port).";
716
Guido van Rossum30a685f1991-06-27 15:51:29 +0000717
Guido van Rossume4485b01994-09-07 14:32:49 +0000718/* s.setblocking(1 | 0) method */
719
Guido van Rossum73624e91994-10-10 17:59:00 +0000720static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000721PySocketSock_setblocking(PySocketSockObject *s, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000722{
723 int block;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000724#ifndef MS_WINDOWS
Guido van Rossume4485b01994-09-07 14:32:49 +0000725 int delay_flag;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000726#endif
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000727 if (!PyArg_ParseTuple(args, "i:setblocking", &block))
Guido van Rossume4485b01994-09-07 14:32:49 +0000728 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000729 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000730#ifdef __BEOS__
731 block = !block;
732 setsockopt( s->sock_fd, SOL_SOCKET, SO_NONBLOCK,
733 (void *)(&block), sizeof( int ) );
734#else
Guido van Rossum6f489d91996-06-28 20:15:15 +0000735#ifndef MS_WINDOWS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000736#ifdef PYOS_OS2
737 block = !block;
738 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
739#else /* !PYOS_OS2 */
Guido van Rossume4485b01994-09-07 14:32:49 +0000740 delay_flag = fcntl (s->sock_fd, F_GETFL, 0);
741 if (block)
742 delay_flag &= (~O_NDELAY);
743 else
744 delay_flag |= O_NDELAY;
745 fcntl (s->sock_fd, F_SETFL, delay_flag);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000746#endif /* !PYOS_OS2 */
747#else /* MS_WINDOWS */
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000748 block = !block;
749 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000750#endif /* MS_WINDOWS */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000751#endif /* __BEOS__ */
Guido van Rossum73624e91994-10-10 17:59:00 +0000752 Py_END_ALLOW_THREADS
Guido van Rossume4485b01994-09-07 14:32:49 +0000753
Guido van Rossum73624e91994-10-10 17:59:00 +0000754 Py_INCREF(Py_None);
755 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000756}
Guido van Rossume4485b01994-09-07 14:32:49 +0000757
Guido van Rossum82a5c661998-07-07 20:45:43 +0000758static char setblocking_doc[] =
759"setblocking(flag)\n\
760\n\
761Set the socket to blocking (flag is true) or non-blocking (false).\n\
762This uses the FIONBIO ioctl with the O_NDELAY flag.";
763
Guido van Rossume4485b01994-09-07 14:32:49 +0000764
Guido van Rossumaee08791992-09-08 09:05:33 +0000765/* s.setsockopt() method.
766 With an integer third argument, sets an integer option.
767 With a string third argument, sets an option from a buffer;
768 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000769
Guido van Rossum73624e91994-10-10 17:59:00 +0000770static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000771PySocketSock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000772{
773 int level;
774 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000775 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +0000776 char *buf;
777 int buflen;
778 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000779
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000780 if (PyArg_ParseTuple(args, "iii:setsockopt",
781 &level, &optname, &flag)) {
Guido van Rossumaee08791992-09-08 09:05:33 +0000782 buf = (char *) &flag;
783 buflen = sizeof flag;
784 }
785 else {
Guido van Rossum73624e91994-10-10 17:59:00 +0000786 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000787 if (!PyArg_ParseTuple(args, "iis#:setsockopt",
788 &level, &optname, &buf, &buflen))
Guido van Rossumaee08791992-09-08 09:05:33 +0000789 return NULL;
790 }
Thomas Wouters334fb892000-07-25 12:56:38 +0000791 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000792 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000793 return PySocket_Err();
794 Py_INCREF(Py_None);
795 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000796}
797
Guido van Rossum82a5c661998-07-07 20:45:43 +0000798static char setsockopt_doc[] =
799"setsockopt(level, option, value)\n\
800\n\
801Set a socket option. See the Unix manual for level and option.\n\
802The value argument can either be an integer or a string.";
803
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000804
Guido van Rossumaee08791992-09-08 09:05:33 +0000805/* s.getsockopt() method.
806 With two arguments, retrieves an integer option.
807 With a third integer argument, retrieves a string buffer of that size;
808 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000809
Guido van Rossum73624e91994-10-10 17:59:00 +0000810static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000811PySocketSock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000812{
813 int level;
814 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000815 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +0000816 PyObject *buf;
Guido van Rossumff3ab422000-04-24 15:16:03 +0000817 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000818
Guido van Rossumbcc20741998-08-04 22:53:56 +0000819#ifdef __BEOS__
820/* We have incomplete socket support. */
821 PyErr_SetString( PySocket_Error, "getsockopt not supported" );
822 return NULL;
823#else
824
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000825 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
826 &level, &optname, &buflen))
Guido van Rossumbe32c891996-06-20 16:25:29 +0000827 return NULL;
828
829 if (buflen == 0) {
Guido van Rossumaee08791992-09-08 09:05:33 +0000830 int flag = 0;
Guido van Rossumff3ab422000-04-24 15:16:03 +0000831 socklen_t flagsize = sizeof flag;
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000832 res = getsockopt(s->sock_fd, level, optname,
Thomas Wouters334fb892000-07-25 12:56:38 +0000833 (void *)&flag, &flagsize);
Guido van Rossumaee08791992-09-08 09:05:33 +0000834 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000835 return PySocket_Err();
836 return PyInt_FromLong(flag);
Guido van Rossumaee08791992-09-08 09:05:33 +0000837 }
Guido van Rossumaee08791992-09-08 09:05:33 +0000838 if (buflen <= 0 || buflen > 1024) {
Barry Warsaw752300b1997-01-03 17:18:10 +0000839 PyErr_SetString(PySocket_Error,
840 "getsockopt buflen out of range");
Guido van Rossumaee08791992-09-08 09:05:33 +0000841 return NULL;
842 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000843 buf = PyString_FromStringAndSize((char *)NULL, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000844 if (buf == NULL)
845 return NULL;
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000846 res = getsockopt(s->sock_fd, level, optname,
Thomas Wouters334fb892000-07-25 12:56:38 +0000847 (void *)PyString_AsString(buf), &buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000848 if (res < 0) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000849 Py_DECREF(buf);
850 return PySocket_Err();
Guido van Rossumaee08791992-09-08 09:05:33 +0000851 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000852 _PyString_Resize(&buf, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000853 return buf;
Guido van Rossumbcc20741998-08-04 22:53:56 +0000854#endif /* __BEOS__ */
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000855}
856
Guido van Rossum82a5c661998-07-07 20:45:43 +0000857static char getsockopt_doc[] =
858"getsockopt(level, option[, buffersize]) -> value\n\
859\n\
860Get a socket option. See the Unix manual for level and option.\n\
861If a nonzero buffersize argument is given, the return value is a\n\
862string of that length; otherwise it is an integer.";
863
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000864
Fred Drake728819a2000-07-01 03:40:12 +0000865/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000866
Guido van Rossum73624e91994-10-10 17:59:00 +0000867static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000868PySocketSock_bind(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000869{
870 struct sockaddr *addr;
871 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000872 int res;
Fred Drake728819a2000-07-01 03:40:12 +0000873 PyObject *addro;
874 if (!PyArg_ParseTuple(args, "O:bind", &addro))
875 return NULL;
876 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000877 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000878 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000879 res = bind(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000880 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000881 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000882 return PySocket_Err();
883 Py_INCREF(Py_None);
884 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000885}
886
Guido van Rossum82a5c661998-07-07 20:45:43 +0000887static char bind_doc[] =
888"bind(address)\n\
889\n\
890Bind the socket to a local address. For IP sockets, the address is a\n\
891pair (host, port); the host must refer to the local host.";
892
Guido van Rossum30a685f1991-06-27 15:51:29 +0000893
894/* s.close() method.
895 Set the file descriptor to -1 so operations tried subsequently
896 will surely fail. */
897
Guido van Rossum73624e91994-10-10 17:59:00 +0000898static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000899PySocketSock_close(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000900{
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000901 if (!PyArg_ParseTuple(args, ":close"))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000902 return NULL;
Guido van Rossum08481461996-10-12 14:07:22 +0000903 if (s->sock_fd != -1) {
904 Py_BEGIN_ALLOW_THREADS
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000905 (void) SOCKETCLOSE(s->sock_fd);
Guido van Rossum08481461996-10-12 14:07:22 +0000906 Py_END_ALLOW_THREADS
907 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000908 s->sock_fd = -1;
Guido van Rossum73624e91994-10-10 17:59:00 +0000909 Py_INCREF(Py_None);
910 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000911}
912
Guido van Rossum82a5c661998-07-07 20:45:43 +0000913static char close_doc[] =
914"close()\n\
915\n\
916Close the socket. It cannot be used after this call.";
917
Guido van Rossum30a685f1991-06-27 15:51:29 +0000918
Fred Drake728819a2000-07-01 03:40:12 +0000919/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000920
Guido van Rossum73624e91994-10-10 17:59:00 +0000921static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000922PySocketSock_connect(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000923{
924 struct sockaddr *addr;
925 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000926 int res;
Fred Drake728819a2000-07-01 03:40:12 +0000927 PyObject *addro;
928 if (!PyArg_ParseTuple(args, "O:connect", &addro))
929 return NULL;
930 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000931 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000932 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000933 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000934 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000935 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000936 return PySocket_Err();
937 Py_INCREF(Py_None);
938 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000939}
940
Guido van Rossum82a5c661998-07-07 20:45:43 +0000941static char connect_doc[] =
942"connect(address)\n\
943\n\
944Connect the socket to a remote address. For IP sockets, the address\n\
945is a pair (host, port).";
946
Guido van Rossum30a685f1991-06-27 15:51:29 +0000947
Fred Drake728819a2000-07-01 03:40:12 +0000948/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +0000949
950static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000951PySocketSock_connect_ex(PySocketSockObject *s, PyObject *args)
Guido van Rossumfc4255d1997-11-19 18:57:13 +0000952{
953 struct sockaddr *addr;
954 int addrlen;
955 int res;
Fred Drake728819a2000-07-01 03:40:12 +0000956 PyObject *addro;
957 if (!PyArg_ParseTuple(args, "O:connect_ex", &addro))
958 return NULL;
959 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossumfc4255d1997-11-19 18:57:13 +0000960 return NULL;
961 Py_BEGIN_ALLOW_THREADS
962 res = connect(s->sock_fd, addr, addrlen);
963 Py_END_ALLOW_THREADS
964 if (res != 0)
965 res = errno;
966 return PyInt_FromLong((long) res);
967}
968
Guido van Rossum82a5c661998-07-07 20:45:43 +0000969static char connect_ex_doc[] =
970"connect_ex(address)\n\
971\n\
972This is like connect(address), but returns an error code (the errno value)\n\
973instead of raising an exception when an error occurs.";
974
Guido van Rossumfc4255d1997-11-19 18:57:13 +0000975
Guido van Rossumed233a51992-06-23 09:07:03 +0000976/* s.fileno() method */
977
Guido van Rossum73624e91994-10-10 17:59:00 +0000978static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000979PySocketSock_fileno(PySocketSockObject *s, PyObject *args)
Guido van Rossumed233a51992-06-23 09:07:03 +0000980{
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000981 if (!PyArg_ParseTuple(args, ":fileno"))
Guido van Rossumed233a51992-06-23 09:07:03 +0000982 return NULL;
Fred Drakea04eaad2000-06-30 02:46:07 +0000983#if SIZEOF_SOCKET_T <= SIZEOF_LONG
Guido van Rossum73624e91994-10-10 17:59:00 +0000984 return PyInt_FromLong((long) s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +0000985#else
986 return PyLong_FromLongLong((LONG_LONG)s->sock_fd);
987#endif
Guido van Rossumed233a51992-06-23 09:07:03 +0000988}
989
Guido van Rossum82a5c661998-07-07 20:45:43 +0000990static char fileno_doc[] =
991"fileno() -> integer\n\
992\n\
993Return the integer file descriptor of the socket.";
994
Guido van Rossumed233a51992-06-23 09:07:03 +0000995
Guido van Rossumbe32c891996-06-20 16:25:29 +0000996#ifndef NO_DUP
997/* s.dup() method */
998
999static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001000PySocketSock_dup(PySocketSockObject *s, PyObject *args)
Guido van Rossumbe32c891996-06-20 16:25:29 +00001001{
Fred Drakea04eaad2000-06-30 02:46:07 +00001002 SOCKET_T newfd;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001003 PyObject *sock;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001004 if (!PyArg_ParseTuple(args, ":dup"))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001005 return NULL;
1006 newfd = dup(s->sock_fd);
1007 if (newfd < 0)
1008 return PySocket_Err();
1009 sock = (PyObject *) PySocketSock_New(newfd,
Barry Warsaw752300b1997-01-03 17:18:10 +00001010 s->sock_family,
1011 s->sock_type,
1012 s->sock_proto);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001013 if (sock == NULL)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001014 SOCKETCLOSE(newfd);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001015 return sock;
1016}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001017
1018static char dup_doc[] =
1019"dup() -> socket object\n\
1020\n\
1021Return a new socket object connected to the same system resource.";
1022
Guido van Rossumbe32c891996-06-20 16:25:29 +00001023#endif
1024
1025
Guido van Rossumc89705d1992-11-26 08:54:07 +00001026/* s.getsockname() method */
1027
Guido van Rossum73624e91994-10-10 17:59:00 +00001028static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001029PySocketSock_getsockname(PySocketSockObject *s, PyObject *args)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001030{
1031 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001032 int res;
1033 socklen_t addrlen;
1034
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001035 if (!PyArg_ParseTuple(args, ":getsockname"))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001036 return NULL;
1037 if (!getsockaddrlen(s, &addrlen))
1038 return NULL;
Guido van Rossumca42b161996-01-12 01:36:05 +00001039 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001040 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001041 res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001042 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001043 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001044 return PySocket_Err();
Guido van Rossumc89705d1992-11-26 08:54:07 +00001045 return makesockaddr((struct sockaddr *) addrbuf, addrlen);
1046}
1047
Guido van Rossum82a5c661998-07-07 20:45:43 +00001048static char getsockname_doc[] =
1049"getsockname() -> address info\n\
1050\n\
1051Return the address of the local endpoint. For IP sockets, the address\n\
1052info is a pair (hostaddr, port).";
1053
Guido van Rossumc89705d1992-11-26 08:54:07 +00001054
Guido van Rossumb6775db1994-08-01 11:34:53 +00001055#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001056/* s.getpeername() method */
1057
Guido van Rossum73624e91994-10-10 17:59:00 +00001058static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001059PySocketSock_getpeername(PySocketSockObject *s, PyObject *args)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001060{
1061 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001062 int res;
1063 socklen_t addrlen;
1064
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001065 if (!PyArg_ParseTuple(args, ":getpeername"))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001066 return NULL;
1067 if (!getsockaddrlen(s, &addrlen))
1068 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001069 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001070 res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001071 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001072 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001073 return PySocket_Err();
Guido van Rossumc89705d1992-11-26 08:54:07 +00001074 return makesockaddr((struct sockaddr *) addrbuf, addrlen);
1075}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001076
1077static char getpeername_doc[] =
1078"getpeername() -> address info\n\
1079\n\
1080Return the address of the remote endpoint. For IP sockets, the address\n\
1081info is a pair (hostaddr, port).";
1082
Guido van Rossumb6775db1994-08-01 11:34:53 +00001083#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001084
1085
Guido van Rossum30a685f1991-06-27 15:51:29 +00001086/* s.listen(n) method */
1087
Guido van Rossum73624e91994-10-10 17:59:00 +00001088static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001089PySocketSock_listen(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001090{
1091 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001092 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001093 if (!PyArg_ParseTuple(args, "i:listen", &backlog))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001094 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001095 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +00001096 if (backlog < 1)
1097 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001098 res = listen(s->sock_fd, backlog);
Guido van Rossum73624e91994-10-10 17:59:00 +00001099 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001100 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001101 return PySocket_Err();
1102 Py_INCREF(Py_None);
1103 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001104}
1105
Guido van Rossum82a5c661998-07-07 20:45:43 +00001106static char listen_doc[] =
1107"listen(backlog)\n\
1108\n\
1109Enable a server to accept connections. The backlog argument must be at\n\
1110least 1; it specifies the number of unaccepted connection that the system\n\
1111will allow before refusing new connections.";
1112
1113
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001114#ifndef NO_DUP
Guido van Rossum30a685f1991-06-27 15:51:29 +00001115/* s.makefile(mode) method.
1116 Create a new open file object referring to a dupped version of
1117 the socket's file descriptor. (The dup() call is necessary so
1118 that the open file and socket objects may be closed independent
1119 of each other.)
1120 The mode argument specifies 'r' or 'w' passed to fdopen(). */
1121
Guido van Rossum73624e91994-10-10 17:59:00 +00001122static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001123PySocketSock_makefile(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001124{
Tim Petersdbd9ba62000-07-09 03:09:57 +00001125 extern int fclose(FILE *);
Guido van Rossum6b144911995-03-14 15:05:13 +00001126 char *mode = "r";
1127 int bufsize = -1;
Fred Drakea04eaad2000-06-30 02:46:07 +00001128#ifdef MS_WIN32
1129 intptr_t fd;
1130#else
Guido van Rossum30a685f1991-06-27 15:51:29 +00001131 int fd;
Fred Drakea04eaad2000-06-30 02:46:07 +00001132#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001133 FILE *fp;
Guido van Rossum6b144911995-03-14 15:05:13 +00001134 PyObject *f;
1135
Guido van Rossum43713e52000-02-29 13:59:29 +00001136 if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001137 return NULL;
Guido van Rossum8d665e61996-06-26 18:22:49 +00001138#ifdef MS_WIN32
Guido van Rossum82a5c661998-07-07 20:45:43 +00001139 if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) ||
1140 ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL))
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001141#else
Guido van Rossum82a5c661998-07-07 20:45:43 +00001142 if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL)
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001143#endif
Guido van Rossum82a5c661998-07-07 20:45:43 +00001144 {
Guido van Rossum6b144911995-03-14 15:05:13 +00001145 if (fd >= 0)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001146 SOCKETCLOSE(fd);
Guido van Rossum73624e91994-10-10 17:59:00 +00001147 return PySocket_Err();
Guido van Rossum6b144911995-03-14 15:05:13 +00001148 }
1149 f = PyFile_FromFile(fp, "<socket>", mode, fclose);
1150 if (f != NULL)
1151 PyFile_SetBufSize(f, bufsize);
1152 return f;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001153}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001154
1155static char makefile_doc[] =
1156"makefile([mode[, buffersize]]) -> file object\n\
1157\n\
1158Return a regular file object corresponding to the socket.\n\
1159The mode and buffersize arguments are as for the built-in open() function.";
1160
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001161#endif /* NO_DUP */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001162
Guido van Rossum82a5c661998-07-07 20:45:43 +00001163
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001164/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001165
Guido van Rossum73624e91994-10-10 17:59:00 +00001166static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001167PySocketSock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001168{
Guido van Rossumbe32c891996-06-20 16:25:29 +00001169 int len, n, flags = 0;
Guido van Rossum73624e91994-10-10 17:59:00 +00001170 PyObject *buf;
Guido van Rossum43713e52000-02-29 13:59:29 +00001171 if (!PyArg_ParseTuple(args, "i|i:recv", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001172 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001173 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001174 if (buf == NULL)
1175 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001176 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma597dde1995-01-10 20:56:29 +00001177 n = recv(s->sock_fd, PyString_AsString(buf), len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001178 Py_END_ALLOW_THREADS
Guido van Rossum7c53b771995-09-13 18:39:47 +00001179 if (n < 0) {
1180 Py_DECREF(buf);
Guido van Rossum73624e91994-10-10 17:59:00 +00001181 return PySocket_Err();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001182 }
Guido van Rossum53a9bf81996-06-11 18:35:24 +00001183 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001184 return NULL;
1185 return buf;
1186}
1187
Guido van Rossum82a5c661998-07-07 20:45:43 +00001188static char recv_doc[] =
1189"recv(buffersize[, flags]) -> data\n\
1190\n\
1191Receive up to buffersize bytes from the socket. For the optional flags\n\
1192argument, see the Unix manual. When no data is available, block until\n\
1193at least one byte is available or until the remote end is closed. When\n\
1194the remote end is closed and all data is read, return the empty string.";
1195
Guido van Rossum30a685f1991-06-27 15:51:29 +00001196
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001197/* s.recvfrom(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001198
Guido van Rossum73624e91994-10-10 17:59:00 +00001199static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001200PySocketSock_recvfrom(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001201{
1202 char addrbuf[256];
Barry Warsaw752300b1997-01-03 17:18:10 +00001203 PyObject *buf = NULL;
1204 PyObject *addr = NULL;
1205 PyObject *ret = NULL;
1206
Guido van Rossumff3ab422000-04-24 15:16:03 +00001207 int len, n, flags = 0;
1208 socklen_t addrlen;
Guido van Rossum43713e52000-02-29 13:59:29 +00001209 if (!PyArg_ParseTuple(args, "i|i:recvfrom", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001210 return NULL;
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00001211 if (!getsockaddrlen(s, &addrlen))
1212 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001213 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00001214 if (buf == NULL)
1215 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001216 Py_BEGIN_ALLOW_THREADS
1217 n = recvfrom(s->sock_fd, PyString_AsString(buf), len, flags,
Guido van Rossum8d665e61996-06-26 18:22:49 +00001218#ifndef MS_WINDOWS
Guido van Rossum32c575d1997-12-02 20:37:32 +00001219#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001220 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00001221#else
Thomas Wouters334fb892000-07-25 12:56:38 +00001222 (void *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00001223#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001224#else
Barry Warsaw752300b1997-01-03 17:18:10 +00001225 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001226#endif
Barry Warsaw752300b1997-01-03 17:18:10 +00001227 );
Guido van Rossum73624e91994-10-10 17:59:00 +00001228 Py_END_ALLOW_THREADS
Guido van Rossum7c53b771995-09-13 18:39:47 +00001229 if (n < 0) {
1230 Py_DECREF(buf);
Guido van Rossum73624e91994-10-10 17:59:00 +00001231 return PySocket_Err();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001232 }
Guido van Rossum53a9bf81996-06-11 18:35:24 +00001233 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001234 return NULL;
Barry Warsaw752300b1997-01-03 17:18:10 +00001235
1236 if (!(addr = makesockaddr((struct sockaddr *)addrbuf, addrlen)))
1237 goto finally;
1238
Guido van Rossum73624e91994-10-10 17:59:00 +00001239 ret = Py_BuildValue("OO", buf, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00001240 finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001241 Py_XDECREF(addr);
1242 Py_XDECREF(buf);
Guido van Rossum6f5afc91993-02-05 09:46:15 +00001243 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001244}
1245
Guido van Rossum82a5c661998-07-07 20:45:43 +00001246static char recvfrom_doc[] =
1247"recvfrom(buffersize[, flags]) -> (data, address info)\n\
1248\n\
1249Like recv(buffersize, flags) but also return the sender's address info.";
1250
Guido van Rossum30a685f1991-06-27 15:51:29 +00001251
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001252/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001253
Guido van Rossum73624e91994-10-10 17:59:00 +00001254static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001255PySocketSock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001256{
Guido van Rossumff4949e1992-08-05 19:58:53 +00001257 char *buf;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001258 int len, n, flags = 0;
Guido van Rossum43713e52000-02-29 13:59:29 +00001259 if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001260 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001261 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001262 n = send(s->sock_fd, buf, len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001263 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00001264 if (n < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001265 return PySocket_Err();
1266 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00001267}
1268
Guido van Rossum82a5c661998-07-07 20:45:43 +00001269static char send_doc[] =
1270"send(data[, flags])\n\
1271\n\
1272Send a data string to the socket. For the optional flags\n\
1273argument, see the Unix manual.";
1274
Guido van Rossum30a685f1991-06-27 15:51:29 +00001275
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001276/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001277
Guido van Rossum73624e91994-10-10 17:59:00 +00001278static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001279PySocketSock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001280{
Guido van Rossum73624e91994-10-10 17:59:00 +00001281 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001282 char *buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001283 struct sockaddr *addr;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001284 int addrlen, len, n, flags;
1285 flags = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001286 if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001287 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001288 if (!PyArg_ParseTuple(args, "s#iO:sendto",
1289 &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001290 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001291 }
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001292 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001293 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001294 Py_BEGIN_ALLOW_THREADS
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001295 n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001296 Py_END_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001297 if (n < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001298 return PySocket_Err();
1299 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001300}
1301
Guido van Rossum82a5c661998-07-07 20:45:43 +00001302static char sendto_doc[] =
1303"sendto(data[, flags], address)\n\
1304\n\
1305Like send(data, flags) but allows specifying the destination address.\n\
1306For IP sockets, the address is a pair (hostaddr, port).";
1307
Guido van Rossum30a685f1991-06-27 15:51:29 +00001308
1309/* s.shutdown(how) method */
1310
Guido van Rossum73624e91994-10-10 17:59:00 +00001311static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001312PySocketSock_shutdown(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001313{
1314 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001315 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001316 if (!PyArg_ParseTuple(args, "i:shutdown", &how))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001317 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001318 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001319 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +00001320 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001321 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001322 return PySocket_Err();
1323 Py_INCREF(Py_None);
1324 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001325}
1326
Guido van Rossum82a5c661998-07-07 20:45:43 +00001327static char shutdown_doc[] =
1328"shutdown(flag)\n\
1329\n\
1330Shut down the reading side of the socket (flag == 0), the writing side\n\
1331of the socket (flag == 1), or both ends (flag == 2).";
1332
Guido van Rossum30a685f1991-06-27 15:51:29 +00001333
1334/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001335
Guido van Rossum73624e91994-10-10 17:59:00 +00001336static PyMethodDef PySocketSock_methods[] = {
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00001337 {"accept", (PyCFunction)PySocketSock_accept, METH_VARARGS,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001338 accept_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00001339 {"bind", (PyCFunction)PySocketSock_bind, METH_VARARGS,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001340 bind_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00001341 {"close", (PyCFunction)PySocketSock_close, METH_VARARGS,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001342 close_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00001343 {"connect", (PyCFunction)PySocketSock_connect, METH_VARARGS,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001344 connect_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00001345 {"connect_ex", (PyCFunction)PySocketSock_connect_ex, METH_VARARGS,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001346 connect_ex_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00001347#ifndef NO_DUP
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00001348 {"dup", (PyCFunction)PySocketSock_dup, METH_VARARGS,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001349 dup_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00001350#endif
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00001351 {"fileno", (PyCFunction)PySocketSock_fileno, METH_VARARGS,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001352 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001353#ifdef HAVE_GETPEERNAME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00001354 {"getpeername", (PyCFunction)PySocketSock_getpeername, METH_VARARGS,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001355 getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00001356#endif
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00001357 {"getsockname", (PyCFunction)PySocketSock_getsockname, METH_VARARGS,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001358 getsockname_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00001359 {"getsockopt", (PyCFunction)PySocketSock_getsockopt, METH_VARARGS,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001360 getsockopt_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00001361 {"listen", (PyCFunction)PySocketSock_listen, METH_VARARGS,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001362 listen_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001363#ifndef NO_DUP
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00001364 {"makefile", (PyCFunction)PySocketSock_makefile, METH_VARARGS,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001365 makefile_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001366#endif
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00001367 {"recv", (PyCFunction)PySocketSock_recv, METH_VARARGS,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001368 recv_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00001369 {"recvfrom", (PyCFunction)PySocketSock_recvfrom, METH_VARARGS,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001370 recvfrom_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00001371 {"send", (PyCFunction)PySocketSock_send, METH_VARARGS,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001372 send_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00001373 {"sendto", (PyCFunction)PySocketSock_sendto, METH_VARARGS,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001374 sendto_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00001375 {"setblocking", (PyCFunction)PySocketSock_setblocking, METH_VARARGS,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001376 setblocking_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00001377 {"setsockopt", (PyCFunction)PySocketSock_setsockopt, METH_VARARGS,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001378 setsockopt_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00001379 {"shutdown", (PyCFunction)PySocketSock_shutdown, METH_VARARGS,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001380 shutdown_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001381 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001382};
1383
Guido van Rossum30a685f1991-06-27 15:51:29 +00001384
Guido van Rossum73624e91994-10-10 17:59:00 +00001385/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00001386 First close the file description. */
1387
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001388static void
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001389PySocketSock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001390{
Guido van Rossumfa972c92000-04-10 12:45:45 +00001391 if (s->sock_fd != -1)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001392 (void) SOCKETCLOSE(s->sock_fd);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001393 PyObject_Del(s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001394}
1395
Guido van Rossum30a685f1991-06-27 15:51:29 +00001396
1397/* Return a socket object's named attribute. */
1398
Guido van Rossum73624e91994-10-10 17:59:00 +00001399static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001400PySocketSock_getattr(PySocketSockObject *s, char *name)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001401{
Guido van Rossum73624e91994-10-10 17:59:00 +00001402 return Py_FindMethod(PySocketSock_methods, (PyObject *) s, name);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001403}
1404
Guido van Rossum30a685f1991-06-27 15:51:29 +00001405
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001406static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001407PySocketSock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001408{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001409 char buf[512];
Fred Drakea04eaad2000-06-30 02:46:07 +00001410#if SIZEOF_SOCKET_T > SIZEOF_LONG
1411 if (s->sock_fd > LONG_MAX) {
1412 /* this can occur on Win64, and actually there is a special
1413 ugly printf formatter for decimal pointer length integer
1414 printing, only bother if necessary*/
1415 PyErr_SetString(PyExc_OverflowError,
1416 "no printf formatter to display the socket descriptor in decimal");
1417 return NULL;
1418 }
1419#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001420 sprintf(buf,
Fred Drakea04eaad2000-06-30 02:46:07 +00001421 "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>",
1422 (long)s->sock_fd, s->sock_family, s->sock_type, s->sock_proto);
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00001423 return PyString_FromString(buf);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001424}
1425
1426
Guido van Rossumb6775db1994-08-01 11:34:53 +00001427/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001428
Guido van Rossum73624e91994-10-10 17:59:00 +00001429static PyTypeObject PySocketSock_Type = {
Guido van Rossum55558541996-05-23 22:54:50 +00001430 PyObject_HEAD_INIT(0) /* Must fill in type value later */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001431 0,
1432 "socket",
Guido van Rossum73624e91994-10-10 17:59:00 +00001433 sizeof(PySocketSockObject),
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001434 0,
Guido van Rossum73624e91994-10-10 17:59:00 +00001435 (destructor)PySocketSock_dealloc, /*tp_dealloc*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001436 0, /*tp_print*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001437 (getattrfunc)PySocketSock_getattr, /*tp_getattr*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001438 0, /*tp_setattr*/
1439 0, /*tp_compare*/
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001440 (reprfunc)PySocketSock_repr, /*tp_repr*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001441 0, /*tp_as_number*/
1442 0, /*tp_as_sequence*/
1443 0, /*tp_as_mapping*/
1444};
1445
Guido van Rossum30a685f1991-06-27 15:51:29 +00001446
Guido van Rossum81194471991-07-27 21:42:02 +00001447/* Python interface to gethostname(). */
1448
1449/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001450static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001451PySocket_gethostname(PyObject *self, PyObject *args)
Guido van Rossum81194471991-07-27 21:42:02 +00001452{
1453 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001454 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001455 if (!PyArg_ParseTuple(args, ":gethostname"))
Guido van Rossum81194471991-07-27 21:42:02 +00001456 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001457 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001458 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00001459 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001460 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001461 return PySocket_Err();
Guido van Rossum81194471991-07-27 21:42:02 +00001462 buf[sizeof buf - 1] = '\0';
Guido van Rossum73624e91994-10-10 17:59:00 +00001463 return PyString_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00001464}
Guido van Rossumff4949e1992-08-05 19:58:53 +00001465
Guido van Rossum82a5c661998-07-07 20:45:43 +00001466static char gethostname_doc[] =
1467"gethostname() -> string\n\
1468\n\
1469Return the current host name.";
1470
Guido van Rossumff4949e1992-08-05 19:58:53 +00001471
Guido van Rossum30a685f1991-06-27 15:51:29 +00001472/* Python interface to gethostbyname(name). */
1473
1474/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001475static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001476PySocket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001477{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001478 char *name;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001479 struct sockaddr_in addrbuf;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001480 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00001481 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001482 if (setipaddr(name, &addrbuf) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001483 return NULL;
1484 return makeipaddr(&addrbuf);
1485}
1486
Guido van Rossum82a5c661998-07-07 20:45:43 +00001487static char gethostbyname_doc[] =
1488"gethostbyname(host) -> address\n\
1489\n\
1490Return the IP address (a string of the form '255.255.255.255') for a host.";
1491
1492
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001493/* Convenience function common to gethostbyname_ex and gethostbyaddr */
1494
1495static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001496gethost_common(struct hostent *h, struct sockaddr_in *addr)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001497{
1498 char **pch;
1499 PyObject *rtn_tuple = (PyObject *)NULL;
1500 PyObject *name_list = (PyObject *)NULL;
1501 PyObject *addr_list = (PyObject *)NULL;
1502 PyObject *tmp;
1503 if (h == NULL) {
1504#ifdef HAVE_HSTRERROR
1505 /* Let's get real error message to return */
1506 extern int h_errno;
1507 PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno));
1508#else
1509 PyErr_SetString(PySocket_Error, "host not found");
1510#endif
1511 return NULL;
1512 }
1513 if ((name_list = PyList_New(0)) == NULL)
1514 goto err;
1515 if ((addr_list = PyList_New(0)) == NULL)
1516 goto err;
1517 for (pch = h->h_aliases; *pch != NULL; pch++) {
1518 int status;
1519 tmp = PyString_FromString(*pch);
1520 if (tmp == NULL)
1521 goto err;
1522 status = PyList_Append(name_list, tmp);
1523 Py_DECREF(tmp);
1524 if (status)
1525 goto err;
1526 }
1527 for (pch = h->h_addr_list; *pch != NULL; pch++) {
1528 int status;
1529 memcpy((char *) &addr->sin_addr, *pch, h->h_length);
1530 tmp = makeipaddr(addr);
1531 if (tmp == NULL)
1532 goto err;
1533 status = PyList_Append(addr_list, tmp);
1534 Py_DECREF(tmp);
1535 if (status)
1536 goto err;
1537 }
1538 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
1539 err:
1540 Py_XDECREF(name_list);
1541 Py_XDECREF(addr_list);
1542 return rtn_tuple;
1543}
1544
1545
1546/* Python interface to gethostbyname_ex(name). */
1547
1548/*ARGSUSED*/
1549static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001550PySocket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001551{
1552 char *name;
1553 struct hostent *h;
1554 struct sockaddr_in addr;
Guido van Rossum955becc1999-03-22 20:14:53 +00001555 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001556#ifdef HAVE_GETHOSTBYNAME_R
1557 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001558#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
1559 struct hostent_data data;
1560#else
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001561 char buf[16384];
1562 int buf_len = (sizeof buf) - 1;
1563 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001564#endif
1565#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001566 int result;
1567#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001568#endif /* HAVE_GETHOSTBYNAME_R */
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001569 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001570 return NULL;
1571 if (setipaddr(name, &addr) < 0)
1572 return NULL;
1573 Py_BEGIN_ALLOW_THREADS
1574#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001575#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001576 result = gethostbyname_r(name, &hp_allocated, buf, buf_len, &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001577#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001578 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001579#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00001580 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001581 result = gethostbyname_r(name, &hp_allocated, &data);
1582 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001583#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001584#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00001585#ifdef USE_GETHOSTBYNAME_LOCK
1586 PyThread_acquire_lock(gethostbyname_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001587#endif
1588 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001589#endif /* HAVE_GETHOSTBYNAME_R */
1590 Py_END_ALLOW_THREADS
Guido van Rossum3baaa131999-03-22 21:44:51 +00001591 ret = gethost_common(h, &addr);
1592#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum955becc1999-03-22 20:14:53 +00001593 PyThread_release_lock(gethostbyname_lock);
1594#endif
1595 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001596}
1597
1598static char ghbn_ex_doc[] =
1599"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
1600\n\
1601Return the true host name, a list of aliases, and a list of IP addresses,\n\
1602for a host. The host argument is a string giving a host name or IP number.";
1603
1604
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001605/* Python interface to gethostbyaddr(IP). */
1606
1607/*ARGSUSED*/
1608static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001609PySocket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001610{
1611 struct sockaddr_in addr;
1612 char *ip_num;
1613 struct hostent *h;
Guido van Rossum3baaa131999-03-22 21:44:51 +00001614 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001615#ifdef HAVE_GETHOSTBYNAME_R
1616 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001617#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
1618 struct hostent_data data;
1619#else
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001620 char buf[16384];
1621 int buf_len = (sizeof buf) - 1;
1622 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001623#endif
1624#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001625 int result;
1626#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001627#endif /* HAVE_GETHOSTBYNAME_R */
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001628
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001629 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001630 return NULL;
1631 if (setipaddr(ip_num, &addr) < 0)
1632 return NULL;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001633 Py_BEGIN_ALLOW_THREADS
1634#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001635#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001636 result = gethostbyaddr_r((char *)&addr.sin_addr,
1637 sizeof(addr.sin_addr),
1638 AF_INET, &hp_allocated, buf, buf_len,
1639 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001640#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001641 h = gethostbyaddr_r((char *)&addr.sin_addr,
1642 sizeof(addr.sin_addr),
1643 AF_INET,
1644 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001645#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00001646 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001647 result = gethostbyaddr_r((char *)&addr.sin_addr,
1648 sizeof(addr.sin_addr),
1649 AF_INET, &hp_allocated, &data);
1650 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001651#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001652#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00001653#ifdef USE_GETHOSTBYNAME_LOCK
1654 PyThread_acquire_lock(gethostbyname_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001655#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001656 h = gethostbyaddr((char *)&addr.sin_addr,
1657 sizeof(addr.sin_addr),
1658 AF_INET);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001659#endif /* HAVE_GETHOSTBYNAME_R */
1660 Py_END_ALLOW_THREADS
Guido van Rossum3baaa131999-03-22 21:44:51 +00001661 ret = gethost_common(h, &addr);
1662#ifdef USE_GETHOSTBYNAME_LOCK
1663 PyThread_release_lock(gethostbyname_lock);
1664#endif
1665 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001666}
1667
Guido van Rossum82a5c661998-07-07 20:45:43 +00001668static char gethostbyaddr_doc[] =
1669"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
1670\n\
1671Return the true host name, a list of aliases, and a list of IP addresses,\n\
1672for a host. The host argument is a string giving a host name or IP number.";
1673
Guido van Rossum30a685f1991-06-27 15:51:29 +00001674
1675/* Python interface to getservbyname(name).
1676 This only returns the port number, since the other info is already
1677 known or not useful (like the list of aliases). */
1678
1679/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001680static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001681PySocket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001682{
Guido van Rossumff4949e1992-08-05 19:58:53 +00001683 char *name, *proto;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001684 struct servent *sp;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001685 if (!PyArg_ParseTuple(args, "ss:getservbyname", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00001686 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001687 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001688 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00001689 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00001690 if (sp == NULL) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001691 PyErr_SetString(PySocket_Error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00001692 return NULL;
1693 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001694 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00001695}
1696
Guido van Rossum82a5c661998-07-07 20:45:43 +00001697static char getservbyname_doc[] =
1698"getservbyname(servicename, protocolname) -> integer\n\
1699\n\
1700Return a port number from a service name and protocol name.\n\
1701The protocol name should be 'tcp' or 'udp'.";
1702
Guido van Rossum30a685f1991-06-27 15:51:29 +00001703
Guido van Rossum3901d851996-12-19 16:35:04 +00001704/* Python interface to getprotobyname(name).
1705 This only returns the protocol number, since the other info is
1706 already known or not useful (like the list of aliases). */
1707
1708/*ARGSUSED*/
1709static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001710PySocket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00001711{
1712 char *name;
1713 struct protoent *sp;
Guido van Rossumbcc20741998-08-04 22:53:56 +00001714#ifdef __BEOS__
1715/* Not available in BeOS yet. - [cjh] */
1716 PyErr_SetString( PySocket_Error, "getprotobyname not supported" );
1717 return NULL;
1718#else
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001719 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
Guido van Rossum3901d851996-12-19 16:35:04 +00001720 return NULL;
1721 Py_BEGIN_ALLOW_THREADS
1722 sp = getprotobyname(name);
1723 Py_END_ALLOW_THREADS
1724 if (sp == NULL) {
1725 PyErr_SetString(PySocket_Error, "protocol not found");
1726 return NULL;
1727 }
1728 return PyInt_FromLong((long) sp->p_proto);
Guido van Rossumbcc20741998-08-04 22:53:56 +00001729#endif
Guido van Rossum3901d851996-12-19 16:35:04 +00001730}
1731
Guido van Rossum82a5c661998-07-07 20:45:43 +00001732static char getprotobyname_doc[] =
1733"getprotobyname(name) -> integer\n\
1734\n\
1735Return the protocol number for the named protocol. (Rarely used.)";
1736
Guido van Rossum3901d851996-12-19 16:35:04 +00001737
Guido van Rossum30a685f1991-06-27 15:51:29 +00001738/* Python interface to socket(family, type, proto).
1739 The third (protocol) argument is optional.
1740 Return a new socket object. */
1741
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001742/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001743static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001744PySocket_socket(PyObject *self, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001745{
Guido van Rossum73624e91994-10-10 17:59:00 +00001746 PySocketSockObject *s;
Fred Drakea04eaad2000-06-30 02:46:07 +00001747 SOCKET_T fd;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001748 int family, type, proto = 0;
Guido van Rossum43713e52000-02-29 13:59:29 +00001749 if (!PyArg_ParseTuple(args, "ii|i:socket", &family, &type, &proto))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001750 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001751 Py_BEGIN_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001752 fd = socket(family, type, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00001753 Py_END_ALLOW_THREADS
Guido van Rossum6f489d91996-06-28 20:15:15 +00001754#ifdef MS_WINDOWS
Guido van Rossumd639d4d1996-06-12 04:04:22 +00001755 if (fd == INVALID_SOCKET)
1756#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001757 if (fd < 0)
Guido van Rossumd639d4d1996-06-12 04:04:22 +00001758#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001759 return PySocket_Err();
1760 s = PySocketSock_New(fd, family, type, proto);
Guido van Rossum30a685f1991-06-27 15:51:29 +00001761 /* If the object can't be created, don't forget to close the
1762 file descriptor again! */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001763 if (s == NULL)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001764 (void) SOCKETCLOSE(fd);
Guido van Rossum81194471991-07-27 21:42:02 +00001765 /* From now on, ignore SIGPIPE and let the error checking
1766 do the work. */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001767#ifdef SIGPIPE
Guido van Rossum81194471991-07-27 21:42:02 +00001768 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001769#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001770 return (PyObject *) s;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001771}
1772
Guido van Rossum82a5c661998-07-07 20:45:43 +00001773static char socket_doc[] =
1774"socket(family, type[, proto]) -> socket object\n\
1775\n\
1776Open a socket of the given type. The family argument specifies the\n\
1777address family; it is normally AF_INET, sometimes AF_UNIX.\n\
1778The type argument specifies whether this is a stream (SOCK_STREAM)\n\
1779or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\
1780specifying the default protocol.";
1781
1782
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001783#ifndef NO_DUP
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001784/* Create a socket object from a numeric file description.
1785 Useful e.g. if stdin is a socket.
1786 Additional arguments as for socket(). */
1787
1788/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001789static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001790PySocket_fromfd(PyObject *self, PyObject *args)
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001791{
Guido van Rossum73624e91994-10-10 17:59:00 +00001792 PySocketSockObject *s;
Fred Drakea04eaad2000-06-30 02:46:07 +00001793 SOCKET_T fd;
1794 int family, type, proto = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001795 if (!PyArg_ParseTuple(args, "iii|i:fromfd",
1796 &fd, &family, &type, &proto))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001797 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +00001798 /* Dup the fd so it and the socket can be closed independently */
1799 fd = dup(fd);
1800 if (fd < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001801 return PySocket_Err();
1802 s = PySocketSock_New(fd, family, type, proto);
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001803 /* From now on, ignore SIGPIPE and let the error checking
1804 do the work. */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001805#ifdef SIGPIPE
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001806 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001807#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001808 return (PyObject *) s;
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001809}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001810
1811static char fromfd_doc[] =
1812"fromfd(fd, family, type[, proto]) -> socket object\n\
1813\n\
1814Create a socket object from the given file descriptor.\n\
1815The remaining arguments are the same as for socket().";
1816
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001817#endif /* NO_DUP */
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001818
Guido van Rossum82a5c661998-07-07 20:45:43 +00001819
Guido van Rossum006bf911996-06-12 04:04:55 +00001820static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001821PySocket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00001822{
1823 int x1, x2;
1824
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001825 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00001826 return NULL;
1827 }
1828 x2 = (int)ntohs((short)x1);
1829 return PyInt_FromLong(x2);
1830}
1831
Guido van Rossum82a5c661998-07-07 20:45:43 +00001832static char ntohs_doc[] =
1833"ntohs(integer) -> integer\n\
1834\n\
1835Convert a 16-bit integer from network to host byte order.";
1836
1837
Guido van Rossum006bf911996-06-12 04:04:55 +00001838static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001839PySocket_ntohl(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00001840{
1841 int x1, x2;
1842
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001843 if (!PyArg_ParseTuple(args, "i:ntohl", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00001844 return NULL;
1845 }
1846 x2 = ntohl(x1);
1847 return PyInt_FromLong(x2);
1848}
1849
Guido van Rossum82a5c661998-07-07 20:45:43 +00001850static char ntohl_doc[] =
1851"ntohl(integer) -> integer\n\
1852\n\
1853Convert a 32-bit integer from network to host byte order.";
1854
1855
Guido van Rossum006bf911996-06-12 04:04:55 +00001856static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001857PySocket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00001858{
1859 int x1, x2;
1860
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001861 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00001862 return NULL;
1863 }
1864 x2 = (int)htons((short)x1);
1865 return PyInt_FromLong(x2);
1866}
1867
Guido van Rossum82a5c661998-07-07 20:45:43 +00001868static char htons_doc[] =
1869"htons(integer) -> integer\n\
1870\n\
1871Convert a 16-bit integer from host to network byte order.";
1872
1873
Guido van Rossum006bf911996-06-12 04:04:55 +00001874static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001875PySocket_htonl(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00001876{
1877 int x1, x2;
1878
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001879 if (!PyArg_ParseTuple(args, "i:htonl", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00001880 return NULL;
1881 }
1882 x2 = htonl(x1);
1883 return PyInt_FromLong(x2);
1884}
1885
Guido van Rossum82a5c661998-07-07 20:45:43 +00001886static char htonl_doc[] =
1887"htonl(integer) -> integer\n\
1888\n\
1889Convert a 32-bit integer from host to network byte order.";
1890
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001891/*
1892 * socket.inet_aton() and socket.inet_ntoa() functions
1893 *
1894 * written 20 Aug 1999 by Ben Gertzfield <che@debian.org> <- blame him!
1895 *
1896 */
1897
1898static char inet_aton_doc[] =
1899"inet_aton(string) -> packed 32-bit IP representation\n\
1900\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00001901Convert an IP address in string format (123.45.67.89) to the 32-bit packed\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001902binary format used in low-level network functions.";
1903
1904static PyObject*
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001905PySocket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001906{
Guido van Rossuma2e48551999-09-09 15:42:59 +00001907#ifndef INADDR_NONE
1908#define INADDR_NONE (-1)
1909#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001910
Guido van Rossumc6a164b1999-08-20 19:11:27 +00001911 /* Have to use inet_addr() instead */
1912 char *ip_addr;
1913 long packed_addr;
1914
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001915 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) {
Guido van Rossumc6a164b1999-08-20 19:11:27 +00001916 return NULL;
1917 }
Guido van Rossumff3ab422000-04-24 15:16:03 +00001918#ifdef USE_GUSI1
Guido van Rossum1b6e4631999-11-04 18:22:29 +00001919 packed_addr = (long)inet_addr(ip_addr).s_addr;
1920#else
Guido van Rossumc6a164b1999-08-20 19:11:27 +00001921 packed_addr = inet_addr(ip_addr);
Guido van Rossum1b6e4631999-11-04 18:22:29 +00001922#endif
Guido van Rossumc6a164b1999-08-20 19:11:27 +00001923
1924 if (packed_addr == INADDR_NONE) { /* invalid address */
1925 PyErr_SetString(PySocket_Error,
1926 "illegal IP address string passed to inet_aton");
1927 return NULL;
1928 }
1929
1930 return PyString_FromStringAndSize((char *) &packed_addr,
1931 sizeof(packed_addr));
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001932}
1933
1934static char inet_ntoa_doc[] =
Fred Drakee0661342000-03-07 14:05:16 +00001935"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001936\n\
1937Convert an IP address from 32-bit packed binary format to string format";
1938
1939static PyObject*
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001940PySocket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001941{
1942 char *packed_str;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00001943 int addr_len;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001944 struct in_addr packed_addr;
1945
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001946 if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) {
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001947 return NULL;
1948 }
1949
1950 if (addr_len != sizeof(packed_addr)) {
1951 PyErr_SetString(PySocket_Error,
1952 "packed IP wrong length for inet_ntoa");
1953 return NULL;
1954 }
1955
1956 memcpy(&packed_addr, packed_str, addr_len);
1957
1958 return PyString_FromString(inet_ntoa(packed_addr));
1959}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001960
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001961
1962#ifdef USE_SSL
1963
1964/* This is a C function to be called for new object initialization */
1965static SSLObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001966newSSLObject(PySocketSockObject *Sock, char *key_file, char *cert_file)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001967{
1968 SSLObject *self;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001969
Guido van Rossumb18618d2000-05-03 23:44:39 +00001970 self = PyObject_New(SSLObject, &SSL_Type); /* Create new object */
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001971 if (self == NULL){
1972 PyErr_SetObject(SSLErrorObject,
1973 PyString_FromString("newSSLObject error"));
1974 return NULL;
1975 }
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001976 memset(self->server, '\0', sizeof(char) * 256);
1977 memset(self->issuer, '\0', sizeof(char) * 256);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001978
1979 self->x_attr = PyDict_New();
1980 self->ctx = SSL_CTX_new(SSLv23_method()); /* Set up context */
1981 if (self->ctx == NULL) {
1982 PyErr_SetObject(SSLErrorObject,
1983 PyString_FromString("SSL_CTX_new error"));
Guido van Rossumb18618d2000-05-03 23:44:39 +00001984 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001985 return NULL;
1986 }
1987
1988 if ( (key_file && !cert_file) || (!key_file && cert_file) )
1989 {
1990 PyErr_SetObject(SSLErrorObject,
1991 PyString_FromString(
1992 "Both the key & certificate files must be specified"));
Guido van Rossumb18618d2000-05-03 23:44:39 +00001993 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001994 return NULL;
1995 }
1996
1997 if (key_file && cert_file)
1998 {
1999 if (SSL_CTX_use_PrivateKey_file(self->ctx, key_file,
2000 SSL_FILETYPE_PEM) < 1)
2001 {
2002 PyErr_SetObject(SSLErrorObject,
2003 PyString_FromString(
2004 "SSL_CTX_use_PrivateKey_file error"));
Guido van Rossumb18618d2000-05-03 23:44:39 +00002005 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002006 return NULL;
2007 }
2008
2009 if (SSL_CTX_use_certificate_chain_file(self->ctx,
2010 cert_file) < 1)
2011 {
2012 PyErr_SetObject(SSLErrorObject,
2013 PyString_FromString(
2014 "SSL_CTX_use_certificate_chain_file error"));
Guido van Rossumb18618d2000-05-03 23:44:39 +00002015 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002016 return NULL;
2017 }
2018 }
2019
2020 SSL_CTX_set_verify(self->ctx,
2021 SSL_VERIFY_NONE, NULL); /* set verify lvl */
2022 self->ssl = SSL_new(self->ctx); /* New ssl struct */
2023 SSL_set_fd(self->ssl, Sock->sock_fd); /* Set the socket for SSL */
2024 SSL_set_connect_state(self->ssl);
2025
2026 if ((SSL_connect(self->ssl)) == -1) {
2027 /* Actually negotiate SSL connection */
2028 PyErr_SetObject(SSLErrorObject,
2029 PyString_FromString("SSL_connect error"));
Guido van Rossumb18618d2000-05-03 23:44:39 +00002030 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002031 return NULL;
2032 }
2033 self->ssl->debug = 1;
2034
2035 if ((self->server_cert = SSL_get_peer_certificate(self->ssl))) {
2036 X509_NAME_oneline(X509_get_subject_name(self->server_cert),
2037 self->server, 256);
2038 X509_NAME_oneline(X509_get_issuer_name(self->server_cert),
2039 self->issuer, 256);
2040 }
2041 self->x_attr = NULL;
2042 self->Socket = Sock;
2043 Py_INCREF(self->Socket);
2044 return self;
2045}
2046
2047/* This is the Python function called for new object initialization */
2048static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002049PySocket_ssl(PyObject *self, PyObject *args)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002050{
2051 SSLObject *rv;
2052 PySocketSockObject *Sock;
2053 char *key_file;
2054 char *cert_file;
2055
Guido van Rossum43713e52000-02-29 13:59:29 +00002056 if (!PyArg_ParseTuple(args, "O!zz:ssl",
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002057 &PySocketSock_Type, (PyObject*)&Sock,
2058 &key_file, &cert_file) )
2059 return NULL;
2060
2061 rv = newSSLObject(Sock, key_file, cert_file);
2062 if ( rv == NULL )
2063 return NULL;
2064 return (PyObject *)rv;
2065}
2066
2067static char ssl_doc[] =
2068"ssl(socket, keyfile, certfile) -> sslobject";
2069
2070static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002071SSL_server(SSLObject *self, PyObject *args)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002072{
2073 return PyString_FromString(self->server);
2074}
2075
2076static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002077SSL_issuer(SSLObject *self, PyObject *args)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002078{
2079 return PyString_FromString(self->issuer);
2080}
2081
2082
2083/* SSL object methods */
2084
2085static PyMethodDef SSLMethods[] = {
2086 { "write", (PyCFunction)SSL_SSLwrite, 1 },
2087 { "read", (PyCFunction)SSL_SSLread, 1 },
2088 { "server", (PyCFunction)SSL_server, 1 },
2089 { "issuer", (PyCFunction)SSL_issuer, 1 },
2090 { NULL, NULL}
2091};
2092
2093static void SSL_dealloc(SSLObject *self)
2094{
2095 if (self->server_cert) /* Possible not to have one? */
2096 X509_free (self->server_cert);
2097 SSL_CTX_free(self->ctx);
2098 SSL_free(self->ssl);
2099 Py_XDECREF(self->x_attr);
2100 Py_XDECREF(self->Socket);
Guido van Rossumb18618d2000-05-03 23:44:39 +00002101 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002102}
2103
2104static PyObject *SSL_getattr(SSLObject *self, char *name)
2105{
2106 return Py_FindMethod(SSLMethods, (PyObject *)self, name);
2107}
2108
2109staticforward PyTypeObject SSL_Type = {
2110 PyObject_HEAD_INIT(&PyType_Type)
2111 0, /*ob_size*/
2112 "SSL", /*tp_name*/
2113 sizeof(SSLObject), /*tp_basicsize*/
2114 0, /*tp_itemsize*/
2115 /* methods */
2116 (destructor)SSL_dealloc, /*tp_dealloc*/
2117 0, /*tp_print*/
2118 (getattrfunc)SSL_getattr, /*tp_getattr*/
2119 0, /*tp_setattr*/
2120 0, /*tp_compare*/
2121 0, /*tp_repr*/
2122 0, /*tp_as_number*/
2123 0, /*tp_as_sequence*/
2124 0, /*tp_as_mapping*/
2125 0, /*tp_hash*/
2126};
2127
2128
2129
2130static PyObject *SSL_SSLwrite(SSLObject *self, PyObject *args)
2131{
2132 char *data;
Fred Drakea04eaad2000-06-30 02:46:07 +00002133 size_t len = 0;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002134
Guido van Rossum43713e52000-02-29 13:59:29 +00002135 if (!PyArg_ParseTuple(args, "s|i:write", &data, &len))
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002136 return NULL;
2137
2138 if (!len)
2139 len = strlen(data);
2140
2141 len = SSL_write(self->ssl, data, len);
2142 return PyInt_FromLong((long)len);
2143}
2144
2145static PyObject *SSL_SSLread(SSLObject *self, PyObject *args)
2146{
2147 PyObject *buf;
2148 int count = 0;
2149 int len = 1024;
2150 int res;
2151
Guido van Rossum43713e52000-02-29 13:59:29 +00002152 PyArg_ParseTuple(args, "|i:read", &len);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002153
2154 if (!(buf = PyString_FromStringAndSize((char *) 0, len)))
2155 return NULL; /* Error object should already be set */
2156
2157 count = SSL_read(self->ssl, PyString_AsString(buf), len);
2158 res = SSL_get_error(self->ssl, count);
2159
2160 switch (res) {
2161 case 0: /* Good return value! */
2162 break;
2163 case 6:
2164 PyErr_SetString(SSLErrorObject, "EOF");
2165 Py_DECREF(buf);
2166 return NULL;
2167 break;
2168 case 5:
2169 default:
2170 return PyErr_SetFromErrno(SSLErrorObject);
2171 break;
2172 }
2173
2174 fflush(stderr);
2175
2176 if (count < 0) {
2177 Py_DECREF(buf);
2178 return PyErr_SetFromErrno(SSLErrorObject);
2179 }
2180
2181 if (count != len && _PyString_Resize(&buf, count) < 0)
2182 return NULL;
2183 return buf;
2184}
2185
2186#endif /* USE_SSL */
2187
2188
Guido van Rossum30a685f1991-06-27 15:51:29 +00002189/* List of functions exported by this module. */
2190
Guido van Rossum73624e91994-10-10 17:59:00 +00002191static PyMethodDef PySocket_methods[] = {
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002192 {"gethostbyname", PySocket_gethostbyname,
2193 METH_VARARGS, gethostbyname_doc},
2194 {"gethostbyname_ex", PySocket_gethostbyname_ex,
2195 METH_VARARGS, ghbn_ex_doc},
2196 {"gethostbyaddr", PySocket_gethostbyaddr,
2197 METH_VARARGS, gethostbyaddr_doc},
2198 {"gethostname", PySocket_gethostname,
2199 METH_VARARGS, gethostname_doc},
2200 {"getservbyname", PySocket_getservbyname,
2201 METH_VARARGS, getservbyname_doc},
2202 {"getprotobyname", PySocket_getprotobyname,
2203 METH_VARARGS,getprotobyname_doc},
2204 {"socket", PySocket_socket,
2205 METH_VARARGS, socket_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002206#ifndef NO_DUP
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002207 {"fromfd", PySocket_fromfd,
2208 METH_VARARGS, fromfd_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002209#endif
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002210 {"ntohs", PySocket_ntohs,
2211 METH_VARARGS, ntohs_doc},
2212 {"ntohl", PySocket_ntohl,
2213 METH_VARARGS, ntohl_doc},
2214 {"htons", PySocket_htons,
2215 METH_VARARGS, htons_doc},
2216 {"htonl", PySocket_htonl,
2217 METH_VARARGS, htonl_doc},
2218 {"inet_aton", PySocket_inet_aton,
2219 METH_VARARGS, inet_aton_doc},
2220 {"inet_ntoa", PySocket_inet_ntoa,
2221 METH_VARARGS, inet_ntoa_doc},
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002222#ifdef USE_SSL
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002223 {"ssl", PySocket_ssl,
2224 METH_VARARGS, ssl_doc},
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002225#endif /* USE_SSL */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002226 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002227};
2228
Guido van Rossum30a685f1991-06-27 15:51:29 +00002229
2230/* Convenience routine to export an integer value.
Barry Warsaw752300b1997-01-03 17:18:10 +00002231 *
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002232 * Errors are silently ignored, for better or for worse...
Barry Warsaw752300b1997-01-03 17:18:10 +00002233 */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002234static void
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002235insint(PyObject *d, char *name, int value)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002236{
Guido van Rossum73624e91994-10-10 17:59:00 +00002237 PyObject *v = PyInt_FromLong((long) value);
Barry Warsaw752300b1997-01-03 17:18:10 +00002238 if (!v || PyDict_SetItemString(d, name, v))
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002239 PyErr_Clear();
Barry Warsaw752300b1997-01-03 17:18:10 +00002240
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002241 Py_XDECREF(v);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002242}
2243
Guido van Rossum30a685f1991-06-27 15:51:29 +00002244
Guido van Rossum8d665e61996-06-26 18:22:49 +00002245#ifdef MS_WINDOWS
Guido van Rossumbe32c891996-06-20 16:25:29 +00002246
2247/* Additional initialization and cleanup for NT/Windows */
2248
2249static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00002250NTcleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00002251{
2252 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00002253}
2254
2255static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00002256NTinit(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00002257{
2258 WSADATA WSAData;
2259 int ret;
2260 char buf[100];
2261 ret = WSAStartup(0x0101, &WSAData);
2262 switch (ret) {
2263 case 0: /* no error */
2264 atexit(NTcleanup);
2265 return 1;
2266 case WSASYSNOTREADY:
2267 PyErr_SetString(PyExc_ImportError,
2268 "WSAStartup failed: network not ready");
2269 break;
2270 case WSAVERNOTSUPPORTED:
2271 case WSAEINVAL:
2272 PyErr_SetString(PyExc_ImportError,
2273 "WSAStartup failed: requested version not supported");
2274 break;
2275 default:
2276 sprintf(buf, "WSAStartup failed: error code %d", ret);
2277 PyErr_SetString(PyExc_ImportError, buf);
2278 break;
2279 }
2280 return 0;
2281}
2282
Guido van Rossum8d665e61996-06-26 18:22:49 +00002283#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00002284
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002285#if defined(PYOS_OS2)
2286
2287/* Additional initialization and cleanup for OS/2 */
2288
2289static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00002290OS2cleanup(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002291{
2292 /* No cleanup is necessary for OS/2 Sockets */
2293}
2294
2295static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00002296OS2init(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002297{
2298 char reason[64];
2299 int rc = sock_init();
2300
2301 if (rc == 0) {
Guido van Rossum32c575d1997-12-02 20:37:32 +00002302 atexit(OS2cleanup);
2303 return 1; /* Indicate Success */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002304 }
2305
2306 sprintf(reason, "OS/2 TCP/IP Error# %d", sock_errno());
2307 PyErr_SetString(PyExc_ImportError, reason);
2308
Guido van Rossum32c575d1997-12-02 20:37:32 +00002309 return 0; /* Indicate Failure */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002310}
2311
2312#endif /* PYOS_OS2 */
2313
Guido van Rossum30a685f1991-06-27 15:51:29 +00002314/* Initialize this module.
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002315 * This is called when the first 'import socket' is done,
2316 * via a table in config.c, if config.c is compiled with USE_SOCKET
2317 * defined.
2318 *
2319 * For MS_WINDOWS (which means any Windows variant), this module
2320 * is actually called "_socket", and there's a wrapper "socket.py"
2321 * which implements some missing functionality (such as makefile(),
2322 * dup() and fromfd()). The import of "_socket" may fail with an
2323 * ImportError exception if initialization of WINSOCK fails. When
2324 * WINSOCK is initialized succesfully, a call to WSACleanup() is
2325 * scheduled to be made at exit time.
2326 *
2327 * For OS/2, this module is also called "_socket" and uses a wrapper
2328 * "socket.py" which implements that functionality that is missing
2329 * when PC operating systems don't put socket descriptors in the
2330 * operating system's filesystem layer.
2331 */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002332
Guido van Rossum82a5c661998-07-07 20:45:43 +00002333static char module_doc[] =
Fred Drakea136d492000-08-16 14:18:30 +00002334"Implementation module for socket operations. See the socket module\n\
2335for documentation.";
Guido van Rossum82a5c661998-07-07 20:45:43 +00002336
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)
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00002365init_socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002366{
Guido van Rossum73624e91994-10-10 17:59:00 +00002367 PyObject *m, *d;
Guido van Rossum8d665e61996-06-26 18:22:49 +00002368#ifdef MS_WINDOWS
Guido van Rossumbe32c891996-06-20 16:25:29 +00002369 if (!NTinit())
2370 return;
Guido van Rossumbe32c891996-06-20 16:25:29 +00002371#else
Guido van Rossum32c575d1997-12-02 20:37:32 +00002372#if defined(__TOS_OS2__)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002373 if (!OS2init())
2374 return;
Fred Drakea136d492000-08-16 14:18:30 +00002375#endif /* __TOS_OS2__ */
2376#endif /* MS_WINDOWS */
Guido van Rossum82a5c661998-07-07 20:45:43 +00002377 m = Py_InitModule3("_socket", PySocket_methods, module_doc);
Guido van Rossum73624e91994-10-10 17:59:00 +00002378 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002379 PySocket_Error = PyErr_NewException("socket.error", NULL, NULL);
2380 if (PySocket_Error == NULL)
2381 return;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002382#ifdef USE_SSL
2383 SSL_load_error_strings();
2384 SSLeay_add_ssl_algorithms();
2385 SSLErrorObject = PyErr_NewException("socket.sslerror", NULL, NULL);
2386 if (SSLErrorObject == NULL)
2387 return;
2388 PyDict_SetItemString(d, "sslerror", SSLErrorObject);
2389 Py_INCREF(&SSL_Type);
2390 if (PyDict_SetItemString(d, "SSLType",
2391 (PyObject *)&SSL_Type) != 0)
2392 return;
2393#endif /* USE_SSL */
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002394 PyDict_SetItemString(d, "error", PySocket_Error);
Guido van Rossum3b4b6fc1997-05-21 14:37:37 +00002395 PySocketSock_Type.ob_type = &PyType_Type;
Guido van Rossum82a5c661998-07-07 20:45:43 +00002396 PySocketSock_Type.tp_doc = sockettype_doc;
Guido van Rossum3b4b6fc1997-05-21 14:37:37 +00002397 Py_INCREF(&PySocketSock_Type);
Guido van Rossum91ba64d1997-06-02 22:18:09 +00002398 if (PyDict_SetItemString(d, "SocketType",
Guido van Rossum3b4b6fc1997-05-21 14:37:37 +00002399 (PyObject *)&PySocketSock_Type) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002400 return;
Guido van Rossum09be4091999-08-09 14:40:40 +00002401
2402 /* Address families (we only support AF_INET and AF_UNIX) */
2403#ifdef AF_UNSPEC
2404 insint(d, "AF_UNSPEC", AF_UNSPEC);
2405#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002406 insint(d, "AF_INET", AF_INET);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002407#ifdef AF_UNIX
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002408 insint(d, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002409#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00002410#ifdef AF_AX25
2411 insint(d, "AF_AX25", AF_AX25); /* Amateur Radio AX.25 */
2412#endif
2413#ifdef AF_IPX
2414 insint(d, "AF_IPX", AF_IPX); /* Novell IPX */
2415#endif
2416#ifdef AF_APPLETALK
2417 insint(d, "AF_APPLETALK", AF_APPLETALK); /* Appletalk DDP */
2418#endif
2419#ifdef AF_NETROM
2420 insint(d, "AF_NETROM", AF_NETROM); /* Amateur radio NetROM */
2421#endif
2422#ifdef AF_BRIDGE
2423 insint(d, "AF_BRIDGE", AF_BRIDGE); /* Multiprotocol bridge */
2424#endif
2425#ifdef AF_AAL5
2426 insint(d, "AF_AAL5", AF_AAL5); /* Reserved for Werner's ATM */
2427#endif
2428#ifdef AF_X25
2429 insint(d, "AF_X25", AF_X25); /* Reserved for X.25 project */
2430#endif
2431#ifdef AF_INET6
2432 insint(d, "AF_INET6", AF_INET6); /* IP version 6 */
2433#endif
2434#ifdef AF_ROSE
2435 insint(d, "AF_ROSE", AF_ROSE); /* Amateur Radio X.25 PLP */
2436#endif
2437
2438 /* Socket types */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002439 insint(d, "SOCK_STREAM", SOCK_STREAM);
2440 insint(d, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00002441#ifndef __BEOS__
2442/* We have incomplete socket support. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002443 insint(d, "SOCK_RAW", SOCK_RAW);
2444 insint(d, "SOCK_SEQPACKET", SOCK_SEQPACKET);
2445 insint(d, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00002446#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002447
2448#ifdef SO_DEBUG
2449 insint(d, "SO_DEBUG", SO_DEBUG);
2450#endif
2451#ifdef SO_ACCEPTCONN
2452 insint(d, "SO_ACCEPTCONN", SO_ACCEPTCONN);
2453#endif
2454#ifdef SO_REUSEADDR
2455 insint(d, "SO_REUSEADDR", SO_REUSEADDR);
2456#endif
2457#ifdef SO_KEEPALIVE
2458 insint(d, "SO_KEEPALIVE", SO_KEEPALIVE);
2459#endif
2460#ifdef SO_DONTROUTE
2461 insint(d, "SO_DONTROUTE", SO_DONTROUTE);
2462#endif
2463#ifdef SO_BROADCAST
2464 insint(d, "SO_BROADCAST", SO_BROADCAST);
2465#endif
2466#ifdef SO_USELOOPBACK
2467 insint(d, "SO_USELOOPBACK", SO_USELOOPBACK);
2468#endif
2469#ifdef SO_LINGER
2470 insint(d, "SO_LINGER", SO_LINGER);
2471#endif
2472#ifdef SO_OOBINLINE
2473 insint(d, "SO_OOBINLINE", SO_OOBINLINE);
2474#endif
2475#ifdef SO_REUSEPORT
2476 insint(d, "SO_REUSEPORT", SO_REUSEPORT);
2477#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002478#ifdef SO_SNDBUF
2479 insint(d, "SO_SNDBUF", SO_SNDBUF);
2480#endif
2481#ifdef SO_RCVBUF
2482 insint(d, "SO_RCVBUF", SO_RCVBUF);
2483#endif
2484#ifdef SO_SNDLOWAT
2485 insint(d, "SO_SNDLOWAT", SO_SNDLOWAT);
2486#endif
2487#ifdef SO_RCVLOWAT
2488 insint(d, "SO_RCVLOWAT", SO_RCVLOWAT);
2489#endif
2490#ifdef SO_SNDTIMEO
2491 insint(d, "SO_SNDTIMEO", SO_SNDTIMEO);
2492#endif
2493#ifdef SO_RCVTIMEO
2494 insint(d, "SO_RCVTIMEO", SO_RCVTIMEO);
2495#endif
2496#ifdef SO_ERROR
2497 insint(d, "SO_ERROR", SO_ERROR);
2498#endif
2499#ifdef SO_TYPE
2500 insint(d, "SO_TYPE", SO_TYPE);
2501#endif
2502
2503 /* Maximum number of connections for "listen" */
2504#ifdef SOMAXCONN
2505 insint(d, "SOMAXCONN", SOMAXCONN);
2506#else
2507 insint(d, "SOMAXCONN", 5); /* Common value */
2508#endif
2509
2510 /* Flags for send, recv */
2511#ifdef MSG_OOB
2512 insint(d, "MSG_OOB", MSG_OOB);
2513#endif
2514#ifdef MSG_PEEK
2515 insint(d, "MSG_PEEK", MSG_PEEK);
2516#endif
2517#ifdef MSG_DONTROUTE
2518 insint(d, "MSG_DONTROUTE", MSG_DONTROUTE);
2519#endif
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00002520#ifdef MSG_DONTWAIT
Guido van Rossum14f3f592000-04-25 21:53:58 +00002521 insint(d, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00002522#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002523#ifdef MSG_EOR
2524 insint(d, "MSG_EOR", MSG_EOR);
2525#endif
2526#ifdef MSG_TRUNC
2527 insint(d, "MSG_TRUNC", MSG_TRUNC);
2528#endif
2529#ifdef MSG_CTRUNC
2530 insint(d, "MSG_CTRUNC", MSG_CTRUNC);
2531#endif
2532#ifdef MSG_WAITALL
2533 insint(d, "MSG_WAITALL", MSG_WAITALL);
2534#endif
2535#ifdef MSG_BTAG
2536 insint(d, "MSG_BTAG", MSG_BTAG);
2537#endif
2538#ifdef MSG_ETAG
2539 insint(d, "MSG_ETAG", MSG_ETAG);
2540#endif
2541
2542 /* Protocol level and numbers, usable for [gs]etsockopt */
2543#ifdef SOL_SOCKET
2544 insint(d, "SOL_SOCKET", SOL_SOCKET);
2545#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00002546#ifdef SOL_IP
2547 insint(d, "SOL_IP", SOL_IP);
2548#else
2549 insint(d, "SOL_IP", 0);
2550#endif
2551#ifdef SOL_IPX
2552 insint(d, "SOL_IPX", SOL_IPX);
2553#endif
2554#ifdef SOL_AX25
2555 insint(d, "SOL_AX25", SOL_AX25);
2556#endif
2557#ifdef SOL_ATALK
2558 insint(d, "SOL_ATALK", SOL_ATALK);
2559#endif
2560#ifdef SOL_NETROM
2561 insint(d, "SOL_NETROM", SOL_NETROM);
2562#endif
2563#ifdef SOL_ROSE
2564 insint(d, "SOL_ROSE", SOL_ROSE);
2565#endif
2566#ifdef SOL_TCP
2567 insint(d, "SOL_TCP", SOL_TCP);
2568#else
2569 insint(d, "SOL_TCP", 6);
2570#endif
2571#ifdef SOL_UDP
2572 insint(d, "SOL_UDP", SOL_UDP);
2573#else
2574 insint(d, "SOL_UDP", 17);
2575#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002576#ifdef IPPROTO_IP
2577 insint(d, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00002578#else
2579 insint(d, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002580#endif
2581#ifdef IPPROTO_ICMP
2582 insint(d, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00002583#else
2584 insint(d, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002585#endif
2586#ifdef IPPROTO_IGMP
2587 insint(d, "IPPROTO_IGMP", IPPROTO_IGMP);
2588#endif
2589#ifdef IPPROTO_GGP
2590 insint(d, "IPPROTO_GGP", IPPROTO_GGP);
2591#endif
2592#ifdef IPPROTO_TCP
2593 insint(d, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00002594#else
2595 insint(d, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002596#endif
2597#ifdef IPPROTO_EGP
2598 insint(d, "IPPROTO_EGP", IPPROTO_EGP);
2599#endif
2600#ifdef IPPROTO_PUP
2601 insint(d, "IPPROTO_PUP", IPPROTO_PUP);
2602#endif
2603#ifdef IPPROTO_UDP
2604 insint(d, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00002605#else
2606 insint(d, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002607#endif
2608#ifdef IPPROTO_IDP
2609 insint(d, "IPPROTO_IDP", IPPROTO_IDP);
2610#endif
2611#ifdef IPPROTO_HELLO
2612 insint(d, "IPPROTO_HELLO", IPPROTO_HELLO);
2613#endif
2614#ifdef IPPROTO_ND
2615 insint(d, "IPPROTO_ND", IPPROTO_ND);
2616#endif
2617#ifdef IPPROTO_TP
2618 insint(d, "IPPROTO_TP", IPPROTO_TP);
2619#endif
2620#ifdef IPPROTO_XTP
2621 insint(d, "IPPROTO_XTP", IPPROTO_XTP);
2622#endif
2623#ifdef IPPROTO_EON
2624 insint(d, "IPPROTO_EON", IPPROTO_EON);
2625#endif
2626#ifdef IPPROTO_BIP
2627 insint(d, "IPPROTO_BIP", IPPROTO_BIP);
2628#endif
2629/**/
2630#ifdef IPPROTO_RAW
2631 insint(d, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00002632#else
2633 insint(d, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002634#endif
2635#ifdef IPPROTO_MAX
2636 insint(d, "IPPROTO_MAX", IPPROTO_MAX);
2637#endif
2638
2639 /* Some port configuration */
2640#ifdef IPPORT_RESERVED
2641 insint(d, "IPPORT_RESERVED", IPPORT_RESERVED);
2642#else
2643 insint(d, "IPPORT_RESERVED", 1024);
2644#endif
2645#ifdef IPPORT_USERRESERVED
2646 insint(d, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
2647#else
2648 insint(d, "IPPORT_USERRESERVED", 5000);
2649#endif
2650
Guido van Rossum5f05eb41995-02-17 15:11:07 +00002651 /* Some reserved IP v.4 addresses */
2652#ifdef INADDR_ANY
2653 insint(d, "INADDR_ANY", INADDR_ANY);
2654#else
2655 insint(d, "INADDR_ANY", 0x00000000);
2656#endif
2657#ifdef INADDR_BROADCAST
2658 insint(d, "INADDR_BROADCAST", INADDR_BROADCAST);
2659#else
2660 insint(d, "INADDR_BROADCAST", 0xffffffff);
2661#endif
2662#ifdef INADDR_LOOPBACK
2663 insint(d, "INADDR_LOOPBACK", INADDR_LOOPBACK);
2664#else
2665 insint(d, "INADDR_LOOPBACK", 0x7F000001);
2666#endif
2667#ifdef INADDR_UNSPEC_GROUP
2668 insint(d, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
2669#else
2670 insint(d, "INADDR_UNSPEC_GROUP", 0xe0000000);
2671#endif
2672#ifdef INADDR_ALLHOSTS_GROUP
2673 insint(d, "INADDR_ALLHOSTS_GROUP", INADDR_ALLHOSTS_GROUP);
2674#else
2675 insint(d, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
2676#endif
2677#ifdef INADDR_MAX_LOCAL_GROUP
2678 insint(d, "INADDR_MAX_LOCAL_GROUP", INADDR_MAX_LOCAL_GROUP);
2679#else
2680 insint(d, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
2681#endif
2682#ifdef INADDR_NONE
2683 insint(d, "INADDR_NONE", INADDR_NONE);
2684#else
2685 insint(d, "INADDR_NONE", 0xffffffff);
2686#endif
2687
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002688 /* IP [gs]etsockopt options */
2689#ifdef IP_OPTIONS
2690 insint(d, "IP_OPTIONS", IP_OPTIONS);
2691#endif
2692#ifdef IP_HDRINCL
2693 insint(d, "IP_HDRINCL", IP_HDRINCL);
2694#endif
2695#ifdef IP_TOS
2696 insint(d, "IP_TOS", IP_TOS);
2697#endif
2698#ifdef IP_TTL
2699 insint(d, "IP_TTL", IP_TTL);
2700#endif
2701#ifdef IP_RECVOPTS
2702 insint(d, "IP_RECVOPTS", IP_RECVOPTS);
2703#endif
2704#ifdef IP_RECVRETOPTS
2705 insint(d, "IP_RECVRETOPTS", IP_RECVRETOPTS);
2706#endif
2707#ifdef IP_RECVDSTADDR
2708 insint(d, "IP_RECVDSTADDR", IP_RECVDSTADDR);
2709#endif
2710#ifdef IP_RETOPTS
2711 insint(d, "IP_RETOPTS", IP_RETOPTS);
2712#endif
2713#ifdef IP_MULTICAST_IF
2714 insint(d, "IP_MULTICAST_IF", IP_MULTICAST_IF);
2715#endif
2716#ifdef IP_MULTICAST_TTL
2717 insint(d, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
2718#endif
2719#ifdef IP_MULTICAST_LOOP
2720 insint(d, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
2721#endif
2722#ifdef IP_ADD_MEMBERSHIP
2723 insint(d, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
2724#endif
2725#ifdef IP_DROP_MEMBERSHIP
2726 insint(d, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
2727#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00002728#ifdef IP_DEFAULT_MULTICAST_TTL
2729 insint(d, "IP_DEFAULT_MULTICAST_TTL", IP_DEFAULT_MULTICAST_TTL);
2730#endif
2731#ifdef IP_DEFAULT_MULTICAST_LOOP
2732 insint(d, "IP_DEFAULT_MULTICAST_LOOP", IP_DEFAULT_MULTICAST_LOOP);
2733#endif
2734#ifdef IP_MAX_MEMBERSHIPS
2735 insint(d, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
2736#endif
2737
2738 /* TCP options */
2739#ifdef TCP_NODELAY
2740 insint(d, "TCP_NODELAY", TCP_NODELAY);
2741#endif
2742#ifdef TCP_MAXSEG
2743 insint(d, "TCP_MAXSEG", TCP_MAXSEG);
2744#endif
2745
2746 /* IPX options */
2747#ifdef IPX_TYPE
2748 insint(d, "IPX_TYPE", IPX_TYPE);
2749#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002750
2751 /* Initialize gethostbyname lock */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002752#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum65d5b571998-12-21 19:32:43 +00002753 gethostbyname_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002754#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002755}