blob: c200b2dd923c7930887b2dfda96776816d628c19 [file] [log] [blame]
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum6574b3e1991-06-25 21:36:08 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum6574b3e1991-06-25 21:36:08 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum6574b3e1991-06-25 21:36:08 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum6574b3e1991-06-25 21:36:08 +000029
30******************************************************************/
31
32/* Socket module */
33
34/*
35This module provides an interface to Berkeley socket IPC.
36
37Limitations:
38
Guido van Rossum30a685f1991-06-27 15:51:29 +000039- only AF_INET and AF_UNIX address families are supported
Guido van Rossum81194471991-07-27 21:42:02 +000040- no read/write operations (use send/recv or makefile instead)
Guido van Rossumbe32c891996-06-20 16:25:29 +000041- additional restrictions apply on Windows
Guido van Rossum6574b3e1991-06-25 21:36:08 +000042
Guido van Rossum27e177d1995-03-16 15:43:47 +000043Module interface:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000044
Guido van Rossum27e177d1995-03-16 15:43:47 +000045- socket.error: exception raised for socket specific errors
Guido van Rossum30a685f1991-06-27 15:51:29 +000046- socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd')
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000047- socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...])
Guido van Rossum27e177d1995-03-16 15:43:47 +000048- socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com')
Guido van Rossum25405c71996-12-19 16:42:52 +000049- socket.getprotobyname(protocolname) --> protocol number
Guido van Rossum27e177d1995-03-16 15:43:47 +000050- socket.getservbyname(servicename, protocolname) --> port number
Guido van Rossum4dd2a7e1991-07-01 18:51:33 +000051- socket.socket(family, type [, proto]) --> new socket object
Guido van Rossum006bf911996-06-12 04:04:55 +000052- socket.ntohs(16 bit value) --> new int object
53- socket.ntohl(32 bit value) --> new int object
54- socket.htons(16 bit value) --> new int object
55- socket.htonl(32 bit value) --> new int object
Guido van Rossum27e177d1995-03-16 15:43:47 +000056- socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h>
Guido van Rossum6574b3e1991-06-25 21:36:08 +000057- an Internet socket address is a pair (hostname, port)
58 where hostname can be anything recognized by gethostbyname()
59 (including the dd.dd.dd.dd notation) and port is in host byte order
60- where a hostname is returned, the dd.dd.dd.dd notation is used
Guido van Rossum27e177d1995-03-16 15:43:47 +000061- a UNIX domain socket address is a string specifying the pathname
Guido van Rossum6574b3e1991-06-25 21:36:08 +000062
Guido van Rossum30a685f1991-06-27 15:51:29 +000063Socket methods:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000064
Guido van Rossum81194471991-07-27 21:42:02 +000065- s.accept() --> new socket object, sockaddr
Guido van Rossum27e177d1995-03-16 15:43:47 +000066- s.bind(sockaddr) --> None
67- s.close() --> None
68- s.connect(sockaddr) --> None
Guido van Rossumfc4255d1997-11-19 18:57:13 +000069- s.connect_ex(sockaddr) --> 0 or errno (handy for e.g. async connect)
Guido van Rossum27e177d1995-03-16 15:43:47 +000070- s.fileno() --> file descriptor
Guido van Rossumbe32c891996-06-20 16:25:29 +000071- s.dup() --> same as socket.fromfd(os.dup(s.fileno(), ...)
Guido van Rossumc89705d1992-11-26 08:54:07 +000072- s.getpeername() --> sockaddr
Guido van Rossum27e177d1995-03-16 15:43:47 +000073- s.getsockname() --> sockaddr
74- s.getsockopt(level, optname[, buflen]) --> int or string
75- s.listen(backlog) --> None
Guido van Rossum6b144911995-03-14 15:05:13 +000076- s.makefile([mode[, bufsize]]) --> file object
Guido van Rossum27e177d1995-03-16 15:43:47 +000077- s.recv(buflen [,flags]) --> string
78- s.recvfrom(buflen [,flags]) --> string, sockaddr
Guido van Rossumb6775db1994-08-01 11:34:53 +000079- s.send(string [,flags]) --> nbytes
80- s.sendto(string, [flags,] sockaddr) --> nbytes
Guido van Rossum27e177d1995-03-16 15:43:47 +000081- s.setblocking(0 | 1) --> None
82- s.setsockopt(level, optname, value) --> None
83- s.shutdown(how) --> None
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000084- repr(s) --> "<socket object, fd=%d, family=%d, type=%d, protocol=%d>"
Guido van Rossum30a685f1991-06-27 15:51:29 +000085
Guido van Rossum6574b3e1991-06-25 21:36:08 +000086*/
87
Guido van Rossum73624e91994-10-10 17:59:00 +000088#include "Python.h"
Guido van Rossum4f199ea1998-04-09 20:56:35 +000089#if defined(WITH_THREAD) && !defined(HAVE_GETHOSTBYNAME_R) && !defined(MS_WINDOWS)
90#include "thread.h"
91#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +000092
Guido van Rossuma376cc51996-12-05 23:43:35 +000093#ifdef HAVE_UNISTD_H
94#include <unistd.h>
95#endif
96
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000097#if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
Guido van Rossumfb4130d1996-12-10 15:17:08 +000098extern int gethostname(); /* For Solaris, at least */
99#endif
100
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#if defined(PYCC_VACPP)
102#include <types.h>
103#include <io.h>
104#include <sys/ioctl.h>
105#include <utils.h>
106#include <ctype.h>
107#endif
108
109#if defined(PYOS_OS2)
110#define INCL_DOS
111#define INCL_DOSERRORS
112#define INCL_NOPMAPI
113#include <os2.h>
114#endif
115
Guido van Rossumb6775db1994-08-01 11:34:53 +0000116#include <sys/types.h>
117#include "mytime.h"
Guido van Rossumed233a51992-06-23 09:07:03 +0000118
Guido van Rossum81194471991-07-27 21:42:02 +0000119#include <signal.h>
Guido van Rossum6f489d91996-06-28 20:15:15 +0000120#ifndef MS_WINDOWS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000121#include <netdb.h>
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000122#include <sys/socket.h>
123#include <netinet/in.h>
Guido van Rossume4485b01994-09-07 14:32:49 +0000124#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000125#else
126#include <winsock.h>
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000127#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000128#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000129#ifdef HAVE_SYS_UN_H
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000130#include <sys/un.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000131#else
132#undef AF_UNIX
Guido van Rossum9575a441993-04-07 14:06:14 +0000133#endif
134
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000135#ifndef O_NDELAY
136#define O_NDELAY O_NONBLOCK /* For QNX only? */
137#endif
138
Jack Jansen508537b1996-02-14 15:57:45 +0000139#ifdef USE_GUSI
140/* fdopen() isn't declared in stdio.h (sigh) */
141#include <GUSI.h>
142#endif
143
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000144
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000145/* Here we have some hacks to choose between K&R or ANSI style function
146 definitions. For NT to build this as an extension module (ie, DLL)
147 it must be compiled by the C++ compiler, as it takes the address of
148 a static data item exported from the main Python DLL.
149*/
Guido van Rossum8d665e61996-06-26 18:22:49 +0000150#ifdef MS_WINDOWS
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000151/* seem to be a few differences in the API */
152#define close closesocket
Guido van Rossumbe32c891996-06-20 16:25:29 +0000153#define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000154#define FORCE_ANSI_FUNC_DEFS
155#endif
156
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000157#if defined(PYOS_OS2)
158#define close soclose
159#define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
160#define FORCE_ANSI_FUNC_DEFS
161#endif
162
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000163#ifdef FORCE_ANSI_FUNC_DEFS
164#define BUILD_FUNC_DEF_1( fnname, arg1type, arg1name ) \
165fnname( arg1type arg1name )
166
167#define BUILD_FUNC_DEF_2( fnname, arg1type, arg1name, arg2type, arg2name ) \
168fnname( arg1type arg1name, arg2type arg2name )
169
170#define BUILD_FUNC_DEF_3( fnname, arg1type, arg1name, arg2type, arg2name , arg3type, arg3name ) \
171fnname( arg1type arg1name, arg2type arg2name, arg3type arg3name )
172
173#define BUILD_FUNC_DEF_4( fnname, arg1type, arg1name, arg2type, arg2name , arg3type, arg3name, arg4type, arg4name ) \
174fnname( arg1type arg1name, arg2type arg2name, arg3type arg3name, arg4type arg4name )
175
176#else /* !FORCE_ANSI_FN_DEFS */
177#define BUILD_FUNC_DEF_1( fnname, arg1type, arg1name ) \
178fnname( arg1name ) \
179 arg1type arg1name;
180
181#define BUILD_FUNC_DEF_2( fnname, arg1type, arg1name, arg2type, arg2name ) \
182fnname( arg1name, arg2name ) \
183 arg1type arg1name; \
184 arg2type arg2name;
185
186#define BUILD_FUNC_DEF_3( fnname, arg1type, arg1name, arg2type, arg2name, arg3type, arg3name ) \
187fnname( arg1name, arg2name, arg3name ) \
188 arg1type arg1name; \
189 arg2type arg2name; \
190 arg3type arg3name;
191
192#define BUILD_FUNC_DEF_4( fnname, arg1type, arg1name, arg2type, arg2name, arg3type, arg3name, arg4type, arg4name ) \
193fnname( arg1name, arg2name, arg3name, arg4name ) \
194 arg1type arg1name; \
195 arg2type arg2name; \
196 arg3type arg3name; \
197 arg4type arg4name;
198
199#endif /* !FORCE_ANSI_FN_DEFS */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000200
201/* Global variable holding the exception type for errors detected
202 by this module (but not argument type or memory errors, etc.). */
203
Guido van Rossum73624e91994-10-10 17:59:00 +0000204static PyObject *PySocket_Error;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000205
206
207/* Convenience function to raise an error according to errno
208 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000209
Guido van Rossum73624e91994-10-10 17:59:00 +0000210static PyObject *
211PySocket_Err()
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000212{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000213#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000214 if (WSAGetLastError()) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000215 PyObject *v;
216 v = Py_BuildValue("(is)", WSAGetLastError(), "winsock error");
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000217 if (v != NULL) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000218 PyErr_SetObject(PySocket_Error, v);
219 Py_DECREF(v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000220 }
221 return NULL;
222 }
223 else
224#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000225
226#if defined(PYOS_OS2)
227 if (sock_errno() != NO_ERROR) {
228 APIRET rc;
229 ULONG msglen;
230 char outbuf[100];
231 int myerrorcode = sock_errno();
232
233 /* Retrieve Socket-Related Error Message from MPTN.MSG File */
234 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
235 myerrorcode - SOCBASEERR + 26, "mptn.msg", &msglen);
236 if (rc == NO_ERROR) {
237 PyObject *v;
238
239 outbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
240 if (strlen(outbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
241 char *lastc = &outbuf[ strlen(outbuf)-1 ];
242 while (lastc > outbuf && isspace(*lastc))
243 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
244 }
245 v = Py_BuildValue("(is)", myerrorcode, outbuf);
246 if (v != NULL) {
247 PyErr_SetObject(PySocket_Error, v);
248 Py_DECREF(v);
249 }
250 return NULL;
251 }
252 }
253#endif
254
Guido van Rossum73624e91994-10-10 17:59:00 +0000255 return PyErr_SetFromErrno(PySocket_Error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000256}
257
Guido van Rossum30a685f1991-06-27 15:51:29 +0000258
259/* The object holding a socket. It holds some extra information,
260 like the address family, which is used to decode socket address
261 arguments properly. */
262
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000263typedef struct {
Guido van Rossum73624e91994-10-10 17:59:00 +0000264 PyObject_HEAD
Guido van Rossum30a685f1991-06-27 15:51:29 +0000265 int sock_fd; /* Socket file descriptor */
266 int sock_family; /* Address family, e.g., AF_INET */
267 int sock_type; /* Socket type, e.g., SOCK_STREAM */
268 int sock_proto; /* Protocol type, usually 0 */
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000269 union sock_addr {
270 struct sockaddr_in in;
271#ifdef AF_UNIX
272 struct sockaddr_un un;
273#endif
274 } sock_addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000275} PySocketSockObject;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000276
Guido van Rossum30a685f1991-06-27 15:51:29 +0000277
278/* A forward reference to the Socktype type object.
279 The Socktype variable contains pointers to various functions,
Guido van Rossum73624e91994-10-10 17:59:00 +0000280 some of which call newsockobject(), which uses Socktype, so
Guido van Rossum54ba21b1991-09-10 14:57:12 +0000281 there has to be a circular reference. */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000282
Guido van Rossum73624e91994-10-10 17:59:00 +0000283staticforward PyTypeObject PySocketSock_Type;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000284
Guido van Rossum30a685f1991-06-27 15:51:29 +0000285
286/* Create a new socket object.
287 This just creates the object and initializes it.
288 If the creation fails, return NULL and set an exception (implicit
289 in NEWOBJ()). */
290
Guido van Rossum73624e91994-10-10 17:59:00 +0000291static PySocketSockObject *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000292BUILD_FUNC_DEF_4(PySocketSock_New,int,fd, int,family, int,type, int,proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000293{
Guido van Rossum73624e91994-10-10 17:59:00 +0000294 PySocketSockObject *s;
Guido van Rossum55558541996-05-23 22:54:50 +0000295 PySocketSock_Type.ob_type = &PyType_Type;
Guido van Rossum73624e91994-10-10 17:59:00 +0000296 s = PyObject_NEW(PySocketSockObject, &PySocketSock_Type);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000297 if (s != NULL) {
298 s->sock_fd = fd;
299 s->sock_family = family;
300 s->sock_type = type;
301 s->sock_proto = proto;
302 }
303 return s;
304}
305
Guido van Rossum30a685f1991-06-27 15:51:29 +0000306
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000307/* Lock to allow python interpreter to continue, but only allow one
308 thread to be in gethostbyname */
309#if defined(WITH_THREAD) && !defined(HAVE_GETHOSTBYNAME_R) && !defined(MS_WINDOWS)
310type_lock gethostbyname_lock;
311#endif
312
313
Guido van Rossum30a685f1991-06-27 15:51:29 +0000314/* Convert a string specifying a host name or one of a few symbolic
315 names to a numeric IP address. This usually calls gethostbyname()
316 to do the work; the names "" and "<broadcast>" are special.
317 Return the length (should always be 4 bytes), or negative if
318 an error occurred; then an exception is raised. */
319
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000320static int
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000321BUILD_FUNC_DEF_2(setipaddr, char*,name, struct sockaddr_in *,addr_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000322{
323 struct hostent *hp;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000324 int d1, d2, d3, d4;
325 char ch;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000326#ifdef HAVE_GETHOSTBYNAME_R
327 struct hostent hp_allocated;
328 char buf[1001];
329 int buf_len = (sizeof buf) - 1;
330 int errnop;
331#endif /* HAVE_GETHOSTBYNAME_R */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000332
Guido van Rossuma376cc51996-12-05 23:43:35 +0000333 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
Guido van Rossum30a685f1991-06-27 15:51:29 +0000334 if (name[0] == '\0') {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000335 addr_ret->sin_addr.s_addr = INADDR_ANY;
336 return 4;
337 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000338 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000339 addr_ret->sin_addr.s_addr = INADDR_BROADCAST;
340 return 4;
341 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000342 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
343 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
344 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
345 addr_ret->sin_addr.s_addr = htonl(
346 ((long) d1 << 24) | ((long) d2 << 16) |
347 ((long) d3 << 8) | ((long) d4 << 0));
348 return 4;
349 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000350 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000351#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000352 hp = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000353#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000354#if defined(WITH_THREAD) && !defined(MS_WINDOWS)
355 acquire_lock(gethostbyname_lock,1);
356#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000357 hp = gethostbyname(name);
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000358#if defined(WITH_THREAD) && !defined(MS_WINDOWS)
359 release_lock(gethostbyname_lock);
360#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000361#endif /* HAVE_GETHOSTBYNAME_R */
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000362 Py_END_ALLOW_THREADS
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000363
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000364 if (hp == NULL) {
Guido van Rossumca9b3231995-01-10 16:30:54 +0000365#ifdef HAVE_HSTRERROR
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000366 /* Let's get real error message to return */
367 extern int h_errno;
368 PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno));
369#else
Guido van Rossum73624e91994-10-10 17:59:00 +0000370 PyErr_SetString(PySocket_Error, "host not found");
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000371#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000372 return -1;
373 }
374 memcpy((char *) &addr_ret->sin_addr, hp->h_addr, hp->h_length);
375 return hp->h_length;
376}
377
Guido van Rossum30a685f1991-06-27 15:51:29 +0000378
Guido van Rossum30a685f1991-06-27 15:51:29 +0000379/* Create a string object representing an IP address.
380 This is always a string of the form 'dd.dd.dd.dd' (with variable
381 size numbers). */
382
Guido van Rossum73624e91994-10-10 17:59:00 +0000383static PyObject *
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000384BUILD_FUNC_DEF_1(makeipaddr, struct sockaddr_in *,addr)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000385{
386 long x = ntohl(addr->sin_addr.s_addr);
387 char buf[100];
388 sprintf(buf, "%d.%d.%d.%d",
389 (int) (x>>24) & 0xff, (int) (x>>16) & 0xff,
390 (int) (x>> 8) & 0xff, (int) (x>> 0) & 0xff);
Guido van Rossum73624e91994-10-10 17:59:00 +0000391 return PyString_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000392}
393
394
395/* Create an object representing the given socket address,
396 suitable for passing it back to bind(), connect() etc.
397 The family field of the sockaddr structure is inspected
398 to determine what kind of address it really is. */
399
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000400/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000401static PyObject *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000402BUILD_FUNC_DEF_2(makesockaddr,struct sockaddr *,addr, int,addrlen)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000403{
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000404 if (addrlen == 0) {
405 /* No address -- may be recvfrom() from known socket */
Guido van Rossum73624e91994-10-10 17:59:00 +0000406 Py_INCREF(Py_None);
407 return Py_None;
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000408 }
409
Guido van Rossum30a685f1991-06-27 15:51:29 +0000410 switch (addr->sa_family) {
411
412 case AF_INET:
413 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000414 struct sockaddr_in *a = (struct sockaddr_in *) addr;
Barry Warsaw752300b1997-01-03 17:18:10 +0000415 PyObject *addrobj = makeipaddr(a);
416 PyObject *ret = NULL;
417 if (addrobj) {
418 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
419 Py_DECREF(addrobj);
420 }
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000421 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000422 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000423
Guido van Rossumb6775db1994-08-01 11:34:53 +0000424#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000425 case AF_UNIX:
426 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000427 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000428 return PyString_FromString(a->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000429 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000430#endif /* AF_UNIX */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000431
432 /* More cases here... */
433
434 default:
Guido van Rossumaa948df1997-05-07 17:41:48 +0000435 /* If we don't know the address family, don't raise an
436 exception -- return it as a tuple. */
437 return Py_BuildValue("is#",
438 addr->sa_family,
439 addr->sa_data,
440 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000441
Guido van Rossum30a685f1991-06-27 15:51:29 +0000442 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000443}
444
Guido van Rossum30a685f1991-06-27 15:51:29 +0000445
446/* Parse a socket address argument according to the socket object's
447 address family. Return 1 if the address was in the proper format,
448 0 of not. The address is returned through addr_ret, its length
449 through len_ret. */
450
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000451static int
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000452BUILD_FUNC_DEF_4(
Guido van Rossum73624e91994-10-10 17:59:00 +0000453getsockaddrarg,PySocketSockObject *,s, PyObject *,args, struct sockaddr **,addr_ret, int *,len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000454{
Guido van Rossum30a685f1991-06-27 15:51:29 +0000455 switch (s->sock_family) {
456
Guido van Rossumb6775db1994-08-01 11:34:53 +0000457#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000458 case AF_UNIX:
459 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000460 struct sockaddr_un* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000461 char *path;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000462 int len;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000463 addr = (struct sockaddr_un* )&(s->sock_addr).un;
Guido van Rossum73624e91994-10-10 17:59:00 +0000464 if (!PyArg_Parse(args, "s#", &path, &len))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000465 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000466 if (len > sizeof addr->sun_path) {
Barry Warsaw752300b1997-01-03 17:18:10 +0000467 PyErr_SetString(PySocket_Error,
468 "AF_UNIX path too long");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000469 return 0;
470 }
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000471 addr->sun_family = AF_UNIX;
472 memcpy(addr->sun_path, path, len);
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000473 addr->sun_path[len] = 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000474 *addr_ret = (struct sockaddr *) addr;
Guido van Rossum65af28a1996-06-11 18:36:33 +0000475 *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000476 return 1;
477 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000478#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000479
Guido van Rossum30a685f1991-06-27 15:51:29 +0000480 case AF_INET:
481 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000482 struct sockaddr_in* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000483 char *host;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000484 int port;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000485 addr=(struct sockaddr_in*)&(s->sock_addr).in;
Guido van Rossum73624e91994-10-10 17:59:00 +0000486 if (!PyArg_Parse(args, "(si)", &host, &port))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000487 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000488 if (setipaddr(host, addr) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000489 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000490 addr->sin_family = AF_INET;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000491 addr->sin_port = htons((short)port);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000492 *addr_ret = (struct sockaddr *) addr;
493 *len_ret = sizeof *addr;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000494 return 1;
495 }
496
Guido van Rossum30a685f1991-06-27 15:51:29 +0000497 /* More cases here... */
498
499 default:
Guido van Rossum73624e91994-10-10 17:59:00 +0000500 PyErr_SetString(PySocket_Error, "getsockaddrarg: bad family");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000501 return 0;
502
503 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000504}
505
Guido van Rossum30a685f1991-06-27 15:51:29 +0000506
Guido van Rossum710e1df1992-06-12 10:39:36 +0000507/* Get the address length according to the socket object's address family.
508 Return 1 if the family is known, 0 otherwise. The length is returned
509 through len_ret. */
510
511static int
Guido van Rossum73624e91994-10-10 17:59:00 +0000512BUILD_FUNC_DEF_2(getsockaddrlen,PySocketSockObject *,s, int *,len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +0000513{
514 switch (s->sock_family) {
515
Guido van Rossumb6775db1994-08-01 11:34:53 +0000516#ifdef AF_UNIX
Guido van Rossum710e1df1992-06-12 10:39:36 +0000517 case AF_UNIX:
518 {
519 *len_ret = sizeof (struct sockaddr_un);
520 return 1;
521 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000522#endif /* AF_UNIX */
Guido van Rossum710e1df1992-06-12 10:39:36 +0000523
524 case AF_INET:
525 {
526 *len_ret = sizeof (struct sockaddr_in);
527 return 1;
528 }
529
530 /* More cases here... */
531
532 default:
Guido van Rossum73624e91994-10-10 17:59:00 +0000533 PyErr_SetString(PySocket_Error, "getsockaddrarg: bad family");
Guido van Rossum710e1df1992-06-12 10:39:36 +0000534 return 0;
535
536 }
537}
538
539
Guido van Rossum30a685f1991-06-27 15:51:29 +0000540/* s.accept() method */
541
Guido van Rossum73624e91994-10-10 17:59:00 +0000542static PyObject *
543BUILD_FUNC_DEF_2(PySocketSock_accept,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000544{
545 char addrbuf[256];
546 int addrlen, newfd;
Barry Warsaw752300b1997-01-03 17:18:10 +0000547 PyObject *sock = NULL;
548 PyObject *addr = NULL;
549 PyObject *res = NULL;
550
Guido van Rossum73624e91994-10-10 17:59:00 +0000551 if (!PyArg_NoArgs(args))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000552 return NULL;
Guido van Rossum710e1df1992-06-12 10:39:36 +0000553 if (!getsockaddrlen(s, &addrlen))
554 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000555 Py_BEGIN_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000556 newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000557 Py_END_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000558 if (newfd < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000559 return PySocket_Err();
Barry Warsaw752300b1997-01-03 17:18:10 +0000560
Guido van Rossum30a685f1991-06-27 15:51:29 +0000561 /* Create the new object with unspecified family,
562 to avoid calls to bind() etc. on it. */
Guido van Rossum73624e91994-10-10 17:59:00 +0000563 sock = (PyObject *) PySocketSock_New(newfd,
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000564 s->sock_family,
565 s->sock_type,
566 s->sock_proto);
Barry Warsaw752300b1997-01-03 17:18:10 +0000567 if (sock == NULL) {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000568 close(newfd);
Barry Warsaw752300b1997-01-03 17:18:10 +0000569 goto finally;
570 }
571 if (!(addr = makesockaddr((struct sockaddr *) addrbuf, addrlen)))
572 goto finally;
573
574 if (!(res = Py_BuildValue("OO", sock, addr)))
575 goto finally;
576
577 finally:
Guido van Rossum73624e91994-10-10 17:59:00 +0000578 Py_XDECREF(sock);
579 Py_XDECREF(addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000580 return res;
581}
582
Guido van Rossum30a685f1991-06-27 15:51:29 +0000583
Guido van Rossume4485b01994-09-07 14:32:49 +0000584/* s.setblocking(1 | 0) method */
585
Guido van Rossum73624e91994-10-10 17:59:00 +0000586static PyObject *
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000587BUILD_FUNC_DEF_2(PySocketSock_setblocking,PySocketSockObject*,s,PyObject*,args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000588{
589 int block;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000590#ifndef MS_WINDOWS
Guido van Rossume4485b01994-09-07 14:32:49 +0000591 int delay_flag;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000592#endif
Guido van Rossumfe28fa01997-05-15 19:09:26 +0000593 if (!PyArg_Parse(args, "i", &block))
Guido van Rossume4485b01994-09-07 14:32:49 +0000594 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000595 Py_BEGIN_ALLOW_THREADS
Guido van Rossum6f489d91996-06-28 20:15:15 +0000596#ifndef MS_WINDOWS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000597#ifdef PYOS_OS2
598 block = !block;
599 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
600#else /* !PYOS_OS2 */
Guido van Rossume4485b01994-09-07 14:32:49 +0000601 delay_flag = fcntl (s->sock_fd, F_GETFL, 0);
602 if (block)
603 delay_flag &= (~O_NDELAY);
604 else
605 delay_flag |= O_NDELAY;
606 fcntl (s->sock_fd, F_SETFL, delay_flag);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000607#endif /* !PYOS_OS2 */
608#else /* MS_WINDOWS */
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000609 block = !block;
610 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000611#endif /* MS_WINDOWS */
Guido van Rossum73624e91994-10-10 17:59:00 +0000612 Py_END_ALLOW_THREADS
Guido van Rossume4485b01994-09-07 14:32:49 +0000613
Guido van Rossum73624e91994-10-10 17:59:00 +0000614 Py_INCREF(Py_None);
615 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000616}
Guido van Rossume4485b01994-09-07 14:32:49 +0000617
618
Guido van Rossumaee08791992-09-08 09:05:33 +0000619/* s.setsockopt() method.
620 With an integer third argument, sets an integer option.
621 With a string third argument, sets an option from a buffer;
622 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000623
Guido van Rossum73624e91994-10-10 17:59:00 +0000624static PyObject *
625BUILD_FUNC_DEF_2(PySocketSock_setsockopt,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000626{
627 int level;
628 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000629 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +0000630 char *buf;
631 int buflen;
632 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000633
Guido van Rossum73624e91994-10-10 17:59:00 +0000634 if (PyArg_Parse(args, "(iii)", &level, &optname, &flag)) {
Guido van Rossumaee08791992-09-08 09:05:33 +0000635 buf = (char *) &flag;
636 buflen = sizeof flag;
637 }
638 else {
Guido van Rossum73624e91994-10-10 17:59:00 +0000639 PyErr_Clear();
Barry Warsaw752300b1997-01-03 17:18:10 +0000640 if (!PyArg_Parse(args, "(iis#)", &level, &optname,
641 &buf, &buflen))
Guido van Rossumaee08791992-09-08 09:05:33 +0000642 return NULL;
643 }
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000644 res = setsockopt(s->sock_fd, level, optname, (ANY *)buf, buflen);
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000645 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000646 return PySocket_Err();
647 Py_INCREF(Py_None);
648 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000649}
650
651
Guido van Rossumaee08791992-09-08 09:05:33 +0000652/* s.getsockopt() method.
653 With two arguments, retrieves an integer option.
654 With a third integer argument, retrieves a string buffer of that size;
655 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000656
Guido van Rossum73624e91994-10-10 17:59:00 +0000657static PyObject *
658BUILD_FUNC_DEF_2(PySocketSock_getsockopt,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000659{
660 int level;
661 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000662 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +0000663 PyObject *buf;
Guido van Rossumbe32c891996-06-20 16:25:29 +0000664 int buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000665
Guido van Rossumbe32c891996-06-20 16:25:29 +0000666 if (!PyArg_ParseTuple(args, "ii|i", &level, &optname, &buflen))
667 return NULL;
668
669 if (buflen == 0) {
Guido van Rossumaee08791992-09-08 09:05:33 +0000670 int flag = 0;
671 int flagsize = sizeof flag;
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000672 res = getsockopt(s->sock_fd, level, optname,
673 (ANY *)&flag, &flagsize);
Guido van Rossumaee08791992-09-08 09:05:33 +0000674 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000675 return PySocket_Err();
676 return PyInt_FromLong(flag);
Guido van Rossumaee08791992-09-08 09:05:33 +0000677 }
Guido van Rossumaee08791992-09-08 09:05:33 +0000678 if (buflen <= 0 || buflen > 1024) {
Barry Warsaw752300b1997-01-03 17:18:10 +0000679 PyErr_SetString(PySocket_Error,
680 "getsockopt buflen out of range");
Guido van Rossumaee08791992-09-08 09:05:33 +0000681 return NULL;
682 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000683 buf = PyString_FromStringAndSize((char *)NULL, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000684 if (buf == NULL)
685 return NULL;
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000686 res = getsockopt(s->sock_fd, level, optname,
Guido van Rossuma597dde1995-01-10 20:56:29 +0000687 (ANY *)PyString_AsString(buf), &buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000688 if (res < 0) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000689 Py_DECREF(buf);
690 return PySocket_Err();
Guido van Rossumaee08791992-09-08 09:05:33 +0000691 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000692 _PyString_Resize(&buf, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000693 return buf;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000694}
695
696
Guido van Rossum30a685f1991-06-27 15:51:29 +0000697/* s.bind(sockaddr) method */
698
Guido van Rossum73624e91994-10-10 17:59:00 +0000699static PyObject *
700BUILD_FUNC_DEF_2(PySocketSock_bind,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000701{
702 struct sockaddr *addr;
703 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000704 int res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000705 if (!getsockaddrarg(s, args, &addr, &addrlen))
706 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000707 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000708 res = bind(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000709 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000710 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000711 return PySocket_Err();
712 Py_INCREF(Py_None);
713 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000714}
715
Guido van Rossum30a685f1991-06-27 15:51:29 +0000716
717/* s.close() method.
718 Set the file descriptor to -1 so operations tried subsequently
719 will surely fail. */
720
Guido van Rossum73624e91994-10-10 17:59:00 +0000721static PyObject *
722BUILD_FUNC_DEF_2(PySocketSock_close,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000723{
Guido van Rossum73624e91994-10-10 17:59:00 +0000724 if (!PyArg_NoArgs(args))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000725 return NULL;
Guido van Rossum08481461996-10-12 14:07:22 +0000726 if (s->sock_fd != -1) {
727 Py_BEGIN_ALLOW_THREADS
728 (void) close(s->sock_fd);
729 Py_END_ALLOW_THREADS
730 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000731 s->sock_fd = -1;
Guido van Rossum73624e91994-10-10 17:59:00 +0000732 Py_INCREF(Py_None);
733 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000734}
735
Guido van Rossum30a685f1991-06-27 15:51:29 +0000736
737/* s.connect(sockaddr) method */
738
Guido van Rossum73624e91994-10-10 17:59:00 +0000739static PyObject *
740BUILD_FUNC_DEF_2(PySocketSock_connect,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000741{
742 struct sockaddr *addr;
743 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000744 int res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000745 if (!getsockaddrarg(s, args, &addr, &addrlen))
746 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000747 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000748 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000749 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000750 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000751 return PySocket_Err();
752 Py_INCREF(Py_None);
753 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000754}
755
Guido van Rossum30a685f1991-06-27 15:51:29 +0000756
Guido van Rossumfc4255d1997-11-19 18:57:13 +0000757/* s.connect_ex(sockaddr) method */
758
759static PyObject *
760BUILD_FUNC_DEF_2(PySocketSock_connect_ex,PySocketSockObject *,s, PyObject *,args)
761{
762 struct sockaddr *addr;
763 int addrlen;
764 int res;
765 if (!getsockaddrarg(s, args, &addr, &addrlen))
766 return NULL;
767 Py_BEGIN_ALLOW_THREADS
768 res = connect(s->sock_fd, addr, addrlen);
769 Py_END_ALLOW_THREADS
770 if (res != 0)
771 res = errno;
772 return PyInt_FromLong((long) res);
773}
774
775
Guido van Rossumed233a51992-06-23 09:07:03 +0000776/* s.fileno() method */
777
Guido van Rossum73624e91994-10-10 17:59:00 +0000778static PyObject *
779BUILD_FUNC_DEF_2(PySocketSock_fileno,PySocketSockObject *,s, PyObject *,args)
Guido van Rossumed233a51992-06-23 09:07:03 +0000780{
Guido van Rossum73624e91994-10-10 17:59:00 +0000781 if (!PyArg_NoArgs(args))
Guido van Rossumed233a51992-06-23 09:07:03 +0000782 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000783 return PyInt_FromLong((long) s->sock_fd);
Guido van Rossumed233a51992-06-23 09:07:03 +0000784}
785
786
Guido van Rossumbe32c891996-06-20 16:25:29 +0000787#ifndef NO_DUP
788/* s.dup() method */
789
790static PyObject *
791BUILD_FUNC_DEF_2(PySocketSock_dup,PySocketSockObject *,s, PyObject *,args)
792{
793 int newfd;
794 PyObject *sock;
795 if (!PyArg_NoArgs(args))
796 return NULL;
797 newfd = dup(s->sock_fd);
798 if (newfd < 0)
799 return PySocket_Err();
800 sock = (PyObject *) PySocketSock_New(newfd,
Barry Warsaw752300b1997-01-03 17:18:10 +0000801 s->sock_family,
802 s->sock_type,
803 s->sock_proto);
Guido van Rossumbe32c891996-06-20 16:25:29 +0000804 if (sock == NULL)
805 close(newfd);
806 return sock;
807}
808#endif
809
810
Guido van Rossumc89705d1992-11-26 08:54:07 +0000811/* s.getsockname() method */
812
Guido van Rossum73624e91994-10-10 17:59:00 +0000813static PyObject *
814BUILD_FUNC_DEF_2(PySocketSock_getsockname,PySocketSockObject *,s, PyObject *,args)
Guido van Rossumc89705d1992-11-26 08:54:07 +0000815{
816 char addrbuf[256];
817 int addrlen, res;
Guido van Rossum73624e91994-10-10 17:59:00 +0000818 if (!PyArg_NoArgs(args))
Guido van Rossumc89705d1992-11-26 08:54:07 +0000819 return NULL;
820 if (!getsockaddrlen(s, &addrlen))
821 return NULL;
Guido van Rossumca42b161996-01-12 01:36:05 +0000822 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000823 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +0000824 res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000825 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +0000826 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000827 return PySocket_Err();
Guido van Rossumc89705d1992-11-26 08:54:07 +0000828 return makesockaddr((struct sockaddr *) addrbuf, addrlen);
829}
830
831
Guido van Rossumb6775db1994-08-01 11:34:53 +0000832#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +0000833/* s.getpeername() method */
834
Guido van Rossum73624e91994-10-10 17:59:00 +0000835static PyObject *
836BUILD_FUNC_DEF_2(PySocketSock_getpeername,PySocketSockObject *,s, PyObject *,args)
Guido van Rossumc89705d1992-11-26 08:54:07 +0000837{
838 char addrbuf[256];
839 int addrlen, res;
Guido van Rossum73624e91994-10-10 17:59:00 +0000840 if (!PyArg_NoArgs(args))
Guido van Rossumc89705d1992-11-26 08:54:07 +0000841 return NULL;
842 if (!getsockaddrlen(s, &addrlen))
843 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000844 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +0000845 res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000846 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +0000847 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000848 return PySocket_Err();
Guido van Rossumc89705d1992-11-26 08:54:07 +0000849 return makesockaddr((struct sockaddr *) addrbuf, addrlen);
850}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000851#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +0000852
853
Guido van Rossum30a685f1991-06-27 15:51:29 +0000854/* s.listen(n) method */
855
Guido van Rossum73624e91994-10-10 17:59:00 +0000856static PyObject *
857BUILD_FUNC_DEF_2(PySocketSock_listen,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000858{
859 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000860 int res;
Guido van Rossumfe28fa01997-05-15 19:09:26 +0000861 if (!PyArg_Parse(args, "i", &backlog))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000862 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000863 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000864 if (backlog < 1)
865 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000866 res = listen(s->sock_fd, backlog);
Guido van Rossum73624e91994-10-10 17:59:00 +0000867 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000868 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000869 return PySocket_Err();
870 Py_INCREF(Py_None);
871 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000872}
873
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000874#ifndef NO_DUP
Guido van Rossum30a685f1991-06-27 15:51:29 +0000875/* s.makefile(mode) method.
876 Create a new open file object referring to a dupped version of
877 the socket's file descriptor. (The dup() call is necessary so
878 that the open file and socket objects may be closed independent
879 of each other.)
880 The mode argument specifies 'r' or 'w' passed to fdopen(). */
881
Guido van Rossum73624e91994-10-10 17:59:00 +0000882static PyObject *
883BUILD_FUNC_DEF_2(PySocketSock_makefile,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000884{
Guido van Rossuma597dde1995-01-10 20:56:29 +0000885 extern int fclose Py_PROTO((FILE *));
Guido van Rossum6b144911995-03-14 15:05:13 +0000886 char *mode = "r";
887 int bufsize = -1;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000888 int fd;
889 FILE *fp;
Guido van Rossum6b144911995-03-14 15:05:13 +0000890 PyObject *f;
891
892 if (!PyArg_ParseTuple(args, "|si", &mode, &bufsize))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000893 return NULL;
Guido van Rossum8d665e61996-06-26 18:22:49 +0000894#ifdef MS_WIN32
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000895 if ( ((fd = _open_osfhandle( s->sock_fd, _O_BINARY )) < 0) ||
896 ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL)) {
897#else
Guido van Rossum30a685f1991-06-27 15:51:29 +0000898 if ((fd = dup(s->sock_fd)) < 0 ||
Guido van Rossum6b144911995-03-14 15:05:13 +0000899 (fp = fdopen(fd, mode)) == NULL) {
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000900#endif
Guido van Rossum6b144911995-03-14 15:05:13 +0000901 if (fd >= 0)
902 close(fd);
Guido van Rossum73624e91994-10-10 17:59:00 +0000903 return PySocket_Err();
Guido van Rossum6b144911995-03-14 15:05:13 +0000904 }
905 f = PyFile_FromFile(fp, "<socket>", mode, fclose);
906 if (f != NULL)
907 PyFile_SetBufSize(f, bufsize);
908 return f;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000909}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000910#endif /* NO_DUP */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000911
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000912/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000913
Guido van Rossum73624e91994-10-10 17:59:00 +0000914static PyObject *
915BUILD_FUNC_DEF_2(PySocketSock_recv,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000916{
Guido van Rossumbe32c891996-06-20 16:25:29 +0000917 int len, n, flags = 0;
Guido van Rossum73624e91994-10-10 17:59:00 +0000918 PyObject *buf;
Guido van Rossumbe32c891996-06-20 16:25:29 +0000919 if (!PyArg_ParseTuple(args, "i|i", &len, &flags))
920 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000921 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000922 if (buf == NULL)
923 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000924 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma597dde1995-01-10 20:56:29 +0000925 n = recv(s->sock_fd, PyString_AsString(buf), len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +0000926 Py_END_ALLOW_THREADS
Guido van Rossum7c53b771995-09-13 18:39:47 +0000927 if (n < 0) {
928 Py_DECREF(buf);
Guido van Rossum73624e91994-10-10 17:59:00 +0000929 return PySocket_Err();
Guido van Rossum7c53b771995-09-13 18:39:47 +0000930 }
Guido van Rossum53a9bf81996-06-11 18:35:24 +0000931 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000932 return NULL;
933 return buf;
934}
935
Guido van Rossum30a685f1991-06-27 15:51:29 +0000936
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000937/* s.recvfrom(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000938
Guido van Rossum73624e91994-10-10 17:59:00 +0000939static PyObject *
940BUILD_FUNC_DEF_2(PySocketSock_recvfrom,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000941{
942 char addrbuf[256];
Barry Warsaw752300b1997-01-03 17:18:10 +0000943 PyObject *buf = NULL;
944 PyObject *addr = NULL;
945 PyObject *ret = NULL;
946
Guido van Rossumbe32c891996-06-20 16:25:29 +0000947 int addrlen, len, n, flags = 0;
948 if (!PyArg_ParseTuple(args, "i|i", &len, &flags))
949 return NULL;
Guido van Rossum18c9a4f1993-05-25 12:16:29 +0000950 if (!getsockaddrlen(s, &addrlen))
951 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000952 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum18c9a4f1993-05-25 12:16:29 +0000953 if (buf == NULL)
954 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000955 Py_BEGIN_ALLOW_THREADS
956 n = recvfrom(s->sock_fd, PyString_AsString(buf), len, flags,
Guido van Rossum8d665e61996-06-26 18:22:49 +0000957#ifndef MS_WINDOWS
Guido van Rossum32c575d1997-12-02 20:37:32 +0000958#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000959 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +0000960#else
Barry Warsaw752300b1997-01-03 17:18:10 +0000961 (ANY *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +0000962#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000963#else
Barry Warsaw752300b1997-01-03 17:18:10 +0000964 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000965#endif
Barry Warsaw752300b1997-01-03 17:18:10 +0000966 );
Guido van Rossum73624e91994-10-10 17:59:00 +0000967 Py_END_ALLOW_THREADS
Guido van Rossum7c53b771995-09-13 18:39:47 +0000968 if (n < 0) {
969 Py_DECREF(buf);
Guido van Rossum73624e91994-10-10 17:59:00 +0000970 return PySocket_Err();
Guido van Rossum7c53b771995-09-13 18:39:47 +0000971 }
Guido van Rossum53a9bf81996-06-11 18:35:24 +0000972 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000973 return NULL;
Barry Warsaw752300b1997-01-03 17:18:10 +0000974
975 if (!(addr = makesockaddr((struct sockaddr *)addrbuf, addrlen)))
976 goto finally;
977
Guido van Rossum73624e91994-10-10 17:59:00 +0000978 ret = Py_BuildValue("OO", buf, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +0000979 finally:
Guido van Rossum73624e91994-10-10 17:59:00 +0000980 Py_XDECREF(addr);
981 Py_XDECREF(buf);
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000982 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000983}
984
Guido van Rossum30a685f1991-06-27 15:51:29 +0000985
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000986/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000987
Guido van Rossum73624e91994-10-10 17:59:00 +0000988static PyObject *
989BUILD_FUNC_DEF_2(PySocketSock_send,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000990{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000991 char *buf;
Guido van Rossumbe32c891996-06-20 16:25:29 +0000992 int len, n, flags = 0;
993 if (!PyArg_ParseTuple(args, "s#|i", &buf, &len, &flags))
994 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000995 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000996 n = send(s->sock_fd, buf, len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +0000997 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +0000998 if (n < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000999 return PySocket_Err();
1000 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00001001}
1002
1003
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001004/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001005
Guido van Rossum73624e91994-10-10 17:59:00 +00001006static PyObject *
1007BUILD_FUNC_DEF_2(PySocketSock_sendto,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001008{
Guido van Rossum73624e91994-10-10 17:59:00 +00001009 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001010 char *buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001011 struct sockaddr *addr;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001012 int addrlen, len, n, flags;
1013 flags = 0;
Guido van Rossum73624e91994-10-10 17:59:00 +00001014 if (!PyArg_Parse(args, "(s#O)", &buf, &len, &addro)) {
1015 PyErr_Clear();
1016 if (!PyArg_Parse(args, "(s#iO)", &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001017 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001018 }
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001019 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001020 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001021 Py_BEGIN_ALLOW_THREADS
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001022 n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001023 Py_END_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001024 if (n < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001025 return PySocket_Err();
1026 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001027}
1028
Guido van Rossum30a685f1991-06-27 15:51:29 +00001029
1030/* s.shutdown(how) method */
1031
Guido van Rossum73624e91994-10-10 17:59:00 +00001032static PyObject *
1033BUILD_FUNC_DEF_2(PySocketSock_shutdown,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001034{
1035 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001036 int res;
Guido van Rossumfe28fa01997-05-15 19:09:26 +00001037 if (!PyArg_Parse(args, "i", &how))
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 Rossumff4949e1992-08-05 19:58:53 +00001040 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +00001041 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001042 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001043 return PySocket_Err();
1044 Py_INCREF(Py_None);
1045 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001046}
1047
Guido van Rossum30a685f1991-06-27 15:51:29 +00001048
1049/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001050
Guido van Rossum73624e91994-10-10 17:59:00 +00001051static PyMethodDef PySocketSock_methods[] = {
1052 {"accept", (PyCFunction)PySocketSock_accept},
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001053 {"setblocking", (PyCFunction)PySocketSock_setblocking},
Guido van Rossum73624e91994-10-10 17:59:00 +00001054 {"setsockopt", (PyCFunction)PySocketSock_setsockopt},
Guido van Rossumbe32c891996-06-20 16:25:29 +00001055 {"getsockopt", (PyCFunction)PySocketSock_getsockopt, 1},
Guido van Rossum73624e91994-10-10 17:59:00 +00001056 {"bind", (PyCFunction)PySocketSock_bind},
1057 {"close", (PyCFunction)PySocketSock_close},
1058 {"connect", (PyCFunction)PySocketSock_connect},
Barry Warsaw0d2d7591997-11-20 21:39:02 +00001059 {"connect_ex", (PyCFunction)PySocketSock_connect_ex},
Guido van Rossum73624e91994-10-10 17:59:00 +00001060 {"fileno", (PyCFunction)PySocketSock_fileno},
Guido van Rossumbe32c891996-06-20 16:25:29 +00001061#ifndef NO_DUP
1062 {"dup", (PyCFunction)PySocketSock_dup},
1063#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001064 {"getsockname", (PyCFunction)PySocketSock_getsockname},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001065#ifdef HAVE_GETPEERNAME
Guido van Rossum73624e91994-10-10 17:59:00 +00001066 {"getpeername", (PyCFunction)PySocketSock_getpeername},
Guido van Rossum9575a441993-04-07 14:06:14 +00001067#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001068 {"listen", (PyCFunction)PySocketSock_listen},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001069#ifndef NO_DUP
Guido van Rossum6b144911995-03-14 15:05:13 +00001070 {"makefile", (PyCFunction)PySocketSock_makefile, 1},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001071#endif
Guido van Rossumbe32c891996-06-20 16:25:29 +00001072 {"recv", (PyCFunction)PySocketSock_recv, 1},
1073 {"recvfrom", (PyCFunction)PySocketSock_recvfrom, 1},
1074 {"send", (PyCFunction)PySocketSock_send, 1},
Guido van Rossum73624e91994-10-10 17:59:00 +00001075 {"sendto", (PyCFunction)PySocketSock_sendto},
1076 {"shutdown", (PyCFunction)PySocketSock_shutdown},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001077 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001078};
1079
Guido van Rossum30a685f1991-06-27 15:51:29 +00001080
Guido van Rossum73624e91994-10-10 17:59:00 +00001081/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00001082 First close the file description. */
1083
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001084static void
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001085BUILD_FUNC_DEF_1(PySocketSock_dealloc,PySocketSockObject *,s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001086{
1087 (void) close(s->sock_fd);
Guido van Rossum73624e91994-10-10 17:59:00 +00001088 PyMem_DEL(s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001089}
1090
Guido van Rossum30a685f1991-06-27 15:51:29 +00001091
1092/* Return a socket object's named attribute. */
1093
Guido van Rossum73624e91994-10-10 17:59:00 +00001094static PyObject *
1095BUILD_FUNC_DEF_2(PySocketSock_getattr,PySocketSockObject *,s, char *,name)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001096{
Guido van Rossum73624e91994-10-10 17:59:00 +00001097 return Py_FindMethod(PySocketSock_methods, (PyObject *) s, name);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001098}
1099
Guido van Rossum30a685f1991-06-27 15:51:29 +00001100
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001101static PyObject *
1102BUILD_FUNC_DEF_1(PySocketSock_repr,PySocketSockObject *,s)
1103{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001104 char buf[512];
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001105 sprintf(buf,
1106 "<socket object, fd=%d, family=%d, type=%d, protocol=%d>",
1107 s->sock_fd, s->sock_family, s->sock_type, s->sock_proto);
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00001108 return PyString_FromString(buf);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001109}
1110
1111
Guido van Rossumb6775db1994-08-01 11:34:53 +00001112/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001113
Guido van Rossum73624e91994-10-10 17:59:00 +00001114static PyTypeObject PySocketSock_Type = {
Guido van Rossum55558541996-05-23 22:54:50 +00001115 PyObject_HEAD_INIT(0) /* Must fill in type value later */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001116 0,
1117 "socket",
Guido van Rossum73624e91994-10-10 17:59:00 +00001118 sizeof(PySocketSockObject),
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001119 0,
Guido van Rossum73624e91994-10-10 17:59:00 +00001120 (destructor)PySocketSock_dealloc, /*tp_dealloc*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001121 0, /*tp_print*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001122 (getattrfunc)PySocketSock_getattr, /*tp_getattr*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001123 0, /*tp_setattr*/
1124 0, /*tp_compare*/
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001125 (reprfunc)PySocketSock_repr, /*tp_repr*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001126 0, /*tp_as_number*/
1127 0, /*tp_as_sequence*/
1128 0, /*tp_as_mapping*/
1129};
1130
Guido van Rossum30a685f1991-06-27 15:51:29 +00001131
Guido van Rossum81194471991-07-27 21:42:02 +00001132/* Python interface to gethostname(). */
1133
1134/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001135static PyObject *
1136BUILD_FUNC_DEF_2(PySocket_gethostname,PyObject *,self, PyObject *,args)
Guido van Rossum81194471991-07-27 21:42:02 +00001137{
1138 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001139 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +00001140 if (!PyArg_NoArgs(args))
Guido van Rossum81194471991-07-27 21:42:02 +00001141 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001142 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001143 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00001144 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001145 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001146 return PySocket_Err();
Guido van Rossum81194471991-07-27 21:42:02 +00001147 buf[sizeof buf - 1] = '\0';
Guido van Rossum73624e91994-10-10 17:59:00 +00001148 return PyString_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00001149}
Guido van Rossumff4949e1992-08-05 19:58:53 +00001150
1151
Guido van Rossum30a685f1991-06-27 15:51:29 +00001152/* Python interface to gethostbyname(name). */
1153
1154/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001155static PyObject *
1156BUILD_FUNC_DEF_2(PySocket_gethostbyname,PyObject *,self, PyObject *,args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001157{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001158 char *name;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001159 struct sockaddr_in addrbuf;
Guido van Rossum73624e91994-10-10 17:59:00 +00001160 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00001161 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001162 if (setipaddr(name, &addrbuf) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001163 return NULL;
1164 return makeipaddr(&addrbuf);
1165}
1166
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001167/* Python interface to gethostbyaddr(IP). */
1168
1169/*ARGSUSED*/
1170static PyObject *
1171BUILD_FUNC_DEF_2(PySocket_gethostbyaddr,PyObject *,self, PyObject *, args)
1172{
1173 struct sockaddr_in addr;
1174 char *ip_num;
1175 struct hostent *h;
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00001176 char **pch;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001177 PyObject *rtn_tuple = (PyObject *)NULL;
1178 PyObject *name_list = (PyObject *)NULL;
1179 PyObject *addr_list = (PyObject *)NULL;
1180 PyObject *tmp;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001181#ifdef HAVE_GETHOSTBYNAME_R
1182 struct hostent hp_allocated;
1183 char buf[16384];
1184 int buf_len = (sizeof buf) - 1;
1185 int errnop;
1186#endif /* HAVE_GETHOSTBYNAME_R */
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001187
1188 if (!PyArg_Parse(args, "s", &ip_num))
1189 return NULL;
1190 if (setipaddr(ip_num, &addr) < 0)
1191 return NULL;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001192 Py_BEGIN_ALLOW_THREADS
1193#ifdef HAVE_GETHOSTBYNAME_R
1194 h = gethostbyaddr_r((char *)&addr.sin_addr,
1195 sizeof(addr.sin_addr),
1196 AF_INET,
1197 &hp_allocated, buf, buf_len, &errnop);
1198#else /* not HAVE_GETHOSTBYNAME_R */
1199#if defined(WITH_THREAD) && !defined(MS_WINDOWS)
1200 acquire_lock(gethostbyname_lock,1);
1201#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001202 h = gethostbyaddr((char *)&addr.sin_addr,
1203 sizeof(addr.sin_addr),
1204 AF_INET);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001205#if defined(WITH_THREAD) && !defined(MS_WINDOWS)
1206 release_lock(gethostbyname_lock);
1207#endif
1208#endif /* HAVE_GETHOSTBYNAME_R */
1209 Py_END_ALLOW_THREADS
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001210 if (h == NULL) {
Guido van Rossumca9b3231995-01-10 16:30:54 +00001211#ifdef HAVE_HSTRERROR
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001212 /* Let's get real error message to return */
1213 extern int h_errno;
1214 PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno));
1215#else
1216 PyErr_SetString(PySocket_Error, "host not found");
1217#endif
1218 return NULL;
1219 }
1220 if ((name_list = PyList_New(0)) == NULL)
1221 goto err;
1222 if ((addr_list = PyList_New(0)) == NULL)
1223 goto err;
1224 for (pch = h->h_aliases; *pch != NULL; pch++) {
Barry Warsaw752300b1997-01-03 17:18:10 +00001225 int status;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001226 tmp = PyString_FromString(*pch);
1227 if (tmp == NULL)
1228 goto err;
Barry Warsaw752300b1997-01-03 17:18:10 +00001229 status = PyList_Append(name_list, tmp);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001230 Py_DECREF(tmp);
Barry Warsaw752300b1997-01-03 17:18:10 +00001231 if (status)
1232 goto err;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001233 }
1234 for (pch = h->h_addr_list; *pch != NULL; pch++) {
Barry Warsaw752300b1997-01-03 17:18:10 +00001235 int status;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001236 memcpy((char *) &addr.sin_addr, *pch, h->h_length);
1237 tmp = makeipaddr(&addr);
1238 if (tmp == NULL)
1239 goto err;
Barry Warsaw752300b1997-01-03 17:18:10 +00001240 status = PyList_Append(addr_list, tmp);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001241 Py_DECREF(tmp);
Barry Warsaw752300b1997-01-03 17:18:10 +00001242 if (status)
1243 goto err;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001244 }
1245 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
1246 err:
1247 Py_XDECREF(name_list);
1248 Py_XDECREF(addr_list);
1249 return rtn_tuple;
1250}
1251
Guido van Rossum30a685f1991-06-27 15:51:29 +00001252
1253/* Python interface to getservbyname(name).
1254 This only returns the port number, since the other info is already
1255 known or not useful (like the list of aliases). */
1256
1257/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001258static PyObject *
1259BUILD_FUNC_DEF_2(PySocket_getservbyname,PyObject *,self, PyObject *,args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001260{
Guido van Rossumff4949e1992-08-05 19:58:53 +00001261 char *name, *proto;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001262 struct servent *sp;
Guido van Rossum73624e91994-10-10 17:59:00 +00001263 if (!PyArg_Parse(args, "(ss)", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00001264 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001265 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001266 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00001267 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00001268 if (sp == NULL) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001269 PyErr_SetString(PySocket_Error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00001270 return NULL;
1271 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001272 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00001273}
1274
1275
Guido van Rossum3901d851996-12-19 16:35:04 +00001276/* Python interface to getprotobyname(name).
1277 This only returns the protocol number, since the other info is
1278 already known or not useful (like the list of aliases). */
1279
1280/*ARGSUSED*/
1281static PyObject *
1282BUILD_FUNC_DEF_2(PySocket_getprotobyname,PyObject *,self, PyObject *,args)
1283{
1284 char *name;
1285 struct protoent *sp;
1286 if (!PyArg_Parse(args, "s", &name))
1287 return NULL;
1288 Py_BEGIN_ALLOW_THREADS
1289 sp = getprotobyname(name);
1290 Py_END_ALLOW_THREADS
1291 if (sp == NULL) {
1292 PyErr_SetString(PySocket_Error, "protocol not found");
1293 return NULL;
1294 }
1295 return PyInt_FromLong((long) sp->p_proto);
1296}
1297
1298
Guido van Rossum30a685f1991-06-27 15:51:29 +00001299/* Python interface to socket(family, type, proto).
1300 The third (protocol) argument is optional.
1301 Return a new socket object. */
1302
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001303/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001304static PyObject *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001305BUILD_FUNC_DEF_2(PySocket_socket,PyObject *,self, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001306{
Guido van Rossum73624e91994-10-10 17:59:00 +00001307 PySocketSockObject *s;
Guido van Rossum6f489d91996-06-28 20:15:15 +00001308#ifdef MS_WINDOWS
Guido van Rossumd639d4d1996-06-12 04:04:22 +00001309 SOCKET fd;
1310#else
1311 int fd;
1312#endif
Guido van Rossumbe32c891996-06-20 16:25:29 +00001313 int family, type, proto = 0;
1314 if (!PyArg_ParseTuple(args, "ii|i", &family, &type, &proto))
1315 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001316 Py_BEGIN_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001317 fd = socket(family, type, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00001318 Py_END_ALLOW_THREADS
Guido van Rossum6f489d91996-06-28 20:15:15 +00001319#ifdef MS_WINDOWS
Guido van Rossumd639d4d1996-06-12 04:04:22 +00001320 if (fd == INVALID_SOCKET)
1321#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001322 if (fd < 0)
Guido van Rossumd639d4d1996-06-12 04:04:22 +00001323#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001324 return PySocket_Err();
1325 s = PySocketSock_New(fd, family, type, proto);
Guido van Rossum30a685f1991-06-27 15:51:29 +00001326 /* If the object can't be created, don't forget to close the
1327 file descriptor again! */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001328 if (s == NULL)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001329 (void) close(fd);
Guido van Rossum81194471991-07-27 21:42:02 +00001330 /* From now on, ignore SIGPIPE and let the error checking
1331 do the work. */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001332#ifdef SIGPIPE
Guido van Rossum81194471991-07-27 21:42:02 +00001333 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001334#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001335 return (PyObject *) s;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001336}
1337
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001338#ifndef NO_DUP
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001339/* Create a socket object from a numeric file description.
1340 Useful e.g. if stdin is a socket.
1341 Additional arguments as for socket(). */
1342
1343/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001344static PyObject *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001345BUILD_FUNC_DEF_2(PySocket_fromfd,PyObject *,self, PyObject *,args)
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001346{
Guido van Rossum73624e91994-10-10 17:59:00 +00001347 PySocketSockObject *s;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001348 int fd, family, type, proto = 0;
1349 if (!PyArg_ParseTuple(args, "iii|i", &fd, &family, &type, &proto))
1350 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +00001351 /* Dup the fd so it and the socket can be closed independently */
1352 fd = dup(fd);
1353 if (fd < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001354 return PySocket_Err();
1355 s = PySocketSock_New(fd, family, type, proto);
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001356 /* From now on, ignore SIGPIPE and let the error checking
1357 do the work. */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001358#ifdef SIGPIPE
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001359 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001360#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001361 return (PyObject *) s;
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001362}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001363#endif /* NO_DUP */
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001364
Guido van Rossum006bf911996-06-12 04:04:55 +00001365static PyObject *
1366BUILD_FUNC_DEF_2(PySocket_ntohs, PyObject *, self, PyObject *, args)
1367{
1368 int x1, x2;
1369
1370 if (!PyArg_Parse(args, "i", &x1)) {
1371 return NULL;
1372 }
1373 x2 = (int)ntohs((short)x1);
1374 return PyInt_FromLong(x2);
1375}
1376
1377static PyObject *
1378BUILD_FUNC_DEF_2(PySocket_ntohl, PyObject *, self, PyObject *, args)
1379{
1380 int x1, x2;
1381
1382 if (!PyArg_Parse(args, "i", &x1)) {
1383 return NULL;
1384 }
1385 x2 = ntohl(x1);
1386 return PyInt_FromLong(x2);
1387}
1388
1389static PyObject *
1390BUILD_FUNC_DEF_2(PySocket_htons, PyObject *, self, PyObject *, args)
1391{
1392 int x1, x2;
1393
1394 if (!PyArg_Parse(args, "i", &x1)) {
1395 return NULL;
1396 }
1397 x2 = (int)htons((short)x1);
1398 return PyInt_FromLong(x2);
1399}
1400
1401static PyObject *
1402BUILD_FUNC_DEF_2(PySocket_htonl, PyObject *, self, PyObject *, args)
1403{
1404 int x1, x2;
1405
1406 if (!PyArg_Parse(args, "i", &x1)) {
1407 return NULL;
1408 }
1409 x2 = htonl(x1);
1410 return PyInt_FromLong(x2);
1411}
1412
Guido van Rossum30a685f1991-06-27 15:51:29 +00001413/* List of functions exported by this module. */
1414
Guido van Rossum73624e91994-10-10 17:59:00 +00001415static PyMethodDef PySocket_methods[] = {
1416 {"gethostbyname", PySocket_gethostbyname},
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001417 {"gethostbyaddr", PySocket_gethostbyaddr},
Guido van Rossum73624e91994-10-10 17:59:00 +00001418 {"gethostname", PySocket_gethostname},
1419 {"getservbyname", PySocket_getservbyname},
Guido van Rossum3901d851996-12-19 16:35:04 +00001420 {"getprotobyname", PySocket_getprotobyname},
Guido van Rossumbe32c891996-06-20 16:25:29 +00001421 {"socket", PySocket_socket, 1},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001422#ifndef NO_DUP
Guido van Rossumbe32c891996-06-20 16:25:29 +00001423 {"fromfd", PySocket_fromfd, 1},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001424#endif
Guido van Rossum006bf911996-06-12 04:04:55 +00001425 {"ntohs", PySocket_ntohs},
1426 {"ntohl", PySocket_ntohl},
1427 {"htons", PySocket_htons},
1428 {"htonl", PySocket_htonl},
Guido van Rossum30a685f1991-06-27 15:51:29 +00001429 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001430};
1431
Guido van Rossum30a685f1991-06-27 15:51:29 +00001432
1433/* Convenience routine to export an integer value.
Barry Warsaw752300b1997-01-03 17:18:10 +00001434 *
Guido van Rossum0cb96de1997-10-01 04:29:29 +00001435 * Errors are silently ignored, for better or for worse...
Barry Warsaw752300b1997-01-03 17:18:10 +00001436 */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001437static void
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001438BUILD_FUNC_DEF_3(insint,PyObject *,d, char *,name, int,value)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001439{
Guido van Rossum73624e91994-10-10 17:59:00 +00001440 PyObject *v = PyInt_FromLong((long) value);
Barry Warsaw752300b1997-01-03 17:18:10 +00001441 if (!v || PyDict_SetItemString(d, name, v))
Guido van Rossum0cb96de1997-10-01 04:29:29 +00001442 PyErr_Clear();
Barry Warsaw752300b1997-01-03 17:18:10 +00001443
Guido van Rossum0cb96de1997-10-01 04:29:29 +00001444 Py_XDECREF(v);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001445}
1446
Guido van Rossum30a685f1991-06-27 15:51:29 +00001447
Guido van Rossum8d665e61996-06-26 18:22:49 +00001448#ifdef MS_WINDOWS
Guido van Rossumbe32c891996-06-20 16:25:29 +00001449
1450/* Additional initialization and cleanup for NT/Windows */
1451
1452static void
1453NTcleanup()
1454{
1455 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00001456}
1457
1458static int
1459NTinit()
1460{
1461 WSADATA WSAData;
1462 int ret;
1463 char buf[100];
1464 ret = WSAStartup(0x0101, &WSAData);
1465 switch (ret) {
1466 case 0: /* no error */
1467 atexit(NTcleanup);
1468 return 1;
1469 case WSASYSNOTREADY:
1470 PyErr_SetString(PyExc_ImportError,
1471 "WSAStartup failed: network not ready");
1472 break;
1473 case WSAVERNOTSUPPORTED:
1474 case WSAEINVAL:
1475 PyErr_SetString(PyExc_ImportError,
1476 "WSAStartup failed: requested version not supported");
1477 break;
1478 default:
1479 sprintf(buf, "WSAStartup failed: error code %d", ret);
1480 PyErr_SetString(PyExc_ImportError, buf);
1481 break;
1482 }
1483 return 0;
1484}
1485
Guido van Rossum8d665e61996-06-26 18:22:49 +00001486#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00001487
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001488#if defined(PYOS_OS2)
1489
1490/* Additional initialization and cleanup for OS/2 */
1491
1492static void
1493OS2cleanup()
1494{
1495 /* No cleanup is necessary for OS/2 Sockets */
1496}
1497
1498static int
1499OS2init()
1500{
1501 char reason[64];
1502 int rc = sock_init();
1503
1504 if (rc == 0) {
Guido van Rossum32c575d1997-12-02 20:37:32 +00001505 atexit(OS2cleanup);
1506 return 1; /* Indicate Success */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001507 }
1508
1509 sprintf(reason, "OS/2 TCP/IP Error# %d", sock_errno());
1510 PyErr_SetString(PyExc_ImportError, reason);
1511
Guido van Rossum32c575d1997-12-02 20:37:32 +00001512 return 0; /* Indicate Failure */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001513}
1514
1515#endif /* PYOS_OS2 */
1516
Guido van Rossumbe32c891996-06-20 16:25:29 +00001517
Guido van Rossum30a685f1991-06-27 15:51:29 +00001518/* Initialize this module.
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001519 * This is called when the first 'import socket' is done,
1520 * via a table in config.c, if config.c is compiled with USE_SOCKET
1521 * defined.
1522 *
1523 * For MS_WINDOWS (which means any Windows variant), this module
1524 * is actually called "_socket", and there's a wrapper "socket.py"
1525 * which implements some missing functionality (such as makefile(),
1526 * dup() and fromfd()). The import of "_socket" may fail with an
1527 * ImportError exception if initialization of WINSOCK fails. When
1528 * WINSOCK is initialized succesfully, a call to WSACleanup() is
1529 * scheduled to be made at exit time.
1530 *
1531 * For OS/2, this module is also called "_socket" and uses a wrapper
1532 * "socket.py" which implements that functionality that is missing
1533 * when PC operating systems don't put socket descriptors in the
1534 * operating system's filesystem layer.
1535 */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001536
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001537void
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001538#if defined(MS_WINDOWS) || defined(PYOS_OS2)
Guido van Rossumbe32c891996-06-20 16:25:29 +00001539init_socket()
1540#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001541initsocket()
Guido van Rossumbe32c891996-06-20 16:25:29 +00001542#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001543{
Guido van Rossum73624e91994-10-10 17:59:00 +00001544 PyObject *m, *d;
Guido van Rossum8d665e61996-06-26 18:22:49 +00001545#ifdef MS_WINDOWS
Guido van Rossumbe32c891996-06-20 16:25:29 +00001546 if (!NTinit())
1547 return;
1548 m = Py_InitModule("_socket", PySocket_methods);
1549#else
Guido van Rossum32c575d1997-12-02 20:37:32 +00001550#if defined(__TOS_OS2__)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001551 if (!OS2init())
1552 return;
1553 m = Py_InitModule("_socket", PySocket_methods);
Guido van Rossum32c575d1997-12-02 20:37:32 +00001554#else
Guido van Rossum73624e91994-10-10 17:59:00 +00001555 m = Py_InitModule("socket", PySocket_methods);
Guido van Rossum32c575d1997-12-02 20:37:32 +00001556#endif
Guido van Rossumbe32c891996-06-20 16:25:29 +00001557#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001558 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +00001559 PySocket_Error = PyErr_NewException("socket.error", NULL, NULL);
1560 if (PySocket_Error == NULL)
1561 return;
1562 PyDict_SetItemString(d, "error", PySocket_Error);
Guido van Rossum3b4b6fc1997-05-21 14:37:37 +00001563 PySocketSock_Type.ob_type = &PyType_Type;
1564 Py_INCREF(&PySocketSock_Type);
Guido van Rossum91ba64d1997-06-02 22:18:09 +00001565 if (PyDict_SetItemString(d, "SocketType",
Guido van Rossum3b4b6fc1997-05-21 14:37:37 +00001566 (PyObject *)&PySocketSock_Type) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00001567 return;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001568 insint(d, "AF_INET", AF_INET);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001569#ifdef AF_UNIX
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001570 insint(d, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001571#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001572 insint(d, "SOCK_STREAM", SOCK_STREAM);
1573 insint(d, "SOCK_DGRAM", SOCK_DGRAM);
1574 insint(d, "SOCK_RAW", SOCK_RAW);
1575 insint(d, "SOCK_SEQPACKET", SOCK_SEQPACKET);
1576 insint(d, "SOCK_RDM", SOCK_RDM);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00001577
1578#ifdef SO_DEBUG
1579 insint(d, "SO_DEBUG", SO_DEBUG);
1580#endif
1581#ifdef SO_ACCEPTCONN
1582 insint(d, "SO_ACCEPTCONN", SO_ACCEPTCONN);
1583#endif
1584#ifdef SO_REUSEADDR
1585 insint(d, "SO_REUSEADDR", SO_REUSEADDR);
1586#endif
1587#ifdef SO_KEEPALIVE
1588 insint(d, "SO_KEEPALIVE", SO_KEEPALIVE);
1589#endif
1590#ifdef SO_DONTROUTE
1591 insint(d, "SO_DONTROUTE", SO_DONTROUTE);
1592#endif
1593#ifdef SO_BROADCAST
1594 insint(d, "SO_BROADCAST", SO_BROADCAST);
1595#endif
1596#ifdef SO_USELOOPBACK
1597 insint(d, "SO_USELOOPBACK", SO_USELOOPBACK);
1598#endif
1599#ifdef SO_LINGER
1600 insint(d, "SO_LINGER", SO_LINGER);
1601#endif
1602#ifdef SO_OOBINLINE
1603 insint(d, "SO_OOBINLINE", SO_OOBINLINE);
1604#endif
1605#ifdef SO_REUSEPORT
1606 insint(d, "SO_REUSEPORT", SO_REUSEPORT);
1607#endif
1608
1609#ifdef SO_SNDBUF
1610 insint(d, "SO_SNDBUF", SO_SNDBUF);
1611#endif
1612#ifdef SO_RCVBUF
1613 insint(d, "SO_RCVBUF", SO_RCVBUF);
1614#endif
1615#ifdef SO_SNDLOWAT
1616 insint(d, "SO_SNDLOWAT", SO_SNDLOWAT);
1617#endif
1618#ifdef SO_RCVLOWAT
1619 insint(d, "SO_RCVLOWAT", SO_RCVLOWAT);
1620#endif
1621#ifdef SO_SNDTIMEO
1622 insint(d, "SO_SNDTIMEO", SO_SNDTIMEO);
1623#endif
1624#ifdef SO_RCVTIMEO
1625 insint(d, "SO_RCVTIMEO", SO_RCVTIMEO);
1626#endif
1627#ifdef SO_ERROR
1628 insint(d, "SO_ERROR", SO_ERROR);
1629#endif
1630#ifdef SO_TYPE
1631 insint(d, "SO_TYPE", SO_TYPE);
1632#endif
1633
1634 /* Maximum number of connections for "listen" */
1635#ifdef SOMAXCONN
1636 insint(d, "SOMAXCONN", SOMAXCONN);
1637#else
1638 insint(d, "SOMAXCONN", 5); /* Common value */
1639#endif
1640
1641 /* Flags for send, recv */
1642#ifdef MSG_OOB
1643 insint(d, "MSG_OOB", MSG_OOB);
1644#endif
1645#ifdef MSG_PEEK
1646 insint(d, "MSG_PEEK", MSG_PEEK);
1647#endif
1648#ifdef MSG_DONTROUTE
1649 insint(d, "MSG_DONTROUTE", MSG_DONTROUTE);
1650#endif
1651#ifdef MSG_EOR
1652 insint(d, "MSG_EOR", MSG_EOR);
1653#endif
1654#ifdef MSG_TRUNC
1655 insint(d, "MSG_TRUNC", MSG_TRUNC);
1656#endif
1657#ifdef MSG_CTRUNC
1658 insint(d, "MSG_CTRUNC", MSG_CTRUNC);
1659#endif
1660#ifdef MSG_WAITALL
1661 insint(d, "MSG_WAITALL", MSG_WAITALL);
1662#endif
1663#ifdef MSG_BTAG
1664 insint(d, "MSG_BTAG", MSG_BTAG);
1665#endif
1666#ifdef MSG_ETAG
1667 insint(d, "MSG_ETAG", MSG_ETAG);
1668#endif
1669
1670 /* Protocol level and numbers, usable for [gs]etsockopt */
Guido van Rossum578de301998-05-28 20:18:18 +00001671/* Sigh -- some systems (e.g. Linux) use enums for these. */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00001672#ifdef SOL_SOCKET
1673 insint(d, "SOL_SOCKET", SOL_SOCKET);
1674#endif
1675#ifdef IPPROTO_IP
1676 insint(d, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00001677#else
1678 insint(d, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00001679#endif
1680#ifdef IPPROTO_ICMP
1681 insint(d, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00001682#else
1683 insint(d, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00001684#endif
1685#ifdef IPPROTO_IGMP
1686 insint(d, "IPPROTO_IGMP", IPPROTO_IGMP);
1687#endif
1688#ifdef IPPROTO_GGP
1689 insint(d, "IPPROTO_GGP", IPPROTO_GGP);
1690#endif
1691#ifdef IPPROTO_TCP
1692 insint(d, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00001693#else
1694 insint(d, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00001695#endif
1696#ifdef IPPROTO_EGP
1697 insint(d, "IPPROTO_EGP", IPPROTO_EGP);
1698#endif
1699#ifdef IPPROTO_PUP
1700 insint(d, "IPPROTO_PUP", IPPROTO_PUP);
1701#endif
1702#ifdef IPPROTO_UDP
1703 insint(d, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00001704#else
1705 insint(d, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00001706#endif
1707#ifdef IPPROTO_IDP
1708 insint(d, "IPPROTO_IDP", IPPROTO_IDP);
1709#endif
1710#ifdef IPPROTO_HELLO
1711 insint(d, "IPPROTO_HELLO", IPPROTO_HELLO);
1712#endif
1713#ifdef IPPROTO_ND
1714 insint(d, "IPPROTO_ND", IPPROTO_ND);
1715#endif
1716#ifdef IPPROTO_TP
1717 insint(d, "IPPROTO_TP", IPPROTO_TP);
1718#endif
1719#ifdef IPPROTO_XTP
1720 insint(d, "IPPROTO_XTP", IPPROTO_XTP);
1721#endif
1722#ifdef IPPROTO_EON
1723 insint(d, "IPPROTO_EON", IPPROTO_EON);
1724#endif
1725#ifdef IPPROTO_BIP
1726 insint(d, "IPPROTO_BIP", IPPROTO_BIP);
1727#endif
1728/**/
1729#ifdef IPPROTO_RAW
1730 insint(d, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00001731#else
1732 insint(d, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00001733#endif
1734#ifdef IPPROTO_MAX
1735 insint(d, "IPPROTO_MAX", IPPROTO_MAX);
1736#endif
1737
1738 /* Some port configuration */
1739#ifdef IPPORT_RESERVED
1740 insint(d, "IPPORT_RESERVED", IPPORT_RESERVED);
1741#else
1742 insint(d, "IPPORT_RESERVED", 1024);
1743#endif
1744#ifdef IPPORT_USERRESERVED
1745 insint(d, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
1746#else
1747 insint(d, "IPPORT_USERRESERVED", 5000);
1748#endif
1749
Guido van Rossum5f05eb41995-02-17 15:11:07 +00001750 /* Some reserved IP v.4 addresses */
1751#ifdef INADDR_ANY
1752 insint(d, "INADDR_ANY", INADDR_ANY);
1753#else
1754 insint(d, "INADDR_ANY", 0x00000000);
1755#endif
1756#ifdef INADDR_BROADCAST
1757 insint(d, "INADDR_BROADCAST", INADDR_BROADCAST);
1758#else
1759 insint(d, "INADDR_BROADCAST", 0xffffffff);
1760#endif
1761#ifdef INADDR_LOOPBACK
1762 insint(d, "INADDR_LOOPBACK", INADDR_LOOPBACK);
1763#else
1764 insint(d, "INADDR_LOOPBACK", 0x7F000001);
1765#endif
1766#ifdef INADDR_UNSPEC_GROUP
1767 insint(d, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
1768#else
1769 insint(d, "INADDR_UNSPEC_GROUP", 0xe0000000);
1770#endif
1771#ifdef INADDR_ALLHOSTS_GROUP
1772 insint(d, "INADDR_ALLHOSTS_GROUP", INADDR_ALLHOSTS_GROUP);
1773#else
1774 insint(d, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
1775#endif
1776#ifdef INADDR_MAX_LOCAL_GROUP
1777 insint(d, "INADDR_MAX_LOCAL_GROUP", INADDR_MAX_LOCAL_GROUP);
1778#else
1779 insint(d, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
1780#endif
1781#ifdef INADDR_NONE
1782 insint(d, "INADDR_NONE", INADDR_NONE);
1783#else
1784 insint(d, "INADDR_NONE", 0xffffffff);
1785#endif
1786
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00001787 /* IP [gs]etsockopt options */
1788#ifdef IP_OPTIONS
1789 insint(d, "IP_OPTIONS", IP_OPTIONS);
1790#endif
1791#ifdef IP_HDRINCL
1792 insint(d, "IP_HDRINCL", IP_HDRINCL);
1793#endif
1794#ifdef IP_TOS
1795 insint(d, "IP_TOS", IP_TOS);
1796#endif
1797#ifdef IP_TTL
1798 insint(d, "IP_TTL", IP_TTL);
1799#endif
1800#ifdef IP_RECVOPTS
1801 insint(d, "IP_RECVOPTS", IP_RECVOPTS);
1802#endif
1803#ifdef IP_RECVRETOPTS
1804 insint(d, "IP_RECVRETOPTS", IP_RECVRETOPTS);
1805#endif
1806#ifdef IP_RECVDSTADDR
1807 insint(d, "IP_RECVDSTADDR", IP_RECVDSTADDR);
1808#endif
1809#ifdef IP_RETOPTS
1810 insint(d, "IP_RETOPTS", IP_RETOPTS);
1811#endif
1812#ifdef IP_MULTICAST_IF
1813 insint(d, "IP_MULTICAST_IF", IP_MULTICAST_IF);
1814#endif
1815#ifdef IP_MULTICAST_TTL
1816 insint(d, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
1817#endif
1818#ifdef IP_MULTICAST_LOOP
1819 insint(d, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
1820#endif
1821#ifdef IP_ADD_MEMBERSHIP
1822 insint(d, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
1823#endif
1824#ifdef IP_DROP_MEMBERSHIP
1825 insint(d, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
1826#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00001827
1828 /* Initialize gethostbyname lock */
1829#if defined(WITH_THREAD) && !defined(HAVE_GETHOSTBYNAME_R) && !defined(MS_WINDOWS)
1830 gethostbyname_lock = allocate_lock();
1831#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001832}