blob: c46f1e06de535ffaf448bb263dbf248e49580aa4 [file] [log] [blame]
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossum6574b3e1991-06-25 21:36:08 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum6574b3e1991-06-25 21:36:08 +00009******************************************************************/
10
11/* Socket module */
12
Guido van Rossum09c8b6c1999-12-07 21:37:17 +000013/* SSL support based on patches by Brian E Gallew and Laszlo Kovacs */
14
Guido van Rossum6574b3e1991-06-25 21:36:08 +000015/*
16This module provides an interface to Berkeley socket IPC.
17
18Limitations:
19
Guido van Rossum30a685f1991-06-27 15:51:29 +000020- only AF_INET and AF_UNIX address families are supported
Guido van Rossum81194471991-07-27 21:42:02 +000021- no read/write operations (use send/recv or makefile instead)
Guido van Rossumbe32c891996-06-20 16:25:29 +000022- additional restrictions apply on Windows
Guido van Rossum6574b3e1991-06-25 21:36:08 +000023
Guido van Rossum27e177d1995-03-16 15:43:47 +000024Module interface:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000025
Guido van Rossum27e177d1995-03-16 15:43:47 +000026- socket.error: exception raised for socket specific errors
Guido van Rossum30a685f1991-06-27 15:51:29 +000027- socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd')
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000028- socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...])
Guido van Rossum27e177d1995-03-16 15:43:47 +000029- socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com')
Guido van Rossum25405c71996-12-19 16:42:52 +000030- socket.getprotobyname(protocolname) --> protocol number
Guido van Rossum27e177d1995-03-16 15:43:47 +000031- socket.getservbyname(servicename, protocolname) --> port number
Guido van Rossum4dd2a7e1991-07-01 18:51:33 +000032- socket.socket(family, type [, proto]) --> new socket object
Guido van Rossum006bf911996-06-12 04:04:55 +000033- socket.ntohs(16 bit value) --> new int object
34- socket.ntohl(32 bit value) --> new int object
35- socket.htons(16 bit value) --> new int object
36- socket.htonl(32 bit value) --> new int object
Guido van Rossum27e177d1995-03-16 15:43:47 +000037- socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +000038- socket.inet_aton(IP address) -> 32-bit packed IP representation
39- socket.inet_ntoa(packed IP) -> IP address string
Guido van Rossum09c8b6c1999-12-07 21:37:17 +000040- socket.ssl(socket, keyfile, certfile) -> new ssl object
Guido van Rossum6574b3e1991-06-25 21:36:08 +000041- an Internet socket address is a pair (hostname, port)
42 where hostname can be anything recognized by gethostbyname()
43 (including the dd.dd.dd.dd notation) and port is in host byte order
44- where a hostname is returned, the dd.dd.dd.dd notation is used
Guido van Rossum27e177d1995-03-16 15:43:47 +000045- a UNIX domain socket address is a string specifying the pathname
Guido van Rossum6574b3e1991-06-25 21:36:08 +000046
Guido van Rossum30a685f1991-06-27 15:51:29 +000047Socket methods:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000048
Guido van Rossum81194471991-07-27 21:42:02 +000049- s.accept() --> new socket object, sockaddr
Fred Drake728819a2000-07-01 03:40:12 +000050- s.bind(sockaddr) --> None
Guido van Rossum27e177d1995-03-16 15:43:47 +000051- s.close() --> None
Fred Drake728819a2000-07-01 03:40:12 +000052- s.connect(sockaddr) --> None
53- s.connect_ex(sockaddr) --> 0 or errno (handy for e.g. async connect)
Guido van Rossum27e177d1995-03-16 15:43:47 +000054- s.fileno() --> file descriptor
Guido van Rossumbe32c891996-06-20 16:25:29 +000055- s.dup() --> same as socket.fromfd(os.dup(s.fileno(), ...)
Guido van Rossumc89705d1992-11-26 08:54:07 +000056- s.getpeername() --> sockaddr
Guido van Rossum27e177d1995-03-16 15:43:47 +000057- s.getsockname() --> sockaddr
58- s.getsockopt(level, optname[, buflen]) --> int or string
59- s.listen(backlog) --> None
Guido van Rossum6b144911995-03-14 15:05:13 +000060- s.makefile([mode[, bufsize]]) --> file object
Guido van Rossum27e177d1995-03-16 15:43:47 +000061- s.recv(buflen [,flags]) --> string
62- s.recvfrom(buflen [,flags]) --> string, sockaddr
Guido van Rossumb6775db1994-08-01 11:34:53 +000063- s.send(string [,flags]) --> nbytes
64- s.sendto(string, [flags,] sockaddr) --> nbytes
Guido van Rossum27e177d1995-03-16 15:43:47 +000065- s.setblocking(0 | 1) --> None
66- s.setsockopt(level, optname, value) --> None
67- s.shutdown(how) --> None
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000068- repr(s) --> "<socket object, fd=%d, family=%d, type=%d, protocol=%d>"
Guido van Rossum30a685f1991-06-27 15:51:29 +000069
Guido van Rossum6574b3e1991-06-25 21:36:08 +000070*/
71
Guido van Rossum73624e91994-10-10 17:59:00 +000072#include "Python.h"
Guido van Rossum3baaa131999-03-22 21:44:51 +000073
Guido van Rossum9376b741999-09-15 22:01:40 +000074/* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure
75 script doesn't get this right, so we hardcode some platform checks below.
76 On the other hand, not all Linux versions agree, so there the settings
77 computed by the configure script are needed! */
78
79#ifndef linux
Guido van Rossume7de2061999-03-24 17:24:33 +000080#undef HAVE_GETHOSTBYNAME_R_3_ARG
81#undef HAVE_GETHOSTBYNAME_R_5_ARG
82#undef HAVE_GETHOSTBYNAME_R_6_ARG
Guido van Rossum9376b741999-09-15 22:01:40 +000083#endif
Guido van Rossume7de2061999-03-24 17:24:33 +000084
Guido van Rossum7a122991999-04-13 04:07:32 +000085#ifndef WITH_THREAD
86#undef HAVE_GETHOSTBYNAME_R
87#endif
88
Guido van Rossume7de2061999-03-24 17:24:33 +000089#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7a122991999-04-13 04:07:32 +000090#if defined(_AIX) || defined(__osf__)
Guido van Rossume7de2061999-03-24 17:24:33 +000091#define HAVE_GETHOSTBYNAME_R_3_ARG
92#elif defined(__sun__) || defined(__sgi)
93#define HAVE_GETHOSTBYNAME_R_5_ARG
94#elif defined(linux)
Guido van Rossum9376b741999-09-15 22:01:40 +000095/* Rely on the configure script */
Guido van Rossume7de2061999-03-24 17:24:33 +000096#else
97#undef HAVE_GETHOSTBYNAME_R
98#endif
99#endif
100
Guido van Rossum3baaa131999-03-22 21:44:51 +0000101#if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && !defined(MS_WINDOWS)
102#define USE_GETHOSTBYNAME_LOCK
103#endif
104
105#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum49b56061998-10-01 20:42:43 +0000106#include "pythread.h"
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000107#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000108
Guido van Rossuma376cc51996-12-05 23:43:35 +0000109#ifdef HAVE_UNISTD_H
110#include <unistd.h>
111#endif
112
Guido van Rossumbcc20741998-08-04 22:53:56 +0000113#if !defined(MS_WINDOWS) && !defined(PYOS_OS2) && !defined(__BEOS__)
Guido van Rossumfb4130d1996-12-10 15:17:08 +0000114extern int gethostname(); /* For Solaris, at least */
115#endif
116
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000117#if defined(PYCC_VACPP)
118#include <types.h>
119#include <io.h>
120#include <sys/ioctl.h>
121#include <utils.h>
122#include <ctype.h>
123#endif
124
125#if defined(PYOS_OS2)
126#define INCL_DOS
127#define INCL_DOSERRORS
128#define INCL_NOPMAPI
129#include <os2.h>
130#endif
131
Guido van Rossumbcc20741998-08-04 22:53:56 +0000132#if defined(__BEOS__)
133/* It's in the libs, but not the headers... - [cjh] */
134int shutdown( int, int );
135#endif
136
Guido van Rossumb6775db1994-08-01 11:34:53 +0000137#include <sys/types.h>
138#include "mytime.h"
Guido van Rossumed233a51992-06-23 09:07:03 +0000139
Guido van Rossum81194471991-07-27 21:42:02 +0000140#include <signal.h>
Guido van Rossum6f489d91996-06-28 20:15:15 +0000141#ifndef MS_WINDOWS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000142#include <netdb.h>
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000143#include <sys/socket.h>
144#include <netinet/in.h>
Fred Drake11b09362000-05-16 13:30:12 +0000145#ifndef __BEOS__
Andrew M. Kuchling52a644c2000-03-31 17:24:30 +0000146#include <netinet/tcp.h>
Fred Drake11b09362000-05-16 13:30:12 +0000147#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000148
Guido van Rossum9376b741999-09-15 22:01:40 +0000149/* Headers needed for inet_ntoa() and inet_addr() */
150#ifdef __BEOS__
151#include <net/netdb.h>
152#else
Guido van Rossumff3ab422000-04-24 15:16:03 +0000153#ifndef USE_GUSI1
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000154#include <arpa/inet.h>
Guido van Rossum9376b741999-09-15 22:01:40 +0000155#endif
Guido van Rossum1b6e4631999-11-04 18:22:29 +0000156#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000157
Guido van Rossume4485b01994-09-07 14:32:49 +0000158#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000159#else
160#include <winsock.h>
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000161#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000162#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000163#ifdef HAVE_SYS_UN_H
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000164#include <sys/un.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000165#else
166#undef AF_UNIX
Guido van Rossum9575a441993-04-07 14:06:14 +0000167#endif
168
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000169#ifndef O_NDELAY
170#define O_NDELAY O_NONBLOCK /* For QNX only? */
171#endif
172
Guido van Rossumff3ab422000-04-24 15:16:03 +0000173#ifdef USE_GUSI1
Jack Jansen508537b1996-02-14 15:57:45 +0000174/* fdopen() isn't declared in stdio.h (sigh) */
175#include <GUSI.h>
176#endif
177
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000178#ifdef USE_SSL
179#include "rsa.h"
180#include "crypto.h"
181#include "x509.h"
182#include "pem.h"
183#include "ssl.h"
184#include "err.h"
185#endif /* USE_SSL */
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000186
Guido van Rossumbcc20741998-08-04 22:53:56 +0000187#if defined(MS_WINDOWS) || defined(__BEOS__)
188/* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000189/* seem to be a few differences in the API */
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000190#define SOCKETCLOSE closesocket
Guido van Rossumbe32c891996-06-20 16:25:29 +0000191#define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000192#endif
193
Fred Drakea04eaad2000-06-30 02:46:07 +0000194/* abstract the socket file descriptor type */
195#ifdef MS_WINDOWS
196typedef SOCKET SOCKET_T;
197# ifdef MS_WIN64
198# define SIZEOF_SOCKET_T 8
199# else
200# define SIZEOF_SOCKET_T 4
201# endif
202#else
203typedef int SOCKET_T;
204# define SIZEOF_SOCKET_T SIZEOF_INT
205#endif
206
207
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000208#if defined(PYOS_OS2)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000209#define SOCKETCLOSE soclose
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000210#define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000211#endif
212
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000213#ifndef SOCKETCLOSE
214#define SOCKETCLOSE close
215#endif
216
Guido van Rossum30a685f1991-06-27 15:51:29 +0000217/* Global variable holding the exception type for errors detected
218 by this module (but not argument type or memory errors, etc.). */
219
Guido van Rossum73624e91994-10-10 17:59:00 +0000220static PyObject *PySocket_Error;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000221
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000222#ifdef USE_SSL
223static PyObject *SSLErrorObject;
224#endif /* USE_SSL */
225
Guido van Rossum30a685f1991-06-27 15:51:29 +0000226
227/* Convenience function to raise an error according to errno
228 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000229
Guido van Rossum73624e91994-10-10 17:59:00 +0000230static PyObject *
231PySocket_Err()
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000232{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000233#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000234 if (WSAGetLastError()) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000235 PyObject *v;
236 v = Py_BuildValue("(is)", WSAGetLastError(), "winsock error");
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000237 if (v != NULL) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000238 PyErr_SetObject(PySocket_Error, v);
239 Py_DECREF(v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000240 }
241 return NULL;
242 }
243 else
244#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000245
246#if defined(PYOS_OS2)
247 if (sock_errno() != NO_ERROR) {
248 APIRET rc;
249 ULONG msglen;
250 char outbuf[100];
251 int myerrorcode = sock_errno();
252
253 /* Retrieve Socket-Related Error Message from MPTN.MSG File */
254 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
255 myerrorcode - SOCBASEERR + 26, "mptn.msg", &msglen);
256 if (rc == NO_ERROR) {
257 PyObject *v;
258
259 outbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
260 if (strlen(outbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
261 char *lastc = &outbuf[ strlen(outbuf)-1 ];
262 while (lastc > outbuf && isspace(*lastc))
263 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
264 }
265 v = Py_BuildValue("(is)", myerrorcode, outbuf);
266 if (v != NULL) {
267 PyErr_SetObject(PySocket_Error, v);
268 Py_DECREF(v);
269 }
270 return NULL;
271 }
272 }
273#endif
274
Guido van Rossum73624e91994-10-10 17:59:00 +0000275 return PyErr_SetFromErrno(PySocket_Error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000276}
277
Guido van Rossum30a685f1991-06-27 15:51:29 +0000278
279/* The object holding a socket. It holds some extra information,
280 like the address family, which is used to decode socket address
281 arguments properly. */
282
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000283typedef struct {
Guido van Rossum73624e91994-10-10 17:59:00 +0000284 PyObject_HEAD
Fred Drakea04eaad2000-06-30 02:46:07 +0000285 SOCKET_T sock_fd; /* Socket file descriptor */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000286 int sock_family; /* Address family, e.g., AF_INET */
287 int sock_type; /* Socket type, e.g., SOCK_STREAM */
288 int sock_proto; /* Protocol type, usually 0 */
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000289 union sock_addr {
290 struct sockaddr_in in;
291#ifdef AF_UNIX
292 struct sockaddr_un un;
293#endif
294 } sock_addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000295} PySocketSockObject;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000296
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000297#ifdef USE_SSL
Guido van Rossum30a685f1991-06-27 15:51:29 +0000298
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000299typedef struct {
300 PyObject_HEAD
301 PySocketSockObject *Socket; /* Socket on which we're layered */
302 PyObject *x_attr; /* Attributes dictionary */
303 SSL_CTX* ctx;
304 SSL* ssl;
305 X509* server_cert;
306 BIO* sbio;
307 char server[256];
308 char issuer[256];
309
310} SSLObject;
311
312staticforward PyTypeObject SSL_Type;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +0000313staticforward PyObject *SSL_SSLwrite(SSLObject *self, PyObject *args);
314staticforward PyObject *SSL_SSLread(SSLObject *self, PyObject *args);
315
316#define SSLObject_Check(v) ((v)->ob_type == &SSL_Type)
317
318#endif /* USE_SSL */
319
Guido van Rossum30a685f1991-06-27 15:51:29 +0000320/* A forward reference to the Socktype type object.
321 The Socktype variable contains pointers to various functions,
Guido van Rossum73624e91994-10-10 17:59:00 +0000322 some of which call newsockobject(), which uses Socktype, so
Guido van Rossum54ba21b1991-09-10 14:57:12 +0000323 there has to be a circular reference. */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000324
Guido van Rossum73624e91994-10-10 17:59:00 +0000325staticforward PyTypeObject PySocketSock_Type;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000326
Guido van Rossum30a685f1991-06-27 15:51:29 +0000327
328/* Create a new socket object.
329 This just creates the object and initializes it.
330 If the creation fails, return NULL and set an exception (implicit
331 in NEWOBJ()). */
332
Guido van Rossum73624e91994-10-10 17:59:00 +0000333static PySocketSockObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000334PySocketSock_New(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000335{
Guido van Rossum73624e91994-10-10 17:59:00 +0000336 PySocketSockObject *s;
Guido van Rossum55558541996-05-23 22:54:50 +0000337 PySocketSock_Type.ob_type = &PyType_Type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000338 s = PyObject_New(PySocketSockObject, &PySocketSock_Type);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000339 if (s != NULL) {
340 s->sock_fd = fd;
341 s->sock_family = family;
342 s->sock_type = type;
343 s->sock_proto = proto;
344 }
345 return s;
346}
347
Guido van Rossum30a685f1991-06-27 15:51:29 +0000348
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000349/* Lock to allow python interpreter to continue, but only allow one
350 thread to be in gethostbyname */
Guido van Rossum3baaa131999-03-22 21:44:51 +0000351#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum65d5b571998-12-21 19:32:43 +0000352PyThread_type_lock gethostbyname_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000353#endif
354
355
Guido van Rossum30a685f1991-06-27 15:51:29 +0000356/* Convert a string specifying a host name or one of a few symbolic
357 names to a numeric IP address. This usually calls gethostbyname()
358 to do the work; the names "" and "<broadcast>" are special.
359 Return the length (should always be 4 bytes), or negative if
360 an error occurred; then an exception is raised. */
361
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000362static int
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000363setipaddr(char* name, struct sockaddr_in * addr_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000364{
365 struct hostent *hp;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000366 int d1, d2, d3, d4;
Guido van Rossum955becc1999-03-22 20:14:53 +0000367 int h_length;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000368 char ch;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000369#ifdef HAVE_GETHOSTBYNAME_R
370 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +0000371#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
372 struct hostent_data data;
373#else
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000374 char buf[1001];
375 int buf_len = (sizeof buf) - 1;
376 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +0000377#endif
378#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +0000379 int result;
380#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000381#endif /* HAVE_GETHOSTBYNAME_R */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000382
Guido van Rossuma376cc51996-12-05 23:43:35 +0000383 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
Guido van Rossum30a685f1991-06-27 15:51:29 +0000384 if (name[0] == '\0') {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000385 addr_ret->sin_addr.s_addr = INADDR_ANY;
386 return 4;
387 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000388 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000389 addr_ret->sin_addr.s_addr = INADDR_BROADCAST;
390 return 4;
391 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000392 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
393 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
394 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
395 addr_ret->sin_addr.s_addr = htonl(
396 ((long) d1 << 24) | ((long) d2 << 16) |
397 ((long) d3 << 8) | ((long) d4 << 0));
398 return 4;
399 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000400 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000401#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +0000402#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +0000403 result = gethostbyname_r(name, &hp_allocated, buf, buf_len, &hp, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +0000404#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000405 hp = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +0000406#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +0000407 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +0000408 result = gethostbyname_r(name, &hp_allocated, &data);
409 hp = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +0000410#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000411#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +0000412#ifdef USE_GETHOSTBYNAME_LOCK
413 PyThread_acquire_lock(gethostbyname_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000414#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000415 hp = gethostbyname(name);
416#endif /* HAVE_GETHOSTBYNAME_R */
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000417 Py_END_ALLOW_THREADS
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000418
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000419 if (hp == NULL) {
Guido van Rossumca9b3231995-01-10 16:30:54 +0000420#ifdef HAVE_HSTRERROR
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000421 /* Let's get real error message to return */
422 extern int h_errno;
423 PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno));
424#else
Guido van Rossum73624e91994-10-10 17:59:00 +0000425 PyErr_SetString(PySocket_Error, "host not found");
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000426#endif
Guido van Rossum3baaa131999-03-22 21:44:51 +0000427#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum955becc1999-03-22 20:14:53 +0000428 PyThread_release_lock(gethostbyname_lock);
429#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000430 return -1;
431 }
432 memcpy((char *) &addr_ret->sin_addr, hp->h_addr, hp->h_length);
Guido van Rossum955becc1999-03-22 20:14:53 +0000433 h_length = hp->h_length;
Guido van Rossum3baaa131999-03-22 21:44:51 +0000434#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum955becc1999-03-22 20:14:53 +0000435 PyThread_release_lock(gethostbyname_lock);
436#endif
437 return h_length;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000438}
439
Guido van Rossum30a685f1991-06-27 15:51:29 +0000440
Guido van Rossum30a685f1991-06-27 15:51:29 +0000441/* Create a string object representing an IP address.
442 This is always a string of the form 'dd.dd.dd.dd' (with variable
443 size numbers). */
444
Guido van Rossum73624e91994-10-10 17:59:00 +0000445static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000446makeipaddr(struct sockaddr_in *addr)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000447{
448 long x = ntohl(addr->sin_addr.s_addr);
449 char buf[100];
450 sprintf(buf, "%d.%d.%d.%d",
451 (int) (x>>24) & 0xff, (int) (x>>16) & 0xff,
452 (int) (x>> 8) & 0xff, (int) (x>> 0) & 0xff);
Guido van Rossum73624e91994-10-10 17:59:00 +0000453 return PyString_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000454}
455
456
457/* Create an object representing the given socket address,
458 suitable for passing it back to bind(), connect() etc.
459 The family field of the sockaddr structure is inspected
460 to determine what kind of address it really is. */
461
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000462/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000463static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000464makesockaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000465{
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000466 if (addrlen == 0) {
467 /* No address -- may be recvfrom() from known socket */
Guido van Rossum73624e91994-10-10 17:59:00 +0000468 Py_INCREF(Py_None);
469 return Py_None;
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000470 }
471
Guido van Rossumbcc20741998-08-04 22:53:56 +0000472#ifdef __BEOS__
Guido van Rossum2c8bcb82000-04-25 21:34:53 +0000473 /* XXX: BeOS version of accept() doesn't set family correctly */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000474 addr->sa_family = AF_INET;
475#endif
476
Guido van Rossum30a685f1991-06-27 15:51:29 +0000477 switch (addr->sa_family) {
478
479 case AF_INET:
480 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000481 struct sockaddr_in *a = (struct sockaddr_in *) addr;
Barry Warsaw752300b1997-01-03 17:18:10 +0000482 PyObject *addrobj = makeipaddr(a);
483 PyObject *ret = NULL;
484 if (addrobj) {
485 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
486 Py_DECREF(addrobj);
487 }
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000488 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000489 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000490
Guido van Rossumb6775db1994-08-01 11:34:53 +0000491#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000492 case AF_UNIX:
493 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000494 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000495 return PyString_FromString(a->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000496 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000497#endif /* AF_UNIX */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000498
499 /* More cases here... */
500
501 default:
Guido van Rossumaa948df1997-05-07 17:41:48 +0000502 /* If we don't know the address family, don't raise an
503 exception -- return it as a tuple. */
504 return Py_BuildValue("is#",
505 addr->sa_family,
506 addr->sa_data,
507 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000508
Guido van Rossum30a685f1991-06-27 15:51:29 +0000509 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000510}
511
Guido van Rossum30a685f1991-06-27 15:51:29 +0000512
513/* Parse a socket address argument according to the socket object's
514 address family. Return 1 if the address was in the proper format,
515 0 of not. The address is returned through addr_ret, its length
516 through len_ret. */
517
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000518static int
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000519getsockaddrarg(PySocketSockObject *s, PyObject *args, struct sockaddr **addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000520{
Guido van Rossum30a685f1991-06-27 15:51:29 +0000521 switch (s->sock_family) {
522
Guido van Rossumb6775db1994-08-01 11:34:53 +0000523#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000524 case AF_UNIX:
525 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000526 struct sockaddr_un* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000527 char *path;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000528 int len;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000529 addr = (struct sockaddr_un* )&(s->sock_addr).un;
Guido van Rossum7e488981998-10-08 02:25:24 +0000530 if (!PyArg_Parse(args, "t#", &path, &len))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000531 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000532 if (len > sizeof addr->sun_path) {
Barry Warsaw752300b1997-01-03 17:18:10 +0000533 PyErr_SetString(PySocket_Error,
534 "AF_UNIX path too long");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000535 return 0;
536 }
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000537 addr->sun_family = AF_UNIX;
538 memcpy(addr->sun_path, path, len);
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000539 addr->sun_path[len] = 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000540 *addr_ret = (struct sockaddr *) addr;
Guido van Rossum65af28a1996-06-11 18:36:33 +0000541 *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000542 return 1;
543 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000544#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000545
Guido van Rossum30a685f1991-06-27 15:51:29 +0000546 case AF_INET:
547 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000548 struct sockaddr_in* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000549 char *host;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000550 int port;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000551 addr=(struct sockaddr_in*)&(s->sock_addr).in;
Guido van Rossum73624e91994-10-10 17:59:00 +0000552 if (!PyArg_Parse(args, "(si)", &host, &port))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000553 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000554 if (setipaddr(host, addr) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000555 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000556 addr->sin_family = AF_INET;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000557 addr->sin_port = htons((short)port);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000558 *addr_ret = (struct sockaddr *) addr;
559 *len_ret = sizeof *addr;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000560 return 1;
561 }
562
Guido van Rossum30a685f1991-06-27 15:51:29 +0000563 /* More cases here... */
564
565 default:
Guido van Rossum73624e91994-10-10 17:59:00 +0000566 PyErr_SetString(PySocket_Error, "getsockaddrarg: bad family");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000567 return 0;
568
569 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000570}
571
Guido van Rossum30a685f1991-06-27 15:51:29 +0000572
Guido van Rossum710e1df1992-06-12 10:39:36 +0000573/* Get the address length according to the socket object's address family.
574 Return 1 if the family is known, 0 otherwise. The length is returned
575 through len_ret. */
576
577static int
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000578getsockaddrlen(PySocketSockObject *s, int *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +0000579{
580 switch (s->sock_family) {
581
Guido van Rossumb6775db1994-08-01 11:34:53 +0000582#ifdef AF_UNIX
Guido van Rossum710e1df1992-06-12 10:39:36 +0000583 case AF_UNIX:
584 {
585 *len_ret = sizeof (struct sockaddr_un);
586 return 1;
587 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000588#endif /* AF_UNIX */
Guido van Rossum710e1df1992-06-12 10:39:36 +0000589
590 case AF_INET:
591 {
592 *len_ret = sizeof (struct sockaddr_in);
593 return 1;
594 }
595
596 /* More cases here... */
597
598 default:
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000599 PyErr_SetString(PySocket_Error, "getsockaddrlen: bad family");
Guido van Rossum710e1df1992-06-12 10:39:36 +0000600 return 0;
601
602 }
603}
604
605
Guido van Rossum30a685f1991-06-27 15:51:29 +0000606/* s.accept() method */
607
Guido van Rossum73624e91994-10-10 17:59:00 +0000608static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000609PySocketSock_accept(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000610{
611 char addrbuf[256];
Fred Drakea04eaad2000-06-30 02:46:07 +0000612 SOCKET_T newfd;
Guido van Rossumff3ab422000-04-24 15:16:03 +0000613 socklen_t addrlen;
Barry Warsaw752300b1997-01-03 17:18:10 +0000614 PyObject *sock = NULL;
615 PyObject *addr = NULL;
616 PyObject *res = NULL;
617
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000618 if (!PyArg_ParseTuple(args, ":accept"))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000619 return NULL;
Guido van Rossum710e1df1992-06-12 10:39:36 +0000620 if (!getsockaddrlen(s, &addrlen))
621 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000622 Py_BEGIN_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000623 newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000624 Py_END_ALLOW_THREADS
Fred Drakea04eaad2000-06-30 02:46:07 +0000625#ifdef MS_WINDOWS
626 if (newfd == INVALID_SOCKET)
627#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000628 if (newfd < 0)
Fred Drakea04eaad2000-06-30 02:46:07 +0000629#endif
Guido van Rossum73624e91994-10-10 17:59:00 +0000630 return PySocket_Err();
Barry Warsaw752300b1997-01-03 17:18:10 +0000631
Guido van Rossum30a685f1991-06-27 15:51:29 +0000632 /* Create the new object with unspecified family,
633 to avoid calls to bind() etc. on it. */
Guido van Rossum73624e91994-10-10 17:59:00 +0000634 sock = (PyObject *) PySocketSock_New(newfd,
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000635 s->sock_family,
636 s->sock_type,
637 s->sock_proto);
Barry Warsaw752300b1997-01-03 17:18:10 +0000638 if (sock == NULL) {
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000639 SOCKETCLOSE(newfd);
Barry Warsaw752300b1997-01-03 17:18:10 +0000640 goto finally;
641 }
642 if (!(addr = makesockaddr((struct sockaddr *) addrbuf, addrlen)))
643 goto finally;
644
645 if (!(res = Py_BuildValue("OO", sock, addr)))
646 goto finally;
647
648 finally:
Guido van Rossum73624e91994-10-10 17:59:00 +0000649 Py_XDECREF(sock);
650 Py_XDECREF(addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000651 return res;
652}
653
Guido van Rossum82a5c661998-07-07 20:45:43 +0000654static char accept_doc[] =
655"accept() -> (socket object, address info)\n\
656\n\
657Wait for an incoming connection. Return a new socket representing the\n\
658connection, and the address of the client. For IP sockets, the address\n\
659info is a pair (hostaddr, port).";
660
Guido van Rossum30a685f1991-06-27 15:51:29 +0000661
Guido van Rossume4485b01994-09-07 14:32:49 +0000662/* s.setblocking(1 | 0) method */
663
Guido van Rossum73624e91994-10-10 17:59:00 +0000664static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000665PySocketSock_setblocking(PySocketSockObject *s, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000666{
667 int block;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000668#ifndef MS_WINDOWS
Guido van Rossume4485b01994-09-07 14:32:49 +0000669 int delay_flag;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000670#endif
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000671 if (!PyArg_ParseTuple(args, "i:setblocking", &block))
Guido van Rossume4485b01994-09-07 14:32:49 +0000672 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000673 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000674#ifdef __BEOS__
675 block = !block;
676 setsockopt( s->sock_fd, SOL_SOCKET, SO_NONBLOCK,
677 (void *)(&block), sizeof( int ) );
678#else
Guido van Rossum6f489d91996-06-28 20:15:15 +0000679#ifndef MS_WINDOWS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000680#ifdef PYOS_OS2
681 block = !block;
682 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
683#else /* !PYOS_OS2 */
Guido van Rossume4485b01994-09-07 14:32:49 +0000684 delay_flag = fcntl (s->sock_fd, F_GETFL, 0);
685 if (block)
686 delay_flag &= (~O_NDELAY);
687 else
688 delay_flag |= O_NDELAY;
689 fcntl (s->sock_fd, F_SETFL, delay_flag);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000690#endif /* !PYOS_OS2 */
691#else /* MS_WINDOWS */
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000692 block = !block;
693 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000694#endif /* MS_WINDOWS */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000695#endif /* __BEOS__ */
Guido van Rossum73624e91994-10-10 17:59:00 +0000696 Py_END_ALLOW_THREADS
Guido van Rossume4485b01994-09-07 14:32:49 +0000697
Guido van Rossum73624e91994-10-10 17:59:00 +0000698 Py_INCREF(Py_None);
699 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000700}
Guido van Rossume4485b01994-09-07 14:32:49 +0000701
Guido van Rossum82a5c661998-07-07 20:45:43 +0000702static char setblocking_doc[] =
703"setblocking(flag)\n\
704\n\
705Set the socket to blocking (flag is true) or non-blocking (false).\n\
706This uses the FIONBIO ioctl with the O_NDELAY flag.";
707
Guido van Rossume4485b01994-09-07 14:32:49 +0000708
Guido van Rossumaee08791992-09-08 09:05:33 +0000709/* s.setsockopt() method.
710 With an integer third argument, sets an integer option.
711 With a string third argument, sets an option from a buffer;
712 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000713
Guido van Rossum73624e91994-10-10 17:59:00 +0000714static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000715PySocketSock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000716{
717 int level;
718 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000719 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +0000720 char *buf;
721 int buflen;
722 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000723
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000724 if (PyArg_ParseTuple(args, "iii:setsockopt",
725 &level, &optname, &flag)) {
Guido van Rossumaee08791992-09-08 09:05:33 +0000726 buf = (char *) &flag;
727 buflen = sizeof flag;
728 }
729 else {
Guido van Rossum73624e91994-10-10 17:59:00 +0000730 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000731 if (!PyArg_ParseTuple(args, "iis#:setsockopt",
732 &level, &optname, &buf, &buflen))
Guido van Rossumaee08791992-09-08 09:05:33 +0000733 return NULL;
734 }
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000735 res = setsockopt(s->sock_fd, level, optname, (ANY *)buf, buflen);
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000736 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000737 return PySocket_Err();
738 Py_INCREF(Py_None);
739 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000740}
741
Guido van Rossum82a5c661998-07-07 20:45:43 +0000742static char setsockopt_doc[] =
743"setsockopt(level, option, value)\n\
744\n\
745Set a socket option. See the Unix manual for level and option.\n\
746The value argument can either be an integer or a string.";
747
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000748
Guido van Rossumaee08791992-09-08 09:05:33 +0000749/* s.getsockopt() method.
750 With two arguments, retrieves an integer option.
751 With a third integer argument, retrieves a string buffer of that size;
752 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000753
Guido van Rossum73624e91994-10-10 17:59:00 +0000754static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000755PySocketSock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000756{
757 int level;
758 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000759 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +0000760 PyObject *buf;
Guido van Rossumff3ab422000-04-24 15:16:03 +0000761 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000762
Guido van Rossumbcc20741998-08-04 22:53:56 +0000763#ifdef __BEOS__
764/* We have incomplete socket support. */
765 PyErr_SetString( PySocket_Error, "getsockopt not supported" );
766 return NULL;
767#else
768
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000769 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
770 &level, &optname, &buflen))
Guido van Rossumbe32c891996-06-20 16:25:29 +0000771 return NULL;
772
773 if (buflen == 0) {
Guido van Rossumaee08791992-09-08 09:05:33 +0000774 int flag = 0;
Guido van Rossumff3ab422000-04-24 15:16:03 +0000775 socklen_t flagsize = sizeof flag;
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000776 res = getsockopt(s->sock_fd, level, optname,
777 (ANY *)&flag, &flagsize);
Guido van Rossumaee08791992-09-08 09:05:33 +0000778 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000779 return PySocket_Err();
780 return PyInt_FromLong(flag);
Guido van Rossumaee08791992-09-08 09:05:33 +0000781 }
Guido van Rossumaee08791992-09-08 09:05:33 +0000782 if (buflen <= 0 || buflen > 1024) {
Barry Warsaw752300b1997-01-03 17:18:10 +0000783 PyErr_SetString(PySocket_Error,
784 "getsockopt buflen out of range");
Guido van Rossumaee08791992-09-08 09:05:33 +0000785 return NULL;
786 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000787 buf = PyString_FromStringAndSize((char *)NULL, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000788 if (buf == NULL)
789 return NULL;
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000790 res = getsockopt(s->sock_fd, level, optname,
Guido van Rossuma597dde1995-01-10 20:56:29 +0000791 (ANY *)PyString_AsString(buf), &buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000792 if (res < 0) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000793 Py_DECREF(buf);
794 return PySocket_Err();
Guido van Rossumaee08791992-09-08 09:05:33 +0000795 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000796 _PyString_Resize(&buf, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000797 return buf;
Guido van Rossumbcc20741998-08-04 22:53:56 +0000798#endif /* __BEOS__ */
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000799}
800
Guido van Rossum82a5c661998-07-07 20:45:43 +0000801static char getsockopt_doc[] =
802"getsockopt(level, option[, buffersize]) -> value\n\
803\n\
804Get a socket option. See the Unix manual for level and option.\n\
805If a nonzero buffersize argument is given, the return value is a\n\
806string of that length; otherwise it is an integer.";
807
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000808
Fred Drake728819a2000-07-01 03:40:12 +0000809/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000810
Guido van Rossum73624e91994-10-10 17:59:00 +0000811static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000812PySocketSock_bind(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000813{
814 struct sockaddr *addr;
815 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000816 int res;
Fred Drake728819a2000-07-01 03:40:12 +0000817 PyObject *addro;
818 if (!PyArg_ParseTuple(args, "O:bind", &addro))
819 return NULL;
820 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000821 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000822 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000823 res = bind(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000824 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000825 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000826 return PySocket_Err();
827 Py_INCREF(Py_None);
828 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000829}
830
Guido van Rossum82a5c661998-07-07 20:45:43 +0000831static char bind_doc[] =
832"bind(address)\n\
833\n\
834Bind the socket to a local address. For IP sockets, the address is a\n\
835pair (host, port); the host must refer to the local host.";
836
Guido van Rossum30a685f1991-06-27 15:51:29 +0000837
838/* s.close() method.
839 Set the file descriptor to -1 so operations tried subsequently
840 will surely fail. */
841
Guido van Rossum73624e91994-10-10 17:59:00 +0000842static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000843PySocketSock_close(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000844{
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000845 if (!PyArg_ParseTuple(args, ":close"))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000846 return NULL;
Guido van Rossum08481461996-10-12 14:07:22 +0000847 if (s->sock_fd != -1) {
848 Py_BEGIN_ALLOW_THREADS
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000849 (void) SOCKETCLOSE(s->sock_fd);
Guido van Rossum08481461996-10-12 14:07:22 +0000850 Py_END_ALLOW_THREADS
851 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000852 s->sock_fd = -1;
Guido van Rossum73624e91994-10-10 17:59:00 +0000853 Py_INCREF(Py_None);
854 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000855}
856
Guido van Rossum82a5c661998-07-07 20:45:43 +0000857static char close_doc[] =
858"close()\n\
859\n\
860Close the socket. It cannot be used after this call.";
861
Guido van Rossum30a685f1991-06-27 15:51:29 +0000862
Fred Drake728819a2000-07-01 03:40:12 +0000863/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000864
Guido van Rossum73624e91994-10-10 17:59:00 +0000865static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000866PySocketSock_connect(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000867{
868 struct sockaddr *addr;
869 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000870 int res;
Fred Drake728819a2000-07-01 03:40:12 +0000871 PyObject *addro;
872 if (!PyArg_ParseTuple(args, "O:connect", &addro))
873 return NULL;
874 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000875 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000876 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000877 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000878 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000879 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000880 return PySocket_Err();
881 Py_INCREF(Py_None);
882 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000883}
884
Guido van Rossum82a5c661998-07-07 20:45:43 +0000885static char connect_doc[] =
886"connect(address)\n\
887\n\
888Connect the socket to a remote address. For IP sockets, the address\n\
889is a pair (host, port).";
890
Guido van Rossum30a685f1991-06-27 15:51:29 +0000891
Fred Drake728819a2000-07-01 03:40:12 +0000892/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +0000893
894static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000895PySocketSock_connect_ex(PySocketSockObject *s, PyObject *args)
Guido van Rossumfc4255d1997-11-19 18:57:13 +0000896{
897 struct sockaddr *addr;
898 int addrlen;
899 int res;
Fred Drake728819a2000-07-01 03:40:12 +0000900 PyObject *addro;
901 if (!PyArg_ParseTuple(args, "O:connect_ex", &addro))
902 return NULL;
903 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossumfc4255d1997-11-19 18:57:13 +0000904 return NULL;
905 Py_BEGIN_ALLOW_THREADS
906 res = connect(s->sock_fd, addr, addrlen);
907 Py_END_ALLOW_THREADS
908 if (res != 0)
909 res = errno;
910 return PyInt_FromLong((long) res);
911}
912
Guido van Rossum82a5c661998-07-07 20:45:43 +0000913static char connect_ex_doc[] =
914"connect_ex(address)\n\
915\n\
916This is like connect(address), but returns an error code (the errno value)\n\
917instead of raising an exception when an error occurs.";
918
Guido van Rossumfc4255d1997-11-19 18:57:13 +0000919
Guido van Rossumed233a51992-06-23 09:07:03 +0000920/* s.fileno() method */
921
Guido van Rossum73624e91994-10-10 17:59:00 +0000922static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000923PySocketSock_fileno(PySocketSockObject *s, PyObject *args)
Guido van Rossumed233a51992-06-23 09:07:03 +0000924{
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000925 if (!PyArg_ParseTuple(args, ":fileno"))
Guido van Rossumed233a51992-06-23 09:07:03 +0000926 return NULL;
Fred Drakea04eaad2000-06-30 02:46:07 +0000927#if SIZEOF_SOCKET_T <= SIZEOF_LONG
Guido van Rossum73624e91994-10-10 17:59:00 +0000928 return PyInt_FromLong((long) s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +0000929#else
930 return PyLong_FromLongLong((LONG_LONG)s->sock_fd);
931#endif
Guido van Rossumed233a51992-06-23 09:07:03 +0000932}
933
Guido van Rossum82a5c661998-07-07 20:45:43 +0000934static char fileno_doc[] =
935"fileno() -> integer\n\
936\n\
937Return the integer file descriptor of the socket.";
938
Guido van Rossumed233a51992-06-23 09:07:03 +0000939
Guido van Rossumbe32c891996-06-20 16:25:29 +0000940#ifndef NO_DUP
941/* s.dup() method */
942
943static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000944PySocketSock_dup(PySocketSockObject *s, PyObject *args)
Guido van Rossumbe32c891996-06-20 16:25:29 +0000945{
Fred Drakea04eaad2000-06-30 02:46:07 +0000946 SOCKET_T newfd;
Guido van Rossumbe32c891996-06-20 16:25:29 +0000947 PyObject *sock;
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000948 if (!PyArg_ParseTuple(args, ":dup"))
Guido van Rossumbe32c891996-06-20 16:25:29 +0000949 return NULL;
950 newfd = dup(s->sock_fd);
951 if (newfd < 0)
952 return PySocket_Err();
953 sock = (PyObject *) PySocketSock_New(newfd,
Barry Warsaw752300b1997-01-03 17:18:10 +0000954 s->sock_family,
955 s->sock_type,
956 s->sock_proto);
Guido van Rossumbe32c891996-06-20 16:25:29 +0000957 if (sock == NULL)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000958 SOCKETCLOSE(newfd);
Guido van Rossumbe32c891996-06-20 16:25:29 +0000959 return sock;
960}
Guido van Rossum82a5c661998-07-07 20:45:43 +0000961
962static char dup_doc[] =
963"dup() -> socket object\n\
964\n\
965Return a new socket object connected to the same system resource.";
966
Guido van Rossumbe32c891996-06-20 16:25:29 +0000967#endif
968
969
Guido van Rossumc89705d1992-11-26 08:54:07 +0000970/* s.getsockname() method */
971
Guido van Rossum73624e91994-10-10 17:59:00 +0000972static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +0000973PySocketSock_getsockname(PySocketSockObject *s, PyObject *args)
Guido van Rossumc89705d1992-11-26 08:54:07 +0000974{
975 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +0000976 int res;
977 socklen_t addrlen;
978
Guido van Rossum27fc3c02000-03-24 20:56:56 +0000979 if (!PyArg_ParseTuple(args, ":getsockname"))
Guido van Rossumc89705d1992-11-26 08:54:07 +0000980 return NULL;
981 if (!getsockaddrlen(s, &addrlen))
982 return NULL;
Guido van Rossumca42b161996-01-12 01:36:05 +0000983 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000984 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +0000985 res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000986 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +0000987 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000988 return PySocket_Err();
Guido van Rossumc89705d1992-11-26 08:54:07 +0000989 return makesockaddr((struct sockaddr *) addrbuf, addrlen);
990}
991
Guido van Rossum82a5c661998-07-07 20:45:43 +0000992static char getsockname_doc[] =
993"getsockname() -> address info\n\
994\n\
995Return the address of the local endpoint. For IP sockets, the address\n\
996info is a pair (hostaddr, port).";
997
Guido van Rossumc89705d1992-11-26 08:54:07 +0000998
Guido van Rossumb6775db1994-08-01 11:34:53 +0000999#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001000/* s.getpeername() method */
1001
Guido van Rossum73624e91994-10-10 17:59:00 +00001002static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001003PySocketSock_getpeername(PySocketSockObject *s, PyObject *args)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001004{
1005 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001006 int res;
1007 socklen_t addrlen;
1008
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001009 if (!PyArg_ParseTuple(args, ":getpeername"))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001010 return NULL;
1011 if (!getsockaddrlen(s, &addrlen))
1012 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001013 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001014 res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001015 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001016 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001017 return PySocket_Err();
Guido van Rossumc89705d1992-11-26 08:54:07 +00001018 return makesockaddr((struct sockaddr *) addrbuf, addrlen);
1019}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001020
1021static char getpeername_doc[] =
1022"getpeername() -> address info\n\
1023\n\
1024Return the address of the remote endpoint. For IP sockets, the address\n\
1025info is a pair (hostaddr, port).";
1026
Guido van Rossumb6775db1994-08-01 11:34:53 +00001027#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001028
1029
Guido van Rossum30a685f1991-06-27 15:51:29 +00001030/* s.listen(n) method */
1031
Guido van Rossum73624e91994-10-10 17:59:00 +00001032static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001033PySocketSock_listen(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001034{
1035 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001036 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001037 if (!PyArg_ParseTuple(args, "i:listen", &backlog))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001038 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001039 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +00001040 if (backlog < 1)
1041 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001042 res = listen(s->sock_fd, backlog);
Guido van Rossum73624e91994-10-10 17:59:00 +00001043 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001044 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001045 return PySocket_Err();
1046 Py_INCREF(Py_None);
1047 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001048}
1049
Guido van Rossum82a5c661998-07-07 20:45:43 +00001050static char listen_doc[] =
1051"listen(backlog)\n\
1052\n\
1053Enable a server to accept connections. The backlog argument must be at\n\
1054least 1; it specifies the number of unaccepted connection that the system\n\
1055will allow before refusing new connections.";
1056
1057
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001058#ifndef NO_DUP
Guido van Rossum30a685f1991-06-27 15:51:29 +00001059/* s.makefile(mode) method.
1060 Create a new open file object referring to a dupped version of
1061 the socket's file descriptor. (The dup() call is necessary so
1062 that the open file and socket objects may be closed independent
1063 of each other.)
1064 The mode argument specifies 'r' or 'w' passed to fdopen(). */
1065
Guido van Rossum73624e91994-10-10 17:59:00 +00001066static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001067PySocketSock_makefile(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001068{
Tim Petersdbd9ba62000-07-09 03:09:57 +00001069 extern int fclose(FILE *);
Guido van Rossum6b144911995-03-14 15:05:13 +00001070 char *mode = "r";
1071 int bufsize = -1;
Fred Drakea04eaad2000-06-30 02:46:07 +00001072#ifdef MS_WIN32
1073 intptr_t fd;
1074#else
Guido van Rossum30a685f1991-06-27 15:51:29 +00001075 int fd;
Fred Drakea04eaad2000-06-30 02:46:07 +00001076#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001077 FILE *fp;
Guido van Rossum6b144911995-03-14 15:05:13 +00001078 PyObject *f;
1079
Guido van Rossum43713e52000-02-29 13:59:29 +00001080 if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001081 return NULL;
Guido van Rossum8d665e61996-06-26 18:22:49 +00001082#ifdef MS_WIN32
Guido van Rossum82a5c661998-07-07 20:45:43 +00001083 if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) ||
1084 ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL))
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001085#else
Guido van Rossum82a5c661998-07-07 20:45:43 +00001086 if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL)
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001087#endif
Guido van Rossum82a5c661998-07-07 20:45:43 +00001088 {
Guido van Rossum6b144911995-03-14 15:05:13 +00001089 if (fd >= 0)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001090 SOCKETCLOSE(fd);
Guido van Rossum73624e91994-10-10 17:59:00 +00001091 return PySocket_Err();
Guido van Rossum6b144911995-03-14 15:05:13 +00001092 }
1093 f = PyFile_FromFile(fp, "<socket>", mode, fclose);
1094 if (f != NULL)
1095 PyFile_SetBufSize(f, bufsize);
1096 return f;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001097}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001098
1099static char makefile_doc[] =
1100"makefile([mode[, buffersize]]) -> file object\n\
1101\n\
1102Return a regular file object corresponding to the socket.\n\
1103The mode and buffersize arguments are as for the built-in open() function.";
1104
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001105#endif /* NO_DUP */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001106
Guido van Rossum82a5c661998-07-07 20:45:43 +00001107
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001108/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001109
Guido van Rossum73624e91994-10-10 17:59:00 +00001110static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001111PySocketSock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001112{
Guido van Rossumbe32c891996-06-20 16:25:29 +00001113 int len, n, flags = 0;
Guido van Rossum73624e91994-10-10 17:59:00 +00001114 PyObject *buf;
Guido van Rossum43713e52000-02-29 13:59:29 +00001115 if (!PyArg_ParseTuple(args, "i|i:recv", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001116 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001117 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001118 if (buf == NULL)
1119 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001120 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma597dde1995-01-10 20:56:29 +00001121 n = recv(s->sock_fd, PyString_AsString(buf), len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001122 Py_END_ALLOW_THREADS
Guido van Rossum7c53b771995-09-13 18:39:47 +00001123 if (n < 0) {
1124 Py_DECREF(buf);
Guido van Rossum73624e91994-10-10 17:59:00 +00001125 return PySocket_Err();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001126 }
Guido van Rossum53a9bf81996-06-11 18:35:24 +00001127 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001128 return NULL;
1129 return buf;
1130}
1131
Guido van Rossum82a5c661998-07-07 20:45:43 +00001132static char recv_doc[] =
1133"recv(buffersize[, flags]) -> data\n\
1134\n\
1135Receive up to buffersize bytes from the socket. For the optional flags\n\
1136argument, see the Unix manual. When no data is available, block until\n\
1137at least one byte is available or until the remote end is closed. When\n\
1138the remote end is closed and all data is read, return the empty string.";
1139
Guido van Rossum30a685f1991-06-27 15:51:29 +00001140
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001141/* s.recvfrom(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001142
Guido van Rossum73624e91994-10-10 17:59:00 +00001143static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001144PySocketSock_recvfrom(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001145{
1146 char addrbuf[256];
Barry Warsaw752300b1997-01-03 17:18:10 +00001147 PyObject *buf = NULL;
1148 PyObject *addr = NULL;
1149 PyObject *ret = NULL;
1150
Guido van Rossumff3ab422000-04-24 15:16:03 +00001151 int len, n, flags = 0;
1152 socklen_t addrlen;
Guido van Rossum43713e52000-02-29 13:59:29 +00001153 if (!PyArg_ParseTuple(args, "i|i:recvfrom", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001154 return NULL;
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00001155 if (!getsockaddrlen(s, &addrlen))
1156 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001157 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00001158 if (buf == NULL)
1159 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001160 Py_BEGIN_ALLOW_THREADS
1161 n = recvfrom(s->sock_fd, PyString_AsString(buf), len, flags,
Guido van Rossum8d665e61996-06-26 18:22:49 +00001162#ifndef MS_WINDOWS
Guido van Rossum32c575d1997-12-02 20:37:32 +00001163#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001164 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00001165#else
Barry Warsaw752300b1997-01-03 17:18:10 +00001166 (ANY *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00001167#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001168#else
Barry Warsaw752300b1997-01-03 17:18:10 +00001169 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001170#endif
Barry Warsaw752300b1997-01-03 17:18:10 +00001171 );
Guido van Rossum73624e91994-10-10 17:59:00 +00001172 Py_END_ALLOW_THREADS
Guido van Rossum7c53b771995-09-13 18:39:47 +00001173 if (n < 0) {
1174 Py_DECREF(buf);
Guido van Rossum73624e91994-10-10 17:59:00 +00001175 return PySocket_Err();
Guido van Rossum7c53b771995-09-13 18:39:47 +00001176 }
Guido van Rossum53a9bf81996-06-11 18:35:24 +00001177 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001178 return NULL;
Barry Warsaw752300b1997-01-03 17:18:10 +00001179
1180 if (!(addr = makesockaddr((struct sockaddr *)addrbuf, addrlen)))
1181 goto finally;
1182
Guido van Rossum73624e91994-10-10 17:59:00 +00001183 ret = Py_BuildValue("OO", buf, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00001184 finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001185 Py_XDECREF(addr);
1186 Py_XDECREF(buf);
Guido van Rossum6f5afc91993-02-05 09:46:15 +00001187 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001188}
1189
Guido van Rossum82a5c661998-07-07 20:45:43 +00001190static char recvfrom_doc[] =
1191"recvfrom(buffersize[, flags]) -> (data, address info)\n\
1192\n\
1193Like recv(buffersize, flags) but also return the sender's address info.";
1194
Guido van Rossum30a685f1991-06-27 15:51:29 +00001195
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001196/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001197
Guido van Rossum73624e91994-10-10 17:59:00 +00001198static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001199PySocketSock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001200{
Guido van Rossumff4949e1992-08-05 19:58:53 +00001201 char *buf;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001202 int len, n, flags = 0;
Guido van Rossum43713e52000-02-29 13:59:29 +00001203 if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001204 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001205 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001206 n = send(s->sock_fd, buf, len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00001207 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00001208 if (n < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001209 return PySocket_Err();
1210 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00001211}
1212
Guido van Rossum82a5c661998-07-07 20:45:43 +00001213static char send_doc[] =
1214"send(data[, flags])\n\
1215\n\
1216Send a data string to the socket. For the optional flags\n\
1217argument, see the Unix manual.";
1218
Guido van Rossum30a685f1991-06-27 15:51:29 +00001219
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001220/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001221
Guido van Rossum73624e91994-10-10 17:59:00 +00001222static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001223PySocketSock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001224{
Guido van Rossum73624e91994-10-10 17:59:00 +00001225 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001226 char *buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001227 struct sockaddr *addr;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001228 int addrlen, len, n, flags;
1229 flags = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001230 if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001231 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001232 if (!PyArg_ParseTuple(args, "s#iO:sendto",
1233 &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001234 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001235 }
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001236 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001237 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001238 Py_BEGIN_ALLOW_THREADS
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001239 n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001240 Py_END_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001241 if (n < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001242 return PySocket_Err();
1243 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001244}
1245
Guido van Rossum82a5c661998-07-07 20:45:43 +00001246static char sendto_doc[] =
1247"sendto(data[, flags], address)\n\
1248\n\
1249Like send(data, flags) but allows specifying the destination address.\n\
1250For IP sockets, the address is a pair (hostaddr, port).";
1251
Guido van Rossum30a685f1991-06-27 15:51:29 +00001252
1253/* s.shutdown(how) method */
1254
Guido van Rossum73624e91994-10-10 17:59:00 +00001255static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001256PySocketSock_shutdown(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001257{
1258 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001259 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001260 if (!PyArg_ParseTuple(args, "i:shutdown", &how))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001261 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001262 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001263 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +00001264 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001265 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001266 return PySocket_Err();
1267 Py_INCREF(Py_None);
1268 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001269}
1270
Guido van Rossum82a5c661998-07-07 20:45:43 +00001271static char shutdown_doc[] =
1272"shutdown(flag)\n\
1273\n\
1274Shut down the reading side of the socket (flag == 0), the writing side\n\
1275of the socket (flag == 1), or both ends (flag == 2).";
1276
Guido van Rossum30a685f1991-06-27 15:51:29 +00001277
1278/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001279
Guido van Rossum73624e91994-10-10 17:59:00 +00001280static PyMethodDef PySocketSock_methods[] = {
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001281 {"accept", (PyCFunction)PySocketSock_accept, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001282 accept_doc},
Fred Drake728819a2000-07-01 03:40:12 +00001283 {"bind", (PyCFunction)PySocketSock_bind, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001284 bind_doc},
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001285 {"close", (PyCFunction)PySocketSock_close, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001286 close_doc},
Fred Drake728819a2000-07-01 03:40:12 +00001287 {"connect", (PyCFunction)PySocketSock_connect, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001288 connect_doc},
Fred Drake728819a2000-07-01 03:40:12 +00001289 {"connect_ex", (PyCFunction)PySocketSock_connect_ex, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001290 connect_ex_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00001291#ifndef NO_DUP
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001292 {"dup", (PyCFunction)PySocketSock_dup, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001293 dup_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00001294#endif
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001295 {"fileno", (PyCFunction)PySocketSock_fileno, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001296 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001297#ifdef HAVE_GETPEERNAME
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001298 {"getpeername", (PyCFunction)PySocketSock_getpeername, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001299 getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00001300#endif
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001301 {"getsockname", (PyCFunction)PySocketSock_getsockname, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001302 getsockname_doc},
1303 {"getsockopt", (PyCFunction)PySocketSock_getsockopt, 1,
1304 getsockopt_doc},
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001305 {"listen", (PyCFunction)PySocketSock_listen, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001306 listen_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001307#ifndef NO_DUP
Guido van Rossum82a5c661998-07-07 20:45:43 +00001308 {"makefile", (PyCFunction)PySocketSock_makefile, 1,
1309 makefile_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001310#endif
Guido van Rossum82a5c661998-07-07 20:45:43 +00001311 {"recv", (PyCFunction)PySocketSock_recv, 1,
1312 recv_doc},
1313 {"recvfrom", (PyCFunction)PySocketSock_recvfrom, 1,
1314 recvfrom_doc},
1315 {"send", (PyCFunction)PySocketSock_send, 1,
1316 send_doc},
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001317 {"sendto", (PyCFunction)PySocketSock_sendto, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001318 sendto_doc},
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001319 {"setblocking", (PyCFunction)PySocketSock_setblocking, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001320 setblocking_doc},
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001321 {"setsockopt", (PyCFunction)PySocketSock_setsockopt, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001322 setsockopt_doc},
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001323 {"shutdown", (PyCFunction)PySocketSock_shutdown, 1,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001324 shutdown_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001325 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001326};
1327
Guido van Rossum30a685f1991-06-27 15:51:29 +00001328
Guido van Rossum73624e91994-10-10 17:59:00 +00001329/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00001330 First close the file description. */
1331
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001332static void
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001333PySocketSock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001334{
Guido van Rossumfa972c92000-04-10 12:45:45 +00001335 if (s->sock_fd != -1)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001336 (void) SOCKETCLOSE(s->sock_fd);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001337 PyObject_Del(s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001338}
1339
Guido van Rossum30a685f1991-06-27 15:51:29 +00001340
1341/* Return a socket object's named attribute. */
1342
Guido van Rossum73624e91994-10-10 17:59:00 +00001343static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001344PySocketSock_getattr(PySocketSockObject *s, char *name)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001345{
Guido van Rossum73624e91994-10-10 17:59:00 +00001346 return Py_FindMethod(PySocketSock_methods, (PyObject *) s, name);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001347}
1348
Guido van Rossum30a685f1991-06-27 15:51:29 +00001349
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001350static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001351PySocketSock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001352{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001353 char buf[512];
Fred Drakea04eaad2000-06-30 02:46:07 +00001354#if SIZEOF_SOCKET_T > SIZEOF_LONG
1355 if (s->sock_fd > LONG_MAX) {
1356 /* this can occur on Win64, and actually there is a special
1357 ugly printf formatter for decimal pointer length integer
1358 printing, only bother if necessary*/
1359 PyErr_SetString(PyExc_OverflowError,
1360 "no printf formatter to display the socket descriptor in decimal");
1361 return NULL;
1362 }
1363#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001364 sprintf(buf,
Fred Drakea04eaad2000-06-30 02:46:07 +00001365 "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>",
1366 (long)s->sock_fd, s->sock_family, s->sock_type, s->sock_proto);
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00001367 return PyString_FromString(buf);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001368}
1369
1370
Guido van Rossumb6775db1994-08-01 11:34:53 +00001371/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001372
Guido van Rossum73624e91994-10-10 17:59:00 +00001373static PyTypeObject PySocketSock_Type = {
Guido van Rossum55558541996-05-23 22:54:50 +00001374 PyObject_HEAD_INIT(0) /* Must fill in type value later */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001375 0,
1376 "socket",
Guido van Rossum73624e91994-10-10 17:59:00 +00001377 sizeof(PySocketSockObject),
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001378 0,
Guido van Rossum73624e91994-10-10 17:59:00 +00001379 (destructor)PySocketSock_dealloc, /*tp_dealloc*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001380 0, /*tp_print*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001381 (getattrfunc)PySocketSock_getattr, /*tp_getattr*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001382 0, /*tp_setattr*/
1383 0, /*tp_compare*/
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001384 (reprfunc)PySocketSock_repr, /*tp_repr*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001385 0, /*tp_as_number*/
1386 0, /*tp_as_sequence*/
1387 0, /*tp_as_mapping*/
1388};
1389
Guido van Rossum30a685f1991-06-27 15:51:29 +00001390
Guido van Rossum81194471991-07-27 21:42:02 +00001391/* Python interface to gethostname(). */
1392
1393/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001394static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001395PySocket_gethostname(PyObject *self, PyObject *args)
Guido van Rossum81194471991-07-27 21:42:02 +00001396{
1397 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001398 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001399 if (!PyArg_ParseTuple(args, ":gethostname"))
Guido van Rossum81194471991-07-27 21:42:02 +00001400 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001401 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001402 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00001403 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001404 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001405 return PySocket_Err();
Guido van Rossum81194471991-07-27 21:42:02 +00001406 buf[sizeof buf - 1] = '\0';
Guido van Rossum73624e91994-10-10 17:59:00 +00001407 return PyString_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00001408}
Guido van Rossumff4949e1992-08-05 19:58:53 +00001409
Guido van Rossum82a5c661998-07-07 20:45:43 +00001410static char gethostname_doc[] =
1411"gethostname() -> string\n\
1412\n\
1413Return the current host name.";
1414
Guido van Rossumff4949e1992-08-05 19:58:53 +00001415
Guido van Rossum30a685f1991-06-27 15:51:29 +00001416/* Python interface to gethostbyname(name). */
1417
1418/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001419static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001420PySocket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001421{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001422 char *name;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001423 struct sockaddr_in addrbuf;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001424 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00001425 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001426 if (setipaddr(name, &addrbuf) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001427 return NULL;
1428 return makeipaddr(&addrbuf);
1429}
1430
Guido van Rossum82a5c661998-07-07 20:45:43 +00001431static char gethostbyname_doc[] =
1432"gethostbyname(host) -> address\n\
1433\n\
1434Return the IP address (a string of the form '255.255.255.255') for a host.";
1435
1436
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001437/* Convenience function common to gethostbyname_ex and gethostbyaddr */
1438
1439static PyObject *
1440gethost_common(h, addr)
1441 struct hostent *h;
1442 struct sockaddr_in *addr;
1443{
1444 char **pch;
1445 PyObject *rtn_tuple = (PyObject *)NULL;
1446 PyObject *name_list = (PyObject *)NULL;
1447 PyObject *addr_list = (PyObject *)NULL;
1448 PyObject *tmp;
1449 if (h == NULL) {
1450#ifdef HAVE_HSTRERROR
1451 /* Let's get real error message to return */
1452 extern int h_errno;
1453 PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno));
1454#else
1455 PyErr_SetString(PySocket_Error, "host not found");
1456#endif
1457 return NULL;
1458 }
1459 if ((name_list = PyList_New(0)) == NULL)
1460 goto err;
1461 if ((addr_list = PyList_New(0)) == NULL)
1462 goto err;
1463 for (pch = h->h_aliases; *pch != NULL; pch++) {
1464 int status;
1465 tmp = PyString_FromString(*pch);
1466 if (tmp == NULL)
1467 goto err;
1468 status = PyList_Append(name_list, tmp);
1469 Py_DECREF(tmp);
1470 if (status)
1471 goto err;
1472 }
1473 for (pch = h->h_addr_list; *pch != NULL; pch++) {
1474 int status;
1475 memcpy((char *) &addr->sin_addr, *pch, h->h_length);
1476 tmp = makeipaddr(addr);
1477 if (tmp == NULL)
1478 goto err;
1479 status = PyList_Append(addr_list, tmp);
1480 Py_DECREF(tmp);
1481 if (status)
1482 goto err;
1483 }
1484 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
1485 err:
1486 Py_XDECREF(name_list);
1487 Py_XDECREF(addr_list);
1488 return rtn_tuple;
1489}
1490
1491
1492/* Python interface to gethostbyname_ex(name). */
1493
1494/*ARGSUSED*/
1495static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001496PySocket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001497{
1498 char *name;
1499 struct hostent *h;
1500 struct sockaddr_in addr;
Guido van Rossum955becc1999-03-22 20:14:53 +00001501 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001502#ifdef HAVE_GETHOSTBYNAME_R
1503 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001504#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
1505 struct hostent_data data;
1506#else
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001507 char buf[16384];
1508 int buf_len = (sizeof buf) - 1;
1509 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001510#endif
1511#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001512 int result;
1513#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001514#endif /* HAVE_GETHOSTBYNAME_R */
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001515 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001516 return NULL;
1517 if (setipaddr(name, &addr) < 0)
1518 return NULL;
1519 Py_BEGIN_ALLOW_THREADS
1520#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001521#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001522 result = gethostbyname_r(name, &hp_allocated, buf, buf_len, &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001523#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001524 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001525#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00001526 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001527 result = gethostbyname_r(name, &hp_allocated, &data);
1528 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001529#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001530#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00001531#ifdef USE_GETHOSTBYNAME_LOCK
1532 PyThread_acquire_lock(gethostbyname_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001533#endif
1534 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001535#endif /* HAVE_GETHOSTBYNAME_R */
1536 Py_END_ALLOW_THREADS
Guido van Rossum3baaa131999-03-22 21:44:51 +00001537 ret = gethost_common(h, &addr);
1538#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum955becc1999-03-22 20:14:53 +00001539 PyThread_release_lock(gethostbyname_lock);
1540#endif
1541 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00001542}
1543
1544static char ghbn_ex_doc[] =
1545"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
1546\n\
1547Return the true host name, a list of aliases, and a list of IP addresses,\n\
1548for a host. The host argument is a string giving a host name or IP number.";
1549
1550
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001551/* Python interface to gethostbyaddr(IP). */
1552
1553/*ARGSUSED*/
1554static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001555PySocket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001556{
1557 struct sockaddr_in addr;
1558 char *ip_num;
1559 struct hostent *h;
Guido van Rossum3baaa131999-03-22 21:44:51 +00001560 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001561#ifdef HAVE_GETHOSTBYNAME_R
1562 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001563#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
1564 struct hostent_data data;
1565#else
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001566 char buf[16384];
1567 int buf_len = (sizeof buf) - 1;
1568 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001569#endif
1570#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001571 int result;
1572#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001573#endif /* HAVE_GETHOSTBYNAME_R */
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001574
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001575 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001576 return NULL;
1577 if (setipaddr(ip_num, &addr) < 0)
1578 return NULL;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001579 Py_BEGIN_ALLOW_THREADS
1580#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001581#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001582 result = gethostbyaddr_r((char *)&addr.sin_addr,
1583 sizeof(addr.sin_addr),
1584 AF_INET, &hp_allocated, buf, buf_len,
1585 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001586#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001587 h = gethostbyaddr_r((char *)&addr.sin_addr,
1588 sizeof(addr.sin_addr),
1589 AF_INET,
1590 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001591#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00001592 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00001593 result = gethostbyaddr_r((char *)&addr.sin_addr,
1594 sizeof(addr.sin_addr),
1595 AF_INET, &hp_allocated, &data);
1596 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00001597#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001598#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00001599#ifdef USE_GETHOSTBYNAME_LOCK
1600 PyThread_acquire_lock(gethostbyname_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001601#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001602 h = gethostbyaddr((char *)&addr.sin_addr,
1603 sizeof(addr.sin_addr),
1604 AF_INET);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001605#endif /* HAVE_GETHOSTBYNAME_R */
1606 Py_END_ALLOW_THREADS
Guido van Rossum3baaa131999-03-22 21:44:51 +00001607 ret = gethost_common(h, &addr);
1608#ifdef USE_GETHOSTBYNAME_LOCK
1609 PyThread_release_lock(gethostbyname_lock);
1610#endif
1611 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001612}
1613
Guido van Rossum82a5c661998-07-07 20:45:43 +00001614static char gethostbyaddr_doc[] =
1615"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
1616\n\
1617Return the true host name, a list of aliases, and a list of IP addresses,\n\
1618for a host. The host argument is a string giving a host name or IP number.";
1619
Guido van Rossum30a685f1991-06-27 15:51:29 +00001620
1621/* Python interface to getservbyname(name).
1622 This only returns the port number, since the other info is already
1623 known or not useful (like the list of aliases). */
1624
1625/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001626static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001627PySocket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001628{
Guido van Rossumff4949e1992-08-05 19:58:53 +00001629 char *name, *proto;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001630 struct servent *sp;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001631 if (!PyArg_ParseTuple(args, "ss:getservbyname", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00001632 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001633 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001634 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00001635 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00001636 if (sp == NULL) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001637 PyErr_SetString(PySocket_Error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00001638 return NULL;
1639 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001640 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00001641}
1642
Guido van Rossum82a5c661998-07-07 20:45:43 +00001643static char getservbyname_doc[] =
1644"getservbyname(servicename, protocolname) -> integer\n\
1645\n\
1646Return a port number from a service name and protocol name.\n\
1647The protocol name should be 'tcp' or 'udp'.";
1648
Guido van Rossum30a685f1991-06-27 15:51:29 +00001649
Guido van Rossum3901d851996-12-19 16:35:04 +00001650/* Python interface to getprotobyname(name).
1651 This only returns the protocol number, since the other info is
1652 already known or not useful (like the list of aliases). */
1653
1654/*ARGSUSED*/
1655static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001656PySocket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00001657{
1658 char *name;
1659 struct protoent *sp;
Guido van Rossumbcc20741998-08-04 22:53:56 +00001660#ifdef __BEOS__
1661/* Not available in BeOS yet. - [cjh] */
1662 PyErr_SetString( PySocket_Error, "getprotobyname not supported" );
1663 return NULL;
1664#else
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001665 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
Guido van Rossum3901d851996-12-19 16:35:04 +00001666 return NULL;
1667 Py_BEGIN_ALLOW_THREADS
1668 sp = getprotobyname(name);
1669 Py_END_ALLOW_THREADS
1670 if (sp == NULL) {
1671 PyErr_SetString(PySocket_Error, "protocol not found");
1672 return NULL;
1673 }
1674 return PyInt_FromLong((long) sp->p_proto);
Guido van Rossumbcc20741998-08-04 22:53:56 +00001675#endif
Guido van Rossum3901d851996-12-19 16:35:04 +00001676}
1677
Guido van Rossum82a5c661998-07-07 20:45:43 +00001678static char getprotobyname_doc[] =
1679"getprotobyname(name) -> integer\n\
1680\n\
1681Return the protocol number for the named protocol. (Rarely used.)";
1682
Guido van Rossum3901d851996-12-19 16:35:04 +00001683
Guido van Rossum30a685f1991-06-27 15:51:29 +00001684/* Python interface to socket(family, type, proto).
1685 The third (protocol) argument is optional.
1686 Return a new socket object. */
1687
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001688/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001689static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001690PySocket_socket(PyObject *self, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001691{
Guido van Rossum73624e91994-10-10 17:59:00 +00001692 PySocketSockObject *s;
Fred Drakea04eaad2000-06-30 02:46:07 +00001693 SOCKET_T fd;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001694 int family, type, proto = 0;
Guido van Rossum43713e52000-02-29 13:59:29 +00001695 if (!PyArg_ParseTuple(args, "ii|i:socket", &family, &type, &proto))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001696 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001697 Py_BEGIN_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001698 fd = socket(family, type, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00001699 Py_END_ALLOW_THREADS
Guido van Rossum6f489d91996-06-28 20:15:15 +00001700#ifdef MS_WINDOWS
Guido van Rossumd639d4d1996-06-12 04:04:22 +00001701 if (fd == INVALID_SOCKET)
1702#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001703 if (fd < 0)
Guido van Rossumd639d4d1996-06-12 04:04:22 +00001704#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001705 return PySocket_Err();
1706 s = PySocketSock_New(fd, family, type, proto);
Guido van Rossum30a685f1991-06-27 15:51:29 +00001707 /* If the object can't be created, don't forget to close the
1708 file descriptor again! */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001709 if (s == NULL)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001710 (void) SOCKETCLOSE(fd);
Guido van Rossum81194471991-07-27 21:42:02 +00001711 /* From now on, ignore SIGPIPE and let the error checking
1712 do the work. */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001713#ifdef SIGPIPE
Guido van Rossum81194471991-07-27 21:42:02 +00001714 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001715#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001716 return (PyObject *) s;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001717}
1718
Guido van Rossum82a5c661998-07-07 20:45:43 +00001719static char socket_doc[] =
1720"socket(family, type[, proto]) -> socket object\n\
1721\n\
1722Open a socket of the given type. The family argument specifies the\n\
1723address family; it is normally AF_INET, sometimes AF_UNIX.\n\
1724The type argument specifies whether this is a stream (SOCK_STREAM)\n\
1725or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\
1726specifying the default protocol.";
1727
1728
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001729#ifndef NO_DUP
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001730/* Create a socket object from a numeric file description.
1731 Useful e.g. if stdin is a socket.
1732 Additional arguments as for socket(). */
1733
1734/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001735static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001736PySocket_fromfd(PyObject *self, PyObject *args)
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001737{
Guido van Rossum73624e91994-10-10 17:59:00 +00001738 PySocketSockObject *s;
Fred Drakea04eaad2000-06-30 02:46:07 +00001739 SOCKET_T fd;
1740 int family, type, proto = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001741 if (!PyArg_ParseTuple(args, "iii|i:fromfd",
1742 &fd, &family, &type, &proto))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001743 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +00001744 /* Dup the fd so it and the socket can be closed independently */
1745 fd = dup(fd);
1746 if (fd < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001747 return PySocket_Err();
1748 s = PySocketSock_New(fd, family, type, proto);
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001749 /* From now on, ignore SIGPIPE and let the error checking
1750 do the work. */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001751#ifdef SIGPIPE
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001752 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001753#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001754 return (PyObject *) s;
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001755}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001756
1757static char fromfd_doc[] =
1758"fromfd(fd, family, type[, proto]) -> socket object\n\
1759\n\
1760Create a socket object from the given file descriptor.\n\
1761The remaining arguments are the same as for socket().";
1762
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001763#endif /* NO_DUP */
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001764
Guido van Rossum82a5c661998-07-07 20:45:43 +00001765
Guido van Rossum006bf911996-06-12 04:04:55 +00001766static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001767PySocket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00001768{
1769 int x1, x2;
1770
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001771 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00001772 return NULL;
1773 }
1774 x2 = (int)ntohs((short)x1);
1775 return PyInt_FromLong(x2);
1776}
1777
Guido van Rossum82a5c661998-07-07 20:45:43 +00001778static char ntohs_doc[] =
1779"ntohs(integer) -> integer\n\
1780\n\
1781Convert a 16-bit integer from network to host byte order.";
1782
1783
Guido van Rossum006bf911996-06-12 04:04:55 +00001784static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001785PySocket_ntohl(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00001786{
1787 int x1, x2;
1788
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001789 if (!PyArg_ParseTuple(args, "i:ntohl", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00001790 return NULL;
1791 }
1792 x2 = ntohl(x1);
1793 return PyInt_FromLong(x2);
1794}
1795
Guido van Rossum82a5c661998-07-07 20:45:43 +00001796static char ntohl_doc[] =
1797"ntohl(integer) -> integer\n\
1798\n\
1799Convert a 32-bit integer from network to host byte order.";
1800
1801
Guido van Rossum006bf911996-06-12 04:04:55 +00001802static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001803PySocket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00001804{
1805 int x1, x2;
1806
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001807 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00001808 return NULL;
1809 }
1810 x2 = (int)htons((short)x1);
1811 return PyInt_FromLong(x2);
1812}
1813
Guido van Rossum82a5c661998-07-07 20:45:43 +00001814static char htons_doc[] =
1815"htons(integer) -> integer\n\
1816\n\
1817Convert a 16-bit integer from host to network byte order.";
1818
1819
Guido van Rossum006bf911996-06-12 04:04:55 +00001820static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001821PySocket_htonl(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00001822{
1823 int x1, x2;
1824
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001825 if (!PyArg_ParseTuple(args, "i:htonl", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00001826 return NULL;
1827 }
1828 x2 = htonl(x1);
1829 return PyInt_FromLong(x2);
1830}
1831
Guido van Rossum82a5c661998-07-07 20:45:43 +00001832static char htonl_doc[] =
1833"htonl(integer) -> integer\n\
1834\n\
1835Convert a 32-bit integer from host to network byte order.";
1836
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001837/*
1838 * socket.inet_aton() and socket.inet_ntoa() functions
1839 *
1840 * written 20 Aug 1999 by Ben Gertzfield <che@debian.org> <- blame him!
1841 *
1842 */
1843
1844static char inet_aton_doc[] =
1845"inet_aton(string) -> packed 32-bit IP representation\n\
1846\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00001847Convert an IP address in string format (123.45.67.89) to the 32-bit packed\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001848binary format used in low-level network functions.";
1849
1850static PyObject*
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001851PySocket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001852{
Guido van Rossuma2e48551999-09-09 15:42:59 +00001853#ifndef INADDR_NONE
1854#define INADDR_NONE (-1)
1855#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001856
Guido van Rossumc6a164b1999-08-20 19:11:27 +00001857 /* Have to use inet_addr() instead */
1858 char *ip_addr;
1859 long packed_addr;
1860
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001861 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) {
Guido van Rossumc6a164b1999-08-20 19:11:27 +00001862 return NULL;
1863 }
Guido van Rossumff3ab422000-04-24 15:16:03 +00001864#ifdef USE_GUSI1
Guido van Rossum1b6e4631999-11-04 18:22:29 +00001865 packed_addr = (long)inet_addr(ip_addr).s_addr;
1866#else
Guido van Rossumc6a164b1999-08-20 19:11:27 +00001867 packed_addr = inet_addr(ip_addr);
Guido van Rossum1b6e4631999-11-04 18:22:29 +00001868#endif
Guido van Rossumc6a164b1999-08-20 19:11:27 +00001869
1870 if (packed_addr == INADDR_NONE) { /* invalid address */
1871 PyErr_SetString(PySocket_Error,
1872 "illegal IP address string passed to inet_aton");
1873 return NULL;
1874 }
1875
1876 return PyString_FromStringAndSize((char *) &packed_addr,
1877 sizeof(packed_addr));
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001878}
1879
1880static char inet_ntoa_doc[] =
Fred Drakee0661342000-03-07 14:05:16 +00001881"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001882\n\
1883Convert an IP address from 32-bit packed binary format to string format";
1884
1885static PyObject*
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001886PySocket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001887{
1888 char *packed_str;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00001889 int addr_len;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001890 struct in_addr packed_addr;
1891
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001892 if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) {
Guido van Rossum5c9eb211999-08-20 18:21:51 +00001893 return NULL;
1894 }
1895
1896 if (addr_len != sizeof(packed_addr)) {
1897 PyErr_SetString(PySocket_Error,
1898 "packed IP wrong length for inet_ntoa");
1899 return NULL;
1900 }
1901
1902 memcpy(&packed_addr, packed_str, addr_len);
1903
1904 return PyString_FromString(inet_ntoa(packed_addr));
1905}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001906
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001907
1908#ifdef USE_SSL
1909
1910/* This is a C function to be called for new object initialization */
1911static SSLObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00001912newSSLObject(PySocketSockObject *Sock, char *key_file, char *cert_file)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001913{
1914 SSLObject *self;
1915 char *str;
1916
1917#if 0
1918 meth=SSLv23_client_method();
1919 meth=SSLv3_client_method();
1920 meth=SSLv2_client_method();
1921#endif
1922
Guido van Rossumb18618d2000-05-03 23:44:39 +00001923 self = PyObject_New(SSLObject, &SSL_Type); /* Create new object */
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001924 if (self == NULL){
1925 PyErr_SetObject(SSLErrorObject,
1926 PyString_FromString("newSSLObject error"));
1927 return NULL;
1928 }
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001929 memset(self->server, '\0', sizeof(char) * 256);
1930 memset(self->issuer, '\0', sizeof(char) * 256);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001931
1932 self->x_attr = PyDict_New();
1933 self->ctx = SSL_CTX_new(SSLv23_method()); /* Set up context */
1934 if (self->ctx == NULL) {
1935 PyErr_SetObject(SSLErrorObject,
1936 PyString_FromString("SSL_CTX_new error"));
Guido van Rossumb18618d2000-05-03 23:44:39 +00001937 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001938 return NULL;
1939 }
1940
1941 if ( (key_file && !cert_file) || (!key_file && cert_file) )
1942 {
1943 PyErr_SetObject(SSLErrorObject,
1944 PyString_FromString(
1945 "Both the key & certificate files must be specified"));
Guido van Rossumb18618d2000-05-03 23:44:39 +00001946 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001947 return NULL;
1948 }
1949
1950 if (key_file && cert_file)
1951 {
1952 if (SSL_CTX_use_PrivateKey_file(self->ctx, key_file,
1953 SSL_FILETYPE_PEM) < 1)
1954 {
1955 PyErr_SetObject(SSLErrorObject,
1956 PyString_FromString(
1957 "SSL_CTX_use_PrivateKey_file error"));
Guido van Rossumb18618d2000-05-03 23:44:39 +00001958 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001959 return NULL;
1960 }
1961
1962 if (SSL_CTX_use_certificate_chain_file(self->ctx,
1963 cert_file) < 1)
1964 {
1965 PyErr_SetObject(SSLErrorObject,
1966 PyString_FromString(
1967 "SSL_CTX_use_certificate_chain_file error"));
Guido van Rossumb18618d2000-05-03 23:44:39 +00001968 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001969 return NULL;
1970 }
1971 }
1972
1973 SSL_CTX_set_verify(self->ctx,
1974 SSL_VERIFY_NONE, NULL); /* set verify lvl */
1975 self->ssl = SSL_new(self->ctx); /* New ssl struct */
1976 SSL_set_fd(self->ssl, Sock->sock_fd); /* Set the socket for SSL */
1977 SSL_set_connect_state(self->ssl);
1978
1979 if ((SSL_connect(self->ssl)) == -1) {
1980 /* Actually negotiate SSL connection */
1981 PyErr_SetObject(SSLErrorObject,
1982 PyString_FromString("SSL_connect error"));
Guido van Rossumb18618d2000-05-03 23:44:39 +00001983 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001984 return NULL;
1985 }
1986 self->ssl->debug = 1;
1987
1988 if ((self->server_cert = SSL_get_peer_certificate(self->ssl))) {
1989 X509_NAME_oneline(X509_get_subject_name(self->server_cert),
1990 self->server, 256);
1991 X509_NAME_oneline(X509_get_issuer_name(self->server_cert),
1992 self->issuer, 256);
1993 }
1994 self->x_attr = NULL;
1995 self->Socket = Sock;
1996 Py_INCREF(self->Socket);
1997 return self;
1998}
1999
2000/* This is the Python function called for new object initialization */
2001static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002002PySocket_ssl(PyObject *self, PyObject *args)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002003{
2004 SSLObject *rv;
2005 PySocketSockObject *Sock;
2006 char *key_file;
2007 char *cert_file;
2008
Guido van Rossum43713e52000-02-29 13:59:29 +00002009 if (!PyArg_ParseTuple(args, "O!zz:ssl",
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002010 &PySocketSock_Type, (PyObject*)&Sock,
2011 &key_file, &cert_file) )
2012 return NULL;
2013
2014 rv = newSSLObject(Sock, key_file, cert_file);
2015 if ( rv == NULL )
2016 return NULL;
2017 return (PyObject *)rv;
2018}
2019
2020static char ssl_doc[] =
2021"ssl(socket, keyfile, certfile) -> sslobject";
2022
2023static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002024SSL_server(SSLObject *self, PyObject *args)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002025{
2026 return PyString_FromString(self->server);
2027}
2028
2029static PyObject *
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002030SSL_issuer(SSLObject *self, PyObject *args)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002031{
2032 return PyString_FromString(self->issuer);
2033}
2034
2035
2036/* SSL object methods */
2037
2038static PyMethodDef SSLMethods[] = {
2039 { "write", (PyCFunction)SSL_SSLwrite, 1 },
2040 { "read", (PyCFunction)SSL_SSLread, 1 },
2041 { "server", (PyCFunction)SSL_server, 1 },
2042 { "issuer", (PyCFunction)SSL_issuer, 1 },
2043 { NULL, NULL}
2044};
2045
2046static void SSL_dealloc(SSLObject *self)
2047{
2048 if (self->server_cert) /* Possible not to have one? */
2049 X509_free (self->server_cert);
2050 SSL_CTX_free(self->ctx);
2051 SSL_free(self->ssl);
2052 Py_XDECREF(self->x_attr);
2053 Py_XDECREF(self->Socket);
Guido van Rossumb18618d2000-05-03 23:44:39 +00002054 PyObject_Del(self);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002055}
2056
2057static PyObject *SSL_getattr(SSLObject *self, char *name)
2058{
2059 return Py_FindMethod(SSLMethods, (PyObject *)self, name);
2060}
2061
2062staticforward PyTypeObject SSL_Type = {
2063 PyObject_HEAD_INIT(&PyType_Type)
2064 0, /*ob_size*/
2065 "SSL", /*tp_name*/
2066 sizeof(SSLObject), /*tp_basicsize*/
2067 0, /*tp_itemsize*/
2068 /* methods */
2069 (destructor)SSL_dealloc, /*tp_dealloc*/
2070 0, /*tp_print*/
2071 (getattrfunc)SSL_getattr, /*tp_getattr*/
2072 0, /*tp_setattr*/
2073 0, /*tp_compare*/
2074 0, /*tp_repr*/
2075 0, /*tp_as_number*/
2076 0, /*tp_as_sequence*/
2077 0, /*tp_as_mapping*/
2078 0, /*tp_hash*/
2079};
2080
2081
2082
2083static PyObject *SSL_SSLwrite(SSLObject *self, PyObject *args)
2084{
2085 char *data;
Fred Drakea04eaad2000-06-30 02:46:07 +00002086 size_t len = 0;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002087
Guido van Rossum43713e52000-02-29 13:59:29 +00002088 if (!PyArg_ParseTuple(args, "s|i:write", &data, &len))
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002089 return NULL;
2090
2091 if (!len)
2092 len = strlen(data);
2093
2094 len = SSL_write(self->ssl, data, len);
2095 return PyInt_FromLong((long)len);
2096}
2097
2098static PyObject *SSL_SSLread(SSLObject *self, PyObject *args)
2099{
2100 PyObject *buf;
2101 int count = 0;
2102 int len = 1024;
2103 int res;
2104
Guido van Rossum43713e52000-02-29 13:59:29 +00002105 PyArg_ParseTuple(args, "|i:read", &len);
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002106
2107 if (!(buf = PyString_FromStringAndSize((char *) 0, len)))
2108 return NULL; /* Error object should already be set */
2109
2110 count = SSL_read(self->ssl, PyString_AsString(buf), len);
2111 res = SSL_get_error(self->ssl, count);
2112
2113 switch (res) {
2114 case 0: /* Good return value! */
2115 break;
2116 case 6:
2117 PyErr_SetString(SSLErrorObject, "EOF");
2118 Py_DECREF(buf);
2119 return NULL;
2120 break;
2121 case 5:
2122 default:
2123 return PyErr_SetFromErrno(SSLErrorObject);
2124 break;
2125 }
2126
2127 fflush(stderr);
2128
2129 if (count < 0) {
2130 Py_DECREF(buf);
2131 return PyErr_SetFromErrno(SSLErrorObject);
2132 }
2133
2134 if (count != len && _PyString_Resize(&buf, count) < 0)
2135 return NULL;
2136 return buf;
2137}
2138
2139#endif /* USE_SSL */
2140
2141
Guido van Rossum30a685f1991-06-27 15:51:29 +00002142/* List of functions exported by this module. */
2143
Guido van Rossum73624e91994-10-10 17:59:00 +00002144static PyMethodDef PySocket_methods[] = {
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002145 {"gethostbyname", PySocket_gethostbyname, 1, gethostbyname_doc},
2146 {"gethostbyname_ex", PySocket_gethostbyname_ex, 1, ghbn_ex_doc},
2147 {"gethostbyaddr", PySocket_gethostbyaddr, 1, gethostbyaddr_doc},
2148 {"gethostname", PySocket_gethostname, 1, gethostname_doc},
2149 {"getservbyname", PySocket_getservbyname, 1, getservbyname_doc},
2150 {"getprotobyname", PySocket_getprotobyname, 1,getprotobyname_doc},
Guido van Rossum82a5c661998-07-07 20:45:43 +00002151 {"socket", PySocket_socket, 1, socket_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002152#ifndef NO_DUP
Guido van Rossum82a5c661998-07-07 20:45:43 +00002153 {"fromfd", PySocket_fromfd, 1, fromfd_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002154#endif
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002155 {"ntohs", PySocket_ntohs, 1, ntohs_doc},
2156 {"ntohl", PySocket_ntohl, 1, ntohl_doc},
2157 {"htons", PySocket_htons, 1, htons_doc},
2158 {"htonl", PySocket_htonl, 1, htonl_doc},
2159 {"inet_aton", PySocket_inet_aton, 1, inet_aton_doc},
2160 {"inet_ntoa", PySocket_inet_ntoa, 1, inet_ntoa_doc},
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002161#ifdef USE_SSL
2162 {"ssl", PySocket_ssl, 1, ssl_doc},
2163#endif /* USE_SSL */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002164 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002165};
2166
Guido van Rossum30a685f1991-06-27 15:51:29 +00002167
2168/* Convenience routine to export an integer value.
Barry Warsaw752300b1997-01-03 17:18:10 +00002169 *
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002170 * Errors are silently ignored, for better or for worse...
Barry Warsaw752300b1997-01-03 17:18:10 +00002171 */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002172static void
Fredrik Lundh3c5d43d2000-07-09 15:09:56 +00002173insint(PyObject *d, char *name, int value)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002174{
Guido van Rossum73624e91994-10-10 17:59:00 +00002175 PyObject *v = PyInt_FromLong((long) value);
Barry Warsaw752300b1997-01-03 17:18:10 +00002176 if (!v || PyDict_SetItemString(d, name, v))
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002177 PyErr_Clear();
Barry Warsaw752300b1997-01-03 17:18:10 +00002178
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002179 Py_XDECREF(v);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002180}
2181
Guido van Rossum30a685f1991-06-27 15:51:29 +00002182
Guido van Rossum8d665e61996-06-26 18:22:49 +00002183#ifdef MS_WINDOWS
Guido van Rossumbe32c891996-06-20 16:25:29 +00002184
2185/* Additional initialization and cleanup for NT/Windows */
2186
2187static void
2188NTcleanup()
2189{
2190 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00002191}
2192
2193static int
2194NTinit()
2195{
2196 WSADATA WSAData;
2197 int ret;
2198 char buf[100];
2199 ret = WSAStartup(0x0101, &WSAData);
2200 switch (ret) {
2201 case 0: /* no error */
2202 atexit(NTcleanup);
2203 return 1;
2204 case WSASYSNOTREADY:
2205 PyErr_SetString(PyExc_ImportError,
2206 "WSAStartup failed: network not ready");
2207 break;
2208 case WSAVERNOTSUPPORTED:
2209 case WSAEINVAL:
2210 PyErr_SetString(PyExc_ImportError,
2211 "WSAStartup failed: requested version not supported");
2212 break;
2213 default:
2214 sprintf(buf, "WSAStartup failed: error code %d", ret);
2215 PyErr_SetString(PyExc_ImportError, buf);
2216 break;
2217 }
2218 return 0;
2219}
2220
Guido van Rossum8d665e61996-06-26 18:22:49 +00002221#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00002222
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002223#if defined(PYOS_OS2)
2224
2225/* Additional initialization and cleanup for OS/2 */
2226
2227static void
2228OS2cleanup()
2229{
2230 /* No cleanup is necessary for OS/2 Sockets */
2231}
2232
2233static int
2234OS2init()
2235{
2236 char reason[64];
2237 int rc = sock_init();
2238
2239 if (rc == 0) {
Guido van Rossum32c575d1997-12-02 20:37:32 +00002240 atexit(OS2cleanup);
2241 return 1; /* Indicate Success */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002242 }
2243
2244 sprintf(reason, "OS/2 TCP/IP Error# %d", sock_errno());
2245 PyErr_SetString(PyExc_ImportError, reason);
2246
Guido van Rossum32c575d1997-12-02 20:37:32 +00002247 return 0; /* Indicate Failure */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002248}
2249
2250#endif /* PYOS_OS2 */
2251
Guido van Rossum30a685f1991-06-27 15:51:29 +00002252/* Initialize this module.
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002253 * This is called when the first 'import socket' is done,
2254 * via a table in config.c, if config.c is compiled with USE_SOCKET
2255 * defined.
2256 *
2257 * For MS_WINDOWS (which means any Windows variant), this module
2258 * is actually called "_socket", and there's a wrapper "socket.py"
2259 * which implements some missing functionality (such as makefile(),
2260 * dup() and fromfd()). The import of "_socket" may fail with an
2261 * ImportError exception if initialization of WINSOCK fails. When
2262 * WINSOCK is initialized succesfully, a call to WSACleanup() is
2263 * scheduled to be made at exit time.
2264 *
2265 * For OS/2, this module is also called "_socket" and uses a wrapper
2266 * "socket.py" which implements that functionality that is missing
2267 * when PC operating systems don't put socket descriptors in the
2268 * operating system's filesystem layer.
2269 */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002270
Guido van Rossum82a5c661998-07-07 20:45:43 +00002271static char module_doc[] =
2272"This module provides socket operations and some related functions.\n\
2273On Unix, it supports IP (Internet Protocol) and Unix domain sockets.\n\
2274On other systems, it only supports IP.\n\
2275\n\
2276Functions:\n\
2277\n\
2278socket() -- create a new socket object\n\
2279fromfd() -- create a socket object from an open file descriptor (*)\n\
2280gethostname() -- return the current hostname\n\
2281gethostbyname() -- map a hostname to its IP number\n\
2282gethostbyaddr() -- map an IP number or hostname to DNS info\n\
2283getservbyname() -- map a service name and a protocol name to a port number\n\
2284getprotobyname() -- mape a protocol name (e.g. 'tcp') to a number\n\
2285ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order\n\
2286htons(), htonl() -- convert 16, 32 bit int from host to network byte order\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00002287inet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format\n\
2288inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89)\n\
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002289ssl() -- secure socket layer support (only available if configured)\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002290\n\
2291(*) not available on all platforms!)\n\
2292\n\
2293Special objects:\n\
2294\n\
2295SocketType -- type object for socket objects\n\
2296error -- exception raised for I/O errors\n\
2297\n\
2298Integer constants:\n\
2299\n\
2300AF_INET, AF_UNIX -- socket domains (first argument to socket() call)\n\
2301SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)\n\
2302\n\
2303Many other constants may be defined; these may be used in calls to\n\
2304the setsockopt() and getsockopt() methods.\n\
2305";
2306
2307static char sockettype_doc[] =
2308"A socket represents one endpoint of a network connection.\n\
2309\n\
2310Methods:\n\
2311\n\
2312accept() -- accept a connection, returning new socket and client address\n\
2313bind() -- bind the socket to a local address\n\
2314close() -- close the socket\n\
2315connect() -- connect the socket to a remote address\n\
2316connect_ex() -- connect, return an error code instead of an exception \n\
2317dup() -- return a new socket object identical to the current one (*)\n\
2318fileno() -- return underlying file descriptor\n\
2319getpeername() -- return remote address (*)\n\
2320getsockname() -- return local address\n\
2321getsockopt() -- get socket options\n\
2322listen() -- start listening for incoming connections\n\
2323makefile() -- return a file object corresponding tot the socket (*)\n\
2324recv() -- receive data\n\
2325recvfrom() -- receive data and sender's address\n\
2326send() -- send data\n\
2327sendto() -- send data to a given address\n\
2328setblocking() -- set or clear the blocking I/O flag\n\
2329setsockopt() -- set socket options\n\
2330shutdown() -- shut down traffic in one or both directions\n\
2331\n\
2332(*) not available on all platforms!)";
2333
Guido van Rossum3886bb61998-12-04 18:50:17 +00002334DL_EXPORT(void)
Guido van Rossumbcc20741998-08-04 22:53:56 +00002335#if defined(MS_WINDOWS) || defined(PYOS_OS2) || defined(__BEOS__)
Guido van Rossumbe32c891996-06-20 16:25:29 +00002336init_socket()
2337#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002338initsocket()
Guido van Rossumbe32c891996-06-20 16:25:29 +00002339#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002340{
Guido van Rossum73624e91994-10-10 17:59:00 +00002341 PyObject *m, *d;
Guido van Rossum8d665e61996-06-26 18:22:49 +00002342#ifdef MS_WINDOWS
Guido van Rossumbe32c891996-06-20 16:25:29 +00002343 if (!NTinit())
2344 return;
Guido van Rossum82a5c661998-07-07 20:45:43 +00002345 m = Py_InitModule3("_socket", PySocket_methods, module_doc);
Guido van Rossumbe32c891996-06-20 16:25:29 +00002346#else
Guido van Rossum32c575d1997-12-02 20:37:32 +00002347#if defined(__TOS_OS2__)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002348 if (!OS2init())
2349 return;
Guido van Rossum82a5c661998-07-07 20:45:43 +00002350 m = Py_InitModule3("_socket", PySocket_methods, module_doc);
Guido van Rossum32c575d1997-12-02 20:37:32 +00002351#else
Guido van Rossumbcc20741998-08-04 22:53:56 +00002352#if defined(__BEOS__)
2353 m = Py_InitModule3("_socket", PySocket_methods, module_doc);
2354#else
Guido van Rossum82a5c661998-07-07 20:45:43 +00002355 m = Py_InitModule3("socket", PySocket_methods, module_doc);
Guido van Rossumbcc20741998-08-04 22:53:56 +00002356#endif /* __BEOS__ */
Guido van Rossum32c575d1997-12-02 20:37:32 +00002357#endif
Guido van Rossumbe32c891996-06-20 16:25:29 +00002358#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00002359 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002360 PySocket_Error = PyErr_NewException("socket.error", NULL, NULL);
2361 if (PySocket_Error == NULL)
2362 return;
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00002363#ifdef USE_SSL
2364 SSL_load_error_strings();
2365 SSLeay_add_ssl_algorithms();
2366 SSLErrorObject = PyErr_NewException("socket.sslerror", NULL, NULL);
2367 if (SSLErrorObject == NULL)
2368 return;
2369 PyDict_SetItemString(d, "sslerror", SSLErrorObject);
2370 Py_INCREF(&SSL_Type);
2371 if (PyDict_SetItemString(d, "SSLType",
2372 (PyObject *)&SSL_Type) != 0)
2373 return;
2374#endif /* USE_SSL */
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002375 PyDict_SetItemString(d, "error", PySocket_Error);
Guido van Rossum3b4b6fc1997-05-21 14:37:37 +00002376 PySocketSock_Type.ob_type = &PyType_Type;
Guido van Rossum82a5c661998-07-07 20:45:43 +00002377 PySocketSock_Type.tp_doc = sockettype_doc;
Guido van Rossum3b4b6fc1997-05-21 14:37:37 +00002378 Py_INCREF(&PySocketSock_Type);
Guido van Rossum91ba64d1997-06-02 22:18:09 +00002379 if (PyDict_SetItemString(d, "SocketType",
Guido van Rossum3b4b6fc1997-05-21 14:37:37 +00002380 (PyObject *)&PySocketSock_Type) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002381 return;
Guido van Rossum09be4091999-08-09 14:40:40 +00002382
2383 /* Address families (we only support AF_INET and AF_UNIX) */
2384#ifdef AF_UNSPEC
2385 insint(d, "AF_UNSPEC", AF_UNSPEC);
2386#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002387 insint(d, "AF_INET", AF_INET);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002388#ifdef AF_UNIX
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002389 insint(d, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002390#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00002391#ifdef AF_AX25
2392 insint(d, "AF_AX25", AF_AX25); /* Amateur Radio AX.25 */
2393#endif
2394#ifdef AF_IPX
2395 insint(d, "AF_IPX", AF_IPX); /* Novell IPX */
2396#endif
2397#ifdef AF_APPLETALK
2398 insint(d, "AF_APPLETALK", AF_APPLETALK); /* Appletalk DDP */
2399#endif
2400#ifdef AF_NETROM
2401 insint(d, "AF_NETROM", AF_NETROM); /* Amateur radio NetROM */
2402#endif
2403#ifdef AF_BRIDGE
2404 insint(d, "AF_BRIDGE", AF_BRIDGE); /* Multiprotocol bridge */
2405#endif
2406#ifdef AF_AAL5
2407 insint(d, "AF_AAL5", AF_AAL5); /* Reserved for Werner's ATM */
2408#endif
2409#ifdef AF_X25
2410 insint(d, "AF_X25", AF_X25); /* Reserved for X.25 project */
2411#endif
2412#ifdef AF_INET6
2413 insint(d, "AF_INET6", AF_INET6); /* IP version 6 */
2414#endif
2415#ifdef AF_ROSE
2416 insint(d, "AF_ROSE", AF_ROSE); /* Amateur Radio X.25 PLP */
2417#endif
2418
2419 /* Socket types */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002420 insint(d, "SOCK_STREAM", SOCK_STREAM);
2421 insint(d, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00002422#ifndef __BEOS__
2423/* We have incomplete socket support. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002424 insint(d, "SOCK_RAW", SOCK_RAW);
2425 insint(d, "SOCK_SEQPACKET", SOCK_SEQPACKET);
2426 insint(d, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00002427#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002428
2429#ifdef SO_DEBUG
2430 insint(d, "SO_DEBUG", SO_DEBUG);
2431#endif
2432#ifdef SO_ACCEPTCONN
2433 insint(d, "SO_ACCEPTCONN", SO_ACCEPTCONN);
2434#endif
2435#ifdef SO_REUSEADDR
2436 insint(d, "SO_REUSEADDR", SO_REUSEADDR);
2437#endif
2438#ifdef SO_KEEPALIVE
2439 insint(d, "SO_KEEPALIVE", SO_KEEPALIVE);
2440#endif
2441#ifdef SO_DONTROUTE
2442 insint(d, "SO_DONTROUTE", SO_DONTROUTE);
2443#endif
2444#ifdef SO_BROADCAST
2445 insint(d, "SO_BROADCAST", SO_BROADCAST);
2446#endif
2447#ifdef SO_USELOOPBACK
2448 insint(d, "SO_USELOOPBACK", SO_USELOOPBACK);
2449#endif
2450#ifdef SO_LINGER
2451 insint(d, "SO_LINGER", SO_LINGER);
2452#endif
2453#ifdef SO_OOBINLINE
2454 insint(d, "SO_OOBINLINE", SO_OOBINLINE);
2455#endif
2456#ifdef SO_REUSEPORT
2457 insint(d, "SO_REUSEPORT", SO_REUSEPORT);
2458#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002459#ifdef SO_SNDBUF
2460 insint(d, "SO_SNDBUF", SO_SNDBUF);
2461#endif
2462#ifdef SO_RCVBUF
2463 insint(d, "SO_RCVBUF", SO_RCVBUF);
2464#endif
2465#ifdef SO_SNDLOWAT
2466 insint(d, "SO_SNDLOWAT", SO_SNDLOWAT);
2467#endif
2468#ifdef SO_RCVLOWAT
2469 insint(d, "SO_RCVLOWAT", SO_RCVLOWAT);
2470#endif
2471#ifdef SO_SNDTIMEO
2472 insint(d, "SO_SNDTIMEO", SO_SNDTIMEO);
2473#endif
2474#ifdef SO_RCVTIMEO
2475 insint(d, "SO_RCVTIMEO", SO_RCVTIMEO);
2476#endif
2477#ifdef SO_ERROR
2478 insint(d, "SO_ERROR", SO_ERROR);
2479#endif
2480#ifdef SO_TYPE
2481 insint(d, "SO_TYPE", SO_TYPE);
2482#endif
2483
2484 /* Maximum number of connections for "listen" */
2485#ifdef SOMAXCONN
2486 insint(d, "SOMAXCONN", SOMAXCONN);
2487#else
2488 insint(d, "SOMAXCONN", 5); /* Common value */
2489#endif
2490
2491 /* Flags for send, recv */
2492#ifdef MSG_OOB
2493 insint(d, "MSG_OOB", MSG_OOB);
2494#endif
2495#ifdef MSG_PEEK
2496 insint(d, "MSG_PEEK", MSG_PEEK);
2497#endif
2498#ifdef MSG_DONTROUTE
2499 insint(d, "MSG_DONTROUTE", MSG_DONTROUTE);
2500#endif
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00002501#ifdef MSG_DONTWAIT
Guido van Rossum14f3f592000-04-25 21:53:58 +00002502 insint(d, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00002503#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002504#ifdef MSG_EOR
2505 insint(d, "MSG_EOR", MSG_EOR);
2506#endif
2507#ifdef MSG_TRUNC
2508 insint(d, "MSG_TRUNC", MSG_TRUNC);
2509#endif
2510#ifdef MSG_CTRUNC
2511 insint(d, "MSG_CTRUNC", MSG_CTRUNC);
2512#endif
2513#ifdef MSG_WAITALL
2514 insint(d, "MSG_WAITALL", MSG_WAITALL);
2515#endif
2516#ifdef MSG_BTAG
2517 insint(d, "MSG_BTAG", MSG_BTAG);
2518#endif
2519#ifdef MSG_ETAG
2520 insint(d, "MSG_ETAG", MSG_ETAG);
2521#endif
2522
2523 /* Protocol level and numbers, usable for [gs]etsockopt */
2524#ifdef SOL_SOCKET
2525 insint(d, "SOL_SOCKET", SOL_SOCKET);
2526#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00002527#ifdef SOL_IP
2528 insint(d, "SOL_IP", SOL_IP);
2529#else
2530 insint(d, "SOL_IP", 0);
2531#endif
2532#ifdef SOL_IPX
2533 insint(d, "SOL_IPX", SOL_IPX);
2534#endif
2535#ifdef SOL_AX25
2536 insint(d, "SOL_AX25", SOL_AX25);
2537#endif
2538#ifdef SOL_ATALK
2539 insint(d, "SOL_ATALK", SOL_ATALK);
2540#endif
2541#ifdef SOL_NETROM
2542 insint(d, "SOL_NETROM", SOL_NETROM);
2543#endif
2544#ifdef SOL_ROSE
2545 insint(d, "SOL_ROSE", SOL_ROSE);
2546#endif
2547#ifdef SOL_TCP
2548 insint(d, "SOL_TCP", SOL_TCP);
2549#else
2550 insint(d, "SOL_TCP", 6);
2551#endif
2552#ifdef SOL_UDP
2553 insint(d, "SOL_UDP", SOL_UDP);
2554#else
2555 insint(d, "SOL_UDP", 17);
2556#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002557#ifdef IPPROTO_IP
2558 insint(d, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00002559#else
2560 insint(d, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002561#endif
2562#ifdef IPPROTO_ICMP
2563 insint(d, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00002564#else
2565 insint(d, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002566#endif
2567#ifdef IPPROTO_IGMP
2568 insint(d, "IPPROTO_IGMP", IPPROTO_IGMP);
2569#endif
2570#ifdef IPPROTO_GGP
2571 insint(d, "IPPROTO_GGP", IPPROTO_GGP);
2572#endif
2573#ifdef IPPROTO_TCP
2574 insint(d, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00002575#else
2576 insint(d, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002577#endif
2578#ifdef IPPROTO_EGP
2579 insint(d, "IPPROTO_EGP", IPPROTO_EGP);
2580#endif
2581#ifdef IPPROTO_PUP
2582 insint(d, "IPPROTO_PUP", IPPROTO_PUP);
2583#endif
2584#ifdef IPPROTO_UDP
2585 insint(d, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00002586#else
2587 insint(d, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002588#endif
2589#ifdef IPPROTO_IDP
2590 insint(d, "IPPROTO_IDP", IPPROTO_IDP);
2591#endif
2592#ifdef IPPROTO_HELLO
2593 insint(d, "IPPROTO_HELLO", IPPROTO_HELLO);
2594#endif
2595#ifdef IPPROTO_ND
2596 insint(d, "IPPROTO_ND", IPPROTO_ND);
2597#endif
2598#ifdef IPPROTO_TP
2599 insint(d, "IPPROTO_TP", IPPROTO_TP);
2600#endif
2601#ifdef IPPROTO_XTP
2602 insint(d, "IPPROTO_XTP", IPPROTO_XTP);
2603#endif
2604#ifdef IPPROTO_EON
2605 insint(d, "IPPROTO_EON", IPPROTO_EON);
2606#endif
2607#ifdef IPPROTO_BIP
2608 insint(d, "IPPROTO_BIP", IPPROTO_BIP);
2609#endif
2610/**/
2611#ifdef IPPROTO_RAW
2612 insint(d, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00002613#else
2614 insint(d, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002615#endif
2616#ifdef IPPROTO_MAX
2617 insint(d, "IPPROTO_MAX", IPPROTO_MAX);
2618#endif
2619
2620 /* Some port configuration */
2621#ifdef IPPORT_RESERVED
2622 insint(d, "IPPORT_RESERVED", IPPORT_RESERVED);
2623#else
2624 insint(d, "IPPORT_RESERVED", 1024);
2625#endif
2626#ifdef IPPORT_USERRESERVED
2627 insint(d, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
2628#else
2629 insint(d, "IPPORT_USERRESERVED", 5000);
2630#endif
2631
Guido van Rossum5f05eb41995-02-17 15:11:07 +00002632 /* Some reserved IP v.4 addresses */
2633#ifdef INADDR_ANY
2634 insint(d, "INADDR_ANY", INADDR_ANY);
2635#else
2636 insint(d, "INADDR_ANY", 0x00000000);
2637#endif
2638#ifdef INADDR_BROADCAST
2639 insint(d, "INADDR_BROADCAST", INADDR_BROADCAST);
2640#else
2641 insint(d, "INADDR_BROADCAST", 0xffffffff);
2642#endif
2643#ifdef INADDR_LOOPBACK
2644 insint(d, "INADDR_LOOPBACK", INADDR_LOOPBACK);
2645#else
2646 insint(d, "INADDR_LOOPBACK", 0x7F000001);
2647#endif
2648#ifdef INADDR_UNSPEC_GROUP
2649 insint(d, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
2650#else
2651 insint(d, "INADDR_UNSPEC_GROUP", 0xe0000000);
2652#endif
2653#ifdef INADDR_ALLHOSTS_GROUP
2654 insint(d, "INADDR_ALLHOSTS_GROUP", INADDR_ALLHOSTS_GROUP);
2655#else
2656 insint(d, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
2657#endif
2658#ifdef INADDR_MAX_LOCAL_GROUP
2659 insint(d, "INADDR_MAX_LOCAL_GROUP", INADDR_MAX_LOCAL_GROUP);
2660#else
2661 insint(d, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
2662#endif
2663#ifdef INADDR_NONE
2664 insint(d, "INADDR_NONE", INADDR_NONE);
2665#else
2666 insint(d, "INADDR_NONE", 0xffffffff);
2667#endif
2668
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00002669 /* IP [gs]etsockopt options */
2670#ifdef IP_OPTIONS
2671 insint(d, "IP_OPTIONS", IP_OPTIONS);
2672#endif
2673#ifdef IP_HDRINCL
2674 insint(d, "IP_HDRINCL", IP_HDRINCL);
2675#endif
2676#ifdef IP_TOS
2677 insint(d, "IP_TOS", IP_TOS);
2678#endif
2679#ifdef IP_TTL
2680 insint(d, "IP_TTL", IP_TTL);
2681#endif
2682#ifdef IP_RECVOPTS
2683 insint(d, "IP_RECVOPTS", IP_RECVOPTS);
2684#endif
2685#ifdef IP_RECVRETOPTS
2686 insint(d, "IP_RECVRETOPTS", IP_RECVRETOPTS);
2687#endif
2688#ifdef IP_RECVDSTADDR
2689 insint(d, "IP_RECVDSTADDR", IP_RECVDSTADDR);
2690#endif
2691#ifdef IP_RETOPTS
2692 insint(d, "IP_RETOPTS", IP_RETOPTS);
2693#endif
2694#ifdef IP_MULTICAST_IF
2695 insint(d, "IP_MULTICAST_IF", IP_MULTICAST_IF);
2696#endif
2697#ifdef IP_MULTICAST_TTL
2698 insint(d, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
2699#endif
2700#ifdef IP_MULTICAST_LOOP
2701 insint(d, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
2702#endif
2703#ifdef IP_ADD_MEMBERSHIP
2704 insint(d, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
2705#endif
2706#ifdef IP_DROP_MEMBERSHIP
2707 insint(d, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
2708#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00002709#ifdef IP_DEFAULT_MULTICAST_TTL
2710 insint(d, "IP_DEFAULT_MULTICAST_TTL", IP_DEFAULT_MULTICAST_TTL);
2711#endif
2712#ifdef IP_DEFAULT_MULTICAST_LOOP
2713 insint(d, "IP_DEFAULT_MULTICAST_LOOP", IP_DEFAULT_MULTICAST_LOOP);
2714#endif
2715#ifdef IP_MAX_MEMBERSHIPS
2716 insint(d, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
2717#endif
2718
2719 /* TCP options */
2720#ifdef TCP_NODELAY
2721 insint(d, "TCP_NODELAY", TCP_NODELAY);
2722#endif
2723#ifdef TCP_MAXSEG
2724 insint(d, "TCP_MAXSEG", TCP_MAXSEG);
2725#endif
2726
2727 /* IPX options */
2728#ifdef IPX_TYPE
2729 insint(d, "IPX_TYPE", IPX_TYPE);
2730#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002731
2732 /* Initialize gethostbyname lock */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002733#ifdef USE_GETHOSTBYNAME_LOCK
Guido van Rossum65d5b571998-12-21 19:32:43 +00002734 gethostbyname_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002735#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002736}