blob: c92dc95553a3e1f96e80e29c1edafe3e9fef690f [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
Jeremy Hylton22308652001-02-02 03:23:09 +000010- only AF_INET and AF_UNIX address families are supported in a
11 portable manner, though AF_PACKET is supported under Linux.
Guido van Rossum81194471991-07-27 21:42:02 +000012- no read/write operations (use send/recv or makefile instead)
Guido van Rossumbe32c891996-06-20 16:25:29 +000013- additional restrictions apply on Windows
Guido van Rossum6574b3e1991-06-25 21:36:08 +000014
Guido van Rossum27e177d1995-03-16 15:43:47 +000015Module interface:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000016
Guido van Rossum27e177d1995-03-16 15:43:47 +000017- socket.error: exception raised for socket specific errors
Guido van Rossum30a685f1991-06-27 15:51:29 +000018- socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd')
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000019- socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...])
Guido van Rossum27e177d1995-03-16 15:43:47 +000020- socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com')
Guido van Rossum25405c71996-12-19 16:42:52 +000021- socket.getprotobyname(protocolname) --> protocol number
Guido van Rossum27e177d1995-03-16 15:43:47 +000022- socket.getservbyname(servicename, protocolname) --> port number
Guido van Rossum4dd2a7e1991-07-01 18:51:33 +000023- socket.socket(family, type [, proto]) --> new socket object
Guido van Rossum006bf911996-06-12 04:04:55 +000024- socket.ntohs(16 bit value) --> new int object
25- socket.ntohl(32 bit value) --> new int object
26- socket.htons(16 bit value) --> new int object
27- socket.htonl(32 bit value) --> new int object
Guido van Rossum27e177d1995-03-16 15:43:47 +000028- socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +000029- socket.inet_aton(IP address) -> 32-bit packed IP representation
30- socket.inet_ntoa(packed IP) -> IP address string
Guido van Rossum09c8b6c1999-12-07 21:37:17 +000031- socket.ssl(socket, keyfile, certfile) -> new ssl object
Guido van Rossum6574b3e1991-06-25 21:36:08 +000032- an Internet socket address is a pair (hostname, port)
33 where hostname can be anything recognized by gethostbyname()
34 (including the dd.dd.dd.dd notation) and port is in host byte order
35- where a hostname is returned, the dd.dd.dd.dd notation is used
Guido van Rossum27e177d1995-03-16 15:43:47 +000036- a UNIX domain socket address is a string specifying the pathname
Jeremy Hylton22308652001-02-02 03:23:09 +000037- an AF_PACKET socket address is a tuple containing a string
38 specifying the ethernet interface and an integer specifying
39 the Ethernet protocol number to be received. For example:
Jeremy Hyltondbfb6622001-02-02 19:55:17 +000040 ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple
41 specify packet-type and ha-type/addr -- these are ignored by
Jeremy Hylton22308652001-02-02 03:23:09 +000042 networking code, but accepted since they are returned by the
43 getsockname() method.
Guido van Rossum6574b3e1991-06-25 21:36:08 +000044
Guido van Rossum30a685f1991-06-27 15:51:29 +000045Socket methods:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000046
Guido van Rossum81194471991-07-27 21:42:02 +000047- s.accept() --> new socket object, sockaddr
Fred Drake728819a2000-07-01 03:40:12 +000048- s.bind(sockaddr) --> None
Guido van Rossum27e177d1995-03-16 15:43:47 +000049- s.close() --> None
Fred Drake728819a2000-07-01 03:40:12 +000050- s.connect(sockaddr) --> None
51- s.connect_ex(sockaddr) --> 0 or errno (handy for e.g. async connect)
Guido van Rossum27e177d1995-03-16 15:43:47 +000052- s.fileno() --> file descriptor
Guido van Rossumbe32c891996-06-20 16:25:29 +000053- s.dup() --> same as socket.fromfd(os.dup(s.fileno(), ...)
Guido van Rossumc89705d1992-11-26 08:54:07 +000054- s.getpeername() --> sockaddr
Guido van Rossum27e177d1995-03-16 15:43:47 +000055- s.getsockname() --> sockaddr
56- s.getsockopt(level, optname[, buflen]) --> int or string
57- s.listen(backlog) --> None
Guido van Rossum6b144911995-03-14 15:05:13 +000058- s.makefile([mode[, bufsize]]) --> file object
Guido van Rossum27e177d1995-03-16 15:43:47 +000059- s.recv(buflen [,flags]) --> string
60- s.recvfrom(buflen [,flags]) --> string, sockaddr
Guido van Rossumb6775db1994-08-01 11:34:53 +000061- s.send(string [,flags]) --> nbytes
62- s.sendto(string, [flags,] sockaddr) --> nbytes
Guido van Rossum27e177d1995-03-16 15:43:47 +000063- s.setblocking(0 | 1) --> None
64- s.setsockopt(level, optname, value) --> None
65- s.shutdown(how) --> None
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000066- repr(s) --> "<socket object, fd=%d, family=%d, type=%d, protocol=%d>"
Guido van Rossum30a685f1991-06-27 15:51:29 +000067
Guido van Rossum6574b3e1991-06-25 21:36:08 +000068*/
69
Guido van Rossum73624e91994-10-10 17:59:00 +000070#include "Python.h"
Guido van Rossum3baaa131999-03-22 21:44:51 +000071
Guido van Rossum9376b741999-09-15 22:01:40 +000072/* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure
73 script doesn't get this right, so we hardcode some platform checks below.
74 On the other hand, not all Linux versions agree, so there the settings
75 computed by the configure script are needed! */
76
77#ifndef linux
Guido van Rossume7de2061999-03-24 17:24:33 +000078#undef HAVE_GETHOSTBYNAME_R_3_ARG
79#undef HAVE_GETHOSTBYNAME_R_5_ARG
80#undef HAVE_GETHOSTBYNAME_R_6_ARG
Guido van Rossum9376b741999-09-15 22:01:40 +000081#endif
Guido van Rossume7de2061999-03-24 17:24:33 +000082
Guido van Rossum7a122991999-04-13 04:07:32 +000083#ifndef WITH_THREAD
84#undef HAVE_GETHOSTBYNAME_R
85#endif
86
Guido van Rossume7de2061999-03-24 17:24:33 +000087#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7a122991999-04-13 04:07:32 +000088#if defined(_AIX) || defined(__osf__)
Guido van Rossume7de2061999-03-24 17:24:33 +000089#define HAVE_GETHOSTBYNAME_R_3_ARG
90#elif defined(__sun__) || defined(__sgi)
91#define HAVE_GETHOSTBYNAME_R_5_ARG
92#elif defined(linux)
Guido van Rossum9376b741999-09-15 22:01:40 +000093/* Rely on the configure script */
Guido van Rossume7de2061999-03-24 17:24:33 +000094#else
95#undef HAVE_GETHOSTBYNAME_R
96#endif
97#endif
98
Guido van Rossum3baaa131999-03-22 21:44:51 +000099#if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && !defined(MS_WINDOWS)
100#define USE_GETHOSTBYNAME_LOCK
101#endif
102
103#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum49b56061998-10-01 20:42:43 +0000104#include "pythread.h"
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000105#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000106
Guido van Rossuma376cc51996-12-05 23:43:35 +0000107#ifdef HAVE_UNISTD_H
108#include <unistd.h>
109#endif
110
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000111#if defined(PYCC_VACPP)
112#include <types.h>
113#include <io.h>
114#include <sys/ioctl.h>
115#include <utils.h>
116#include <ctype.h>
117#endif
118
119#if defined(PYOS_OS2)
120#define INCL_DOS
121#define INCL_DOSERRORS
122#define INCL_NOPMAPI
123#include <os2.h>
124#endif
125
Guido van Rossum48a680c2001-03-02 06:34:14 +0000126#ifdef RISCOS
127#define NO_DUP
128#undef off_t
129#undef uid_t
130#undef gid_t
131#undef errno
132#include <signal.h>
133#include "socklib.h"
134#include "inetlib.h"
135#include "netdb.h"
136#include "unixlib.h"
137#include "netinet/in.h"
138#include "sys/ioctl.h"
139#else /*RISCOS*/
140
Guido van Rossumb6775db1994-08-01 11:34:53 +0000141#include <sys/types.h>
Guido van Rossumed233a51992-06-23 09:07:03 +0000142
Guido van Rossum81194471991-07-27 21:42:02 +0000143#include <signal.h>
Guido van Rossum6f489d91996-06-28 20:15:15 +0000144#ifndef MS_WINDOWS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000145#include <netdb.h>
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000146#include <sys/socket.h>
147#include <netinet/in.h>
Fred Drake51d90362000-10-06 15:37:06 +0000148#if !(defined(__BEOS__) || defined(__CYGWIN__))
Andrew M. Kuchling52a644c2000-03-31 17:24:30 +0000149#include <netinet/tcp.h>
Fred Drake11b09362000-05-16 13:30:12 +0000150#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000151
Guido van Rossum9376b741999-09-15 22:01:40 +0000152/* Headers needed for inet_ntoa() and inet_addr() */
153#ifdef __BEOS__
154#include <net/netdb.h>
155#else
Guido van Rossumff3ab422000-04-24 15:16:03 +0000156#ifndef USE_GUSI1
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000157#include <arpa/inet.h>
Guido van Rossum9376b741999-09-15 22:01:40 +0000158#endif
Guido van Rossum1b6e4631999-11-04 18:22:29 +0000159#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000160
Guido van Rossume4485b01994-09-07 14:32:49 +0000161#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000162#else
163#include <winsock.h>
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000164#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000165#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000166
167#endif /*RISCOS*/
168
Guido van Rossumb6775db1994-08-01 11:34:53 +0000169#ifdef HAVE_SYS_UN_H
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000170#include <sys/un.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000171#else
172#undef AF_UNIX
Guido van Rossum9575a441993-04-07 14:06:14 +0000173#endif
174
Jeremy Hylton22308652001-02-02 03:23:09 +0000175#if defined(linux) && defined(AF_PACKET)
176#include <sys/ioctl.h>
177#include <net/if.h>
178#include <netpacket/packet.h>
179#endif
180
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000181#ifndef O_NDELAY
182#define O_NDELAY O_NONBLOCK /* For QNX only? */
183#endif
184
Guido van Rossumff3ab422000-04-24 15:16:03 +0000185#ifdef USE_GUSI1
Jack Jansen508537b1996-02-14 15:57:45 +0000186/* fdopen() isn't declared in stdio.h (sigh) */
187#include <GUSI.h>
188#endif
189
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000190#ifdef USE_SSL
Andrew M. Kuchling697a0b02001-01-18 17:41:41 +0000191#include "openssl/rsa.h"
192#include "openssl/crypto.h"
193#include "openssl/x509.h"
194#include "openssl/pem.h"
195#include "openssl/ssl.h"
196#include "openssl/err.h"
Moshe Zadka8f4eab22001-03-18 17:11:56 +0000197#include "openssl/rand.h"
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000198#endif /* USE_SSL */
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000199
Guido van Rossumbcc20741998-08-04 22:53:56 +0000200#if defined(MS_WINDOWS) || defined(__BEOS__)
201/* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000202/* seem to be a few differences in the API */
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000203#define SOCKETCLOSE closesocket
Guido van Rossumbe32c891996-06-20 16:25:29 +0000204#define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000205#endif
206
Fred Drakea04eaad2000-06-30 02:46:07 +0000207/* abstract the socket file descriptor type */
208#ifdef MS_WINDOWS
209typedef SOCKET SOCKET_T;
210# ifdef MS_WIN64
211# define SIZEOF_SOCKET_T 8
212# else
213# define SIZEOF_SOCKET_T 4
214# endif
215#else
216typedef int SOCKET_T;
217# define SIZEOF_SOCKET_T SIZEOF_INT
218#endif
219
220
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000221#if defined(PYOS_OS2)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000222#define SOCKETCLOSE soclose
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000223#define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000224#endif
225
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000226#ifndef SOCKETCLOSE
227#define SOCKETCLOSE close
228#endif
229
Guido van Rossum30a685f1991-06-27 15:51:29 +0000230/* Global variable holding the exception type for errors detected
231 by this module (but not argument type or memory errors, etc.). */
232
Guido van Rossum73624e91994-10-10 17:59:00 +0000233static PyObject *PySocket_Error;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000234
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000235#ifdef USE_SSL
236static PyObject *SSLErrorObject;
237#endif /* USE_SSL */
238
Guido van Rossum30a685f1991-06-27 15:51:29 +0000239
Guido van Rossum48a680c2001-03-02 06:34:14 +0000240#ifdef RISCOS
241/* Global variable which is !=0 if Python is running in a RISC OS taskwindow */
242static int taskwindow;
243#endif
244
245
Guido van Rossum30a685f1991-06-27 15:51:29 +0000246/* Convenience function to raise an error according to errno
247 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000248
Guido van Rossum73624e91994-10-10 17:59:00 +0000249static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000250PySocket_Err(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000251{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000252#ifdef MS_WINDOWS
Mark Hammond46a733d2000-07-24 01:45:11 +0000253 int err_no = WSAGetLastError();
254 if (err_no) {
255 static struct { int no; const char *msg; } *msgp, msgs[] = {
256 { WSAEINTR, "Interrupted system call" },
257 { WSAEBADF, "Bad file descriptor" },
258 { WSAEACCES, "Permission denied" },
259 { WSAEFAULT, "Bad address" },
260 { WSAEINVAL, "Invalid argument" },
261 { WSAEMFILE, "Too many open files" },
Guido van Rossum48a680c2001-03-02 06:34:14 +0000262 { WSAEWOULDBLOCK,
Mark Hammond46a733d2000-07-24 01:45:11 +0000263 "The socket operation could not complete "
264 "without blocking" },
265 { WSAEINPROGRESS, "Operation now in progress" },
266 { WSAEALREADY, "Operation already in progress" },
267 { WSAENOTSOCK, "Socket operation on non-socket" },
268 { WSAEDESTADDRREQ, "Destination address required" },
269 { WSAEMSGSIZE, "Message too long" },
270 { WSAEPROTOTYPE, "Protocol wrong type for socket" },
271 { WSAENOPROTOOPT, "Protocol not available" },
272 { WSAEPROTONOSUPPORT, "Protocol not supported" },
273 { WSAESOCKTNOSUPPORT, "Socket type not supported" },
274 { WSAEOPNOTSUPP, "Operation not supported" },
275 { WSAEPFNOSUPPORT, "Protocol family not supported" },
276 { WSAEAFNOSUPPORT, "Address family not supported" },
277 { WSAEADDRINUSE, "Address already in use" },
278 { WSAEADDRNOTAVAIL,
279 "Can't assign requested address" },
280 { WSAENETDOWN, "Network is down" },
281 { WSAENETUNREACH, "Network is unreachable" },
Guido van Rossum48a680c2001-03-02 06:34:14 +0000282 { WSAENETRESET,
Mark Hammond46a733d2000-07-24 01:45:11 +0000283 "Network dropped connection on reset" },
Guido van Rossum48a680c2001-03-02 06:34:14 +0000284 { WSAECONNABORTED,
Mark Hammond46a733d2000-07-24 01:45:11 +0000285 "Software caused connection abort" },
286 { WSAECONNRESET, "Connection reset by peer" },
287 { WSAENOBUFS, "No buffer space available" },
288 { WSAEISCONN, "Socket is already connected" },
289 { WSAENOTCONN, "Socket is not connected" },
290 { WSAESHUTDOWN, "Can't send after socket shutdown" },
291 { WSAETOOMANYREFS,
292 "Too many references: can't splice" },
293 { WSAETIMEDOUT, "Operation timed out" },
294 { WSAECONNREFUSED, "Connection refused" },
295 { WSAELOOP, "Too many levels of symbolic links" },
296 { WSAENAMETOOLONG, "File name too long" },
297 { WSAEHOSTDOWN, "Host is down" },
298 { WSAEHOSTUNREACH, "No route to host" },
299 { WSAENOTEMPTY, "Directory not empty" },
300 { WSAEPROCLIM, "Too many processes" },
301 { WSAEUSERS, "Too many users" },
302 { WSAEDQUOT, "Disc quota exceeded" },
303 { WSAESTALE, "Stale NFS file handle" },
304 { WSAEREMOTE, "Too many levels of remote in path" },
305 { WSASYSNOTREADY,
306 "Network subsystem is unvailable" },
307 { WSAVERNOTSUPPORTED,
308 "WinSock version is not supported" },
Guido van Rossum48a680c2001-03-02 06:34:14 +0000309 { WSANOTINITIALISED,
Mark Hammond46a733d2000-07-24 01:45:11 +0000310 "Successful WSAStartup() not yet performed" },
311 { WSAEDISCON, "Graceful shutdown in progress" },
312 /* Resolver errors */
313 { WSAHOST_NOT_FOUND, "No such host is known" },
314 { WSATRY_AGAIN, "Host not found, or server failed" },
315 { WSANO_RECOVERY,
316 "Unexpected server error encountered" },
317 { WSANO_DATA, "Valid name without requested data" },
318 { WSANO_ADDRESS, "No address, look for MX record" },
319 { 0, NULL }
320 };
Guido van Rossum73624e91994-10-10 17:59:00 +0000321 PyObject *v;
Mark Hammond46a733d2000-07-24 01:45:11 +0000322 const char *msg = "winsock error";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000323
Mark Hammond46a733d2000-07-24 01:45:11 +0000324 for (msgp = msgs; msgp->msg; msgp++) {
325 if (err_no == msgp->no) {
326 msg = msgp->msg;
327 break;
328 }
329 }
330
331 v = Py_BuildValue("(is)", err_no, msg);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000332 if (v != NULL) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000333 PyErr_SetObject(PySocket_Error, v);
334 Py_DECREF(v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000335 }
336 return NULL;
337 }
338 else
339#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000340
341#if defined(PYOS_OS2)
342 if (sock_errno() != NO_ERROR) {
343 APIRET rc;
344 ULONG msglen;
345 char outbuf[100];
346 int myerrorcode = sock_errno();
347
348 /* Retrieve Socket-Related Error Message from MPTN.MSG File */
349 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
350 myerrorcode - SOCBASEERR + 26, "mptn.msg", &msglen);
351 if (rc == NO_ERROR) {
352 PyObject *v;
353
354 outbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
355 if (strlen(outbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
356 char *lastc = &outbuf[ strlen(outbuf)-1 ];
357 while (lastc > outbuf && isspace(*lastc))
358 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
359 }
360 v = Py_BuildValue("(is)", myerrorcode, outbuf);
361 if (v != NULL) {
362 PyErr_SetObject(PySocket_Error, v);
363 Py_DECREF(v);
364 }
365 return NULL;
366 }
367 }
368#endif
369
Guido van Rossum73624e91994-10-10 17:59:00 +0000370 return PyErr_SetFromErrno(PySocket_Error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000371}
372
Guido van Rossum30a685f1991-06-27 15:51:29 +0000373
374/* The object holding a socket. It holds some extra information,
375 like the address family, which is used to decode socket address
376 arguments properly. */
377
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000378typedef struct {
Guido van Rossum73624e91994-10-10 17:59:00 +0000379 PyObject_HEAD
Fred Drakea04eaad2000-06-30 02:46:07 +0000380 SOCKET_T sock_fd; /* Socket file descriptor */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000381 int sock_family; /* Address family, e.g., AF_INET */
382 int sock_type; /* Socket type, e.g., SOCK_STREAM */
383 int sock_proto; /* Protocol type, usually 0 */
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000384 union sock_addr {
385 struct sockaddr_in in;
386#ifdef AF_UNIX
387 struct sockaddr_un un;
388#endif
Jeremy Hylton22308652001-02-02 03:23:09 +0000389#if defined(linux) && defined(AF_PACKET)
390 struct sockaddr_ll ll;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000391#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000392 } sock_addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000393} PySocketSockObject;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000394
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000395#ifdef USE_SSL
Guido van Rossum30a685f1991-06-27 15:51:29 +0000396
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000397typedef struct {
398 PyObject_HEAD
399 PySocketSockObject *Socket; /* Socket on which we're layered */
400 PyObject *x_attr; /* Attributes dictionary */
401 SSL_CTX* ctx;
402 SSL* ssl;
403 X509* server_cert;
404 BIO* sbio;
405 char server[256];
406 char issuer[256];
407
408} SSLObject;
409
410staticforward PyTypeObject SSL_Type;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000411staticforward PyObject *SSL_SSLwrite(SSLObject *self, PyObject *args);
412staticforward PyObject *SSL_SSLread(SSLObject *self, PyObject *args);
413
414#define SSLObject_Check(v) ((v)->ob_type == &SSL_Type)
415
416#endif /* USE_SSL */
Guido van Rossum48a680c2001-03-02 06:34:14 +0000417
Guido van Rossum30a685f1991-06-27 15:51:29 +0000418/* A forward reference to the Socktype type object.
419 The Socktype variable contains pointers to various functions,
Guido van Rossum73624e91994-10-10 17:59:00 +0000420 some of which call newsockobject(), which uses Socktype, so
Guido van Rossum54ba21b1991-09-10 14:57:12 +0000421 there has to be a circular reference. */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000422
Guido van Rossum73624e91994-10-10 17:59:00 +0000423staticforward PyTypeObject PySocketSock_Type;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000424
Guido van Rossum30a685f1991-06-27 15:51:29 +0000425
426/* Create a new socket object.
427 This just creates the object and initializes it.
428 If the creation fails, return NULL and set an exception (implicit
429 in NEWOBJ()). */
430
Guido van Rossum73624e91994-10-10 17:59:00 +0000431static PySocketSockObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000432PySocketSock_New(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000433{
Guido van Rossum48a680c2001-03-02 06:34:14 +0000434#ifdef RISCOS
435 int block = 1;
436#endif
Guido van Rossum73624e91994-10-10 17:59:00 +0000437 PySocketSockObject *s;
Guido van Rossum55558541996-05-23 22:54:50 +0000438 PySocketSock_Type.ob_type = &PyType_Type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000439 s = PyObject_New(PySocketSockObject, &PySocketSock_Type);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000440 if (s != NULL) {
441 s->sock_fd = fd;
442 s->sock_family = family;
443 s->sock_type = type;
444 s->sock_proto = proto;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000445#ifdef RISCOS
446 if(taskwindow) {
447 socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
448 }
449#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000450 }
451 return s;
452}
453
Guido van Rossum30a685f1991-06-27 15:51:29 +0000454
Guido van Rossum48a680c2001-03-02 06:34:14 +0000455/* Lock to allow python interpreter to continue, but only allow one
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000456 thread to be in gethostbyname */
Guido van Rossum3baaa131999-03-22 21:44:51 +0000457#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum65d5b571998-12-21 19:32:43 +0000458PyThread_type_lock gethostbyname_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000459#endif
460
461
Guido van Rossum30a685f1991-06-27 15:51:29 +0000462/* Convert a string specifying a host name or one of a few symbolic
463 names to a numeric IP address. This usually calls gethostbyname()
464 to do the work; the names "" and "<broadcast>" are special.
465 Return the length (should always be 4 bytes), or negative if
466 an error occurred; then an exception is raised. */
467
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000468static int
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000469setipaddr(char* name, struct sockaddr_in * addr_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000470{
471 struct hostent *hp;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000472 int d1, d2, d3, d4;
Guido van Rossum955becc1999-03-22 20:14:53 +0000473 int h_length;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000474 char ch;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000475#ifdef HAVE_GETHOSTBYNAME_R
476 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +0000477#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
478 struct hostent_data data;
479#else
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000480 char buf[1001];
481 int buf_len = (sizeof buf) - 1;
482 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +0000483#endif
484#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +0000485 int result;
486#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000487#endif /* HAVE_GETHOSTBYNAME_R */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000488
Guido van Rossuma376cc51996-12-05 23:43:35 +0000489 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
Guido van Rossum30a685f1991-06-27 15:51:29 +0000490 if (name[0] == '\0') {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000491 addr_ret->sin_addr.s_addr = INADDR_ANY;
492 return 4;
493 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000494 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000495 addr_ret->sin_addr.s_addr = INADDR_BROADCAST;
496 return 4;
497 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000498 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
499 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
500 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
501 addr_ret->sin_addr.s_addr = htonl(
502 ((long) d1 << 24) | ((long) d2 << 16) |
503 ((long) d3 << 8) | ((long) d4 << 0));
504 return 4;
505 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000506 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000507#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +0000508#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +0000509 result = gethostbyname_r(name, &hp_allocated, buf, buf_len, &hp, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +0000510#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000511 hp = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +0000512#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +0000513 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +0000514 result = gethostbyname_r(name, &hp_allocated, &data);
515 hp = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +0000516#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000517#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +0000518#ifdef USE_GETHOSTBYNAME_LOCK
519 PyThread_acquire_lock(gethostbyname_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000520#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000521 hp = gethostbyname(name);
522#endif /* HAVE_GETHOSTBYNAME_R */
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000523 Py_END_ALLOW_THREADS
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000524
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000525 if (hp == NULL) {
Guido van Rossumca9b3231995-01-10 16:30:54 +0000526#ifdef HAVE_HSTRERROR
Guido van Rossum48a680c2001-03-02 06:34:14 +0000527 /* Let's get real error message to return */
528 extern int h_errno;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000529 PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno));
530#else
Guido van Rossum73624e91994-10-10 17:59:00 +0000531 PyErr_SetString(PySocket_Error, "host not found");
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000532#endif
Guido van Rossum3baaa131999-03-22 21:44:51 +0000533#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum955becc1999-03-22 20:14:53 +0000534 PyThread_release_lock(gethostbyname_lock);
535#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000536 return -1;
537 }
538 memcpy((char *) &addr_ret->sin_addr, hp->h_addr, hp->h_length);
Guido van Rossum955becc1999-03-22 20:14:53 +0000539 h_length = hp->h_length;
Guido van Rossum3baaa131999-03-22 21:44:51 +0000540#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum955becc1999-03-22 20:14:53 +0000541 PyThread_release_lock(gethostbyname_lock);
542#endif
543 return h_length;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000544}
545
Guido van Rossum30a685f1991-06-27 15:51:29 +0000546
Guido van Rossum30a685f1991-06-27 15:51:29 +0000547/* Create a string object representing an IP address.
548 This is always a string of the form 'dd.dd.dd.dd' (with variable
549 size numbers). */
550
Guido van Rossum73624e91994-10-10 17:59:00 +0000551static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000552makeipaddr(struct sockaddr_in *addr)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000553{
554 long x = ntohl(addr->sin_addr.s_addr);
555 char buf[100];
556 sprintf(buf, "%d.%d.%d.%d",
557 (int) (x>>24) & 0xff, (int) (x>>16) & 0xff,
558 (int) (x>> 8) & 0xff, (int) (x>> 0) & 0xff);
Guido van Rossum73624e91994-10-10 17:59:00 +0000559 return PyString_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000560}
561
562
563/* Create an object representing the given socket address,
564 suitable for passing it back to bind(), connect() etc.
565 The family field of the sockaddr structure is inspected
566 to determine what kind of address it really is. */
567
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000568/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000569static PyObject *
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000570makesockaddr(int sockfd, struct sockaddr *addr, int addrlen)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000571{
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000572 if (addrlen == 0) {
573 /* No address -- may be recvfrom() from known socket */
Guido van Rossum73624e91994-10-10 17:59:00 +0000574 Py_INCREF(Py_None);
575 return Py_None;
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000576 }
577
Guido van Rossumbcc20741998-08-04 22:53:56 +0000578#ifdef __BEOS__
Guido van Rossum2c8bcb82000-04-25 21:34:53 +0000579 /* XXX: BeOS version of accept() doesn't set family correctly */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000580 addr->sa_family = AF_INET;
581#endif
582
Guido van Rossum30a685f1991-06-27 15:51:29 +0000583 switch (addr->sa_family) {
584
585 case AF_INET:
586 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000587 struct sockaddr_in *a = (struct sockaddr_in *) addr;
Barry Warsaw752300b1997-01-03 17:18:10 +0000588 PyObject *addrobj = makeipaddr(a);
589 PyObject *ret = NULL;
590 if (addrobj) {
591 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
592 Py_DECREF(addrobj);
593 }
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000594 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000595 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000596
Guido van Rossumb6775db1994-08-01 11:34:53 +0000597#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000598 case AF_UNIX:
599 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000600 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000601 return PyString_FromString(a->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000602 }
Jeremy Hylton22308652001-02-02 03:23:09 +0000603#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +0000604
Jeremy Hylton22308652001-02-02 03:23:09 +0000605#if defined(linux) && defined(AF_PACKET)
606 case AF_PACKET:
607 {
608 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
609 char *ifname = "";
610 struct ifreq ifr;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000611 /* need to look up interface name give index */
612 if (a->sll_ifindex) {
Jeremy Hylton22308652001-02-02 03:23:09 +0000613 ifr.ifr_ifindex = a->sll_ifindex;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000614 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
Jeremy Hylton22308652001-02-02 03:23:09 +0000615 ifname = ifr.ifr_name;
Jeremy Hylton22308652001-02-02 03:23:09 +0000616 }
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000617 return Py_BuildValue("shbhs#", ifname, ntohs(a->sll_protocol),
Guido van Rossum48a680c2001-03-02 06:34:14 +0000618 a->sll_pkttype, a->sll_hatype,
619 a->sll_addr, a->sll_halen);
Jeremy Hylton22308652001-02-02 03:23:09 +0000620 }
621#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000622
Guido van Rossum30a685f1991-06-27 15:51:29 +0000623 /* More cases here... */
624
625 default:
Guido van Rossumaa948df1997-05-07 17:41:48 +0000626 /* If we don't know the address family, don't raise an
627 exception -- return it as a tuple. */
628 return Py_BuildValue("is#",
629 addr->sa_family,
630 addr->sa_data,
631 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000632
Guido van Rossum30a685f1991-06-27 15:51:29 +0000633 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000634}
635
Guido van Rossum30a685f1991-06-27 15:51:29 +0000636
637/* Parse a socket address argument according to the socket object's
638 address family. Return 1 if the address was in the proper format,
639 0 of not. The address is returned through addr_ret, its length
640 through len_ret. */
641
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000642static int
Guido van Rossum48a680c2001-03-02 06:34:14 +0000643getsockaddrarg(PySocketSockObject *s, PyObject *args,
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000644 struct sockaddr **addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000645{
Guido van Rossum30a685f1991-06-27 15:51:29 +0000646 switch (s->sock_family) {
647
Guido van Rossumb6775db1994-08-01 11:34:53 +0000648#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000649 case AF_UNIX:
650 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000651 struct sockaddr_un* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000652 char *path;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000653 int len;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000654 addr = (struct sockaddr_un* )&(s->sock_addr).un;
Guido van Rossum7e488981998-10-08 02:25:24 +0000655 if (!PyArg_Parse(args, "t#", &path, &len))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000656 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000657 if (len > sizeof addr->sun_path) {
Barry Warsaw752300b1997-01-03 17:18:10 +0000658 PyErr_SetString(PySocket_Error,
659 "AF_UNIX path too long");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000660 return 0;
661 }
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000662 addr->sun_family = AF_UNIX;
663 memcpy(addr->sun_path, path, len);
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000664 addr->sun_path[len] = 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000665 *addr_ret = (struct sockaddr *) addr;
Guido van Rossum65af28a1996-06-11 18:36:33 +0000666 *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000667 return 1;
668 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000669#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000670
Guido van Rossum30a685f1991-06-27 15:51:29 +0000671 case AF_INET:
672 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000673 struct sockaddr_in* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000674 char *host;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000675 int port;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000676 addr=(struct sockaddr_in*)&(s->sock_addr).in;
Guido van Rossume4dad902000-12-01 13:13:11 +0000677 if (!PyTuple_Check(args)) {
678 PyErr_Format(PyExc_TypeError,
679 "getsockaddrarg: AF_INET address must be tuple, not %.500s",
680 args->ob_type->tp_name);
681 return 0;
682 }
683 if (!PyArg_ParseTuple(args, "si:getsockaddrarg", &host, &port))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000684 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000685 if (setipaddr(host, addr) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000686 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000687 addr->sin_family = AF_INET;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000688 addr->sin_port = htons((short)port);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000689 *addr_ret = (struct sockaddr *) addr;
690 *len_ret = sizeof *addr;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000691 return 1;
692 }
693
Jeremy Hylton22308652001-02-02 03:23:09 +0000694#if defined(linux) && defined(AF_PACKET)
695 case AF_PACKET:
696 {
697 struct sockaddr_ll* addr;
698 struct ifreq ifr;
699 char *interfaceName;
700 int protoNumber;
701 int hatype = 0;
702 int pkttype = 0;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000703 char *haddr;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000704
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000705 if (!PyArg_ParseTuple(args, "si|iis", &interfaceName,
706 &protoNumber, &pkttype, &hatype, &haddr))
Jeremy Hylton22308652001-02-02 03:23:09 +0000707 return 0;
708 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
709 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000710 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
711 PyErr_SetFromErrno(PySocket_Error);
Jeremy Hylton22308652001-02-02 03:23:09 +0000712 return 0;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000713 }
Jeremy Hylton22308652001-02-02 03:23:09 +0000714 addr = &(s->sock_addr.ll);
715 addr->sll_family = AF_PACKET;
716 addr->sll_protocol = htons((short)protoNumber);
717 addr->sll_ifindex = ifr.ifr_ifindex;
718 addr->sll_pkttype = pkttype;
719 addr->sll_hatype = hatype;
720 *addr_ret = (struct sockaddr *) addr;
721 *len_ret = sizeof *addr;
722 return 1;
723 }
Guido van Rossum48a680c2001-03-02 06:34:14 +0000724#endif
725
726
Guido van Rossum30a685f1991-06-27 15:51:29 +0000727 /* More cases here... */
728
729 default:
Guido van Rossum73624e91994-10-10 17:59:00 +0000730 PyErr_SetString(PySocket_Error, "getsockaddrarg: bad family");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000731 return 0;
732
733 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000734}
735
Guido van Rossum30a685f1991-06-27 15:51:29 +0000736
Guido van Rossum48a680c2001-03-02 06:34:14 +0000737/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +0000738 Return 1 if the family is known, 0 otherwise. The length is returned
739 through len_ret. */
740
741static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +0000742getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +0000743{
744 switch (s->sock_family) {
745
Guido van Rossumb6775db1994-08-01 11:34:53 +0000746#ifdef AF_UNIX
Guido van Rossum710e1df1992-06-12 10:39:36 +0000747 case AF_UNIX:
748 {
749 *len_ret = sizeof (struct sockaddr_un);
750 return 1;
751 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000752#endif /* AF_UNIX */
Guido van Rossum710e1df1992-06-12 10:39:36 +0000753
754 case AF_INET:
755 {
756 *len_ret = sizeof (struct sockaddr_in);
757 return 1;
758 }
759
Jeremy Hylton22308652001-02-02 03:23:09 +0000760#if defined(linux) && defined(AF_PACKET)
761 case AF_PACKET:
762 {
763 *len_ret = sizeof (struct sockaddr_ll);
764 return 1;
765 }
766#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000767
Guido van Rossum710e1df1992-06-12 10:39:36 +0000768 /* More cases here... */
769
770 default:
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000771 PyErr_SetString(PySocket_Error, "getsockaddrlen: bad family");
Guido van Rossum710e1df1992-06-12 10:39:36 +0000772 return 0;
773
774 }
775}
776
777
Guido van Rossum30a685f1991-06-27 15:51:29 +0000778/* s.accept() method */
779
Guido van Rossum73624e91994-10-10 17:59:00 +0000780static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000781PySocketSock_accept(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000782{
783 char addrbuf[256];
Fred Drakea04eaad2000-06-30 02:46:07 +0000784 SOCKET_T newfd;
Guido van Rossumff3ab422000-04-24 15:16:03 +0000785 socklen_t addrlen;
Barry Warsaw752300b1997-01-03 17:18:10 +0000786 PyObject *sock = NULL;
787 PyObject *addr = NULL;
788 PyObject *res = NULL;
789
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000790 if (!PyArg_ParseTuple(args, ":accept"))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000791 return NULL;
Guido van Rossum710e1df1992-06-12 10:39:36 +0000792 if (!getsockaddrlen(s, &addrlen))
793 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000794 Py_BEGIN_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000795 newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000796 Py_END_ALLOW_THREADS
Fred Drakea04eaad2000-06-30 02:46:07 +0000797#ifdef MS_WINDOWS
798 if (newfd == INVALID_SOCKET)
799#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000800 if (newfd < 0)
Fred Drakea04eaad2000-06-30 02:46:07 +0000801#endif
Guido van Rossum73624e91994-10-10 17:59:00 +0000802 return PySocket_Err();
Barry Warsaw752300b1997-01-03 17:18:10 +0000803
Guido van Rossum30a685f1991-06-27 15:51:29 +0000804 /* Create the new object with unspecified family,
805 to avoid calls to bind() etc. on it. */
Guido van Rossum73624e91994-10-10 17:59:00 +0000806 sock = (PyObject *) PySocketSock_New(newfd,
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000807 s->sock_family,
808 s->sock_type,
809 s->sock_proto);
Barry Warsaw752300b1997-01-03 17:18:10 +0000810 if (sock == NULL) {
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000811 SOCKETCLOSE(newfd);
Barry Warsaw752300b1997-01-03 17:18:10 +0000812 goto finally;
813 }
Guido van Rossum48a680c2001-03-02 06:34:14 +0000814 addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf,
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000815 addrlen);
816 if (addr == NULL)
Barry Warsaw752300b1997-01-03 17:18:10 +0000817 goto finally;
818
Jeremy Hyltondbfb6622001-02-02 19:55:17 +0000819 res = Py_BuildValue("OO", sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +0000820
821 finally:
Guido van Rossum73624e91994-10-10 17:59:00 +0000822 Py_XDECREF(sock);
823 Py_XDECREF(addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000824 return res;
825}
826
Guido van Rossum82a5c661998-07-07 20:45:43 +0000827static char accept_doc[] =
828"accept() -> (socket object, address info)\n\
829\n\
830Wait for an incoming connection. Return a new socket representing the\n\
831connection, and the address of the client. For IP sockets, the address\n\
832info is a pair (hostaddr, port).";
833
Guido van Rossum30a685f1991-06-27 15:51:29 +0000834
Guido van Rossume4485b01994-09-07 14:32:49 +0000835/* s.setblocking(1 | 0) method */
836
Guido van Rossum73624e91994-10-10 17:59:00 +0000837static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000838PySocketSock_setblocking(PySocketSockObject *s, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000839{
840 int block;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000841#ifndef RISCOS
Guido van Rossum644a12b1997-04-09 19:24:53 +0000842#ifndef MS_WINDOWS
Guido van Rossume4485b01994-09-07 14:32:49 +0000843 int delay_flag;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000844#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000845#endif
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000846 if (!PyArg_ParseTuple(args, "i:setblocking", &block))
Guido van Rossume4485b01994-09-07 14:32:49 +0000847 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000848 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000849#ifdef __BEOS__
850 block = !block;
851 setsockopt( s->sock_fd, SOL_SOCKET, SO_NONBLOCK,
852 (void *)(&block), sizeof( int ) );
853#else
Guido van Rossum48a680c2001-03-02 06:34:14 +0000854#ifndef RISCOS
Guido van Rossum6f489d91996-06-28 20:15:15 +0000855#ifndef MS_WINDOWS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000856#ifdef PYOS_OS2
857 block = !block;
858 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
859#else /* !PYOS_OS2 */
Guido van Rossume4485b01994-09-07 14:32:49 +0000860 delay_flag = fcntl (s->sock_fd, F_GETFL, 0);
861 if (block)
862 delay_flag &= (~O_NDELAY);
863 else
864 delay_flag |= O_NDELAY;
865 fcntl (s->sock_fd, F_SETFL, delay_flag);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000866#endif /* !PYOS_OS2 */
867#else /* MS_WINDOWS */
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000868 block = !block;
869 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000870#endif /* MS_WINDOWS */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000871#endif /* __BEOS__ */
Guido van Rossum48a680c2001-03-02 06:34:14 +0000872#endif /* RISCOS */
Guido van Rossum73624e91994-10-10 17:59:00 +0000873 Py_END_ALLOW_THREADS
Guido van Rossume4485b01994-09-07 14:32:49 +0000874
Guido van Rossum73624e91994-10-10 17:59:00 +0000875 Py_INCREF(Py_None);
876 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000877}
Guido van Rossume4485b01994-09-07 14:32:49 +0000878
Guido van Rossum82a5c661998-07-07 20:45:43 +0000879static char setblocking_doc[] =
880"setblocking(flag)\n\
881\n\
882Set the socket to blocking (flag is true) or non-blocking (false).\n\
883This uses the FIONBIO ioctl with the O_NDELAY flag.";
884
Guido van Rossume4485b01994-09-07 14:32:49 +0000885
Guido van Rossum48a680c2001-03-02 06:34:14 +0000886#ifdef RISCOS
887/* s.sleeptaskw(1 | 0) method */
888
889static PyObject *
890PySocketSock_sleeptaskw(PySocketSockObject *s,PyObject *args)
891{
892 int block;
893 int delay_flag;
894 if (!PyArg_GetInt(args, &block))
895 return NULL;
896 Py_BEGIN_ALLOW_THREADS
897 socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
898 Py_END_ALLOW_THREADS
899
900 Py_INCREF(Py_None);
901 return Py_None;
902}
903static char sleeptaskw_doc[] =
904"sleeptaskw(flag)\n\
905\n\
906Allow sleeps in taskwindows.";
907#endif
908
909
Guido van Rossumaee08791992-09-08 09:05:33 +0000910/* s.setsockopt() method.
911 With an integer third argument, sets an integer option.
912 With a string third argument, sets an option from a buffer;
913 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000914
Guido van Rossum73624e91994-10-10 17:59:00 +0000915static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000916PySocketSock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000917{
918 int level;
919 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000920 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +0000921 char *buf;
922 int buflen;
923 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000924
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000925 if (PyArg_ParseTuple(args, "iii:setsockopt",
926 &level, &optname, &flag)) {
Guido van Rossumaee08791992-09-08 09:05:33 +0000927 buf = (char *) &flag;
928 buflen = sizeof flag;
929 }
930 else {
Guido van Rossum73624e91994-10-10 17:59:00 +0000931 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000932 if (!PyArg_ParseTuple(args, "iis#:setsockopt",
933 &level, &optname, &buf, &buflen))
Guido van Rossumaee08791992-09-08 09:05:33 +0000934 return NULL;
935 }
Thomas Wouters334fb892000-07-25 12:56:38 +0000936 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000937 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000938 return PySocket_Err();
939 Py_INCREF(Py_None);
940 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000941}
942
Guido van Rossum82a5c661998-07-07 20:45:43 +0000943static char setsockopt_doc[] =
944"setsockopt(level, option, value)\n\
945\n\
946Set a socket option. See the Unix manual for level and option.\n\
947The value argument can either be an integer or a string.";
948
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000949
Guido van Rossumaee08791992-09-08 09:05:33 +0000950/* s.getsockopt() method.
951 With two arguments, retrieves an integer option.
952 With a third integer argument, retrieves a string buffer of that size;
953 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000954
Guido van Rossum73624e91994-10-10 17:59:00 +0000955static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000956PySocketSock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000957{
958 int level;
959 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000960 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +0000961 PyObject *buf;
Guido van Rossumff3ab422000-04-24 15:16:03 +0000962 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000963
Guido van Rossumbcc20741998-08-04 22:53:56 +0000964#ifdef __BEOS__
965/* We have incomplete socket support. */
966 PyErr_SetString( PySocket_Error, "getsockopt not supported" );
967 return NULL;
968#else
969
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000970 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
971 &level, &optname, &buflen))
Guido van Rossumbe32c891996-06-20 16:25:29 +0000972 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000973
Guido van Rossumbe32c891996-06-20 16:25:29 +0000974 if (buflen == 0) {
Guido van Rossumaee08791992-09-08 09:05:33 +0000975 int flag = 0;
Guido van Rossumff3ab422000-04-24 15:16:03 +0000976 socklen_t flagsize = sizeof flag;
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000977 res = getsockopt(s->sock_fd, level, optname,
Thomas Wouters334fb892000-07-25 12:56:38 +0000978 (void *)&flag, &flagsize);
Guido van Rossumaee08791992-09-08 09:05:33 +0000979 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000980 return PySocket_Err();
981 return PyInt_FromLong(flag);
Guido van Rossumaee08791992-09-08 09:05:33 +0000982 }
Guido van Rossumaee08791992-09-08 09:05:33 +0000983 if (buflen <= 0 || buflen > 1024) {
Barry Warsaw752300b1997-01-03 17:18:10 +0000984 PyErr_SetString(PySocket_Error,
985 "getsockopt buflen out of range");
Guido van Rossumaee08791992-09-08 09:05:33 +0000986 return NULL;
987 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000988 buf = PyString_FromStringAndSize((char *)NULL, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000989 if (buf == NULL)
990 return NULL;
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000991 res = getsockopt(s->sock_fd, level, optname,
Thomas Wouters334fb892000-07-25 12:56:38 +0000992 (void *)PyString_AsString(buf), &buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000993 if (res < 0) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000994 Py_DECREF(buf);
995 return PySocket_Err();
Guido van Rossumaee08791992-09-08 09:05:33 +0000996 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000997 _PyString_Resize(&buf, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000998 return buf;
Guido van Rossumbcc20741998-08-04 22:53:56 +0000999#endif /* __BEOS__ */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001000}
1001
Guido van Rossum82a5c661998-07-07 20:45:43 +00001002static char getsockopt_doc[] =
1003"getsockopt(level, option[, buffersize]) -> value\n\
1004\n\
1005Get a socket option. See the Unix manual for level and option.\n\
1006If a nonzero buffersize argument is given, the return value is a\n\
1007string of that length; otherwise it is an integer.";
1008
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001009
Fred Drake728819a2000-07-01 03:40:12 +00001010/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001011
Guido van Rossum73624e91994-10-10 17:59:00 +00001012static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001013PySocketSock_bind(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001014{
1015 struct sockaddr *addr;
1016 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001017 int res;
Fred Drake728819a2000-07-01 03:40:12 +00001018 PyObject *addro;
1019 if (!PyArg_ParseTuple(args, "O:bind", &addro))
1020 return NULL;
1021 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001022 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001023 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001024 res = bind(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001025 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001026 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001027 return PySocket_Err();
1028 Py_INCREF(Py_None);
1029 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001030}
1031
Guido van Rossum82a5c661998-07-07 20:45:43 +00001032static char bind_doc[] =
1033"bind(address)\n\
1034\n\
1035Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00001036pair (host, port); the host must refer to the local host. For raw packet\n\
1037sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])";
Guido van Rossum82a5c661998-07-07 20:45:43 +00001038
Guido van Rossum30a685f1991-06-27 15:51:29 +00001039
1040/* s.close() method.
1041 Set the file descriptor to -1 so operations tried subsequently
1042 will surely fail. */
1043
Guido van Rossum73624e91994-10-10 17:59:00 +00001044static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001045PySocketSock_close(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001046{
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001047 SOCKET_T fd;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001048 if (!PyArg_ParseTuple(args, ":close"))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001049 return NULL;
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001050 if ((fd = s->sock_fd) != -1) {
1051 s->sock_fd = -1;
Guido van Rossum08481461996-10-12 14:07:22 +00001052 Py_BEGIN_ALLOW_THREADS
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001053 (void) SOCKETCLOSE(fd);
Guido van Rossum08481461996-10-12 14:07:22 +00001054 Py_END_ALLOW_THREADS
1055 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001056 Py_INCREF(Py_None);
1057 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001058}
1059
Guido van Rossum82a5c661998-07-07 20:45:43 +00001060static char close_doc[] =
1061"close()\n\
1062\n\
1063Close the socket. It cannot be used after this call.";
1064
Guido van Rossum30a685f1991-06-27 15:51:29 +00001065
Fred Drake728819a2000-07-01 03:40:12 +00001066/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001067
Guido van Rossum73624e91994-10-10 17:59:00 +00001068static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001069PySocketSock_connect(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001070{
1071 struct sockaddr *addr;
1072 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001073 int res;
Fred Drake728819a2000-07-01 03:40:12 +00001074 PyObject *addro;
1075 if (!PyArg_ParseTuple(args, "O:connect", &addro))
1076 return NULL;
1077 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001078 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001079 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001080 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001081 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001082 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001083 return PySocket_Err();
1084 Py_INCREF(Py_None);
1085 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001086}
1087
Guido van Rossum82a5c661998-07-07 20:45:43 +00001088static char connect_doc[] =
1089"connect(address)\n\
1090\n\
1091Connect the socket to a remote address. For IP sockets, the address\n\
1092is a pair (host, port).";
1093
Guido van Rossum30a685f1991-06-27 15:51:29 +00001094
Fred Drake728819a2000-07-01 03:40:12 +00001095/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001096
1097static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001098PySocketSock_connect_ex(PySocketSockObject *s, PyObject *args)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001099{
1100 struct sockaddr *addr;
1101 int addrlen;
1102 int res;
Fred Drake728819a2000-07-01 03:40:12 +00001103 PyObject *addro;
1104 if (!PyArg_ParseTuple(args, "O:connect_ex", &addro))
1105 return NULL;
1106 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001107 return NULL;
1108 Py_BEGIN_ALLOW_THREADS
1109 res = connect(s->sock_fd, addr, addrlen);
1110 Py_END_ALLOW_THREADS
1111 if (res != 0)
1112 res = errno;
1113 return PyInt_FromLong((long) res);
1114}
1115
Guido van Rossum82a5c661998-07-07 20:45:43 +00001116static char connect_ex_doc[] =
1117"connect_ex(address)\n\
1118\n\
1119This is like connect(address), but returns an error code (the errno value)\n\
1120instead of raising an exception when an error occurs.";
1121
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001122
Guido van Rossumed233a51992-06-23 09:07:03 +00001123/* s.fileno() method */
1124
Guido van Rossum73624e91994-10-10 17:59:00 +00001125static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001126PySocketSock_fileno(PySocketSockObject *s, PyObject *args)
Guido van Rossumed233a51992-06-23 09:07:03 +00001127{
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001128 if (!PyArg_ParseTuple(args, ":fileno"))
Guido van Rossumed233a51992-06-23 09:07:03 +00001129 return NULL;
Fred Drakea04eaad2000-06-30 02:46:07 +00001130#if SIZEOF_SOCKET_T <= SIZEOF_LONG
Guido van Rossum73624e91994-10-10 17:59:00 +00001131 return PyInt_FromLong((long) s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +00001132#else
1133 return PyLong_FromLongLong((LONG_LONG)s->sock_fd);
1134#endif
Guido van Rossumed233a51992-06-23 09:07:03 +00001135}
1136
Guido van Rossum82a5c661998-07-07 20:45:43 +00001137static char fileno_doc[] =
1138"fileno() -> integer\n\
1139\n\
1140Return the integer file descriptor of the socket.";
1141
Guido van Rossumed233a51992-06-23 09:07:03 +00001142
Guido van Rossumbe32c891996-06-20 16:25:29 +00001143#ifndef NO_DUP
1144/* s.dup() method */
1145
1146static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001147PySocketSock_dup(PySocketSockObject *s, PyObject *args)
Guido van Rossumbe32c891996-06-20 16:25:29 +00001148{
Fred Drakea04eaad2000-06-30 02:46:07 +00001149 SOCKET_T newfd;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001150 PyObject *sock;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001151 if (!PyArg_ParseTuple(args, ":dup"))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001152 return NULL;
1153 newfd = dup(s->sock_fd);
1154 if (newfd < 0)
1155 return PySocket_Err();
1156 sock = (PyObject *) PySocketSock_New(newfd,
Barry Warsaw752300b1997-01-03 17:18:10 +00001157 s->sock_family,
1158 s->sock_type,
1159 s->sock_proto);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001160 if (sock == NULL)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001161 SOCKETCLOSE(newfd);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001162 return sock;
1163}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001164
1165static char dup_doc[] =
1166"dup() -> socket object\n\
1167\n\
1168Return a new socket object connected to the same system resource.";
1169
Guido van Rossumbe32c891996-06-20 16:25:29 +00001170#endif
1171
1172
Guido van Rossumc89705d1992-11-26 08:54:07 +00001173/* s.getsockname() method */
1174
Guido van Rossum73624e91994-10-10 17:59:00 +00001175static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001176PySocketSock_getsockname(PySocketSockObject *s, PyObject *args)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001177{
1178 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001179 int res;
1180 socklen_t addrlen;
1181
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001182 if (!PyArg_ParseTuple(args, ":getsockname"))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001183 return NULL;
1184 if (!getsockaddrlen(s, &addrlen))
1185 return NULL;
Guido van Rossumca42b161996-01-12 01:36:05 +00001186 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001187 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001188 res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001189 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001190 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001191 return PySocket_Err();
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001192 return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001193}
1194
Guido van Rossum82a5c661998-07-07 20:45:43 +00001195static char getsockname_doc[] =
1196"getsockname() -> address info\n\
1197\n\
1198Return the address of the local endpoint. For IP sockets, the address\n\
1199info is a pair (hostaddr, port).";
1200
Guido van Rossumc89705d1992-11-26 08:54:07 +00001201
Guido van Rossumb6775db1994-08-01 11:34:53 +00001202#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001203/* s.getpeername() method */
1204
Guido van Rossum73624e91994-10-10 17:59:00 +00001205static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001206PySocketSock_getpeername(PySocketSockObject *s, PyObject *args)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001207{
1208 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001209 int res;
1210 socklen_t addrlen;
1211
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001212 if (!PyArg_ParseTuple(args, ":getpeername"))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001213 return NULL;
1214 if (!getsockaddrlen(s, &addrlen))
1215 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001216 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001217 res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001218 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001219 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001220 return PySocket_Err();
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001221 return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001222}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001223
1224static char getpeername_doc[] =
1225"getpeername() -> address info\n\
1226\n\
1227Return the address of the remote endpoint. For IP sockets, the address\n\
1228info is a pair (hostaddr, port).";
1229
Guido van Rossumb6775db1994-08-01 11:34:53 +00001230#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001231
1232
Guido van Rossum30a685f1991-06-27 15:51:29 +00001233/* s.listen(n) method */
1234
Guido van Rossum73624e91994-10-10 17:59:00 +00001235static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001236PySocketSock_listen(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001237{
1238 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001239 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001240 if (!PyArg_ParseTuple(args, "i:listen", &backlog))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001241 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001242 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +00001243 if (backlog < 1)
1244 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001245 res = listen(s->sock_fd, backlog);
Guido van Rossum73624e91994-10-10 17:59:00 +00001246 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001247 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001248 return PySocket_Err();
1249 Py_INCREF(Py_None);
1250 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001251}
1252
Guido van Rossum82a5c661998-07-07 20:45:43 +00001253static char listen_doc[] =
1254"listen(backlog)\n\
1255\n\
1256Enable a server to accept connections. The backlog argument must be at\n\
1257least 1; it specifies the number of unaccepted connection that the system\n\
1258will allow before refusing new connections.";
1259
1260
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001261#ifndef NO_DUP
Guido van Rossum30a685f1991-06-27 15:51:29 +00001262/* s.makefile(mode) method.
1263 Create a new open file object referring to a dupped version of
1264 the socket's file descriptor. (The dup() call is necessary so
1265 that the open file and socket objects may be closed independent
1266 of each other.)
1267 The mode argument specifies 'r' or 'w' passed to fdopen(). */
1268
Guido van Rossum73624e91994-10-10 17:59:00 +00001269static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001270PySocketSock_makefile(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001271{
Tim Petersdbd9ba62000-07-09 03:09:57 +00001272 extern int fclose(FILE *);
Guido van Rossum6b144911995-03-14 15:05:13 +00001273 char *mode = "r";
1274 int bufsize = -1;
Fred Drakea04eaad2000-06-30 02:46:07 +00001275#ifdef MS_WIN32
1276 intptr_t fd;
1277#else
Guido van Rossum30a685f1991-06-27 15:51:29 +00001278 int fd;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001279#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001280 FILE *fp;
Guido van Rossum6b144911995-03-14 15:05:13 +00001281 PyObject *f;
1282
Guido van Rossum43713e52000-02-29 13:59:29 +00001283 if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001284 return NULL;
Guido van Rossum8d665e61996-06-26 18:22:49 +00001285#ifdef MS_WIN32
Guido van Rossum82a5c661998-07-07 20:45:43 +00001286 if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) ||
1287 ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL))
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001288#else
Guido van Rossum82a5c661998-07-07 20:45:43 +00001289 if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL)
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001290#endif
Guido van Rossum82a5c661998-07-07 20:45:43 +00001291 {
Guido van Rossum6b144911995-03-14 15:05:13 +00001292 if (fd >= 0)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001293 SOCKETCLOSE(fd);
Guido van Rossum73624e91994-10-10 17:59:00 +00001294 return PySocket_Err();
Guido van Rossum6b144911995-03-14 15:05:13 +00001295 }
1296 f = PyFile_FromFile(fp, "<socket>", mode, fclose);
1297 if (f != NULL)
1298 PyFile_SetBufSize(f, bufsize);
1299 return f;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001300}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001301
1302static char makefile_doc[] =
1303"makefile([mode[, buffersize]]) -> file object\n\
1304\n\
1305Return a regular file object corresponding to the socket.\n\
1306The mode and buffersize arguments are as for the built-in open() function.";
1307
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001308#endif /* NO_DUP */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001309
Guido van Rossum48a680c2001-03-02 06:34:14 +00001310
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001311/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001312
Guido van Rossum73624e91994-10-10 17:59:00 +00001313static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001314PySocketSock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001315{
Guido van Rossumbe32c891996-06-20 16:25:29 +00001316 int len, n, flags = 0;
Guido van Rossum73624e91994-10-10 17:59:00 +00001317 PyObject *buf;
Guido van Rossum43713e52000-02-29 13:59:29 +00001318 if (!PyArg_ParseTuple(args, "i|i:recv", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001319 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001320 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001321 if (buf == NULL)
1322 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001323 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma597dde1995-01-10 20:56:29 +00001324 n = recv(s->sock_fd, PyString_AsString(buf), len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001325 Py_END_ALLOW_THREADS
Guido van Rossum7c53b771995-09-13 18:39:47 +00001326 if (n < 0) {
1327 Py_DECREF(buf);
Guido van Rossum73624e91994-10-10 17:59:00 +00001328 return PySocket_Err();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001329 }
Guido van Rossum53a9bf81996-06-11 18:35:24 +00001330 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001331 return NULL;
1332 return buf;
1333}
1334
Guido van Rossum82a5c661998-07-07 20:45:43 +00001335static char recv_doc[] =
1336"recv(buffersize[, flags]) -> data\n\
1337\n\
1338Receive up to buffersize bytes from the socket. For the optional flags\n\
1339argument, see the Unix manual. When no data is available, block until\n\
1340at least one byte is available or until the remote end is closed. When\n\
1341the remote end is closed and all data is read, return the empty string.";
1342
Guido van Rossum30a685f1991-06-27 15:51:29 +00001343
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001344/* s.recvfrom(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001345
Guido van Rossum73624e91994-10-10 17:59:00 +00001346static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001347PySocketSock_recvfrom(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001348{
1349 char addrbuf[256];
Barry Warsaw752300b1997-01-03 17:18:10 +00001350 PyObject *buf = NULL;
1351 PyObject *addr = NULL;
1352 PyObject *ret = NULL;
1353
Guido van Rossumff3ab422000-04-24 15:16:03 +00001354 int len, n, flags = 0;
1355 socklen_t addrlen;
Guido van Rossum43713e52000-02-29 13:59:29 +00001356 if (!PyArg_ParseTuple(args, "i|i:recvfrom", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001357 return NULL;
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00001358 if (!getsockaddrlen(s, &addrlen))
1359 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001360 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00001361 if (buf == NULL)
1362 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001363 Py_BEGIN_ALLOW_THREADS
1364 n = recvfrom(s->sock_fd, PyString_AsString(buf), len, flags,
Guido van Rossum8d665e61996-06-26 18:22:49 +00001365#ifndef MS_WINDOWS
Guido van Rossum32c575d1997-12-02 20:37:32 +00001366#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001367 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00001368#else
Thomas Wouters334fb892000-07-25 12:56:38 +00001369 (void *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00001370#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001371#else
Barry Warsaw752300b1997-01-03 17:18:10 +00001372 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001373#endif
Barry Warsaw752300b1997-01-03 17:18:10 +00001374 );
Guido van Rossum73624e91994-10-10 17:59:00 +00001375 Py_END_ALLOW_THREADS
Guido van Rossum7c53b771995-09-13 18:39:47 +00001376 if (n < 0) {
1377 Py_DECREF(buf);
Guido van Rossum73624e91994-10-10 17:59:00 +00001378 return PySocket_Err();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001379 }
Guido van Rossum53a9bf81996-06-11 18:35:24 +00001380 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001381 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001382
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001383 if (!(addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf, addrlen)))
Barry Warsaw752300b1997-01-03 17:18:10 +00001384 goto finally;
1385
Guido van Rossum73624e91994-10-10 17:59:00 +00001386 ret = Py_BuildValue("OO", buf, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00001387 finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001388 Py_XDECREF(addr);
1389 Py_XDECREF(buf);
Guido van Rossum6f5afc91993-02-05 09:46:15 +00001390 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001391}
1392
Guido van Rossum82a5c661998-07-07 20:45:43 +00001393static char recvfrom_doc[] =
1394"recvfrom(buffersize[, flags]) -> (data, address info)\n\
1395\n\
1396Like recv(buffersize, flags) but also return the sender's address info.";
1397
Guido van Rossum30a685f1991-06-27 15:51:29 +00001398
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001399/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001400
Guido van Rossum73624e91994-10-10 17:59:00 +00001401static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001402PySocketSock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001403{
Guido van Rossumff4949e1992-08-05 19:58:53 +00001404 char *buf;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001405 int len, n, flags = 0;
Guido van Rossum43713e52000-02-29 13:59:29 +00001406 if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001407 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001408 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001409 n = send(s->sock_fd, buf, len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001410 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00001411 if (n < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001412 return PySocket_Err();
1413 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00001414}
1415
Guido van Rossum82a5c661998-07-07 20:45:43 +00001416static char send_doc[] =
1417"send(data[, flags])\n\
1418\n\
1419Send a data string to the socket. For the optional flags\n\
1420argument, see the Unix manual.";
1421
Guido van Rossum30a685f1991-06-27 15:51:29 +00001422
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001423/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001424
Guido van Rossum73624e91994-10-10 17:59:00 +00001425static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001426PySocketSock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001427{
Guido van Rossum73624e91994-10-10 17:59:00 +00001428 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001429 char *buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001430 struct sockaddr *addr;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001431 int addrlen, len, n, flags;
1432 flags = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001433 if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001434 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001435 if (!PyArg_ParseTuple(args, "s#iO:sendto",
1436 &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001437 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001438 }
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001439 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001440 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001441 Py_BEGIN_ALLOW_THREADS
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001442 n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001443 Py_END_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001444 if (n < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001445 return PySocket_Err();
1446 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001447}
1448
Guido van Rossum82a5c661998-07-07 20:45:43 +00001449static char sendto_doc[] =
1450"sendto(data[, flags], address)\n\
1451\n\
1452Like send(data, flags) but allows specifying the destination address.\n\
1453For IP sockets, the address is a pair (hostaddr, port).";
1454
Guido van Rossum30a685f1991-06-27 15:51:29 +00001455
1456/* s.shutdown(how) method */
1457
Guido van Rossum73624e91994-10-10 17:59:00 +00001458static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001459PySocketSock_shutdown(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001460{
1461 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001462 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001463 if (!PyArg_ParseTuple(args, "i:shutdown", &how))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001464 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001465 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001466 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +00001467 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001468 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001469 return PySocket_Err();
1470 Py_INCREF(Py_None);
1471 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001472}
1473
Guido van Rossum82a5c661998-07-07 20:45:43 +00001474static char shutdown_doc[] =
1475"shutdown(flag)\n\
1476\n\
1477Shut down the reading side of the socket (flag == 0), the writing side\n\
1478of the socket (flag == 1), or both ends (flag == 2).";
1479
Guido van Rossum30a685f1991-06-27 15:51:29 +00001480
1481/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001482
Guido van Rossum73624e91994-10-10 17:59:00 +00001483static PyMethodDef PySocketSock_methods[] = {
Guido van Rossum48a680c2001-03-02 06:34:14 +00001484 {"accept", (PyCFunction)PySocketSock_accept, METH_VARARGS,
1485 accept_doc},
1486 {"bind", (PyCFunction)PySocketSock_bind, METH_VARARGS,
1487 bind_doc},
1488 {"close", (PyCFunction)PySocketSock_close, METH_VARARGS,
1489 close_doc},
1490 {"connect", (PyCFunction)PySocketSock_connect, METH_VARARGS,
1491 connect_doc},
1492 {"connect_ex", (PyCFunction)PySocketSock_connect_ex, METH_VARARGS,
1493 connect_ex_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00001494#ifndef NO_DUP
Guido van Rossum48a680c2001-03-02 06:34:14 +00001495 {"dup", (PyCFunction)PySocketSock_dup, METH_VARARGS,
1496 dup_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00001497#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001498 {"fileno", (PyCFunction)PySocketSock_fileno, METH_VARARGS,
1499 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001500#ifdef HAVE_GETPEERNAME
Guido van Rossum48a680c2001-03-02 06:34:14 +00001501 {"getpeername", (PyCFunction)PySocketSock_getpeername, METH_VARARGS,
1502 getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00001503#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001504 {"getsockname", (PyCFunction)PySocketSock_getsockname, METH_VARARGS,
1505 getsockname_doc},
1506 {"getsockopt", (PyCFunction)PySocketSock_getsockopt, METH_VARARGS,
1507 getsockopt_doc},
1508 {"listen", (PyCFunction)PySocketSock_listen, METH_VARARGS,
1509 listen_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001510#ifndef NO_DUP
Guido van Rossum48a680c2001-03-02 06:34:14 +00001511 {"makefile", (PyCFunction)PySocketSock_makefile, METH_VARARGS,
1512 makefile_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001513#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001514 {"recv", (PyCFunction)PySocketSock_recv, METH_VARARGS,
1515 recv_doc},
1516 {"recvfrom", (PyCFunction)PySocketSock_recvfrom, METH_VARARGS,
1517 recvfrom_doc},
1518 {"send", (PyCFunction)PySocketSock_send, METH_VARARGS,
1519 send_doc},
1520 {"sendto", (PyCFunction)PySocketSock_sendto, METH_VARARGS,
1521 sendto_doc},
1522 {"setblocking", (PyCFunction)PySocketSock_setblocking, METH_VARARGS,
1523 setblocking_doc},
1524 {"setsockopt", (PyCFunction)PySocketSock_setsockopt, METH_VARARGS,
1525 setsockopt_doc},
1526 {"shutdown", (PyCFunction)PySocketSock_shutdown, METH_VARARGS,
1527 shutdown_doc},
1528#ifdef RISCOS
1529 {"sleeptaskw", (PyCFunction)PySocketSock_sleeptaskw, METH_VARARGS,
1530 sleeptaskw_doc},
1531#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00001532 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001533};
1534
Guido van Rossum30a685f1991-06-27 15:51:29 +00001535
Guido van Rossum73624e91994-10-10 17:59:00 +00001536/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00001537 First close the file description. */
1538
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001539static void
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001540PySocketSock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001541{
Guido van Rossumfa972c92000-04-10 12:45:45 +00001542 if (s->sock_fd != -1)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001543 (void) SOCKETCLOSE(s->sock_fd);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001544 PyObject_Del(s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001545}
1546
Guido van Rossum30a685f1991-06-27 15:51:29 +00001547
1548/* Return a socket object's named attribute. */
1549
Guido van Rossum73624e91994-10-10 17:59:00 +00001550static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001551PySocketSock_getattr(PySocketSockObject *s, char *name)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001552{
Guido van Rossum73624e91994-10-10 17:59:00 +00001553 return Py_FindMethod(PySocketSock_methods, (PyObject *) s, name);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001554}
1555
Guido van Rossum30a685f1991-06-27 15:51:29 +00001556
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001557static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001558PySocketSock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001559{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001560 char buf[512];
Fred Drakea04eaad2000-06-30 02:46:07 +00001561#if SIZEOF_SOCKET_T > SIZEOF_LONG
1562 if (s->sock_fd > LONG_MAX) {
1563 /* this can occur on Win64, and actually there is a special
1564 ugly printf formatter for decimal pointer length integer
1565 printing, only bother if necessary*/
1566 PyErr_SetString(PyExc_OverflowError,
1567 "no printf formatter to display the socket descriptor in decimal");
1568 return NULL;
1569 }
1570#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001571 sprintf(buf,
1572 "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>",
Fred Drakea04eaad2000-06-30 02:46:07 +00001573 (long)s->sock_fd, s->sock_family, s->sock_type, s->sock_proto);
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00001574 return PyString_FromString(buf);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001575}
1576
1577
Guido van Rossumb6775db1994-08-01 11:34:53 +00001578/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001579
Guido van Rossum73624e91994-10-10 17:59:00 +00001580static PyTypeObject PySocketSock_Type = {
Guido van Rossum55558541996-05-23 22:54:50 +00001581 PyObject_HEAD_INIT(0) /* Must fill in type value later */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001582 0,
1583 "socket",
Guido van Rossum73624e91994-10-10 17:59:00 +00001584 sizeof(PySocketSockObject),
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001585 0,
Guido van Rossum73624e91994-10-10 17:59:00 +00001586 (destructor)PySocketSock_dealloc, /*tp_dealloc*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001587 0, /*tp_print*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001588 (getattrfunc)PySocketSock_getattr, /*tp_getattr*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001589 0, /*tp_setattr*/
1590 0, /*tp_compare*/
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001591 (reprfunc)PySocketSock_repr, /*tp_repr*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001592 0, /*tp_as_number*/
1593 0, /*tp_as_sequence*/
1594 0, /*tp_as_mapping*/
1595};
1596
Guido van Rossum30a685f1991-06-27 15:51:29 +00001597
Guido van Rossum81194471991-07-27 21:42:02 +00001598/* Python interface to gethostname(). */
1599
1600/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001601static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001602PySocket_gethostname(PyObject *self, PyObject *args)
Guido van Rossum81194471991-07-27 21:42:02 +00001603{
1604 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001605 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001606 if (!PyArg_ParseTuple(args, ":gethostname"))
Guido van Rossum81194471991-07-27 21:42:02 +00001607 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001608 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001609 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00001610 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001611 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001612 return PySocket_Err();
Guido van Rossum81194471991-07-27 21:42:02 +00001613 buf[sizeof buf - 1] = '\0';
Guido van Rossum73624e91994-10-10 17:59:00 +00001614 return PyString_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00001615}
Guido van Rossumff4949e1992-08-05 19:58:53 +00001616
Guido van Rossum82a5c661998-07-07 20:45:43 +00001617static char gethostname_doc[] =
1618"gethostname() -> string\n\
1619\n\
1620Return the current host name.";
1621
Guido van Rossumff4949e1992-08-05 19:58:53 +00001622
Guido van Rossum30a685f1991-06-27 15:51:29 +00001623/* Python interface to gethostbyname(name). */
1624
1625/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001626static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001627PySocket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001628{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001629 char *name;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001630 struct sockaddr_in addrbuf;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001631 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00001632 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001633 if (setipaddr(name, &addrbuf) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001634 return NULL;
1635 return makeipaddr(&addrbuf);
1636}
1637
Guido van Rossum82a5c661998-07-07 20:45:43 +00001638static char gethostbyname_doc[] =
1639"gethostbyname(host) -> address\n\
1640\n\
1641Return the IP address (a string of the form '255.255.255.255') for a host.";
1642
1643
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001644/* Convenience function common to gethostbyname_ex and gethostbyaddr */
1645
1646static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001647gethost_common(struct hostent *h, struct sockaddr_in *addr)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001648{
1649 char **pch;
1650 PyObject *rtn_tuple = (PyObject *)NULL;
1651 PyObject *name_list = (PyObject *)NULL;
1652 PyObject *addr_list = (PyObject *)NULL;
1653 PyObject *tmp;
1654 if (h == NULL) {
1655#ifdef HAVE_HSTRERROR
Guido van Rossum48a680c2001-03-02 06:34:14 +00001656 /* Let's get real error message to return */
1657 extern int h_errno;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001658 PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno));
1659#else
1660 PyErr_SetString(PySocket_Error, "host not found");
1661#endif
1662 return NULL;
1663 }
1664 if ((name_list = PyList_New(0)) == NULL)
1665 goto err;
1666 if ((addr_list = PyList_New(0)) == NULL)
1667 goto err;
1668 for (pch = h->h_aliases; *pch != NULL; pch++) {
1669 int status;
1670 tmp = PyString_FromString(*pch);
1671 if (tmp == NULL)
1672 goto err;
1673 status = PyList_Append(name_list, tmp);
1674 Py_DECREF(tmp);
1675 if (status)
1676 goto err;
1677 }
1678 for (pch = h->h_addr_list; *pch != NULL; pch++) {
1679 int status;
1680 memcpy((char *) &addr->sin_addr, *pch, h->h_length);
1681 tmp = makeipaddr(addr);
1682 if (tmp == NULL)
1683 goto err;
1684 status = PyList_Append(addr_list, tmp);
1685 Py_DECREF(tmp);
1686 if (status)
1687 goto err;
1688 }
1689 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
1690 err:
1691 Py_XDECREF(name_list);
1692 Py_XDECREF(addr_list);
1693 return rtn_tuple;
1694}
1695
1696
1697/* Python interface to gethostbyname_ex(name). */
1698
1699/*ARGSUSED*/
1700static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001701PySocket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001702{
1703 char *name;
1704 struct hostent *h;
1705 struct sockaddr_in addr;
Guido van Rossum955becc1999-03-22 20:14:53 +00001706 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001707#ifdef HAVE_GETHOSTBYNAME_R
1708 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001709#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
1710 struct hostent_data data;
1711#else
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001712 char buf[16384];
1713 int buf_len = (sizeof buf) - 1;
1714 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001715#endif
1716#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001717 int result;
1718#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001719#endif /* HAVE_GETHOSTBYNAME_R */
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001720 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001721 return NULL;
1722 if (setipaddr(name, &addr) < 0)
1723 return NULL;
1724 Py_BEGIN_ALLOW_THREADS
1725#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001726#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001727 result = gethostbyname_r(name, &hp_allocated, buf, buf_len, &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001728#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001729 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001730#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00001731 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001732 result = gethostbyname_r(name, &hp_allocated, &data);
1733 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001734#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001735#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00001736#ifdef USE_GETHOSTBYNAME_LOCK
1737 PyThread_acquire_lock(gethostbyname_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001738#endif
1739 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001740#endif /* HAVE_GETHOSTBYNAME_R */
1741 Py_END_ALLOW_THREADS
Guido van Rossum3baaa131999-03-22 21:44:51 +00001742 ret = gethost_common(h, &addr);
1743#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum955becc1999-03-22 20:14:53 +00001744 PyThread_release_lock(gethostbyname_lock);
1745#endif
1746 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001747}
1748
1749static char ghbn_ex_doc[] =
1750"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
1751\n\
1752Return the true host name, a list of aliases, and a list of IP addresses,\n\
1753for a host. The host argument is a string giving a host name or IP number.";
1754
1755
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001756/* Python interface to gethostbyaddr(IP). */
1757
1758/*ARGSUSED*/
1759static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001760PySocket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001761{
Guido van Rossum48a680c2001-03-02 06:34:14 +00001762 struct sockaddr_in addr;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001763 char *ip_num;
1764 struct hostent *h;
Guido van Rossum3baaa131999-03-22 21:44:51 +00001765 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001766#ifdef HAVE_GETHOSTBYNAME_R
1767 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001768#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
1769 struct hostent_data data;
1770#else
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001771 char buf[16384];
1772 int buf_len = (sizeof buf) - 1;
1773 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001774#endif
1775#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001776 int result;
1777#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001778#endif /* HAVE_GETHOSTBYNAME_R */
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001779
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001780 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001781 return NULL;
1782 if (setipaddr(ip_num, &addr) < 0)
1783 return NULL;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001784 Py_BEGIN_ALLOW_THREADS
1785#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001786#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001787 result = gethostbyaddr_r((char *)&addr.sin_addr,
1788 sizeof(addr.sin_addr),
1789 AF_INET, &hp_allocated, buf, buf_len,
1790 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001791#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001792 h = gethostbyaddr_r((char *)&addr.sin_addr,
1793 sizeof(addr.sin_addr),
Guido van Rossum48a680c2001-03-02 06:34:14 +00001794 AF_INET,
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001795 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001796#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00001797 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001798 result = gethostbyaddr_r((char *)&addr.sin_addr,
1799 sizeof(addr.sin_addr),
1800 AF_INET, &hp_allocated, &data);
1801 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001802#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001803#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00001804#ifdef USE_GETHOSTBYNAME_LOCK
1805 PyThread_acquire_lock(gethostbyname_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001806#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001807 h = gethostbyaddr((char *)&addr.sin_addr,
1808 sizeof(addr.sin_addr),
1809 AF_INET);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001810#endif /* HAVE_GETHOSTBYNAME_R */
1811 Py_END_ALLOW_THREADS
Guido van Rossum3baaa131999-03-22 21:44:51 +00001812 ret = gethost_common(h, &addr);
1813#ifdef USE_GETHOSTBYNAME_LOCK
1814 PyThread_release_lock(gethostbyname_lock);
1815#endif
1816 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001817}
1818
Guido van Rossum82a5c661998-07-07 20:45:43 +00001819static char gethostbyaddr_doc[] =
1820"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
1821\n\
1822Return the true host name, a list of aliases, and a list of IP addresses,\n\
1823for a host. The host argument is a string giving a host name or IP number.";
1824
Guido van Rossum30a685f1991-06-27 15:51:29 +00001825
1826/* Python interface to getservbyname(name).
1827 This only returns the port number, since the other info is already
1828 known or not useful (like the list of aliases). */
1829
1830/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001831static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001832PySocket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001833{
Guido van Rossumff4949e1992-08-05 19:58:53 +00001834 char *name, *proto;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001835 struct servent *sp;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001836 if (!PyArg_ParseTuple(args, "ss:getservbyname", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00001837 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001838 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001839 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00001840 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00001841 if (sp == NULL) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001842 PyErr_SetString(PySocket_Error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00001843 return NULL;
1844 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001845 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00001846}
1847
Guido van Rossum82a5c661998-07-07 20:45:43 +00001848static char getservbyname_doc[] =
1849"getservbyname(servicename, protocolname) -> integer\n\
1850\n\
1851Return a port number from a service name and protocol name.\n\
1852The protocol name should be 'tcp' or 'udp'.";
1853
Guido van Rossum30a685f1991-06-27 15:51:29 +00001854
Guido van Rossum3901d851996-12-19 16:35:04 +00001855/* Python interface to getprotobyname(name).
1856 This only returns the protocol number, since the other info is
1857 already known or not useful (like the list of aliases). */
1858
1859/*ARGSUSED*/
1860static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001861PySocket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00001862{
1863 char *name;
1864 struct protoent *sp;
Guido van Rossumbcc20741998-08-04 22:53:56 +00001865#ifdef __BEOS__
1866/* Not available in BeOS yet. - [cjh] */
1867 PyErr_SetString( PySocket_Error, "getprotobyname not supported" );
1868 return NULL;
1869#else
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001870 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
Guido van Rossum3901d851996-12-19 16:35:04 +00001871 return NULL;
1872 Py_BEGIN_ALLOW_THREADS
1873 sp = getprotobyname(name);
1874 Py_END_ALLOW_THREADS
1875 if (sp == NULL) {
1876 PyErr_SetString(PySocket_Error, "protocol not found");
1877 return NULL;
1878 }
1879 return PyInt_FromLong((long) sp->p_proto);
Guido van Rossumbcc20741998-08-04 22:53:56 +00001880#endif
Guido van Rossum3901d851996-12-19 16:35:04 +00001881}
1882
Guido van Rossum82a5c661998-07-07 20:45:43 +00001883static char getprotobyname_doc[] =
1884"getprotobyname(name) -> integer\n\
1885\n\
1886Return the protocol number for the named protocol. (Rarely used.)";
1887
Guido van Rossum3901d851996-12-19 16:35:04 +00001888
Guido van Rossum30a685f1991-06-27 15:51:29 +00001889/* Python interface to socket(family, type, proto).
1890 The third (protocol) argument is optional.
1891 Return a new socket object. */
1892
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001893/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001894static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001895PySocket_socket(PyObject *self, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001896{
Guido van Rossum73624e91994-10-10 17:59:00 +00001897 PySocketSockObject *s;
Fred Drakea04eaad2000-06-30 02:46:07 +00001898 SOCKET_T fd;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001899 int family, type, proto = 0;
Guido van Rossum43713e52000-02-29 13:59:29 +00001900 if (!PyArg_ParseTuple(args, "ii|i:socket", &family, &type, &proto))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001901 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001902 Py_BEGIN_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001903 fd = socket(family, type, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00001904 Py_END_ALLOW_THREADS
Guido van Rossum6f489d91996-06-28 20:15:15 +00001905#ifdef MS_WINDOWS
Guido van Rossumd639d4d1996-06-12 04:04:22 +00001906 if (fd == INVALID_SOCKET)
1907#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001908 if (fd < 0)
Guido van Rossumd639d4d1996-06-12 04:04:22 +00001909#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001910 return PySocket_Err();
1911 s = PySocketSock_New(fd, family, type, proto);
Guido van Rossum30a685f1991-06-27 15:51:29 +00001912 /* If the object can't be created, don't forget to close the
1913 file descriptor again! */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001914 if (s == NULL)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001915 (void) SOCKETCLOSE(fd);
Guido van Rossum81194471991-07-27 21:42:02 +00001916 /* From now on, ignore SIGPIPE and let the error checking
1917 do the work. */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001918#ifdef SIGPIPE
Guido van Rossum81194471991-07-27 21:42:02 +00001919 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum48a680c2001-03-02 06:34:14 +00001920#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001921 return (PyObject *) s;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001922}
1923
Guido van Rossum82a5c661998-07-07 20:45:43 +00001924static char socket_doc[] =
1925"socket(family, type[, proto]) -> socket object\n\
1926\n\
1927Open a socket of the given type. The family argument specifies the\n\
1928address family; it is normally AF_INET, sometimes AF_UNIX.\n\
1929The type argument specifies whether this is a stream (SOCK_STREAM)\n\
1930or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\
1931specifying the default protocol.";
1932
1933
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001934#ifndef NO_DUP
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001935/* Create a socket object from a numeric file description.
1936 Useful e.g. if stdin is a socket.
1937 Additional arguments as for socket(). */
1938
1939/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001940static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001941PySocket_fromfd(PyObject *self, PyObject *args)
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001942{
Guido van Rossum73624e91994-10-10 17:59:00 +00001943 PySocketSockObject *s;
Fred Drakea04eaad2000-06-30 02:46:07 +00001944 SOCKET_T fd;
1945 int family, type, proto = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001946 if (!PyArg_ParseTuple(args, "iii|i:fromfd",
1947 &fd, &family, &type, &proto))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001948 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +00001949 /* Dup the fd so it and the socket can be closed independently */
1950 fd = dup(fd);
1951 if (fd < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001952 return PySocket_Err();
1953 s = PySocketSock_New(fd, family, type, proto);
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001954 /* From now on, ignore SIGPIPE and let the error checking
1955 do the work. */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001956#ifdef SIGPIPE
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001957 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum48a680c2001-03-02 06:34:14 +00001958#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001959 return (PyObject *) s;
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001960}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001961
1962static char fromfd_doc[] =
1963"fromfd(fd, family, type[, proto]) -> socket object\n\
1964\n\
1965Create a socket object from the given file descriptor.\n\
1966The remaining arguments are the same as for socket().";
1967
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001968#endif /* NO_DUP */
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001969
Guido van Rossum82a5c661998-07-07 20:45:43 +00001970
Guido van Rossum006bf911996-06-12 04:04:55 +00001971static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001972PySocket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00001973{
1974 int x1, x2;
1975
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001976 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00001977 return NULL;
1978 }
1979 x2 = (int)ntohs((short)x1);
1980 return PyInt_FromLong(x2);
1981}
1982
Guido van Rossum82a5c661998-07-07 20:45:43 +00001983static char ntohs_doc[] =
1984"ntohs(integer) -> integer\n\
1985\n\
1986Convert a 16-bit integer from network to host byte order.";
1987
1988
Guido van Rossum006bf911996-06-12 04:04:55 +00001989static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001990PySocket_ntohl(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00001991{
1992 int x1, x2;
1993
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001994 if (!PyArg_ParseTuple(args, "i:ntohl", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00001995 return NULL;
1996 }
1997 x2 = ntohl(x1);
1998 return PyInt_FromLong(x2);
1999}
2000
Guido van Rossum82a5c661998-07-07 20:45:43 +00002001static char ntohl_doc[] =
2002"ntohl(integer) -> integer\n\
2003\n\
2004Convert a 32-bit integer from network to host byte order.";
2005
2006
Guido van Rossum006bf911996-06-12 04:04:55 +00002007static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002008PySocket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00002009{
2010 int x1, x2;
2011
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002012 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00002013 return NULL;
2014 }
2015 x2 = (int)htons((short)x1);
2016 return PyInt_FromLong(x2);
2017}
2018
Guido van Rossum82a5c661998-07-07 20:45:43 +00002019static char htons_doc[] =
2020"htons(integer) -> integer\n\
2021\n\
2022Convert a 16-bit integer from host to network byte order.";
2023
2024
Guido van Rossum006bf911996-06-12 04:04:55 +00002025static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002026PySocket_htonl(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00002027{
2028 int x1, x2;
2029
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002030 if (!PyArg_ParseTuple(args, "i:htonl", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00002031 return NULL;
2032 }
2033 x2 = htonl(x1);
2034 return PyInt_FromLong(x2);
2035}
2036
Guido van Rossum82a5c661998-07-07 20:45:43 +00002037static char htonl_doc[] =
2038"htonl(integer) -> integer\n\
2039\n\
2040Convert a 32-bit integer from host to network byte order.";
2041
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002042/*
2043 * socket.inet_aton() and socket.inet_ntoa() functions
2044 *
2045 * written 20 Aug 1999 by Ben Gertzfield <che@debian.org> <- blame him!
2046 *
2047 */
2048
Guido van Rossum48a680c2001-03-02 06:34:14 +00002049static char inet_aton_doc[] =
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002050"inet_aton(string) -> packed 32-bit IP representation\n\
2051\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002052Convert an IP address in string format (123.45.67.89) to the 32-bit packed\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002053binary format used in low-level network functions.";
2054
2055static PyObject*
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002056PySocket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002057{
Guido van Rossuma2e48551999-09-09 15:42:59 +00002058#ifndef INADDR_NONE
2059#define INADDR_NONE (-1)
2060#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002061
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002062 /* Have to use inet_addr() instead */
2063 char *ip_addr;
2064 long packed_addr;
2065
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002066 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) {
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002067 return NULL;
2068 }
Guido van Rossumff3ab422000-04-24 15:16:03 +00002069#ifdef USE_GUSI1
Guido van Rossum1b6e4631999-11-04 18:22:29 +00002070 packed_addr = (long)inet_addr(ip_addr).s_addr;
2071#else
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002072 packed_addr = inet_addr(ip_addr);
Guido van Rossum1b6e4631999-11-04 18:22:29 +00002073#endif
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002074
2075 if (packed_addr == INADDR_NONE) { /* invalid address */
2076 PyErr_SetString(PySocket_Error,
2077 "illegal IP address string passed to inet_aton");
2078 return NULL;
2079 }
2080
2081 return PyString_FromStringAndSize((char *) &packed_addr,
2082 sizeof(packed_addr));
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002083}
2084
Guido van Rossum48a680c2001-03-02 06:34:14 +00002085static char inet_ntoa_doc[] =
Fred Drakee0661342000-03-07 14:05:16 +00002086"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002087\n\
2088Convert an IP address from 32-bit packed binary format to string format";
2089
2090static PyObject*
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002091PySocket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002092{
2093 char *packed_str;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00002094 int addr_len;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002095 struct in_addr packed_addr;
2096
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002097 if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) {
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002098 return NULL;
2099 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00002100
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002101 if (addr_len != sizeof(packed_addr)) {
2102 PyErr_SetString(PySocket_Error,
2103 "packed IP wrong length for inet_ntoa");
2104 return NULL;
2105 }
2106
2107 memcpy(&packed_addr, packed_str, addr_len);
2108
2109 return PyString_FromString(inet_ntoa(packed_addr));
2110}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002111
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002112
2113#ifdef USE_SSL
2114
2115/* This is a C function to be called for new object initialization */
2116static SSLObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002117newSSLObject(PySocketSockObject *Sock, char *key_file, char *cert_file)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002118{
2119 SSLObject *self;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002120
Guido van Rossumb18618d2000-05-03 23:44:39 +00002121 self = PyObject_New(SSLObject, &SSL_Type); /* Create new object */
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002122 if (self == NULL){
2123 PyErr_SetObject(SSLErrorObject,
2124 PyString_FromString("newSSLObject error"));
2125 return NULL;
2126 }
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00002127 memset(self->server, '\0', sizeof(char) * 256);
Guido van Rossum48a680c2001-03-02 06:34:14 +00002128 memset(self->issuer, '\0', sizeof(char) * 256);
2129
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002130 self->x_attr = PyDict_New();
2131 self->ctx = SSL_CTX_new(SSLv23_method()); /* Set up context */
2132 if (self->ctx == NULL) {
2133 PyErr_SetObject(SSLErrorObject,
2134 PyString_FromString("SSL_CTX_new error"));
Guido van Rossumb18618d2000-05-03 23:44:39 +00002135 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002136 return NULL;
2137 }
2138
2139 if ( (key_file && !cert_file) || (!key_file && cert_file) )
2140 {
2141 PyErr_SetObject(SSLErrorObject,
2142 PyString_FromString(
2143 "Both the key & certificate files must be specified"));
Guido van Rossumb18618d2000-05-03 23:44:39 +00002144 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002145 return NULL;
2146 }
2147
2148 if (key_file && cert_file)
2149 {
2150 if (SSL_CTX_use_PrivateKey_file(self->ctx, key_file,
2151 SSL_FILETYPE_PEM) < 1)
2152 {
2153 PyErr_SetObject(SSLErrorObject,
2154 PyString_FromString(
2155 "SSL_CTX_use_PrivateKey_file error"));
Guido van Rossumb18618d2000-05-03 23:44:39 +00002156 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002157 return NULL;
2158 }
2159
2160 if (SSL_CTX_use_certificate_chain_file(self->ctx,
2161 cert_file) < 1)
2162 {
2163 PyErr_SetObject(SSLErrorObject,
2164 PyString_FromString(
2165 "SSL_CTX_use_certificate_chain_file error"));
Guido van Rossumb18618d2000-05-03 23:44:39 +00002166 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002167 return NULL;
2168 }
2169 }
2170
2171 SSL_CTX_set_verify(self->ctx,
2172 SSL_VERIFY_NONE, NULL); /* set verify lvl */
2173 self->ssl = SSL_new(self->ctx); /* New ssl struct */
2174 SSL_set_fd(self->ssl, Sock->sock_fd); /* Set the socket for SSL */
2175 SSL_set_connect_state(self->ssl);
2176
2177 if ((SSL_connect(self->ssl)) == -1) {
2178 /* Actually negotiate SSL connection */
2179 PyErr_SetObject(SSLErrorObject,
2180 PyString_FromString("SSL_connect error"));
Guido van Rossumb18618d2000-05-03 23:44:39 +00002181 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002182 return NULL;
2183 }
2184 self->ssl->debug = 1;
2185
2186 if ((self->server_cert = SSL_get_peer_certificate(self->ssl))) {
2187 X509_NAME_oneline(X509_get_subject_name(self->server_cert),
2188 self->server, 256);
2189 X509_NAME_oneline(X509_get_issuer_name(self->server_cert),
2190 self->issuer, 256);
2191 }
2192 self->x_attr = NULL;
2193 self->Socket = Sock;
2194 Py_INCREF(self->Socket);
2195 return self;
2196}
2197
2198/* This is the Python function called for new object initialization */
2199static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002200PySocket_ssl(PyObject *self, PyObject *args)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002201{
2202 SSLObject *rv;
2203 PySocketSockObject *Sock;
2204 char *key_file;
2205 char *cert_file;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002206
Guido van Rossum43713e52000-02-29 13:59:29 +00002207 if (!PyArg_ParseTuple(args, "O!zz:ssl",
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002208 &PySocketSock_Type, (PyObject*)&Sock,
2209 &key_file, &cert_file) )
2210 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002211
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002212 rv = newSSLObject(Sock, key_file, cert_file);
2213 if ( rv == NULL )
2214 return NULL;
2215 return (PyObject *)rv;
2216}
2217
2218static char ssl_doc[] =
2219"ssl(socket, keyfile, certfile) -> sslobject";
2220
2221static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002222SSL_server(SSLObject *self, PyObject *args)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002223{
2224 return PyString_FromString(self->server);
2225}
2226
2227static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002228SSL_issuer(SSLObject *self, PyObject *args)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002229{
2230 return PyString_FromString(self->issuer);
2231}
2232
2233
2234/* SSL object methods */
2235
2236static PyMethodDef SSLMethods[] = {
2237 { "write", (PyCFunction)SSL_SSLwrite, 1 },
2238 { "read", (PyCFunction)SSL_SSLread, 1 },
2239 { "server", (PyCFunction)SSL_server, 1 },
2240 { "issuer", (PyCFunction)SSL_issuer, 1 },
2241 { NULL, NULL}
2242};
2243
2244static void SSL_dealloc(SSLObject *self)
2245{
2246 if (self->server_cert) /* Possible not to have one? */
2247 X509_free (self->server_cert);
2248 SSL_CTX_free(self->ctx);
2249 SSL_free(self->ssl);
2250 Py_XDECREF(self->x_attr);
2251 Py_XDECREF(self->Socket);
Guido van Rossumb18618d2000-05-03 23:44:39 +00002252 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002253}
2254
2255static PyObject *SSL_getattr(SSLObject *self, char *name)
2256{
2257 return Py_FindMethod(SSLMethods, (PyObject *)self, name);
2258}
2259
2260staticforward PyTypeObject SSL_Type = {
Guido van Rossuma120ffc2001-01-22 15:29:14 +00002261 PyObject_HEAD_INIT(NULL)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002262 0, /*ob_size*/
2263 "SSL", /*tp_name*/
2264 sizeof(SSLObject), /*tp_basicsize*/
2265 0, /*tp_itemsize*/
2266 /* methods */
2267 (destructor)SSL_dealloc, /*tp_dealloc*/
2268 0, /*tp_print*/
2269 (getattrfunc)SSL_getattr, /*tp_getattr*/
2270 0, /*tp_setattr*/
2271 0, /*tp_compare*/
2272 0, /*tp_repr*/
2273 0, /*tp_as_number*/
2274 0, /*tp_as_sequence*/
2275 0, /*tp_as_mapping*/
2276 0, /*tp_hash*/
2277};
2278
2279
2280
2281static PyObject *SSL_SSLwrite(SSLObject *self, PyObject *args)
2282{
2283 char *data;
Andrew M. Kuchlingb38175e2001-02-07 20:41:17 +00002284 size_t len;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002285
Andrew M. Kuchlingb38175e2001-02-07 20:41:17 +00002286 if (!PyArg_ParseTuple(args, "s#:write", &data, &len))
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002287 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002288
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002289 len = SSL_write(self->ssl, data, len);
2290 return PyInt_FromLong((long)len);
2291}
2292
2293static PyObject *SSL_SSLread(SSLObject *self, PyObject *args)
2294{
2295 PyObject *buf;
2296 int count = 0;
2297 int len = 1024;
2298 int res;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002299
Guido van Rossum43713e52000-02-29 13:59:29 +00002300 PyArg_ParseTuple(args, "|i:read", &len);
Guido van Rossum48a680c2001-03-02 06:34:14 +00002301
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002302 if (!(buf = PyString_FromStringAndSize((char *) 0, len)))
2303 return NULL; /* Error object should already be set */
Guido van Rossum48a680c2001-03-02 06:34:14 +00002304
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002305 count = SSL_read(self->ssl, PyString_AsString(buf), len);
2306 res = SSL_get_error(self->ssl, count);
2307
2308 switch (res) {
Jeremy Hylton42dd01a2001-02-01 23:35:20 +00002309 case SSL_ERROR_NONE:
2310 assert(count > 0);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002311 break;
Jeremy Hylton42dd01a2001-02-01 23:35:20 +00002312 case SSL_ERROR_ZERO_RETURN: /* normal EOF */
2313 assert(count == 0);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002314 break;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002315 default:
2316 return PyErr_SetFromErrno(SSLErrorObject);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002317 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00002318
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002319 fflush(stderr);
Guido van Rossum48a680c2001-03-02 06:34:14 +00002320
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002321 if (count < 0) {
2322 Py_DECREF(buf);
2323 return PyErr_SetFromErrno(SSLErrorObject);
2324 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00002325
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002326 if (count != len && _PyString_Resize(&buf, count) < 0)
2327 return NULL;
2328 return buf;
2329}
2330
2331#endif /* USE_SSL */
2332
2333
Guido van Rossum30a685f1991-06-27 15:51:29 +00002334/* List of functions exported by this module. */
2335
Guido van Rossum73624e91994-10-10 17:59:00 +00002336static PyMethodDef PySocket_methods[] = {
Guido van Rossum48a680c2001-03-02 06:34:14 +00002337 {"gethostbyname", PySocket_gethostbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002338 METH_VARARGS, gethostbyname_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002339 {"gethostbyname_ex", PySocket_gethostbyname_ex,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002340 METH_VARARGS, ghbn_ex_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002341 {"gethostbyaddr", PySocket_gethostbyaddr,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002342 METH_VARARGS, gethostbyaddr_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002343 {"gethostname", PySocket_gethostname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002344 METH_VARARGS, gethostname_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002345 {"getservbyname", PySocket_getservbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002346 METH_VARARGS, getservbyname_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002347 {"getprotobyname", PySocket_getprotobyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002348 METH_VARARGS,getprotobyname_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002349 {"socket", PySocket_socket,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002350 METH_VARARGS, socket_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002351#ifndef NO_DUP
Guido van Rossum48a680c2001-03-02 06:34:14 +00002352 {"fromfd", PySocket_fromfd,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002353 METH_VARARGS, fromfd_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002354#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00002355 {"ntohs", PySocket_ntohs,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002356 METH_VARARGS, ntohs_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002357 {"ntohl", PySocket_ntohl,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002358 METH_VARARGS, ntohl_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002359 {"htons", PySocket_htons,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002360 METH_VARARGS, htons_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002361 {"htonl", PySocket_htonl,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002362 METH_VARARGS, htonl_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002363 {"inet_aton", PySocket_inet_aton,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002364 METH_VARARGS, inet_aton_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002365 {"inet_ntoa", PySocket_inet_ntoa,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002366 METH_VARARGS, inet_ntoa_doc},
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002367#ifdef USE_SSL
Guido van Rossum48a680c2001-03-02 06:34:14 +00002368 {"ssl", PySocket_ssl,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00002369 METH_VARARGS, ssl_doc},
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002370#endif /* USE_SSL */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002371 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002372};
2373
Guido van Rossum30a685f1991-06-27 15:51:29 +00002374
2375/* Convenience routine to export an integer value.
Barry Warsaw752300b1997-01-03 17:18:10 +00002376 *
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002377 * Errors are silently ignored, for better or for worse...
Barry Warsaw752300b1997-01-03 17:18:10 +00002378 */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002379static void
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002380insint(PyObject *d, char *name, int value)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002381{
Guido van Rossum73624e91994-10-10 17:59:00 +00002382 PyObject *v = PyInt_FromLong((long) value);
Barry Warsaw752300b1997-01-03 17:18:10 +00002383 if (!v || PyDict_SetItemString(d, name, v))
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002384 PyErr_Clear();
Barry Warsaw752300b1997-01-03 17:18:10 +00002385
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002386 Py_XDECREF(v);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002387}
2388
Guido van Rossum30a685f1991-06-27 15:51:29 +00002389
Guido van Rossum8d665e61996-06-26 18:22:49 +00002390#ifdef MS_WINDOWS
Guido van Rossumbe32c891996-06-20 16:25:29 +00002391
2392/* Additional initialization and cleanup for NT/Windows */
2393
2394static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00002395NTcleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00002396{
2397 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00002398}
2399
2400static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00002401NTinit(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00002402{
2403 WSADATA WSAData;
2404 int ret;
2405 char buf[100];
2406 ret = WSAStartup(0x0101, &WSAData);
2407 switch (ret) {
2408 case 0: /* no error */
2409 atexit(NTcleanup);
2410 return 1;
2411 case WSASYSNOTREADY:
2412 PyErr_SetString(PyExc_ImportError,
2413 "WSAStartup failed: network not ready");
2414 break;
2415 case WSAVERNOTSUPPORTED:
2416 case WSAEINVAL:
2417 PyErr_SetString(PyExc_ImportError,
2418 "WSAStartup failed: requested version not supported");
2419 break;
2420 default:
2421 sprintf(buf, "WSAStartup failed: error code %d", ret);
2422 PyErr_SetString(PyExc_ImportError, buf);
2423 break;
2424 }
2425 return 0;
2426}
2427
Guido van Rossum8d665e61996-06-26 18:22:49 +00002428#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00002429
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002430#if defined(PYOS_OS2)
2431
2432/* Additional initialization and cleanup for OS/2 */
2433
2434static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00002435OS2cleanup(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002436{
2437 /* No cleanup is necessary for OS/2 Sockets */
2438}
2439
2440static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00002441OS2init(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002442{
2443 char reason[64];
2444 int rc = sock_init();
2445
2446 if (rc == 0) {
Guido van Rossum32c575d1997-12-02 20:37:32 +00002447 atexit(OS2cleanup);
2448 return 1; /* Indicate Success */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002449 }
2450
2451 sprintf(reason, "OS/2 TCP/IP Error# %d", sock_errno());
2452 PyErr_SetString(PyExc_ImportError, reason);
2453
Guido van Rossum32c575d1997-12-02 20:37:32 +00002454 return 0; /* Indicate Failure */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002455}
2456
2457#endif /* PYOS_OS2 */
2458
Guido van Rossum30a685f1991-06-27 15:51:29 +00002459/* Initialize this module.
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002460 * This is called when the first 'import socket' is done,
2461 * via a table in config.c, if config.c is compiled with USE_SOCKET
2462 * defined.
2463 *
2464 * For MS_WINDOWS (which means any Windows variant), this module
2465 * is actually called "_socket", and there's a wrapper "socket.py"
2466 * which implements some missing functionality (such as makefile(),
2467 * dup() and fromfd()). The import of "_socket" may fail with an
2468 * ImportError exception if initialization of WINSOCK fails. When
2469 * WINSOCK is initialized succesfully, a call to WSACleanup() is
2470 * scheduled to be made at exit time.
2471 *
2472 * For OS/2, this module is also called "_socket" and uses a wrapper
2473 * "socket.py" which implements that functionality that is missing
2474 * when PC operating systems don't put socket descriptors in the
2475 * operating system's filesystem layer.
2476 */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002477
Guido van Rossum82a5c661998-07-07 20:45:43 +00002478static char module_doc[] =
Fred Drakea136d492000-08-16 14:18:30 +00002479"Implementation module for socket operations. See the socket module\n\
2480for documentation.";
Guido van Rossum82a5c661998-07-07 20:45:43 +00002481
2482static char sockettype_doc[] =
2483"A socket represents one endpoint of a network connection.\n\
2484\n\
2485Methods:\n\
2486\n\
2487accept() -- accept a connection, returning new socket and client address\n\
2488bind() -- bind the socket to a local address\n\
2489close() -- close the socket\n\
2490connect() -- connect the socket to a remote address\n\
2491connect_ex() -- connect, return an error code instead of an exception \n\
2492dup() -- return a new socket object identical to the current one (*)\n\
2493fileno() -- return underlying file descriptor\n\
2494getpeername() -- return remote address (*)\n\
2495getsockname() -- return local address\n\
2496getsockopt() -- get socket options\n\
2497listen() -- start listening for incoming connections\n\
2498makefile() -- return a file object corresponding tot the socket (*)\n\
2499recv() -- receive data\n\
2500recvfrom() -- receive data and sender's address\n\
2501send() -- send data\n\
2502sendto() -- send data to a given address\n\
2503setblocking() -- set or clear the blocking I/O flag\n\
2504setsockopt() -- set socket options\n\
2505shutdown() -- shut down traffic in one or both directions\n\
2506\n\
2507(*) not available on all platforms!)";
2508
Guido van Rossum3886bb61998-12-04 18:50:17 +00002509DL_EXPORT(void)
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00002510init_socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002511{
Guido van Rossum73624e91994-10-10 17:59:00 +00002512 PyObject *m, *d;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002513#ifdef RISCOS
2514 _kernel_swi_regs r;
2515 r.r[0]=0;
2516 _kernel_swi(0x43380, &r, &r);
2517 taskwindow = r.r[0];
2518#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00002519#ifdef MS_WINDOWS
Guido van Rossumbe32c891996-06-20 16:25:29 +00002520 if (!NTinit())
2521 return;
Guido van Rossumbe32c891996-06-20 16:25:29 +00002522#else
Guido van Rossum32c575d1997-12-02 20:37:32 +00002523#if defined(__TOS_OS2__)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002524 if (!OS2init())
2525 return;
Fred Drakea136d492000-08-16 14:18:30 +00002526#endif /* __TOS_OS2__ */
2527#endif /* MS_WINDOWS */
Guido van Rossum48a680c2001-03-02 06:34:14 +00002528#endif /* RISCOS */
Guido van Rossuma120ffc2001-01-22 15:29:14 +00002529#ifdef USE_SSL
2530 SSL_Type.ob_type = &PyType_Type;
2531#endif
Guido van Rossum82a5c661998-07-07 20:45:43 +00002532 m = Py_InitModule3("_socket", PySocket_methods, module_doc);
Guido van Rossum73624e91994-10-10 17:59:00 +00002533 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002534 PySocket_Error = PyErr_NewException("socket.error", NULL, NULL);
2535 if (PySocket_Error == NULL)
2536 return;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002537#ifdef USE_SSL
2538 SSL_load_error_strings();
2539 SSLeay_add_ssl_algorithms();
2540 SSLErrorObject = PyErr_NewException("socket.sslerror", NULL, NULL);
2541 if (SSLErrorObject == NULL)
2542 return;
2543 PyDict_SetItemString(d, "sslerror", SSLErrorObject);
2544 Py_INCREF(&SSL_Type);
2545 if (PyDict_SetItemString(d, "SSLType",
2546 (PyObject *)&SSL_Type) != 0)
2547 return;
Moshe Zadka8f4eab22001-03-18 17:11:56 +00002548 if (RAND_status() == 0) {
2549#ifdef USE_EGD
2550 char random_device[MAXPATHLEN+1];
2551 if (!RAND_file_name (random_device, MAXPATHLEN + 1)) {
2552 PyErr_SetObject(SSLErrorObject,
2553 PyString_FromString("RAND_file_name error"));
2554 return;
2555 }
2556 if (RAND_egd (random_device) == -1) {
2557 PyErr_SetObject(SSLErrorObject,
2558 PyString_FromString("RAND_egd error"));
2559 return;
2560 }
2561#else /* USE_EGD not defined */
2562 char random_string[32];
2563 int i;
2564
2565 PyErr_Warn(PyExc_RuntimeWarning,
2566 "using insecure method to generate random numbers");
2567 srand(time(NULL));
2568 for(i=0; i<sizeof(random_string); i++) {
2569 random_string[i] = rand();
2570 }
2571 RAND_seed(random_string, sizeof(random_string));
2572#endif /* USE_EGD */
2573 }
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002574#endif /* USE_SSL */
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002575 PyDict_SetItemString(d, "error", PySocket_Error);
Guido van Rossum3b4b6fc1997-05-21 14:37:37 +00002576 PySocketSock_Type.ob_type = &PyType_Type;
Guido van Rossum82a5c661998-07-07 20:45:43 +00002577 PySocketSock_Type.tp_doc = sockettype_doc;
Guido van Rossum3b4b6fc1997-05-21 14:37:37 +00002578 Py_INCREF(&PySocketSock_Type);
Guido van Rossum91ba64d1997-06-02 22:18:09 +00002579 if (PyDict_SetItemString(d, "SocketType",
Guido van Rossum3b4b6fc1997-05-21 14:37:37 +00002580 (PyObject *)&PySocketSock_Type) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002581 return;
Guido van Rossum09be4091999-08-09 14:40:40 +00002582
2583 /* Address families (we only support AF_INET and AF_UNIX) */
2584#ifdef AF_UNSPEC
2585 insint(d, "AF_UNSPEC", AF_UNSPEC);
2586#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002587 insint(d, "AF_INET", AF_INET);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002588#ifdef AF_UNIX
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002589 insint(d, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002590#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00002591#ifdef AF_AX25
2592 insint(d, "AF_AX25", AF_AX25); /* Amateur Radio AX.25 */
2593#endif
2594#ifdef AF_IPX
2595 insint(d, "AF_IPX", AF_IPX); /* Novell IPX */
2596#endif
2597#ifdef AF_APPLETALK
2598 insint(d, "AF_APPLETALK", AF_APPLETALK); /* Appletalk DDP */
2599#endif
2600#ifdef AF_NETROM
2601 insint(d, "AF_NETROM", AF_NETROM); /* Amateur radio NetROM */
2602#endif
2603#ifdef AF_BRIDGE
2604 insint(d, "AF_BRIDGE", AF_BRIDGE); /* Multiprotocol bridge */
2605#endif
2606#ifdef AF_AAL5
2607 insint(d, "AF_AAL5", AF_AAL5); /* Reserved for Werner's ATM */
2608#endif
2609#ifdef AF_X25
2610 insint(d, "AF_X25", AF_X25); /* Reserved for X.25 project */
2611#endif
2612#ifdef AF_INET6
2613 insint(d, "AF_INET6", AF_INET6); /* IP version 6 */
2614#endif
2615#ifdef AF_ROSE
2616 insint(d, "AF_ROSE", AF_ROSE); /* Amateur Radio X.25 PLP */
2617#endif
Jeremy Hylton22308652001-02-02 03:23:09 +00002618#if defined(linux) && defined(AF_PACKET)
2619 insint(d, "AF_PACKET", AF_PACKET);
2620 insint(d, "PF_PACKET", PF_PACKET);
2621 insint(d, "PACKET_HOST", PACKET_HOST);
2622 insint(d, "PACKET_BROADCAST", PACKET_BROADCAST);
2623 insint(d, "PACKET_MULTICAST", PACKET_MULTICAST);
2624 insint(d, "PACKET_OTHERHOST", PACKET_OTHERHOST);
2625 insint(d, "PACKET_OUTGOING", PACKET_OUTGOING);
2626 insint(d, "PACKET_LOOPBACK", PACKET_LOOPBACK);
2627 insint(d, "PACKET_FASTROUTE", PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00002628#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00002629
2630 /* Socket types */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002631 insint(d, "SOCK_STREAM", SOCK_STREAM);
2632 insint(d, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00002633#ifndef __BEOS__
2634/* We have incomplete socket support. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002635 insint(d, "SOCK_RAW", SOCK_RAW);
2636 insint(d, "SOCK_SEQPACKET", SOCK_SEQPACKET);
2637 insint(d, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00002638#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002639
2640#ifdef SO_DEBUG
2641 insint(d, "SO_DEBUG", SO_DEBUG);
2642#endif
2643#ifdef SO_ACCEPTCONN
2644 insint(d, "SO_ACCEPTCONN", SO_ACCEPTCONN);
2645#endif
2646#ifdef SO_REUSEADDR
2647 insint(d, "SO_REUSEADDR", SO_REUSEADDR);
2648#endif
2649#ifdef SO_KEEPALIVE
2650 insint(d, "SO_KEEPALIVE", SO_KEEPALIVE);
2651#endif
2652#ifdef SO_DONTROUTE
2653 insint(d, "SO_DONTROUTE", SO_DONTROUTE);
2654#endif
2655#ifdef SO_BROADCAST
2656 insint(d, "SO_BROADCAST", SO_BROADCAST);
2657#endif
2658#ifdef SO_USELOOPBACK
2659 insint(d, "SO_USELOOPBACK", SO_USELOOPBACK);
2660#endif
2661#ifdef SO_LINGER
2662 insint(d, "SO_LINGER", SO_LINGER);
2663#endif
2664#ifdef SO_OOBINLINE
2665 insint(d, "SO_OOBINLINE", SO_OOBINLINE);
2666#endif
2667#ifdef SO_REUSEPORT
2668 insint(d, "SO_REUSEPORT", SO_REUSEPORT);
2669#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002670#ifdef SO_SNDBUF
2671 insint(d, "SO_SNDBUF", SO_SNDBUF);
2672#endif
2673#ifdef SO_RCVBUF
2674 insint(d, "SO_RCVBUF", SO_RCVBUF);
2675#endif
2676#ifdef SO_SNDLOWAT
2677 insint(d, "SO_SNDLOWAT", SO_SNDLOWAT);
2678#endif
2679#ifdef SO_RCVLOWAT
2680 insint(d, "SO_RCVLOWAT", SO_RCVLOWAT);
2681#endif
2682#ifdef SO_SNDTIMEO
2683 insint(d, "SO_SNDTIMEO", SO_SNDTIMEO);
2684#endif
2685#ifdef SO_RCVTIMEO
2686 insint(d, "SO_RCVTIMEO", SO_RCVTIMEO);
2687#endif
2688#ifdef SO_ERROR
2689 insint(d, "SO_ERROR", SO_ERROR);
2690#endif
2691#ifdef SO_TYPE
2692 insint(d, "SO_TYPE", SO_TYPE);
2693#endif
2694
2695 /* Maximum number of connections for "listen" */
2696#ifdef SOMAXCONN
2697 insint(d, "SOMAXCONN", SOMAXCONN);
2698#else
2699 insint(d, "SOMAXCONN", 5); /* Common value */
2700#endif
2701
2702 /* Flags for send, recv */
2703#ifdef MSG_OOB
2704 insint(d, "MSG_OOB", MSG_OOB);
2705#endif
2706#ifdef MSG_PEEK
2707 insint(d, "MSG_PEEK", MSG_PEEK);
2708#endif
2709#ifdef MSG_DONTROUTE
2710 insint(d, "MSG_DONTROUTE", MSG_DONTROUTE);
2711#endif
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00002712#ifdef MSG_DONTWAIT
Guido van Rossum14f3f592000-04-25 21:53:58 +00002713 insint(d, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00002714#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002715#ifdef MSG_EOR
2716 insint(d, "MSG_EOR", MSG_EOR);
2717#endif
2718#ifdef MSG_TRUNC
2719 insint(d, "MSG_TRUNC", MSG_TRUNC);
2720#endif
2721#ifdef MSG_CTRUNC
2722 insint(d, "MSG_CTRUNC", MSG_CTRUNC);
2723#endif
2724#ifdef MSG_WAITALL
2725 insint(d, "MSG_WAITALL", MSG_WAITALL);
2726#endif
2727#ifdef MSG_BTAG
2728 insint(d, "MSG_BTAG", MSG_BTAG);
2729#endif
2730#ifdef MSG_ETAG
2731 insint(d, "MSG_ETAG", MSG_ETAG);
2732#endif
2733
2734 /* Protocol level and numbers, usable for [gs]etsockopt */
2735#ifdef SOL_SOCKET
2736 insint(d, "SOL_SOCKET", SOL_SOCKET);
2737#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00002738#ifdef SOL_IP
2739 insint(d, "SOL_IP", SOL_IP);
2740#else
2741 insint(d, "SOL_IP", 0);
2742#endif
2743#ifdef SOL_IPX
2744 insint(d, "SOL_IPX", SOL_IPX);
2745#endif
2746#ifdef SOL_AX25
2747 insint(d, "SOL_AX25", SOL_AX25);
2748#endif
2749#ifdef SOL_ATALK
2750 insint(d, "SOL_ATALK", SOL_ATALK);
2751#endif
2752#ifdef SOL_NETROM
2753 insint(d, "SOL_NETROM", SOL_NETROM);
2754#endif
2755#ifdef SOL_ROSE
2756 insint(d, "SOL_ROSE", SOL_ROSE);
2757#endif
2758#ifdef SOL_TCP
2759 insint(d, "SOL_TCP", SOL_TCP);
2760#else
2761 insint(d, "SOL_TCP", 6);
2762#endif
2763#ifdef SOL_UDP
2764 insint(d, "SOL_UDP", SOL_UDP);
2765#else
2766 insint(d, "SOL_UDP", 17);
2767#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002768#ifdef IPPROTO_IP
2769 insint(d, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00002770#else
2771 insint(d, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002772#endif
2773#ifdef IPPROTO_ICMP
2774 insint(d, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00002775#else
2776 insint(d, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002777#endif
2778#ifdef IPPROTO_IGMP
2779 insint(d, "IPPROTO_IGMP", IPPROTO_IGMP);
2780#endif
2781#ifdef IPPROTO_GGP
2782 insint(d, "IPPROTO_GGP", IPPROTO_GGP);
2783#endif
2784#ifdef IPPROTO_TCP
2785 insint(d, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00002786#else
2787 insint(d, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002788#endif
2789#ifdef IPPROTO_EGP
2790 insint(d, "IPPROTO_EGP", IPPROTO_EGP);
2791#endif
2792#ifdef IPPROTO_PUP
2793 insint(d, "IPPROTO_PUP", IPPROTO_PUP);
2794#endif
2795#ifdef IPPROTO_UDP
2796 insint(d, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00002797#else
2798 insint(d, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002799#endif
2800#ifdef IPPROTO_IDP
2801 insint(d, "IPPROTO_IDP", IPPROTO_IDP);
2802#endif
2803#ifdef IPPROTO_HELLO
2804 insint(d, "IPPROTO_HELLO", IPPROTO_HELLO);
2805#endif
2806#ifdef IPPROTO_ND
2807 insint(d, "IPPROTO_ND", IPPROTO_ND);
2808#endif
2809#ifdef IPPROTO_TP
2810 insint(d, "IPPROTO_TP", IPPROTO_TP);
2811#endif
2812#ifdef IPPROTO_XTP
2813 insint(d, "IPPROTO_XTP", IPPROTO_XTP);
2814#endif
2815#ifdef IPPROTO_EON
2816 insint(d, "IPPROTO_EON", IPPROTO_EON);
2817#endif
2818#ifdef IPPROTO_BIP
2819 insint(d, "IPPROTO_BIP", IPPROTO_BIP);
2820#endif
2821/**/
2822#ifdef IPPROTO_RAW
2823 insint(d, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00002824#else
2825 insint(d, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002826#endif
2827#ifdef IPPROTO_MAX
2828 insint(d, "IPPROTO_MAX", IPPROTO_MAX);
2829#endif
2830
2831 /* Some port configuration */
2832#ifdef IPPORT_RESERVED
2833 insint(d, "IPPORT_RESERVED", IPPORT_RESERVED);
2834#else
2835 insint(d, "IPPORT_RESERVED", 1024);
2836#endif
2837#ifdef IPPORT_USERRESERVED
2838 insint(d, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
2839#else
2840 insint(d, "IPPORT_USERRESERVED", 5000);
2841#endif
2842
Guido van Rossum5f05eb41995-02-17 15:11:07 +00002843 /* Some reserved IP v.4 addresses */
2844#ifdef INADDR_ANY
2845 insint(d, "INADDR_ANY", INADDR_ANY);
2846#else
2847 insint(d, "INADDR_ANY", 0x00000000);
2848#endif
2849#ifdef INADDR_BROADCAST
2850 insint(d, "INADDR_BROADCAST", INADDR_BROADCAST);
2851#else
2852 insint(d, "INADDR_BROADCAST", 0xffffffff);
2853#endif
2854#ifdef INADDR_LOOPBACK
2855 insint(d, "INADDR_LOOPBACK", INADDR_LOOPBACK);
2856#else
2857 insint(d, "INADDR_LOOPBACK", 0x7F000001);
2858#endif
2859#ifdef INADDR_UNSPEC_GROUP
2860 insint(d, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
2861#else
2862 insint(d, "INADDR_UNSPEC_GROUP", 0xe0000000);
2863#endif
2864#ifdef INADDR_ALLHOSTS_GROUP
2865 insint(d, "INADDR_ALLHOSTS_GROUP", INADDR_ALLHOSTS_GROUP);
2866#else
2867 insint(d, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
2868#endif
2869#ifdef INADDR_MAX_LOCAL_GROUP
2870 insint(d, "INADDR_MAX_LOCAL_GROUP", INADDR_MAX_LOCAL_GROUP);
2871#else
2872 insint(d, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
2873#endif
2874#ifdef INADDR_NONE
2875 insint(d, "INADDR_NONE", INADDR_NONE);
2876#else
2877 insint(d, "INADDR_NONE", 0xffffffff);
2878#endif
2879
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002880 /* IP [gs]etsockopt options */
2881#ifdef IP_OPTIONS
2882 insint(d, "IP_OPTIONS", IP_OPTIONS);
2883#endif
2884#ifdef IP_HDRINCL
2885 insint(d, "IP_HDRINCL", IP_HDRINCL);
2886#endif
2887#ifdef IP_TOS
2888 insint(d, "IP_TOS", IP_TOS);
2889#endif
2890#ifdef IP_TTL
2891 insint(d, "IP_TTL", IP_TTL);
2892#endif
2893#ifdef IP_RECVOPTS
2894 insint(d, "IP_RECVOPTS", IP_RECVOPTS);
2895#endif
2896#ifdef IP_RECVRETOPTS
2897 insint(d, "IP_RECVRETOPTS", IP_RECVRETOPTS);
2898#endif
2899#ifdef IP_RECVDSTADDR
2900 insint(d, "IP_RECVDSTADDR", IP_RECVDSTADDR);
2901#endif
2902#ifdef IP_RETOPTS
2903 insint(d, "IP_RETOPTS", IP_RETOPTS);
2904#endif
2905#ifdef IP_MULTICAST_IF
2906 insint(d, "IP_MULTICAST_IF", IP_MULTICAST_IF);
2907#endif
2908#ifdef IP_MULTICAST_TTL
2909 insint(d, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
2910#endif
2911#ifdef IP_MULTICAST_LOOP
2912 insint(d, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
2913#endif
2914#ifdef IP_ADD_MEMBERSHIP
2915 insint(d, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
2916#endif
2917#ifdef IP_DROP_MEMBERSHIP
2918 insint(d, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
2919#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00002920#ifdef IP_DEFAULT_MULTICAST_TTL
2921 insint(d, "IP_DEFAULT_MULTICAST_TTL", IP_DEFAULT_MULTICAST_TTL);
2922#endif
2923#ifdef IP_DEFAULT_MULTICAST_LOOP
2924 insint(d, "IP_DEFAULT_MULTICAST_LOOP", IP_DEFAULT_MULTICAST_LOOP);
2925#endif
2926#ifdef IP_MAX_MEMBERSHIPS
2927 insint(d, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
2928#endif
2929
2930 /* TCP options */
2931#ifdef TCP_NODELAY
2932 insint(d, "TCP_NODELAY", TCP_NODELAY);
2933#endif
2934#ifdef TCP_MAXSEG
2935 insint(d, "TCP_MAXSEG", TCP_MAXSEG);
2936#endif
2937
2938 /* IPX options */
2939#ifdef IPX_TYPE
2940 insint(d, "IPX_TYPE", IPX_TYPE);
2941#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002942
2943 /* Initialize gethostbyname lock */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002944#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum65d5b571998-12-21 19:32:43 +00002945 gethostbyname_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002946#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002947}