blob: 6c7253962011ff84d55bc450263b868636b9e937 [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')
49- socket.getservbyname(servicename, protocolname) --> port number
Guido van Rossum4dd2a7e1991-07-01 18:51:33 +000050- socket.socket(family, type [, proto]) --> new socket object
Guido van Rossum006bf911996-06-12 04:04:55 +000051- socket.ntohs(16 bit value) --> new int object
52- socket.ntohl(32 bit value) --> new int object
53- socket.htons(16 bit value) --> new int object
54- socket.htonl(32 bit value) --> new int object
Guido van Rossum27e177d1995-03-16 15:43:47 +000055- socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h>
Guido van Rossum6574b3e1991-06-25 21:36:08 +000056- an Internet socket address is a pair (hostname, port)
57 where hostname can be anything recognized by gethostbyname()
58 (including the dd.dd.dd.dd notation) and port is in host byte order
59- where a hostname is returned, the dd.dd.dd.dd notation is used
Guido van Rossum27e177d1995-03-16 15:43:47 +000060- a UNIX domain socket address is a string specifying the pathname
Guido van Rossum6574b3e1991-06-25 21:36:08 +000061
Guido van Rossum30a685f1991-06-27 15:51:29 +000062Socket methods:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000063
Guido van Rossum81194471991-07-27 21:42:02 +000064- s.accept() --> new socket object, sockaddr
Guido van Rossum27e177d1995-03-16 15:43:47 +000065- s.bind(sockaddr) --> None
66- s.close() --> None
67- s.connect(sockaddr) --> None
68- s.fileno() --> file descriptor
Guido van Rossumbe32c891996-06-20 16:25:29 +000069- s.dup() --> same as socket.fromfd(os.dup(s.fileno(), ...)
Guido van Rossumc89705d1992-11-26 08:54:07 +000070- s.getpeername() --> sockaddr
Guido van Rossum27e177d1995-03-16 15:43:47 +000071- s.getsockname() --> sockaddr
72- s.getsockopt(level, optname[, buflen]) --> int or string
73- s.listen(backlog) --> None
Guido van Rossum6b144911995-03-14 15:05:13 +000074- s.makefile([mode[, bufsize]]) --> file object
Guido van Rossum27e177d1995-03-16 15:43:47 +000075- s.recv(buflen [,flags]) --> string
76- s.recvfrom(buflen [,flags]) --> string, sockaddr
Guido van Rossumb6775db1994-08-01 11:34:53 +000077- s.send(string [,flags]) --> nbytes
78- s.sendto(string, [flags,] sockaddr) --> nbytes
Guido van Rossum27e177d1995-03-16 15:43:47 +000079- s.setblocking(0 | 1) --> None
80- s.setsockopt(level, optname, value) --> None
81- s.shutdown(how) --> None
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000082- repr(s) --> "<socket object, fd=%d, family=%d, type=%d, protocol=%d>"
Guido van Rossum30a685f1991-06-27 15:51:29 +000083
Guido van Rossum6574b3e1991-06-25 21:36:08 +000084*/
85
Guido van Rossum73624e91994-10-10 17:59:00 +000086#include "Python.h"
Guido van Rossum6574b3e1991-06-25 21:36:08 +000087
Guido van Rossuma376cc51996-12-05 23:43:35 +000088#ifdef HAVE_UNISTD_H
89#include <unistd.h>
90#endif
91
Guido van Rossumfb4130d1996-12-10 15:17:08 +000092#ifndef MS_WINDOWS
93extern int gethostname(); /* For Solaris, at least */
94#endif
95
Guido van Rossumb6775db1994-08-01 11:34:53 +000096#include <sys/types.h>
97#include "mytime.h"
Guido van Rossumed233a51992-06-23 09:07:03 +000098
Guido van Rossum81194471991-07-27 21:42:02 +000099#include <signal.h>
Guido van Rossum6f489d91996-06-28 20:15:15 +0000100#ifndef MS_WINDOWS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000101#include <netdb.h>
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000102#include <sys/socket.h>
103#include <netinet/in.h>
Guido van Rossume4485b01994-09-07 14:32:49 +0000104#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000105#else
106#include <winsock.h>
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000107#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000108#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000109#ifdef HAVE_SYS_UN_H
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000110#include <sys/un.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000111#else
112#undef AF_UNIX
Guido van Rossum9575a441993-04-07 14:06:14 +0000113#endif
114
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000115#ifndef O_NDELAY
116#define O_NDELAY O_NONBLOCK /* For QNX only? */
117#endif
118
Jack Jansen508537b1996-02-14 15:57:45 +0000119#ifdef USE_GUSI
120/* fdopen() isn't declared in stdio.h (sigh) */
121#include <GUSI.h>
122#endif
123
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000124
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000125/* Here we have some hacks to choose between K&R or ANSI style function
126 definitions. For NT to build this as an extension module (ie, DLL)
127 it must be compiled by the C++ compiler, as it takes the address of
128 a static data item exported from the main Python DLL.
129*/
Guido van Rossum8d665e61996-06-26 18:22:49 +0000130#ifdef MS_WINDOWS
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000131/* seem to be a few differences in the API */
132#define close closesocket
Guido van Rossumbe32c891996-06-20 16:25:29 +0000133#define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000134#define FORCE_ANSI_FUNC_DEFS
135#endif
136
137#ifdef FORCE_ANSI_FUNC_DEFS
138#define BUILD_FUNC_DEF_1( fnname, arg1type, arg1name ) \
139fnname( arg1type arg1name )
140
141#define BUILD_FUNC_DEF_2( fnname, arg1type, arg1name, arg2type, arg2name ) \
142fnname( arg1type arg1name, arg2type arg2name )
143
144#define BUILD_FUNC_DEF_3( fnname, arg1type, arg1name, arg2type, arg2name , arg3type, arg3name ) \
145fnname( arg1type arg1name, arg2type arg2name, arg3type arg3name )
146
147#define BUILD_FUNC_DEF_4( fnname, arg1type, arg1name, arg2type, arg2name , arg3type, arg3name, arg4type, arg4name ) \
148fnname( arg1type arg1name, arg2type arg2name, arg3type arg3name, arg4type arg4name )
149
150#else /* !FORCE_ANSI_FN_DEFS */
151#define BUILD_FUNC_DEF_1( fnname, arg1type, arg1name ) \
152fnname( arg1name ) \
153 arg1type arg1name;
154
155#define BUILD_FUNC_DEF_2( fnname, arg1type, arg1name, arg2type, arg2name ) \
156fnname( arg1name, arg2name ) \
157 arg1type arg1name; \
158 arg2type arg2name;
159
160#define BUILD_FUNC_DEF_3( fnname, arg1type, arg1name, arg2type, arg2name, arg3type, arg3name ) \
161fnname( arg1name, arg2name, arg3name ) \
162 arg1type arg1name; \
163 arg2type arg2name; \
164 arg3type arg3name;
165
166#define BUILD_FUNC_DEF_4( fnname, arg1type, arg1name, arg2type, arg2name, arg3type, arg3name, arg4type, arg4name ) \
167fnname( arg1name, arg2name, arg3name, arg4name ) \
168 arg1type arg1name; \
169 arg2type arg2name; \
170 arg3type arg3name; \
171 arg4type arg4name;
172
173#endif /* !FORCE_ANSI_FN_DEFS */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000174
175/* Global variable holding the exception type for errors detected
176 by this module (but not argument type or memory errors, etc.). */
177
Guido van Rossum73624e91994-10-10 17:59:00 +0000178static PyObject *PySocket_Error;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000179
180
181/* Convenience function to raise an error according to errno
182 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000183
Guido van Rossum73624e91994-10-10 17:59:00 +0000184static PyObject *
185PySocket_Err()
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000186{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000187#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000188 if (WSAGetLastError()) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000189 PyObject *v;
190 v = Py_BuildValue("(is)", WSAGetLastError(), "winsock error");
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000191 if (v != NULL) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000192 PyErr_SetObject(PySocket_Error, v);
193 Py_DECREF(v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000194 }
195 return NULL;
196 }
197 else
198#endif
Guido van Rossum73624e91994-10-10 17:59:00 +0000199 return PyErr_SetFromErrno(PySocket_Error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000200}
201
Guido van Rossum30a685f1991-06-27 15:51:29 +0000202
203/* The object holding a socket. It holds some extra information,
204 like the address family, which is used to decode socket address
205 arguments properly. */
206
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000207typedef struct {
Guido van Rossum73624e91994-10-10 17:59:00 +0000208 PyObject_HEAD
Guido van Rossum30a685f1991-06-27 15:51:29 +0000209 int sock_fd; /* Socket file descriptor */
210 int sock_family; /* Address family, e.g., AF_INET */
211 int sock_type; /* Socket type, e.g., SOCK_STREAM */
212 int sock_proto; /* Protocol type, usually 0 */
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000213 union sock_addr {
214 struct sockaddr_in in;
215#ifdef AF_UNIX
216 struct sockaddr_un un;
217#endif
218 } sock_addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000219} PySocketSockObject;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000220
Guido van Rossum30a685f1991-06-27 15:51:29 +0000221
222/* A forward reference to the Socktype type object.
223 The Socktype variable contains pointers to various functions,
Guido van Rossum73624e91994-10-10 17:59:00 +0000224 some of which call newsockobject(), which uses Socktype, so
Guido van Rossum54ba21b1991-09-10 14:57:12 +0000225 there has to be a circular reference. */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000226
Guido van Rossum73624e91994-10-10 17:59:00 +0000227staticforward PyTypeObject PySocketSock_Type;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000228
Guido van Rossum30a685f1991-06-27 15:51:29 +0000229
230/* Create a new socket object.
231 This just creates the object and initializes it.
232 If the creation fails, return NULL and set an exception (implicit
233 in NEWOBJ()). */
234
Guido van Rossum73624e91994-10-10 17:59:00 +0000235static PySocketSockObject *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000236BUILD_FUNC_DEF_4(PySocketSock_New,int,fd, int,family, int,type, int,proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000237{
Guido van Rossum73624e91994-10-10 17:59:00 +0000238 PySocketSockObject *s;
Guido van Rossum55558541996-05-23 22:54:50 +0000239 PySocketSock_Type.ob_type = &PyType_Type;
Guido van Rossum73624e91994-10-10 17:59:00 +0000240 s = PyObject_NEW(PySocketSockObject, &PySocketSock_Type);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000241 if (s != NULL) {
242 s->sock_fd = fd;
243 s->sock_family = family;
244 s->sock_type = type;
245 s->sock_proto = proto;
246 }
247 return s;
248}
249
Guido van Rossum30a685f1991-06-27 15:51:29 +0000250
251/* Convert a string specifying a host name or one of a few symbolic
252 names to a numeric IP address. This usually calls gethostbyname()
253 to do the work; the names "" and "<broadcast>" are special.
254 Return the length (should always be 4 bytes), or negative if
255 an error occurred; then an exception is raised. */
256
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000257static int
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000258BUILD_FUNC_DEF_2(setipaddr, char*,name, struct sockaddr_in *,addr_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000259{
260 struct hostent *hp;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000261 int d1, d2, d3, d4;
262 char ch;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000263#ifdef HAVE_GETHOSTBYNAME_R
264 struct hostent hp_allocated;
265 char buf[1001];
266 int buf_len = (sizeof buf) - 1;
267 int errnop;
268#endif /* HAVE_GETHOSTBYNAME_R */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000269
Guido van Rossuma376cc51996-12-05 23:43:35 +0000270 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
Guido van Rossum30a685f1991-06-27 15:51:29 +0000271 if (name[0] == '\0') {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000272 addr_ret->sin_addr.s_addr = INADDR_ANY;
273 return 4;
274 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000275 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000276 addr_ret->sin_addr.s_addr = INADDR_BROADCAST;
277 return 4;
278 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000279 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
280 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
281 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
282 addr_ret->sin_addr.s_addr = htonl(
283 ((long) d1 << 24) | ((long) d2 << 16) |
284 ((long) d3 << 8) | ((long) d4 << 0));
285 return 4;
286 }
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000287#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum73624e91994-10-10 17:59:00 +0000288 Py_BEGIN_ALLOW_THREADS
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000289 hp = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum73624e91994-10-10 17:59:00 +0000290 Py_END_ALLOW_THREADS
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000291#else /* not HAVE_GETHOSTBYNAME_R */
292 hp = gethostbyname(name);
293#endif /* HAVE_GETHOSTBYNAME_R */
294
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000295 if (hp == NULL) {
Guido van Rossumca9b3231995-01-10 16:30:54 +0000296#ifdef HAVE_HSTRERROR
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000297 /* Let's get real error message to return */
298 extern int h_errno;
299 PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno));
300#else
Guido van Rossum73624e91994-10-10 17:59:00 +0000301 PyErr_SetString(PySocket_Error, "host not found");
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000302#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000303 return -1;
304 }
305 memcpy((char *) &addr_ret->sin_addr, hp->h_addr, hp->h_length);
306 return hp->h_length;
307}
308
Guido van Rossum30a685f1991-06-27 15:51:29 +0000309
Guido van Rossum30a685f1991-06-27 15:51:29 +0000310/* Create a string object representing an IP address.
311 This is always a string of the form 'dd.dd.dd.dd' (with variable
312 size numbers). */
313
Guido van Rossum73624e91994-10-10 17:59:00 +0000314static PyObject *
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000315BUILD_FUNC_DEF_1(makeipaddr, struct sockaddr_in *,addr)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000316{
317 long x = ntohl(addr->sin_addr.s_addr);
318 char buf[100];
319 sprintf(buf, "%d.%d.%d.%d",
320 (int) (x>>24) & 0xff, (int) (x>>16) & 0xff,
321 (int) (x>> 8) & 0xff, (int) (x>> 0) & 0xff);
Guido van Rossum73624e91994-10-10 17:59:00 +0000322 return PyString_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000323}
324
325
326/* Create an object representing the given socket address,
327 suitable for passing it back to bind(), connect() etc.
328 The family field of the sockaddr structure is inspected
329 to determine what kind of address it really is. */
330
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000331/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000332static PyObject *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000333BUILD_FUNC_DEF_2(makesockaddr,struct sockaddr *,addr, int,addrlen)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000334{
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000335 if (addrlen == 0) {
336 /* No address -- may be recvfrom() from known socket */
Guido van Rossum73624e91994-10-10 17:59:00 +0000337 Py_INCREF(Py_None);
338 return Py_None;
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000339 }
340
Guido van Rossum30a685f1991-06-27 15:51:29 +0000341 switch (addr->sa_family) {
342
343 case AF_INET:
344 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000345 struct sockaddr_in *a = (struct sockaddr_in *) addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000346 PyObject *addr = makeipaddr(a);
347 PyObject *ret = Py_BuildValue("Oi", addr, ntohs(a->sin_port));
348 Py_XDECREF(addr);
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000349 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000350 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000351
Guido van Rossumb6775db1994-08-01 11:34:53 +0000352#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000353 case AF_UNIX:
354 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000355 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000356 return PyString_FromString(a->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000357 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000358#endif /* AF_UNIX */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000359
360 /* More cases here... */
361
362 default:
Guido van Rossum73624e91994-10-10 17:59:00 +0000363 PyErr_SetString(PySocket_Error, "return unknown socket address type");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000364 return NULL;
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000365
Guido van Rossum30a685f1991-06-27 15:51:29 +0000366 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000367}
368
Guido van Rossum30a685f1991-06-27 15:51:29 +0000369
370/* Parse a socket address argument according to the socket object's
371 address family. Return 1 if the address was in the proper format,
372 0 of not. The address is returned through addr_ret, its length
373 through len_ret. */
374
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000375static int
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000376BUILD_FUNC_DEF_4(
Guido van Rossum73624e91994-10-10 17:59:00 +0000377getsockaddrarg,PySocketSockObject *,s, PyObject *,args, struct sockaddr **,addr_ret, int *,len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000378{
Guido van Rossum30a685f1991-06-27 15:51:29 +0000379 switch (s->sock_family) {
380
Guido van Rossumb6775db1994-08-01 11:34:53 +0000381#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000382 case AF_UNIX:
383 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000384 struct sockaddr_un* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000385 char *path;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000386 int len;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000387 addr = (struct sockaddr_un* )&(s->sock_addr).un;
Guido van Rossum73624e91994-10-10 17:59:00 +0000388 if (!PyArg_Parse(args, "s#", &path, &len))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000389 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000390 if (len > sizeof addr->sun_path) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000391 PyErr_SetString(PySocket_Error, "AF_UNIX path too long");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000392 return 0;
393 }
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000394 addr->sun_family = AF_UNIX;
395 memcpy(addr->sun_path, path, len);
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000396 addr->sun_path[len] = 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000397 *addr_ret = (struct sockaddr *) addr;
Guido van Rossum65af28a1996-06-11 18:36:33 +0000398 *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000399 return 1;
400 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000401#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000402
Guido van Rossum30a685f1991-06-27 15:51:29 +0000403 case AF_INET:
404 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000405 struct sockaddr_in* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000406 char *host;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000407 int port;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000408 addr=(struct sockaddr_in*)&(s->sock_addr).in;
Guido van Rossum73624e91994-10-10 17:59:00 +0000409 if (!PyArg_Parse(args, "(si)", &host, &port))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000410 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000411 if (setipaddr(host, addr) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000412 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000413 addr->sin_family = AF_INET;
414 addr->sin_port = htons(port);
415 *addr_ret = (struct sockaddr *) addr;
416 *len_ret = sizeof *addr;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000417 return 1;
418 }
419
Guido van Rossum30a685f1991-06-27 15:51:29 +0000420 /* More cases here... */
421
422 default:
Guido van Rossum73624e91994-10-10 17:59:00 +0000423 PyErr_SetString(PySocket_Error, "getsockaddrarg: bad family");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000424 return 0;
425
426 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000427}
428
Guido van Rossum30a685f1991-06-27 15:51:29 +0000429
Guido van Rossum710e1df1992-06-12 10:39:36 +0000430/* Get the address length according to the socket object's address family.
431 Return 1 if the family is known, 0 otherwise. The length is returned
432 through len_ret. */
433
434static int
Guido van Rossum73624e91994-10-10 17:59:00 +0000435BUILD_FUNC_DEF_2(getsockaddrlen,PySocketSockObject *,s, int *,len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +0000436{
437 switch (s->sock_family) {
438
Guido van Rossumb6775db1994-08-01 11:34:53 +0000439#ifdef AF_UNIX
Guido van Rossum710e1df1992-06-12 10:39:36 +0000440 case AF_UNIX:
441 {
442 *len_ret = sizeof (struct sockaddr_un);
443 return 1;
444 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000445#endif /* AF_UNIX */
Guido van Rossum710e1df1992-06-12 10:39:36 +0000446
447 case AF_INET:
448 {
449 *len_ret = sizeof (struct sockaddr_in);
450 return 1;
451 }
452
453 /* More cases here... */
454
455 default:
Guido van Rossum73624e91994-10-10 17:59:00 +0000456 PyErr_SetString(PySocket_Error, "getsockaddrarg: bad family");
Guido van Rossum710e1df1992-06-12 10:39:36 +0000457 return 0;
458
459 }
460}
461
462
Guido van Rossum30a685f1991-06-27 15:51:29 +0000463/* s.accept() method */
464
Guido van Rossum73624e91994-10-10 17:59:00 +0000465static PyObject *
466BUILD_FUNC_DEF_2(PySocketSock_accept,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000467{
468 char addrbuf[256];
469 int addrlen, newfd;
Guido van Rossum73624e91994-10-10 17:59:00 +0000470 PyObject *sock, *addr, *res;
471 if (!PyArg_NoArgs(args))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000472 return NULL;
Guido van Rossum710e1df1992-06-12 10:39:36 +0000473 if (!getsockaddrlen(s, &addrlen))
474 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000475 Py_BEGIN_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000476 newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000477 Py_END_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000478 if (newfd < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000479 return PySocket_Err();
Guido van Rossum30a685f1991-06-27 15:51:29 +0000480 /* Create the new object with unspecified family,
481 to avoid calls to bind() etc. on it. */
Guido van Rossum73624e91994-10-10 17:59:00 +0000482 sock = (PyObject *) PySocketSock_New(newfd,
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000483 s->sock_family,
484 s->sock_type,
485 s->sock_proto);
486 if (sock == NULL)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000487 close(newfd);
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000488 addr = makesockaddr((struct sockaddr *) addrbuf, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000489 res = Py_BuildValue("OO", sock, addr);
490 Py_XDECREF(sock);
491 Py_XDECREF(addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000492 return res;
493}
494
Guido van Rossum30a685f1991-06-27 15:51:29 +0000495
Guido van Rossume4485b01994-09-07 14:32:49 +0000496/* s.setblocking(1 | 0) method */
497
Guido van Rossum73624e91994-10-10 17:59:00 +0000498static PyObject *
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000499BUILD_FUNC_DEF_2(PySocketSock_setblocking,PySocketSockObject*,s,PyObject*,args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000500{
501 int block;
502 int delay_flag;
Guido van Rossum73624e91994-10-10 17:59:00 +0000503 if (!PyArg_GetInt(args, &block))
Guido van Rossume4485b01994-09-07 14:32:49 +0000504 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000505 Py_BEGIN_ALLOW_THREADS
Guido van Rossum6f489d91996-06-28 20:15:15 +0000506#ifndef MS_WINDOWS
Guido van Rossume4485b01994-09-07 14:32:49 +0000507 delay_flag = fcntl (s->sock_fd, F_GETFL, 0);
508 if (block)
509 delay_flag &= (~O_NDELAY);
510 else
511 delay_flag |= O_NDELAY;
512 fcntl (s->sock_fd, F_SETFL, delay_flag);
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000513#else
514 block = !block;
515 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
516#endif
Guido van Rossum73624e91994-10-10 17:59:00 +0000517 Py_END_ALLOW_THREADS
Guido van Rossume4485b01994-09-07 14:32:49 +0000518
Guido van Rossum73624e91994-10-10 17:59:00 +0000519 Py_INCREF(Py_None);
520 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000521}
Guido van Rossume4485b01994-09-07 14:32:49 +0000522
523
Guido van Rossumaee08791992-09-08 09:05:33 +0000524/* s.setsockopt() method.
525 With an integer third argument, sets an integer option.
526 With a string third argument, sets an option from a buffer;
527 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000528
Guido van Rossum73624e91994-10-10 17:59:00 +0000529static PyObject *
530BUILD_FUNC_DEF_2(PySocketSock_setsockopt,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000531{
532 int level;
533 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000534 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +0000535 char *buf;
536 int buflen;
537 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000538
Guido van Rossum73624e91994-10-10 17:59:00 +0000539 if (PyArg_Parse(args, "(iii)", &level, &optname, &flag)) {
Guido van Rossumaee08791992-09-08 09:05:33 +0000540 buf = (char *) &flag;
541 buflen = sizeof flag;
542 }
543 else {
Guido van Rossum73624e91994-10-10 17:59:00 +0000544 PyErr_Clear();
545 if (!PyArg_Parse(args, "(iis#)", &level, &optname, &buf, &buflen))
Guido van Rossumaee08791992-09-08 09:05:33 +0000546 return NULL;
547 }
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000548 res = setsockopt(s->sock_fd, level, optname, (ANY *)buf, buflen);
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000549 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000550 return PySocket_Err();
551 Py_INCREF(Py_None);
552 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000553}
554
555
Guido van Rossumaee08791992-09-08 09:05:33 +0000556/* s.getsockopt() method.
557 With two arguments, retrieves an integer option.
558 With a third integer argument, retrieves a string buffer of that size;
559 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000560
Guido van Rossum73624e91994-10-10 17:59:00 +0000561static PyObject *
562BUILD_FUNC_DEF_2(PySocketSock_getsockopt,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000563{
564 int level;
565 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000566 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +0000567 PyObject *buf;
Guido van Rossumbe32c891996-06-20 16:25:29 +0000568 int buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000569
Guido van Rossumbe32c891996-06-20 16:25:29 +0000570 if (!PyArg_ParseTuple(args, "ii|i", &level, &optname, &buflen))
571 return NULL;
572
573 if (buflen == 0) {
Guido van Rossumaee08791992-09-08 09:05:33 +0000574 int flag = 0;
575 int flagsize = sizeof flag;
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000576 res = getsockopt(s->sock_fd, level, optname,
577 (ANY *)&flag, &flagsize);
Guido van Rossumaee08791992-09-08 09:05:33 +0000578 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000579 return PySocket_Err();
580 return PyInt_FromLong(flag);
Guido van Rossumaee08791992-09-08 09:05:33 +0000581 }
Guido van Rossumaee08791992-09-08 09:05:33 +0000582 if (buflen <= 0 || buflen > 1024) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000583 PyErr_SetString(PySocket_Error, "getsockopt buflen out of range");
Guido van Rossumaee08791992-09-08 09:05:33 +0000584 return NULL;
585 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000586 buf = PyString_FromStringAndSize((char *)NULL, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000587 if (buf == NULL)
588 return NULL;
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000589 res = getsockopt(s->sock_fd, level, optname,
Guido van Rossuma597dde1995-01-10 20:56:29 +0000590 (ANY *)PyString_AsString(buf), &buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000591 if (res < 0) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000592 Py_DECREF(buf);
593 return PySocket_Err();
Guido van Rossumaee08791992-09-08 09:05:33 +0000594 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000595 _PyString_Resize(&buf, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000596 return buf;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000597}
598
599
Guido van Rossum30a685f1991-06-27 15:51:29 +0000600/* s.bind(sockaddr) method */
601
Guido van Rossum73624e91994-10-10 17:59:00 +0000602static PyObject *
603BUILD_FUNC_DEF_2(PySocketSock_bind,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000604{
605 struct sockaddr *addr;
606 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000607 int res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000608 if (!getsockaddrarg(s, args, &addr, &addrlen))
609 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000610 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000611 res = bind(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000612 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000613 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000614 return PySocket_Err();
615 Py_INCREF(Py_None);
616 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000617}
618
Guido van Rossum30a685f1991-06-27 15:51:29 +0000619
620/* s.close() method.
621 Set the file descriptor to -1 so operations tried subsequently
622 will surely fail. */
623
Guido van Rossum73624e91994-10-10 17:59:00 +0000624static PyObject *
625BUILD_FUNC_DEF_2(PySocketSock_close,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000626{
Guido van Rossum73624e91994-10-10 17:59:00 +0000627 if (!PyArg_NoArgs(args))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000628 return NULL;
Guido van Rossum08481461996-10-12 14:07:22 +0000629 if (s->sock_fd != -1) {
630 Py_BEGIN_ALLOW_THREADS
631 (void) close(s->sock_fd);
632 Py_END_ALLOW_THREADS
633 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000634 s->sock_fd = -1;
Guido van Rossum73624e91994-10-10 17:59:00 +0000635 Py_INCREF(Py_None);
636 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000637}
638
Guido van Rossum30a685f1991-06-27 15:51:29 +0000639
640/* s.connect(sockaddr) method */
641
Guido van Rossum73624e91994-10-10 17:59:00 +0000642static PyObject *
643BUILD_FUNC_DEF_2(PySocketSock_connect,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000644{
645 struct sockaddr *addr;
646 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000647 int res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000648 if (!getsockaddrarg(s, args, &addr, &addrlen))
649 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000650 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000651 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000652 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000653 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000654 return PySocket_Err();
655 Py_INCREF(Py_None);
656 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000657}
658
Guido van Rossum30a685f1991-06-27 15:51:29 +0000659
Guido van Rossumed233a51992-06-23 09:07:03 +0000660/* s.fileno() method */
661
Guido van Rossum73624e91994-10-10 17:59:00 +0000662static PyObject *
663BUILD_FUNC_DEF_2(PySocketSock_fileno,PySocketSockObject *,s, PyObject *,args)
Guido van Rossumed233a51992-06-23 09:07:03 +0000664{
Guido van Rossum73624e91994-10-10 17:59:00 +0000665 if (!PyArg_NoArgs(args))
Guido van Rossumed233a51992-06-23 09:07:03 +0000666 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000667 return PyInt_FromLong((long) s->sock_fd);
Guido van Rossumed233a51992-06-23 09:07:03 +0000668}
669
670
Guido van Rossumbe32c891996-06-20 16:25:29 +0000671#ifndef NO_DUP
672/* s.dup() method */
673
674static PyObject *
675BUILD_FUNC_DEF_2(PySocketSock_dup,PySocketSockObject *,s, PyObject *,args)
676{
677 int newfd;
678 PyObject *sock;
679 if (!PyArg_NoArgs(args))
680 return NULL;
681 newfd = dup(s->sock_fd);
682 if (newfd < 0)
683 return PySocket_Err();
684 sock = (PyObject *) PySocketSock_New(newfd,
685 s->sock_family,
686 s->sock_type,
687 s->sock_proto);
688 if (sock == NULL)
689 close(newfd);
690 return sock;
691}
692#endif
693
694
Guido van Rossumc89705d1992-11-26 08:54:07 +0000695/* s.getsockname() method */
696
Guido van Rossum73624e91994-10-10 17:59:00 +0000697static PyObject *
698BUILD_FUNC_DEF_2(PySocketSock_getsockname,PySocketSockObject *,s, PyObject *,args)
Guido van Rossumc89705d1992-11-26 08:54:07 +0000699{
700 char addrbuf[256];
701 int addrlen, res;
Guido van Rossum73624e91994-10-10 17:59:00 +0000702 if (!PyArg_NoArgs(args))
Guido van Rossumc89705d1992-11-26 08:54:07 +0000703 return NULL;
704 if (!getsockaddrlen(s, &addrlen))
705 return NULL;
Guido van Rossumca42b161996-01-12 01:36:05 +0000706 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000707 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +0000708 res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000709 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +0000710 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000711 return PySocket_Err();
Guido van Rossumc89705d1992-11-26 08:54:07 +0000712 return makesockaddr((struct sockaddr *) addrbuf, addrlen);
713}
714
715
Guido van Rossumb6775db1994-08-01 11:34:53 +0000716#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +0000717/* s.getpeername() method */
718
Guido van Rossum73624e91994-10-10 17:59:00 +0000719static PyObject *
720BUILD_FUNC_DEF_2(PySocketSock_getpeername,PySocketSockObject *,s, PyObject *,args)
Guido van Rossumc89705d1992-11-26 08:54:07 +0000721{
722 char addrbuf[256];
723 int addrlen, res;
Guido van Rossum73624e91994-10-10 17:59:00 +0000724 if (!PyArg_NoArgs(args))
Guido van Rossumc89705d1992-11-26 08:54:07 +0000725 return NULL;
726 if (!getsockaddrlen(s, &addrlen))
727 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000728 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +0000729 res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000730 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +0000731 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000732 return PySocket_Err();
Guido van Rossumc89705d1992-11-26 08:54:07 +0000733 return makesockaddr((struct sockaddr *) addrbuf, addrlen);
734}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000735#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +0000736
737
Guido van Rossum30a685f1991-06-27 15:51:29 +0000738/* s.listen(n) method */
739
Guido van Rossum73624e91994-10-10 17:59:00 +0000740static PyObject *
741BUILD_FUNC_DEF_2(PySocketSock_listen,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000742{
743 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000744 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +0000745 if (!PyArg_GetInt(args, &backlog))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000746 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000747 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000748 if (backlog < 1)
749 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000750 res = listen(s->sock_fd, backlog);
Guido van Rossum73624e91994-10-10 17:59:00 +0000751 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000752 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000753 return PySocket_Err();
754 Py_INCREF(Py_None);
755 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000756}
757
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000758#ifndef NO_DUP
Guido van Rossum30a685f1991-06-27 15:51:29 +0000759/* s.makefile(mode) method.
760 Create a new open file object referring to a dupped version of
761 the socket's file descriptor. (The dup() call is necessary so
762 that the open file and socket objects may be closed independent
763 of each other.)
764 The mode argument specifies 'r' or 'w' passed to fdopen(). */
765
Guido van Rossum73624e91994-10-10 17:59:00 +0000766static PyObject *
767BUILD_FUNC_DEF_2(PySocketSock_makefile,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000768{
Guido van Rossuma597dde1995-01-10 20:56:29 +0000769 extern int fclose Py_PROTO((FILE *));
Guido van Rossum6b144911995-03-14 15:05:13 +0000770 char *mode = "r";
771 int bufsize = -1;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000772 int fd;
773 FILE *fp;
Guido van Rossum6b144911995-03-14 15:05:13 +0000774 PyObject *f;
775
776 if (!PyArg_ParseTuple(args, "|si", &mode, &bufsize))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000777 return NULL;
Guido van Rossum8d665e61996-06-26 18:22:49 +0000778#ifdef MS_WIN32
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000779 if ( ((fd = _open_osfhandle( s->sock_fd, _O_BINARY )) < 0) ||
780 ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL)) {
781#else
Guido van Rossum30a685f1991-06-27 15:51:29 +0000782 if ((fd = dup(s->sock_fd)) < 0 ||
Guido van Rossum6b144911995-03-14 15:05:13 +0000783 (fp = fdopen(fd, mode)) == NULL) {
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000784#endif
Guido van Rossum6b144911995-03-14 15:05:13 +0000785 if (fd >= 0)
786 close(fd);
Guido van Rossum73624e91994-10-10 17:59:00 +0000787 return PySocket_Err();
Guido van Rossum6b144911995-03-14 15:05:13 +0000788 }
789 f = PyFile_FromFile(fp, "<socket>", mode, fclose);
790 if (f != NULL)
791 PyFile_SetBufSize(f, bufsize);
792 return f;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000793}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000794#endif /* NO_DUP */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000795
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000796/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000797
Guido van Rossum73624e91994-10-10 17:59:00 +0000798static PyObject *
799BUILD_FUNC_DEF_2(PySocketSock_recv,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000800{
Guido van Rossumbe32c891996-06-20 16:25:29 +0000801 int len, n, flags = 0;
Guido van Rossum73624e91994-10-10 17:59:00 +0000802 PyObject *buf;
Guido van Rossumbe32c891996-06-20 16:25:29 +0000803 if (!PyArg_ParseTuple(args, "i|i", &len, &flags))
804 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000805 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000806 if (buf == NULL)
807 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000808 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma597dde1995-01-10 20:56:29 +0000809 n = recv(s->sock_fd, PyString_AsString(buf), len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +0000810 Py_END_ALLOW_THREADS
Guido van Rossum7c53b771995-09-13 18:39:47 +0000811 if (n < 0) {
812 Py_DECREF(buf);
Guido van Rossum73624e91994-10-10 17:59:00 +0000813 return PySocket_Err();
Guido van Rossum7c53b771995-09-13 18:39:47 +0000814 }
Guido van Rossum53a9bf81996-06-11 18:35:24 +0000815 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000816 return NULL;
817 return buf;
818}
819
Guido van Rossum30a685f1991-06-27 15:51:29 +0000820
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000821/* s.recvfrom(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000822
Guido van Rossum73624e91994-10-10 17:59:00 +0000823static PyObject *
824BUILD_FUNC_DEF_2(PySocketSock_recvfrom,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000825{
826 char addrbuf[256];
Guido van Rossum73624e91994-10-10 17:59:00 +0000827 PyObject *buf, *addr, *ret;
Guido van Rossumbe32c891996-06-20 16:25:29 +0000828 int addrlen, len, n, flags = 0;
829 if (!PyArg_ParseTuple(args, "i|i", &len, &flags))
830 return NULL;
Guido van Rossum18c9a4f1993-05-25 12:16:29 +0000831 if (!getsockaddrlen(s, &addrlen))
832 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000833 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum18c9a4f1993-05-25 12:16:29 +0000834 if (buf == NULL)
835 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000836 Py_BEGIN_ALLOW_THREADS
837 n = recvfrom(s->sock_fd, PyString_AsString(buf), len, flags,
Guido van Rossum8d665e61996-06-26 18:22:49 +0000838#ifndef MS_WINDOWS
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000839 (ANY *)addrbuf, &addrlen);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000840#else
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000841 (struct sockaddr *)addrbuf, &addrlen);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000842#endif
Guido van Rossum73624e91994-10-10 17:59:00 +0000843 Py_END_ALLOW_THREADS
Guido van Rossum7c53b771995-09-13 18:39:47 +0000844 if (n < 0) {
845 Py_DECREF(buf);
Guido van Rossum73624e91994-10-10 17:59:00 +0000846 return PySocket_Err();
Guido van Rossum7c53b771995-09-13 18:39:47 +0000847 }
Guido van Rossum53a9bf81996-06-11 18:35:24 +0000848 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000849 return NULL;
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000850 addr = makesockaddr((struct sockaddr *)addrbuf, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000851 ret = Py_BuildValue("OO", buf, addr);
852 Py_XDECREF(addr);
853 Py_XDECREF(buf);
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000854 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000855}
856
Guido van Rossum30a685f1991-06-27 15:51:29 +0000857
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000858/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000859
Guido van Rossum73624e91994-10-10 17:59:00 +0000860static PyObject *
861BUILD_FUNC_DEF_2(PySocketSock_send,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000862{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000863 char *buf;
Guido van Rossumbe32c891996-06-20 16:25:29 +0000864 int len, n, flags = 0;
865 if (!PyArg_ParseTuple(args, "s#|i", &buf, &len, &flags))
866 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000867 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000868 n = send(s->sock_fd, buf, len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +0000869 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +0000870 if (n < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000871 return PySocket_Err();
872 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000873}
874
875
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000876/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000877
Guido van Rossum73624e91994-10-10 17:59:00 +0000878static PyObject *
879BUILD_FUNC_DEF_2(PySocketSock_sendto,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000880{
Guido van Rossum73624e91994-10-10 17:59:00 +0000881 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000882 char *buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000883 struct sockaddr *addr;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000884 int addrlen, len, n, flags;
885 flags = 0;
Guido van Rossum73624e91994-10-10 17:59:00 +0000886 if (!PyArg_Parse(args, "(s#O)", &buf, &len, &addro)) {
887 PyErr_Clear();
888 if (!PyArg_Parse(args, "(s#iO)", &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000889 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000890 }
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000891 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000892 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000893 Py_BEGIN_ALLOW_THREADS
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000894 n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000895 Py_END_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000896 if (n < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000897 return PySocket_Err();
898 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000899}
900
Guido van Rossum30a685f1991-06-27 15:51:29 +0000901
902/* s.shutdown(how) method */
903
Guido van Rossum73624e91994-10-10 17:59:00 +0000904static PyObject *
905BUILD_FUNC_DEF_2(PySocketSock_shutdown,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000906{
907 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000908 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +0000909 if (!PyArg_GetInt(args, &how))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000910 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000911 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000912 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +0000913 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000914 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000915 return PySocket_Err();
916 Py_INCREF(Py_None);
917 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000918}
919
Guido van Rossum30a685f1991-06-27 15:51:29 +0000920
921/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000922
Guido van Rossum73624e91994-10-10 17:59:00 +0000923static PyMethodDef PySocketSock_methods[] = {
924 {"accept", (PyCFunction)PySocketSock_accept},
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000925 {"setblocking", (PyCFunction)PySocketSock_setblocking},
Guido van Rossum73624e91994-10-10 17:59:00 +0000926 {"setsockopt", (PyCFunction)PySocketSock_setsockopt},
Guido van Rossumbe32c891996-06-20 16:25:29 +0000927 {"getsockopt", (PyCFunction)PySocketSock_getsockopt, 1},
Guido van Rossum73624e91994-10-10 17:59:00 +0000928 {"bind", (PyCFunction)PySocketSock_bind},
929 {"close", (PyCFunction)PySocketSock_close},
930 {"connect", (PyCFunction)PySocketSock_connect},
931 {"fileno", (PyCFunction)PySocketSock_fileno},
Guido van Rossumbe32c891996-06-20 16:25:29 +0000932#ifndef NO_DUP
933 {"dup", (PyCFunction)PySocketSock_dup},
934#endif
Guido van Rossum73624e91994-10-10 17:59:00 +0000935 {"getsockname", (PyCFunction)PySocketSock_getsockname},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000936#ifdef HAVE_GETPEERNAME
Guido van Rossum73624e91994-10-10 17:59:00 +0000937 {"getpeername", (PyCFunction)PySocketSock_getpeername},
Guido van Rossum9575a441993-04-07 14:06:14 +0000938#endif
Guido van Rossum73624e91994-10-10 17:59:00 +0000939 {"listen", (PyCFunction)PySocketSock_listen},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000940#ifndef NO_DUP
Guido van Rossum6b144911995-03-14 15:05:13 +0000941 {"makefile", (PyCFunction)PySocketSock_makefile, 1},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000942#endif
Guido van Rossumbe32c891996-06-20 16:25:29 +0000943 {"recv", (PyCFunction)PySocketSock_recv, 1},
944 {"recvfrom", (PyCFunction)PySocketSock_recvfrom, 1},
945 {"send", (PyCFunction)PySocketSock_send, 1},
Guido van Rossum73624e91994-10-10 17:59:00 +0000946 {"sendto", (PyCFunction)PySocketSock_sendto},
947 {"shutdown", (PyCFunction)PySocketSock_shutdown},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000948 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000949};
950
Guido van Rossum30a685f1991-06-27 15:51:29 +0000951
Guido van Rossum73624e91994-10-10 17:59:00 +0000952/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +0000953 First close the file description. */
954
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000955static void
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000956BUILD_FUNC_DEF_1(PySocketSock_dealloc,PySocketSockObject *,s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000957{
958 (void) close(s->sock_fd);
Guido van Rossum73624e91994-10-10 17:59:00 +0000959 PyMem_DEL(s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000960}
961
Guido van Rossum30a685f1991-06-27 15:51:29 +0000962
963/* Return a socket object's named attribute. */
964
Guido van Rossum73624e91994-10-10 17:59:00 +0000965static PyObject *
966BUILD_FUNC_DEF_2(PySocketSock_getattr,PySocketSockObject *,s, char *,name)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000967{
Guido van Rossum73624e91994-10-10 17:59:00 +0000968 return Py_FindMethod(PySocketSock_methods, (PyObject *) s, name);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000969}
970
Guido van Rossum30a685f1991-06-27 15:51:29 +0000971
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000972static PyObject *
973BUILD_FUNC_DEF_1(PySocketSock_repr,PySocketSockObject *,s)
974{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000975 char buf[512];
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000976 sprintf(buf,
977 "<socket object, fd=%d, family=%d, type=%d, protocol=%d>",
978 s->sock_fd, s->sock_family, s->sock_type, s->sock_proto);
Guido van Rossumdc1c64a1996-02-27 15:21:19 +0000979 return PyString_FromString(buf);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000980}
981
982
Guido van Rossumb6775db1994-08-01 11:34:53 +0000983/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000984
Guido van Rossum73624e91994-10-10 17:59:00 +0000985static PyTypeObject PySocketSock_Type = {
Guido van Rossum55558541996-05-23 22:54:50 +0000986 PyObject_HEAD_INIT(0) /* Must fill in type value later */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000987 0,
988 "socket",
Guido van Rossum73624e91994-10-10 17:59:00 +0000989 sizeof(PySocketSockObject),
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000990 0,
Guido van Rossum73624e91994-10-10 17:59:00 +0000991 (destructor)PySocketSock_dealloc, /*tp_dealloc*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000992 0, /*tp_print*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000993 (getattrfunc)PySocketSock_getattr, /*tp_getattr*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000994 0, /*tp_setattr*/
995 0, /*tp_compare*/
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000996 (reprfunc)PySocketSock_repr, /*tp_repr*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000997 0, /*tp_as_number*/
998 0, /*tp_as_sequence*/
999 0, /*tp_as_mapping*/
1000};
1001
Guido van Rossum30a685f1991-06-27 15:51:29 +00001002
Guido van Rossum81194471991-07-27 21:42:02 +00001003/* Python interface to gethostname(). */
1004
1005/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001006static PyObject *
1007BUILD_FUNC_DEF_2(PySocket_gethostname,PyObject *,self, PyObject *,args)
Guido van Rossum81194471991-07-27 21:42:02 +00001008{
1009 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001010 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +00001011 if (!PyArg_NoArgs(args))
Guido van Rossum81194471991-07-27 21:42:02 +00001012 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001013 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001014 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00001015 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001016 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001017 return PySocket_Err();
Guido van Rossum81194471991-07-27 21:42:02 +00001018 buf[sizeof buf - 1] = '\0';
Guido van Rossum73624e91994-10-10 17:59:00 +00001019 return PyString_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00001020}
Guido van Rossumff4949e1992-08-05 19:58:53 +00001021
1022
Guido van Rossum30a685f1991-06-27 15:51:29 +00001023/* Python interface to gethostbyname(name). */
1024
1025/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001026static PyObject *
1027BUILD_FUNC_DEF_2(PySocket_gethostbyname,PyObject *,self, PyObject *,args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001028{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001029 char *name;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001030 struct sockaddr_in addrbuf;
Guido van Rossum73624e91994-10-10 17:59:00 +00001031 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00001032 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001033 if (setipaddr(name, &addrbuf) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001034 return NULL;
1035 return makeipaddr(&addrbuf);
1036}
1037
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001038/* Python interface to gethostbyaddr(IP). */
1039
1040/*ARGSUSED*/
1041static PyObject *
1042BUILD_FUNC_DEF_2(PySocket_gethostbyaddr,PyObject *,self, PyObject *, args)
1043{
1044 struct sockaddr_in addr;
1045 char *ip_num;
1046 struct hostent *h;
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00001047 char **pch;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001048 PyObject *rtn_tuple = (PyObject *)NULL;
1049 PyObject *name_list = (PyObject *)NULL;
1050 PyObject *addr_list = (PyObject *)NULL;
1051 PyObject *tmp;
1052
1053 if (!PyArg_Parse(args, "s", &ip_num))
1054 return NULL;
1055 if (setipaddr(ip_num, &addr) < 0)
1056 return NULL;
1057 h = gethostbyaddr((char *)&addr.sin_addr,
1058 sizeof(addr.sin_addr),
1059 AF_INET);
1060 if (h == NULL) {
Guido van Rossumca9b3231995-01-10 16:30:54 +00001061#ifdef HAVE_HSTRERROR
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001062 /* Let's get real error message to return */
1063 extern int h_errno;
1064 PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno));
1065#else
1066 PyErr_SetString(PySocket_Error, "host not found");
1067#endif
1068 return NULL;
1069 }
1070 if ((name_list = PyList_New(0)) == NULL)
1071 goto err;
1072 if ((addr_list = PyList_New(0)) == NULL)
1073 goto err;
1074 for (pch = h->h_aliases; *pch != NULL; pch++) {
1075 tmp = PyString_FromString(*pch);
1076 if (tmp == NULL)
1077 goto err;
1078 PyList_Append(name_list, tmp);
1079 Py_DECREF(tmp);
1080 }
1081 for (pch = h->h_addr_list; *pch != NULL; pch++) {
1082 memcpy((char *) &addr.sin_addr, *pch, h->h_length);
1083 tmp = makeipaddr(&addr);
1084 if (tmp == NULL)
1085 goto err;
1086 PyList_Append(addr_list, tmp);
1087 Py_DECREF(tmp);
1088 }
1089 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
1090 err:
1091 Py_XDECREF(name_list);
1092 Py_XDECREF(addr_list);
1093 return rtn_tuple;
1094}
1095
Guido van Rossum30a685f1991-06-27 15:51:29 +00001096
1097/* Python interface to getservbyname(name).
1098 This only returns the port number, since the other info is already
1099 known or not useful (like the list of aliases). */
1100
1101/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001102static PyObject *
1103BUILD_FUNC_DEF_2(PySocket_getservbyname,PyObject *,self, PyObject *,args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001104{
Guido van Rossumff4949e1992-08-05 19:58:53 +00001105 char *name, *proto;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001106 struct servent *sp;
Guido van Rossum73624e91994-10-10 17:59:00 +00001107 if (!PyArg_Parse(args, "(ss)", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00001108 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001109 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001110 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00001111 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00001112 if (sp == NULL) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001113 PyErr_SetString(PySocket_Error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00001114 return NULL;
1115 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001116 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00001117}
1118
1119
1120/* Python interface to socket(family, type, proto).
1121 The third (protocol) argument is optional.
1122 Return a new socket object. */
1123
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001124/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001125static PyObject *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001126BUILD_FUNC_DEF_2(PySocket_socket,PyObject *,self, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001127{
Guido van Rossum73624e91994-10-10 17:59:00 +00001128 PySocketSockObject *s;
Guido van Rossum6f489d91996-06-28 20:15:15 +00001129#ifdef MS_WINDOWS
Guido van Rossumd639d4d1996-06-12 04:04:22 +00001130 SOCKET fd;
1131#else
1132 int fd;
1133#endif
Guido van Rossumbe32c891996-06-20 16:25:29 +00001134 int family, type, proto = 0;
1135 if (!PyArg_ParseTuple(args, "ii|i", &family, &type, &proto))
1136 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001137 Py_BEGIN_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001138 fd = socket(family, type, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00001139 Py_END_ALLOW_THREADS
Guido van Rossum6f489d91996-06-28 20:15:15 +00001140#ifdef MS_WINDOWS
Guido van Rossumd639d4d1996-06-12 04:04:22 +00001141 if (fd == INVALID_SOCKET)
1142#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001143 if (fd < 0)
Guido van Rossumd639d4d1996-06-12 04:04:22 +00001144#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001145 return PySocket_Err();
1146 s = PySocketSock_New(fd, family, type, proto);
Guido van Rossum30a685f1991-06-27 15:51:29 +00001147 /* If the object can't be created, don't forget to close the
1148 file descriptor again! */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001149 if (s == NULL)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001150 (void) close(fd);
Guido van Rossum81194471991-07-27 21:42:02 +00001151 /* From now on, ignore SIGPIPE and let the error checking
1152 do the work. */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001153#ifdef SIGPIPE
Guido van Rossum81194471991-07-27 21:42:02 +00001154 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001155#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001156 return (PyObject *) s;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001157}
1158
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001159#ifndef NO_DUP
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001160/* Create a socket object from a numeric file description.
1161 Useful e.g. if stdin is a socket.
1162 Additional arguments as for socket(). */
1163
1164/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001165static PyObject *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001166BUILD_FUNC_DEF_2(PySocket_fromfd,PyObject *,self, PyObject *,args)
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001167{
Guido van Rossum73624e91994-10-10 17:59:00 +00001168 PySocketSockObject *s;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001169 int fd, family, type, proto = 0;
1170 if (!PyArg_ParseTuple(args, "iii|i", &fd, &family, &type, &proto))
1171 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +00001172 /* Dup the fd so it and the socket can be closed independently */
1173 fd = dup(fd);
1174 if (fd < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001175 return PySocket_Err();
1176 s = PySocketSock_New(fd, family, type, proto);
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001177 /* From now on, ignore SIGPIPE and let the error checking
1178 do the work. */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001179#ifdef SIGPIPE
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001180 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001181#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001182 return (PyObject *) s;
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001183}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001184#endif /* NO_DUP */
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001185
Guido van Rossum006bf911996-06-12 04:04:55 +00001186static PyObject *
1187BUILD_FUNC_DEF_2(PySocket_ntohs, PyObject *, self, PyObject *, args)
1188{
1189 int x1, x2;
1190
1191 if (!PyArg_Parse(args, "i", &x1)) {
1192 return NULL;
1193 }
1194 x2 = (int)ntohs((short)x1);
1195 return PyInt_FromLong(x2);
1196}
1197
1198static PyObject *
1199BUILD_FUNC_DEF_2(PySocket_ntohl, PyObject *, self, PyObject *, args)
1200{
1201 int x1, x2;
1202
1203 if (!PyArg_Parse(args, "i", &x1)) {
1204 return NULL;
1205 }
1206 x2 = ntohl(x1);
1207 return PyInt_FromLong(x2);
1208}
1209
1210static PyObject *
1211BUILD_FUNC_DEF_2(PySocket_htons, PyObject *, self, PyObject *, args)
1212{
1213 int x1, x2;
1214
1215 if (!PyArg_Parse(args, "i", &x1)) {
1216 return NULL;
1217 }
1218 x2 = (int)htons((short)x1);
1219 return PyInt_FromLong(x2);
1220}
1221
1222static PyObject *
1223BUILD_FUNC_DEF_2(PySocket_htonl, PyObject *, self, PyObject *, args)
1224{
1225 int x1, x2;
1226
1227 if (!PyArg_Parse(args, "i", &x1)) {
1228 return NULL;
1229 }
1230 x2 = htonl(x1);
1231 return PyInt_FromLong(x2);
1232}
1233
Guido van Rossum30a685f1991-06-27 15:51:29 +00001234/* List of functions exported by this module. */
1235
Guido van Rossum73624e91994-10-10 17:59:00 +00001236static PyMethodDef PySocket_methods[] = {
1237 {"gethostbyname", PySocket_gethostbyname},
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001238 {"gethostbyaddr", PySocket_gethostbyaddr},
Guido van Rossum73624e91994-10-10 17:59:00 +00001239 {"gethostname", PySocket_gethostname},
1240 {"getservbyname", PySocket_getservbyname},
Guido van Rossumbe32c891996-06-20 16:25:29 +00001241 {"socket", PySocket_socket, 1},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001242#ifndef NO_DUP
Guido van Rossumbe32c891996-06-20 16:25:29 +00001243 {"fromfd", PySocket_fromfd, 1},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001244#endif
Guido van Rossum006bf911996-06-12 04:04:55 +00001245 {"ntohs", PySocket_ntohs},
1246 {"ntohl", PySocket_ntohl},
1247 {"htons", PySocket_htons},
1248 {"htonl", PySocket_htonl},
Guido van Rossum30a685f1991-06-27 15:51:29 +00001249 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001250};
1251
Guido van Rossum30a685f1991-06-27 15:51:29 +00001252
1253/* Convenience routine to export an integer value.
1254 For simplicity, errors (which are unlikely anyway) are ignored. */
1255
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001256static void
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001257BUILD_FUNC_DEF_3(insint,PyObject *,d, char *,name, int,value)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001258{
Guido van Rossum73624e91994-10-10 17:59:00 +00001259 PyObject *v = PyInt_FromLong((long) value);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001260 if (v == NULL) {
1261 /* Don't bother reporting this error */
Guido van Rossum73624e91994-10-10 17:59:00 +00001262 PyErr_Clear();
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001263 }
1264 else {
Guido van Rossum73624e91994-10-10 17:59:00 +00001265 PyDict_SetItemString(d, name, v);
1266 Py_DECREF(v);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001267 }
1268}
1269
Guido van Rossum30a685f1991-06-27 15:51:29 +00001270
Guido van Rossum8d665e61996-06-26 18:22:49 +00001271#ifdef MS_WINDOWS
Guido van Rossumbe32c891996-06-20 16:25:29 +00001272
1273/* Additional initialization and cleanup for NT/Windows */
1274
1275static void
1276NTcleanup()
1277{
1278 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00001279}
1280
1281static int
1282NTinit()
1283{
1284 WSADATA WSAData;
1285 int ret;
1286 char buf[100];
1287 ret = WSAStartup(0x0101, &WSAData);
1288 switch (ret) {
1289 case 0: /* no error */
1290 atexit(NTcleanup);
1291 return 1;
1292 case WSASYSNOTREADY:
1293 PyErr_SetString(PyExc_ImportError,
1294 "WSAStartup failed: network not ready");
1295 break;
1296 case WSAVERNOTSUPPORTED:
1297 case WSAEINVAL:
1298 PyErr_SetString(PyExc_ImportError,
1299 "WSAStartup failed: requested version not supported");
1300 break;
1301 default:
1302 sprintf(buf, "WSAStartup failed: error code %d", ret);
1303 PyErr_SetString(PyExc_ImportError, buf);
1304 break;
1305 }
1306 return 0;
1307}
1308
Guido van Rossum8d665e61996-06-26 18:22:49 +00001309#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00001310
1311
Guido van Rossum30a685f1991-06-27 15:51:29 +00001312/* Initialize this module.
1313 This is called when the first 'import socket' is done,
1314 via a table in config.c, if config.c is compiled with USE_SOCKET
Guido van Rossumbe32c891996-06-20 16:25:29 +00001315 defined.
1316
Guido van Rossum8d665e61996-06-26 18:22:49 +00001317 For MS_WINDOWS (which means any Windows variant), this module
Guido van Rossumbe32c891996-06-20 16:25:29 +00001318 is actually called "_socket", and there's a wrapper "socket.py"
1319 which implements some missing functionality (such as makefile(),
1320 dup() and fromfd()). The import of "_socket" may fail with an
1321 ImportError exception if initialization of WINSOCK fails. When
1322 WINSOCK is initialized succesfully, a call to WSACleanup() is
1323 scheduled to be made at exit time. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001324
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001325void
Guido van Rossum8d665e61996-06-26 18:22:49 +00001326#ifdef MS_WINDOWS
Guido van Rossumbe32c891996-06-20 16:25:29 +00001327init_socket()
1328#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001329initsocket()
Guido van Rossumbe32c891996-06-20 16:25:29 +00001330#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001331{
Guido van Rossum73624e91994-10-10 17:59:00 +00001332 PyObject *m, *d;
Guido van Rossum8d665e61996-06-26 18:22:49 +00001333#ifdef MS_WINDOWS
Guido van Rossumbe32c891996-06-20 16:25:29 +00001334 if (!NTinit())
1335 return;
1336 m = Py_InitModule("_socket", PySocket_methods);
1337#else
Guido van Rossum73624e91994-10-10 17:59:00 +00001338 m = Py_InitModule("socket", PySocket_methods);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001339#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001340 d = PyModule_GetDict(m);
1341 PySocket_Error = PyString_FromString("socket.error");
1342 if (PySocket_Error == NULL ||
1343 PyDict_SetItemString(d, "error", PySocket_Error) != 0)
1344 Py_FatalError("can't define socket.error");
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001345 insint(d, "AF_INET", AF_INET);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001346#ifdef AF_UNIX
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001347 insint(d, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001348#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001349 insint(d, "SOCK_STREAM", SOCK_STREAM);
1350 insint(d, "SOCK_DGRAM", SOCK_DGRAM);
1351 insint(d, "SOCK_RAW", SOCK_RAW);
1352 insint(d, "SOCK_SEQPACKET", SOCK_SEQPACKET);
1353 insint(d, "SOCK_RDM", SOCK_RDM);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00001354
1355#ifdef SO_DEBUG
1356 insint(d, "SO_DEBUG", SO_DEBUG);
1357#endif
1358#ifdef SO_ACCEPTCONN
1359 insint(d, "SO_ACCEPTCONN", SO_ACCEPTCONN);
1360#endif
1361#ifdef SO_REUSEADDR
1362 insint(d, "SO_REUSEADDR", SO_REUSEADDR);
1363#endif
1364#ifdef SO_KEEPALIVE
1365 insint(d, "SO_KEEPALIVE", SO_KEEPALIVE);
1366#endif
1367#ifdef SO_DONTROUTE
1368 insint(d, "SO_DONTROUTE", SO_DONTROUTE);
1369#endif
1370#ifdef SO_BROADCAST
1371 insint(d, "SO_BROADCAST", SO_BROADCAST);
1372#endif
1373#ifdef SO_USELOOPBACK
1374 insint(d, "SO_USELOOPBACK", SO_USELOOPBACK);
1375#endif
1376#ifdef SO_LINGER
1377 insint(d, "SO_LINGER", SO_LINGER);
1378#endif
1379#ifdef SO_OOBINLINE
1380 insint(d, "SO_OOBINLINE", SO_OOBINLINE);
1381#endif
1382#ifdef SO_REUSEPORT
1383 insint(d, "SO_REUSEPORT", SO_REUSEPORT);
1384#endif
1385
1386#ifdef SO_SNDBUF
1387 insint(d, "SO_SNDBUF", SO_SNDBUF);
1388#endif
1389#ifdef SO_RCVBUF
1390 insint(d, "SO_RCVBUF", SO_RCVBUF);
1391#endif
1392#ifdef SO_SNDLOWAT
1393 insint(d, "SO_SNDLOWAT", SO_SNDLOWAT);
1394#endif
1395#ifdef SO_RCVLOWAT
1396 insint(d, "SO_RCVLOWAT", SO_RCVLOWAT);
1397#endif
1398#ifdef SO_SNDTIMEO
1399 insint(d, "SO_SNDTIMEO", SO_SNDTIMEO);
1400#endif
1401#ifdef SO_RCVTIMEO
1402 insint(d, "SO_RCVTIMEO", SO_RCVTIMEO);
1403#endif
1404#ifdef SO_ERROR
1405 insint(d, "SO_ERROR", SO_ERROR);
1406#endif
1407#ifdef SO_TYPE
1408 insint(d, "SO_TYPE", SO_TYPE);
1409#endif
1410
1411 /* Maximum number of connections for "listen" */
1412#ifdef SOMAXCONN
1413 insint(d, "SOMAXCONN", SOMAXCONN);
1414#else
1415 insint(d, "SOMAXCONN", 5); /* Common value */
1416#endif
1417
1418 /* Flags for send, recv */
1419#ifdef MSG_OOB
1420 insint(d, "MSG_OOB", MSG_OOB);
1421#endif
1422#ifdef MSG_PEEK
1423 insint(d, "MSG_PEEK", MSG_PEEK);
1424#endif
1425#ifdef MSG_DONTROUTE
1426 insint(d, "MSG_DONTROUTE", MSG_DONTROUTE);
1427#endif
1428#ifdef MSG_EOR
1429 insint(d, "MSG_EOR", MSG_EOR);
1430#endif
1431#ifdef MSG_TRUNC
1432 insint(d, "MSG_TRUNC", MSG_TRUNC);
1433#endif
1434#ifdef MSG_CTRUNC
1435 insint(d, "MSG_CTRUNC", MSG_CTRUNC);
1436#endif
1437#ifdef MSG_WAITALL
1438 insint(d, "MSG_WAITALL", MSG_WAITALL);
1439#endif
1440#ifdef MSG_BTAG
1441 insint(d, "MSG_BTAG", MSG_BTAG);
1442#endif
1443#ifdef MSG_ETAG
1444 insint(d, "MSG_ETAG", MSG_ETAG);
1445#endif
1446
1447 /* Protocol level and numbers, usable for [gs]etsockopt */
1448#ifdef SOL_SOCKET
1449 insint(d, "SOL_SOCKET", SOL_SOCKET);
1450#endif
1451#ifdef IPPROTO_IP
1452 insint(d, "IPPROTO_IP", IPPROTO_IP);
1453#endif
1454#ifdef IPPROTO_ICMP
1455 insint(d, "IPPROTO_ICMP", IPPROTO_ICMP);
1456#endif
1457#ifdef IPPROTO_IGMP
1458 insint(d, "IPPROTO_IGMP", IPPROTO_IGMP);
1459#endif
1460#ifdef IPPROTO_GGP
1461 insint(d, "IPPROTO_GGP", IPPROTO_GGP);
1462#endif
1463#ifdef IPPROTO_TCP
1464 insint(d, "IPPROTO_TCP", IPPROTO_TCP);
1465#endif
1466#ifdef IPPROTO_EGP
1467 insint(d, "IPPROTO_EGP", IPPROTO_EGP);
1468#endif
1469#ifdef IPPROTO_PUP
1470 insint(d, "IPPROTO_PUP", IPPROTO_PUP);
1471#endif
1472#ifdef IPPROTO_UDP
1473 insint(d, "IPPROTO_UDP", IPPROTO_UDP);
1474#endif
1475#ifdef IPPROTO_IDP
1476 insint(d, "IPPROTO_IDP", IPPROTO_IDP);
1477#endif
1478#ifdef IPPROTO_HELLO
1479 insint(d, "IPPROTO_HELLO", IPPROTO_HELLO);
1480#endif
1481#ifdef IPPROTO_ND
1482 insint(d, "IPPROTO_ND", IPPROTO_ND);
1483#endif
1484#ifdef IPPROTO_TP
1485 insint(d, "IPPROTO_TP", IPPROTO_TP);
1486#endif
1487#ifdef IPPROTO_XTP
1488 insint(d, "IPPROTO_XTP", IPPROTO_XTP);
1489#endif
1490#ifdef IPPROTO_EON
1491 insint(d, "IPPROTO_EON", IPPROTO_EON);
1492#endif
1493#ifdef IPPROTO_BIP
1494 insint(d, "IPPROTO_BIP", IPPROTO_BIP);
1495#endif
1496/**/
1497#ifdef IPPROTO_RAW
1498 insint(d, "IPPROTO_RAW", IPPROTO_RAW);
1499#endif
1500#ifdef IPPROTO_MAX
1501 insint(d, "IPPROTO_MAX", IPPROTO_MAX);
1502#endif
1503
1504 /* Some port configuration */
1505#ifdef IPPORT_RESERVED
1506 insint(d, "IPPORT_RESERVED", IPPORT_RESERVED);
1507#else
1508 insint(d, "IPPORT_RESERVED", 1024);
1509#endif
1510#ifdef IPPORT_USERRESERVED
1511 insint(d, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
1512#else
1513 insint(d, "IPPORT_USERRESERVED", 5000);
1514#endif
1515
Guido van Rossum5f05eb41995-02-17 15:11:07 +00001516 /* Some reserved IP v.4 addresses */
1517#ifdef INADDR_ANY
1518 insint(d, "INADDR_ANY", INADDR_ANY);
1519#else
1520 insint(d, "INADDR_ANY", 0x00000000);
1521#endif
1522#ifdef INADDR_BROADCAST
1523 insint(d, "INADDR_BROADCAST", INADDR_BROADCAST);
1524#else
1525 insint(d, "INADDR_BROADCAST", 0xffffffff);
1526#endif
1527#ifdef INADDR_LOOPBACK
1528 insint(d, "INADDR_LOOPBACK", INADDR_LOOPBACK);
1529#else
1530 insint(d, "INADDR_LOOPBACK", 0x7F000001);
1531#endif
1532#ifdef INADDR_UNSPEC_GROUP
1533 insint(d, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
1534#else
1535 insint(d, "INADDR_UNSPEC_GROUP", 0xe0000000);
1536#endif
1537#ifdef INADDR_ALLHOSTS_GROUP
1538 insint(d, "INADDR_ALLHOSTS_GROUP", INADDR_ALLHOSTS_GROUP);
1539#else
1540 insint(d, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
1541#endif
1542#ifdef INADDR_MAX_LOCAL_GROUP
1543 insint(d, "INADDR_MAX_LOCAL_GROUP", INADDR_MAX_LOCAL_GROUP);
1544#else
1545 insint(d, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
1546#endif
1547#ifdef INADDR_NONE
1548 insint(d, "INADDR_NONE", INADDR_NONE);
1549#else
1550 insint(d, "INADDR_NONE", 0xffffffff);
1551#endif
1552
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00001553 /* IP [gs]etsockopt options */
1554#ifdef IP_OPTIONS
1555 insint(d, "IP_OPTIONS", IP_OPTIONS);
1556#endif
1557#ifdef IP_HDRINCL
1558 insint(d, "IP_HDRINCL", IP_HDRINCL);
1559#endif
1560#ifdef IP_TOS
1561 insint(d, "IP_TOS", IP_TOS);
1562#endif
1563#ifdef IP_TTL
1564 insint(d, "IP_TTL", IP_TTL);
1565#endif
1566#ifdef IP_RECVOPTS
1567 insint(d, "IP_RECVOPTS", IP_RECVOPTS);
1568#endif
1569#ifdef IP_RECVRETOPTS
1570 insint(d, "IP_RECVRETOPTS", IP_RECVRETOPTS);
1571#endif
1572#ifdef IP_RECVDSTADDR
1573 insint(d, "IP_RECVDSTADDR", IP_RECVDSTADDR);
1574#endif
1575#ifdef IP_RETOPTS
1576 insint(d, "IP_RETOPTS", IP_RETOPTS);
1577#endif
1578#ifdef IP_MULTICAST_IF
1579 insint(d, "IP_MULTICAST_IF", IP_MULTICAST_IF);
1580#endif
1581#ifdef IP_MULTICAST_TTL
1582 insint(d, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
1583#endif
1584#ifdef IP_MULTICAST_LOOP
1585 insint(d, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
1586#endif
1587#ifdef IP_ADD_MEMBERSHIP
1588 insint(d, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
1589#endif
1590#ifdef IP_DROP_MEMBERSHIP
1591 insint(d, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
1592#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001593}