blob: b31ea04e3c95a3db75059c9b3b47e5208d13ff2e [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 Rossumb6775db1994-08-01 11:34:53 +000088#include <sys/types.h>
89#include "mytime.h"
Guido van Rossumed233a51992-06-23 09:07:03 +000090
Guido van Rossum81194471991-07-27 21:42:02 +000091#include <signal.h>
Guido van Rossum6f489d91996-06-28 20:15:15 +000092#ifndef MS_WINDOWS
Guido van Rossumb6775db1994-08-01 11:34:53 +000093#include <netdb.h>
Guido van Rossum6574b3e1991-06-25 21:36:08 +000094#include <sys/socket.h>
95#include <netinet/in.h>
Guido van Rossume4485b01994-09-07 14:32:49 +000096#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000097#else
98#include <winsock.h>
Guido van Rossum30b6b2b1995-06-14 22:28:08 +000099#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000100#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000101#ifdef HAVE_SYS_UN_H
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000102#include <sys/un.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000103#else
104#undef AF_UNIX
Guido van Rossum9575a441993-04-07 14:06:14 +0000105#endif
106
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000107#ifndef O_NDELAY
108#define O_NDELAY O_NONBLOCK /* For QNX only? */
109#endif
110
Jack Jansen508537b1996-02-14 15:57:45 +0000111#ifdef USE_GUSI
112/* fdopen() isn't declared in stdio.h (sigh) */
113#include <GUSI.h>
114#endif
115
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000116
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000117/* Here we have some hacks to choose between K&R or ANSI style function
118 definitions. For NT to build this as an extension module (ie, DLL)
119 it must be compiled by the C++ compiler, as it takes the address of
120 a static data item exported from the main Python DLL.
121*/
Guido van Rossum8d665e61996-06-26 18:22:49 +0000122#ifdef MS_WINDOWS
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000123/* seem to be a few differences in the API */
124#define close closesocket
Guido van Rossumbe32c891996-06-20 16:25:29 +0000125#define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000126#define FORCE_ANSI_FUNC_DEFS
127#endif
128
129#ifdef FORCE_ANSI_FUNC_DEFS
130#define BUILD_FUNC_DEF_1( fnname, arg1type, arg1name ) \
131fnname( arg1type arg1name )
132
133#define BUILD_FUNC_DEF_2( fnname, arg1type, arg1name, arg2type, arg2name ) \
134fnname( arg1type arg1name, arg2type arg2name )
135
136#define BUILD_FUNC_DEF_3( fnname, arg1type, arg1name, arg2type, arg2name , arg3type, arg3name ) \
137fnname( arg1type arg1name, arg2type arg2name, arg3type arg3name )
138
139#define BUILD_FUNC_DEF_4( fnname, arg1type, arg1name, arg2type, arg2name , arg3type, arg3name, arg4type, arg4name ) \
140fnname( arg1type arg1name, arg2type arg2name, arg3type arg3name, arg4type arg4name )
141
142#else /* !FORCE_ANSI_FN_DEFS */
143#define BUILD_FUNC_DEF_1( fnname, arg1type, arg1name ) \
144fnname( arg1name ) \
145 arg1type arg1name;
146
147#define BUILD_FUNC_DEF_2( fnname, arg1type, arg1name, arg2type, arg2name ) \
148fnname( arg1name, arg2name ) \
149 arg1type arg1name; \
150 arg2type arg2name;
151
152#define BUILD_FUNC_DEF_3( fnname, arg1type, arg1name, arg2type, arg2name, arg3type, arg3name ) \
153fnname( arg1name, arg2name, arg3name ) \
154 arg1type arg1name; \
155 arg2type arg2name; \
156 arg3type arg3name;
157
158#define BUILD_FUNC_DEF_4( fnname, arg1type, arg1name, arg2type, arg2name, arg3type, arg3name, arg4type, arg4name ) \
159fnname( arg1name, arg2name, arg3name, arg4name ) \
160 arg1type arg1name; \
161 arg2type arg2name; \
162 arg3type arg3name; \
163 arg4type arg4name;
164
165#endif /* !FORCE_ANSI_FN_DEFS */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000166
167/* Global variable holding the exception type for errors detected
168 by this module (but not argument type or memory errors, etc.). */
169
Guido van Rossum73624e91994-10-10 17:59:00 +0000170static PyObject *PySocket_Error;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000171
172
173/* Convenience function to raise an error according to errno
174 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000175
Guido van Rossum73624e91994-10-10 17:59:00 +0000176static PyObject *
177PySocket_Err()
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000178{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000179#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000180 if (WSAGetLastError()) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000181 PyObject *v;
182 v = Py_BuildValue("(is)", WSAGetLastError(), "winsock error");
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000183 if (v != NULL) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000184 PyErr_SetObject(PySocket_Error, v);
185 Py_DECREF(v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000186 }
187 return NULL;
188 }
189 else
190#endif
Guido van Rossum73624e91994-10-10 17:59:00 +0000191 return PyErr_SetFromErrno(PySocket_Error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000192}
193
Guido van Rossum30a685f1991-06-27 15:51:29 +0000194
195/* The object holding a socket. It holds some extra information,
196 like the address family, which is used to decode socket address
197 arguments properly. */
198
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000199typedef struct {
Guido van Rossum73624e91994-10-10 17:59:00 +0000200 PyObject_HEAD
Guido van Rossum30a685f1991-06-27 15:51:29 +0000201 int sock_fd; /* Socket file descriptor */
202 int sock_family; /* Address family, e.g., AF_INET */
203 int sock_type; /* Socket type, e.g., SOCK_STREAM */
204 int sock_proto; /* Protocol type, usually 0 */
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000205 union sock_addr {
206 struct sockaddr_in in;
207#ifdef AF_UNIX
208 struct sockaddr_un un;
209#endif
210 } sock_addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000211} PySocketSockObject;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000212
Guido van Rossum30a685f1991-06-27 15:51:29 +0000213
214/* A forward reference to the Socktype type object.
215 The Socktype variable contains pointers to various functions,
Guido van Rossum73624e91994-10-10 17:59:00 +0000216 some of which call newsockobject(), which uses Socktype, so
Guido van Rossum54ba21b1991-09-10 14:57:12 +0000217 there has to be a circular reference. */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000218
Guido van Rossum73624e91994-10-10 17:59:00 +0000219staticforward PyTypeObject PySocketSock_Type;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000220
Guido van Rossum30a685f1991-06-27 15:51:29 +0000221
222/* Create a new socket object.
223 This just creates the object and initializes it.
224 If the creation fails, return NULL and set an exception (implicit
225 in NEWOBJ()). */
226
Guido van Rossum73624e91994-10-10 17:59:00 +0000227static PySocketSockObject *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000228BUILD_FUNC_DEF_4(PySocketSock_New,int,fd, int,family, int,type, int,proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000229{
Guido van Rossum73624e91994-10-10 17:59:00 +0000230 PySocketSockObject *s;
Guido van Rossum55558541996-05-23 22:54:50 +0000231 PySocketSock_Type.ob_type = &PyType_Type;
Guido van Rossum73624e91994-10-10 17:59:00 +0000232 s = PyObject_NEW(PySocketSockObject, &PySocketSock_Type);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000233 if (s != NULL) {
234 s->sock_fd = fd;
235 s->sock_family = family;
236 s->sock_type = type;
237 s->sock_proto = proto;
238 }
239 return s;
240}
241
Guido van Rossum30a685f1991-06-27 15:51:29 +0000242
243/* Convert a string specifying a host name or one of a few symbolic
244 names to a numeric IP address. This usually calls gethostbyname()
245 to do the work; the names "" and "<broadcast>" are special.
246 Return the length (should always be 4 bytes), or negative if
247 an error occurred; then an exception is raised. */
248
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000249static int
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000250BUILD_FUNC_DEF_2(setipaddr, char*,name, struct sockaddr_in *,addr_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000251{
252 struct hostent *hp;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000253 int d1, d2, d3, d4;
254 char ch;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000255#ifdef HAVE_GETHOSTBYNAME_R
256 struct hostent hp_allocated;
257 char buf[1001];
258 int buf_len = (sizeof buf) - 1;
259 int errnop;
260#endif /* HAVE_GETHOSTBYNAME_R */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000261
Guido van Rossum30a685f1991-06-27 15:51:29 +0000262 if (name[0] == '\0') {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000263 addr_ret->sin_addr.s_addr = INADDR_ANY;
264 return 4;
265 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000266 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000267 addr_ret->sin_addr.s_addr = INADDR_BROADCAST;
268 return 4;
269 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000270 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
271 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
272 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
273 addr_ret->sin_addr.s_addr = htonl(
274 ((long) d1 << 24) | ((long) d2 << 16) |
275 ((long) d3 << 8) | ((long) d4 << 0));
276 return 4;
277 }
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000278#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum73624e91994-10-10 17:59:00 +0000279 Py_BEGIN_ALLOW_THREADS
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000280 hp = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum73624e91994-10-10 17:59:00 +0000281 Py_END_ALLOW_THREADS
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000282#else /* not HAVE_GETHOSTBYNAME_R */
283 hp = gethostbyname(name);
284#endif /* HAVE_GETHOSTBYNAME_R */
285
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000286 if (hp == NULL) {
Guido van Rossumca9b3231995-01-10 16:30:54 +0000287#ifdef HAVE_HSTRERROR
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000288 /* Let's get real error message to return */
289 extern int h_errno;
290 PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno));
291#else
Guido van Rossum73624e91994-10-10 17:59:00 +0000292 PyErr_SetString(PySocket_Error, "host not found");
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000293#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000294 return -1;
295 }
296 memcpy((char *) &addr_ret->sin_addr, hp->h_addr, hp->h_length);
297 return hp->h_length;
298}
299
Guido van Rossum30a685f1991-06-27 15:51:29 +0000300
Guido van Rossum30a685f1991-06-27 15:51:29 +0000301/* Create a string object representing an IP address.
302 This is always a string of the form 'dd.dd.dd.dd' (with variable
303 size numbers). */
304
Guido van Rossum73624e91994-10-10 17:59:00 +0000305static PyObject *
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000306BUILD_FUNC_DEF_1(makeipaddr, struct sockaddr_in *,addr)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000307{
308 long x = ntohl(addr->sin_addr.s_addr);
309 char buf[100];
310 sprintf(buf, "%d.%d.%d.%d",
311 (int) (x>>24) & 0xff, (int) (x>>16) & 0xff,
312 (int) (x>> 8) & 0xff, (int) (x>> 0) & 0xff);
Guido van Rossum73624e91994-10-10 17:59:00 +0000313 return PyString_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000314}
315
316
317/* Create an object representing the given socket address,
318 suitable for passing it back to bind(), connect() etc.
319 The family field of the sockaddr structure is inspected
320 to determine what kind of address it really is. */
321
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000322/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000323static PyObject *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000324BUILD_FUNC_DEF_2(makesockaddr,struct sockaddr *,addr, int,addrlen)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000325{
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000326 if (addrlen == 0) {
327 /* No address -- may be recvfrom() from known socket */
Guido van Rossum73624e91994-10-10 17:59:00 +0000328 Py_INCREF(Py_None);
329 return Py_None;
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000330 }
331
Guido van Rossum30a685f1991-06-27 15:51:29 +0000332 switch (addr->sa_family) {
333
334 case AF_INET:
335 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000336 struct sockaddr_in *a = (struct sockaddr_in *) addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000337 PyObject *addr = makeipaddr(a);
338 PyObject *ret = Py_BuildValue("Oi", addr, ntohs(a->sin_port));
339 Py_XDECREF(addr);
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000340 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000341 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000342
Guido van Rossumb6775db1994-08-01 11:34:53 +0000343#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000344 case AF_UNIX:
345 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000346 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000347 return PyString_FromString(a->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000348 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000349#endif /* AF_UNIX */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000350
351 /* More cases here... */
352
353 default:
Guido van Rossum73624e91994-10-10 17:59:00 +0000354 PyErr_SetString(PySocket_Error, "return unknown socket address type");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000355 return NULL;
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000356
Guido van Rossum30a685f1991-06-27 15:51:29 +0000357 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000358}
359
Guido van Rossum30a685f1991-06-27 15:51:29 +0000360
361/* Parse a socket address argument according to the socket object's
362 address family. Return 1 if the address was in the proper format,
363 0 of not. The address is returned through addr_ret, its length
364 through len_ret. */
365
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000366static int
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000367BUILD_FUNC_DEF_4(
Guido van Rossum73624e91994-10-10 17:59:00 +0000368getsockaddrarg,PySocketSockObject *,s, PyObject *,args, struct sockaddr **,addr_ret, int *,len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000369{
Guido van Rossum30a685f1991-06-27 15:51:29 +0000370 switch (s->sock_family) {
371
Guido van Rossumb6775db1994-08-01 11:34:53 +0000372#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000373 case AF_UNIX:
374 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000375 struct sockaddr_un* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000376 char *path;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000377 int len;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000378 addr = (struct sockaddr_un* )&(s->sock_addr).un;
Guido van Rossum73624e91994-10-10 17:59:00 +0000379 if (!PyArg_Parse(args, "s#", &path, &len))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000380 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000381 if (len > sizeof addr->sun_path) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000382 PyErr_SetString(PySocket_Error, "AF_UNIX path too long");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000383 return 0;
384 }
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000385 addr->sun_family = AF_UNIX;
386 memcpy(addr->sun_path, path, len);
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000387 addr->sun_path[len] = 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000388 *addr_ret = (struct sockaddr *) addr;
Guido van Rossum65af28a1996-06-11 18:36:33 +0000389 *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000390 return 1;
391 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000392#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000393
Guido van Rossum30a685f1991-06-27 15:51:29 +0000394 case AF_INET:
395 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000396 struct sockaddr_in* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000397 char *host;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000398 int port;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000399 addr=(struct sockaddr_in*)&(s->sock_addr).in;
Guido van Rossum73624e91994-10-10 17:59:00 +0000400 if (!PyArg_Parse(args, "(si)", &host, &port))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000401 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000402 if (setipaddr(host, addr) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000403 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000404 addr->sin_family = AF_INET;
405 addr->sin_port = htons(port);
406 *addr_ret = (struct sockaddr *) addr;
407 *len_ret = sizeof *addr;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000408 return 1;
409 }
410
Guido van Rossum30a685f1991-06-27 15:51:29 +0000411 /* More cases here... */
412
413 default:
Guido van Rossum73624e91994-10-10 17:59:00 +0000414 PyErr_SetString(PySocket_Error, "getsockaddrarg: bad family");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000415 return 0;
416
417 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000418}
419
Guido van Rossum30a685f1991-06-27 15:51:29 +0000420
Guido van Rossum710e1df1992-06-12 10:39:36 +0000421/* Get the address length according to the socket object's address family.
422 Return 1 if the family is known, 0 otherwise. The length is returned
423 through len_ret. */
424
425static int
Guido van Rossum73624e91994-10-10 17:59:00 +0000426BUILD_FUNC_DEF_2(getsockaddrlen,PySocketSockObject *,s, int *,len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +0000427{
428 switch (s->sock_family) {
429
Guido van Rossumb6775db1994-08-01 11:34:53 +0000430#ifdef AF_UNIX
Guido van Rossum710e1df1992-06-12 10:39:36 +0000431 case AF_UNIX:
432 {
433 *len_ret = sizeof (struct sockaddr_un);
434 return 1;
435 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000436#endif /* AF_UNIX */
Guido van Rossum710e1df1992-06-12 10:39:36 +0000437
438 case AF_INET:
439 {
440 *len_ret = sizeof (struct sockaddr_in);
441 return 1;
442 }
443
444 /* More cases here... */
445
446 default:
Guido van Rossum73624e91994-10-10 17:59:00 +0000447 PyErr_SetString(PySocket_Error, "getsockaddrarg: bad family");
Guido van Rossum710e1df1992-06-12 10:39:36 +0000448 return 0;
449
450 }
451}
452
453
Guido van Rossum30a685f1991-06-27 15:51:29 +0000454/* s.accept() method */
455
Guido van Rossum73624e91994-10-10 17:59:00 +0000456static PyObject *
457BUILD_FUNC_DEF_2(PySocketSock_accept,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000458{
459 char addrbuf[256];
460 int addrlen, newfd;
Guido van Rossum73624e91994-10-10 17:59:00 +0000461 PyObject *sock, *addr, *res;
462 if (!PyArg_NoArgs(args))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000463 return NULL;
Guido van Rossum710e1df1992-06-12 10:39:36 +0000464 if (!getsockaddrlen(s, &addrlen))
465 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000466 Py_BEGIN_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000467 newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000468 Py_END_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000469 if (newfd < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000470 return PySocket_Err();
Guido van Rossum30a685f1991-06-27 15:51:29 +0000471 /* Create the new object with unspecified family,
472 to avoid calls to bind() etc. on it. */
Guido van Rossum73624e91994-10-10 17:59:00 +0000473 sock = (PyObject *) PySocketSock_New(newfd,
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000474 s->sock_family,
475 s->sock_type,
476 s->sock_proto);
477 if (sock == NULL)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000478 close(newfd);
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000479 addr = makesockaddr((struct sockaddr *) addrbuf, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000480 res = Py_BuildValue("OO", sock, addr);
481 Py_XDECREF(sock);
482 Py_XDECREF(addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000483 return res;
484}
485
Guido van Rossum30a685f1991-06-27 15:51:29 +0000486
Guido van Rossume4485b01994-09-07 14:32:49 +0000487/* s.setblocking(1 | 0) method */
488
Guido van Rossum73624e91994-10-10 17:59:00 +0000489static PyObject *
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000490BUILD_FUNC_DEF_2(PySocketSock_setblocking,PySocketSockObject*,s,PyObject*,args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000491{
492 int block;
493 int delay_flag;
Guido van Rossum73624e91994-10-10 17:59:00 +0000494 if (!PyArg_GetInt(args, &block))
Guido van Rossume4485b01994-09-07 14:32:49 +0000495 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000496 Py_BEGIN_ALLOW_THREADS
Guido van Rossum6f489d91996-06-28 20:15:15 +0000497#ifndef MS_WINDOWS
Guido van Rossume4485b01994-09-07 14:32:49 +0000498 delay_flag = fcntl (s->sock_fd, F_GETFL, 0);
499 if (block)
500 delay_flag &= (~O_NDELAY);
501 else
502 delay_flag |= O_NDELAY;
503 fcntl (s->sock_fd, F_SETFL, delay_flag);
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000504#else
505 block = !block;
506 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
507#endif
Guido van Rossum73624e91994-10-10 17:59:00 +0000508 Py_END_ALLOW_THREADS
Guido van Rossume4485b01994-09-07 14:32:49 +0000509
Guido van Rossum73624e91994-10-10 17:59:00 +0000510 Py_INCREF(Py_None);
511 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000512}
Guido van Rossume4485b01994-09-07 14:32:49 +0000513
514
Guido van Rossumaee08791992-09-08 09:05:33 +0000515/* s.setsockopt() method.
516 With an integer third argument, sets an integer option.
517 With a string third argument, sets an option from a buffer;
518 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000519
Guido van Rossum73624e91994-10-10 17:59:00 +0000520static PyObject *
521BUILD_FUNC_DEF_2(PySocketSock_setsockopt,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000522{
523 int level;
524 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000525 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +0000526 char *buf;
527 int buflen;
528 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000529
Guido van Rossum73624e91994-10-10 17:59:00 +0000530 if (PyArg_Parse(args, "(iii)", &level, &optname, &flag)) {
Guido van Rossumaee08791992-09-08 09:05:33 +0000531 buf = (char *) &flag;
532 buflen = sizeof flag;
533 }
534 else {
Guido van Rossum73624e91994-10-10 17:59:00 +0000535 PyErr_Clear();
536 if (!PyArg_Parse(args, "(iis#)", &level, &optname, &buf, &buflen))
Guido van Rossumaee08791992-09-08 09:05:33 +0000537 return NULL;
538 }
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000539 res = setsockopt(s->sock_fd, level, optname, (ANY *)buf, buflen);
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000540 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000541 return PySocket_Err();
542 Py_INCREF(Py_None);
543 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000544}
545
546
Guido van Rossumaee08791992-09-08 09:05:33 +0000547/* s.getsockopt() method.
548 With two arguments, retrieves an integer option.
549 With a third integer argument, retrieves a string buffer of that size;
550 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000551
Guido van Rossum73624e91994-10-10 17:59:00 +0000552static PyObject *
553BUILD_FUNC_DEF_2(PySocketSock_getsockopt,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000554{
555 int level;
556 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000557 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +0000558 PyObject *buf;
Guido van Rossumbe32c891996-06-20 16:25:29 +0000559 int buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000560
Guido van Rossumbe32c891996-06-20 16:25:29 +0000561 if (!PyArg_ParseTuple(args, "ii|i", &level, &optname, &buflen))
562 return NULL;
563
564 if (buflen == 0) {
Guido van Rossumaee08791992-09-08 09:05:33 +0000565 int flag = 0;
566 int flagsize = sizeof flag;
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000567 res = getsockopt(s->sock_fd, level, optname,
568 (ANY *)&flag, &flagsize);
Guido van Rossumaee08791992-09-08 09:05:33 +0000569 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000570 return PySocket_Err();
571 return PyInt_FromLong(flag);
Guido van Rossumaee08791992-09-08 09:05:33 +0000572 }
Guido van Rossumaee08791992-09-08 09:05:33 +0000573 if (buflen <= 0 || buflen > 1024) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000574 PyErr_SetString(PySocket_Error, "getsockopt buflen out of range");
Guido van Rossumaee08791992-09-08 09:05:33 +0000575 return NULL;
576 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000577 buf = PyString_FromStringAndSize((char *)NULL, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000578 if (buf == NULL)
579 return NULL;
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000580 res = getsockopt(s->sock_fd, level, optname,
Guido van Rossuma597dde1995-01-10 20:56:29 +0000581 (ANY *)PyString_AsString(buf), &buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000582 if (res < 0) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000583 Py_DECREF(buf);
584 return PySocket_Err();
Guido van Rossumaee08791992-09-08 09:05:33 +0000585 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000586 _PyString_Resize(&buf, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000587 return buf;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000588}
589
590
Guido van Rossum30a685f1991-06-27 15:51:29 +0000591/* s.bind(sockaddr) method */
592
Guido van Rossum73624e91994-10-10 17:59:00 +0000593static PyObject *
594BUILD_FUNC_DEF_2(PySocketSock_bind,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000595{
596 struct sockaddr *addr;
597 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000598 int res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000599 if (!getsockaddrarg(s, args, &addr, &addrlen))
600 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000601 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000602 res = bind(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000603 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000604 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000605 return PySocket_Err();
606 Py_INCREF(Py_None);
607 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000608}
609
Guido van Rossum30a685f1991-06-27 15:51:29 +0000610
611/* s.close() method.
612 Set the file descriptor to -1 so operations tried subsequently
613 will surely fail. */
614
Guido van Rossum73624e91994-10-10 17:59:00 +0000615static PyObject *
616BUILD_FUNC_DEF_2(PySocketSock_close,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000617{
Guido van Rossum73624e91994-10-10 17:59:00 +0000618 if (!PyArg_NoArgs(args))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000619 return NULL;
Guido van Rossum08481461996-10-12 14:07:22 +0000620 if (s->sock_fd != -1) {
621 Py_BEGIN_ALLOW_THREADS
622 (void) close(s->sock_fd);
623 Py_END_ALLOW_THREADS
624 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000625 s->sock_fd = -1;
Guido van Rossum73624e91994-10-10 17:59:00 +0000626 Py_INCREF(Py_None);
627 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000628}
629
Guido van Rossum30a685f1991-06-27 15:51:29 +0000630
631/* s.connect(sockaddr) method */
632
Guido van Rossum73624e91994-10-10 17:59:00 +0000633static PyObject *
634BUILD_FUNC_DEF_2(PySocketSock_connect,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000635{
636 struct sockaddr *addr;
637 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000638 int res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000639 if (!getsockaddrarg(s, args, &addr, &addrlen))
640 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000641 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000642 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000643 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000644 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000645 return PySocket_Err();
646 Py_INCREF(Py_None);
647 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000648}
649
Guido van Rossum30a685f1991-06-27 15:51:29 +0000650
Guido van Rossumed233a51992-06-23 09:07:03 +0000651/* s.fileno() method */
652
Guido van Rossum73624e91994-10-10 17:59:00 +0000653static PyObject *
654BUILD_FUNC_DEF_2(PySocketSock_fileno,PySocketSockObject *,s, PyObject *,args)
Guido van Rossumed233a51992-06-23 09:07:03 +0000655{
Guido van Rossum73624e91994-10-10 17:59:00 +0000656 if (!PyArg_NoArgs(args))
Guido van Rossumed233a51992-06-23 09:07:03 +0000657 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000658 return PyInt_FromLong((long) s->sock_fd);
Guido van Rossumed233a51992-06-23 09:07:03 +0000659}
660
661
Guido van Rossumbe32c891996-06-20 16:25:29 +0000662#ifndef NO_DUP
663/* s.dup() method */
664
665static PyObject *
666BUILD_FUNC_DEF_2(PySocketSock_dup,PySocketSockObject *,s, PyObject *,args)
667{
668 int newfd;
669 PyObject *sock;
670 if (!PyArg_NoArgs(args))
671 return NULL;
672 newfd = dup(s->sock_fd);
673 if (newfd < 0)
674 return PySocket_Err();
675 sock = (PyObject *) PySocketSock_New(newfd,
676 s->sock_family,
677 s->sock_type,
678 s->sock_proto);
679 if (sock == NULL)
680 close(newfd);
681 return sock;
682}
683#endif
684
685
Guido van Rossumc89705d1992-11-26 08:54:07 +0000686/* s.getsockname() method */
687
Guido van Rossum73624e91994-10-10 17:59:00 +0000688static PyObject *
689BUILD_FUNC_DEF_2(PySocketSock_getsockname,PySocketSockObject *,s, PyObject *,args)
Guido van Rossumc89705d1992-11-26 08:54:07 +0000690{
691 char addrbuf[256];
692 int addrlen, res;
Guido van Rossum73624e91994-10-10 17:59:00 +0000693 if (!PyArg_NoArgs(args))
Guido van Rossumc89705d1992-11-26 08:54:07 +0000694 return NULL;
695 if (!getsockaddrlen(s, &addrlen))
696 return NULL;
Guido van Rossumca42b161996-01-12 01:36:05 +0000697 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000698 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +0000699 res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000700 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +0000701 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000702 return PySocket_Err();
Guido van Rossumc89705d1992-11-26 08:54:07 +0000703 return makesockaddr((struct sockaddr *) addrbuf, addrlen);
704}
705
706
Guido van Rossumb6775db1994-08-01 11:34:53 +0000707#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +0000708/* s.getpeername() method */
709
Guido van Rossum73624e91994-10-10 17:59:00 +0000710static PyObject *
711BUILD_FUNC_DEF_2(PySocketSock_getpeername,PySocketSockObject *,s, PyObject *,args)
Guido van Rossumc89705d1992-11-26 08:54:07 +0000712{
713 char addrbuf[256];
714 int addrlen, res;
Guido van Rossum73624e91994-10-10 17:59:00 +0000715 if (!PyArg_NoArgs(args))
Guido van Rossumc89705d1992-11-26 08:54:07 +0000716 return NULL;
717 if (!getsockaddrlen(s, &addrlen))
718 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000719 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +0000720 res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000721 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +0000722 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000723 return PySocket_Err();
Guido van Rossumc89705d1992-11-26 08:54:07 +0000724 return makesockaddr((struct sockaddr *) addrbuf, addrlen);
725}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000726#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +0000727
728
Guido van Rossum30a685f1991-06-27 15:51:29 +0000729/* s.listen(n) method */
730
Guido van Rossum73624e91994-10-10 17:59:00 +0000731static PyObject *
732BUILD_FUNC_DEF_2(PySocketSock_listen,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000733{
734 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000735 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +0000736 if (!PyArg_GetInt(args, &backlog))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000737 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000738 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000739 if (backlog < 1)
740 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000741 res = listen(s->sock_fd, backlog);
Guido van Rossum73624e91994-10-10 17:59:00 +0000742 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000743 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000744 return PySocket_Err();
745 Py_INCREF(Py_None);
746 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000747}
748
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000749#ifndef NO_DUP
Guido van Rossum30a685f1991-06-27 15:51:29 +0000750/* s.makefile(mode) method.
751 Create a new open file object referring to a dupped version of
752 the socket's file descriptor. (The dup() call is necessary so
753 that the open file and socket objects may be closed independent
754 of each other.)
755 The mode argument specifies 'r' or 'w' passed to fdopen(). */
756
Guido van Rossum73624e91994-10-10 17:59:00 +0000757static PyObject *
758BUILD_FUNC_DEF_2(PySocketSock_makefile,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000759{
Guido van Rossuma597dde1995-01-10 20:56:29 +0000760 extern int fclose Py_PROTO((FILE *));
Guido van Rossum6b144911995-03-14 15:05:13 +0000761 char *mode = "r";
762 int bufsize = -1;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000763 int fd;
764 FILE *fp;
Guido van Rossum6b144911995-03-14 15:05:13 +0000765 PyObject *f;
766
767 if (!PyArg_ParseTuple(args, "|si", &mode, &bufsize))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000768 return NULL;
Guido van Rossum8d665e61996-06-26 18:22:49 +0000769#ifdef MS_WIN32
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000770 if ( ((fd = _open_osfhandle( s->sock_fd, _O_BINARY )) < 0) ||
771 ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL)) {
772#else
Guido van Rossum30a685f1991-06-27 15:51:29 +0000773 if ((fd = dup(s->sock_fd)) < 0 ||
Guido van Rossum6b144911995-03-14 15:05:13 +0000774 (fp = fdopen(fd, mode)) == NULL) {
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000775#endif
Guido van Rossum6b144911995-03-14 15:05:13 +0000776 if (fd >= 0)
777 close(fd);
Guido van Rossum73624e91994-10-10 17:59:00 +0000778 return PySocket_Err();
Guido van Rossum6b144911995-03-14 15:05:13 +0000779 }
780 f = PyFile_FromFile(fp, "<socket>", mode, fclose);
781 if (f != NULL)
782 PyFile_SetBufSize(f, bufsize);
783 return f;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000784}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000785#endif /* NO_DUP */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000786
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000787/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000788
Guido van Rossum73624e91994-10-10 17:59:00 +0000789static PyObject *
790BUILD_FUNC_DEF_2(PySocketSock_recv,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000791{
Guido van Rossumbe32c891996-06-20 16:25:29 +0000792 int len, n, flags = 0;
Guido van Rossum73624e91994-10-10 17:59:00 +0000793 PyObject *buf;
Guido van Rossumbe32c891996-06-20 16:25:29 +0000794 if (!PyArg_ParseTuple(args, "i|i", &len, &flags))
795 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000796 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000797 if (buf == NULL)
798 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000799 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma597dde1995-01-10 20:56:29 +0000800 n = recv(s->sock_fd, PyString_AsString(buf), len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +0000801 Py_END_ALLOW_THREADS
Guido van Rossum7c53b771995-09-13 18:39:47 +0000802 if (n < 0) {
803 Py_DECREF(buf);
Guido van Rossum73624e91994-10-10 17:59:00 +0000804 return PySocket_Err();
Guido van Rossum7c53b771995-09-13 18:39:47 +0000805 }
Guido van Rossum53a9bf81996-06-11 18:35:24 +0000806 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000807 return NULL;
808 return buf;
809}
810
Guido van Rossum30a685f1991-06-27 15:51:29 +0000811
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000812/* s.recvfrom(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000813
Guido van Rossum73624e91994-10-10 17:59:00 +0000814static PyObject *
815BUILD_FUNC_DEF_2(PySocketSock_recvfrom,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000816{
817 char addrbuf[256];
Guido van Rossum73624e91994-10-10 17:59:00 +0000818 PyObject *buf, *addr, *ret;
Guido van Rossumbe32c891996-06-20 16:25:29 +0000819 int addrlen, len, n, flags = 0;
820 if (!PyArg_ParseTuple(args, "i|i", &len, &flags))
821 return NULL;
Guido van Rossum18c9a4f1993-05-25 12:16:29 +0000822 if (!getsockaddrlen(s, &addrlen))
823 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000824 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum18c9a4f1993-05-25 12:16:29 +0000825 if (buf == NULL)
826 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000827 Py_BEGIN_ALLOW_THREADS
828 n = recvfrom(s->sock_fd, PyString_AsString(buf), len, flags,
Guido van Rossum8d665e61996-06-26 18:22:49 +0000829#ifndef MS_WINDOWS
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000830 (ANY *)addrbuf, &addrlen);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000831#else
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000832 (struct sockaddr *)addrbuf, &addrlen);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000833#endif
Guido van Rossum73624e91994-10-10 17:59:00 +0000834 Py_END_ALLOW_THREADS
Guido van Rossum7c53b771995-09-13 18:39:47 +0000835 if (n < 0) {
836 Py_DECREF(buf);
Guido van Rossum73624e91994-10-10 17:59:00 +0000837 return PySocket_Err();
Guido van Rossum7c53b771995-09-13 18:39:47 +0000838 }
Guido van Rossum53a9bf81996-06-11 18:35:24 +0000839 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000840 return NULL;
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000841 addr = makesockaddr((struct sockaddr *)addrbuf, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000842 ret = Py_BuildValue("OO", buf, addr);
843 Py_XDECREF(addr);
844 Py_XDECREF(buf);
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000845 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000846}
847
Guido van Rossum30a685f1991-06-27 15:51:29 +0000848
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000849/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000850
Guido van Rossum73624e91994-10-10 17:59:00 +0000851static PyObject *
852BUILD_FUNC_DEF_2(PySocketSock_send,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000853{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000854 char *buf;
Guido van Rossumbe32c891996-06-20 16:25:29 +0000855 int len, n, flags = 0;
856 if (!PyArg_ParseTuple(args, "s#|i", &buf, &len, &flags))
857 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000858 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000859 n = send(s->sock_fd, buf, len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +0000860 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +0000861 if (n < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000862 return PySocket_Err();
863 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000864}
865
866
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000867/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000868
Guido van Rossum73624e91994-10-10 17:59:00 +0000869static PyObject *
870BUILD_FUNC_DEF_2(PySocketSock_sendto,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000871{
Guido van Rossum73624e91994-10-10 17:59:00 +0000872 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000873 char *buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000874 struct sockaddr *addr;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000875 int addrlen, len, n, flags;
876 flags = 0;
Guido van Rossum73624e91994-10-10 17:59:00 +0000877 if (!PyArg_Parse(args, "(s#O)", &buf, &len, &addro)) {
878 PyErr_Clear();
879 if (!PyArg_Parse(args, "(s#iO)", &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000880 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000881 }
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000882 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000883 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000884 Py_BEGIN_ALLOW_THREADS
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000885 n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000886 Py_END_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000887 if (n < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000888 return PySocket_Err();
889 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000890}
891
Guido van Rossum30a685f1991-06-27 15:51:29 +0000892
893/* s.shutdown(how) method */
894
Guido van Rossum73624e91994-10-10 17:59:00 +0000895static PyObject *
896BUILD_FUNC_DEF_2(PySocketSock_shutdown,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000897{
898 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000899 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +0000900 if (!PyArg_GetInt(args, &how))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000901 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000902 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000903 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +0000904 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000905 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000906 return PySocket_Err();
907 Py_INCREF(Py_None);
908 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000909}
910
Guido van Rossum30a685f1991-06-27 15:51:29 +0000911
912/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000913
Guido van Rossum73624e91994-10-10 17:59:00 +0000914static PyMethodDef PySocketSock_methods[] = {
915 {"accept", (PyCFunction)PySocketSock_accept},
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000916 {"setblocking", (PyCFunction)PySocketSock_setblocking},
Guido van Rossum73624e91994-10-10 17:59:00 +0000917 {"setsockopt", (PyCFunction)PySocketSock_setsockopt},
Guido van Rossumbe32c891996-06-20 16:25:29 +0000918 {"getsockopt", (PyCFunction)PySocketSock_getsockopt, 1},
Guido van Rossum73624e91994-10-10 17:59:00 +0000919 {"bind", (PyCFunction)PySocketSock_bind},
920 {"close", (PyCFunction)PySocketSock_close},
921 {"connect", (PyCFunction)PySocketSock_connect},
922 {"fileno", (PyCFunction)PySocketSock_fileno},
Guido van Rossumbe32c891996-06-20 16:25:29 +0000923#ifndef NO_DUP
924 {"dup", (PyCFunction)PySocketSock_dup},
925#endif
Guido van Rossum73624e91994-10-10 17:59:00 +0000926 {"getsockname", (PyCFunction)PySocketSock_getsockname},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000927#ifdef HAVE_GETPEERNAME
Guido van Rossum73624e91994-10-10 17:59:00 +0000928 {"getpeername", (PyCFunction)PySocketSock_getpeername},
Guido van Rossum9575a441993-04-07 14:06:14 +0000929#endif
Guido van Rossum73624e91994-10-10 17:59:00 +0000930 {"listen", (PyCFunction)PySocketSock_listen},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000931#ifndef NO_DUP
Guido van Rossum6b144911995-03-14 15:05:13 +0000932 {"makefile", (PyCFunction)PySocketSock_makefile, 1},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000933#endif
Guido van Rossumbe32c891996-06-20 16:25:29 +0000934 {"recv", (PyCFunction)PySocketSock_recv, 1},
935 {"recvfrom", (PyCFunction)PySocketSock_recvfrom, 1},
936 {"send", (PyCFunction)PySocketSock_send, 1},
Guido van Rossum73624e91994-10-10 17:59:00 +0000937 {"sendto", (PyCFunction)PySocketSock_sendto},
938 {"shutdown", (PyCFunction)PySocketSock_shutdown},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000939 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000940};
941
Guido van Rossum30a685f1991-06-27 15:51:29 +0000942
Guido van Rossum73624e91994-10-10 17:59:00 +0000943/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +0000944 First close the file description. */
945
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000946static void
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000947BUILD_FUNC_DEF_1(PySocketSock_dealloc,PySocketSockObject *,s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000948{
949 (void) close(s->sock_fd);
Guido van Rossum73624e91994-10-10 17:59:00 +0000950 PyMem_DEL(s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000951}
952
Guido van Rossum30a685f1991-06-27 15:51:29 +0000953
954/* Return a socket object's named attribute. */
955
Guido van Rossum73624e91994-10-10 17:59:00 +0000956static PyObject *
957BUILD_FUNC_DEF_2(PySocketSock_getattr,PySocketSockObject *,s, char *,name)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000958{
Guido van Rossum73624e91994-10-10 17:59:00 +0000959 return Py_FindMethod(PySocketSock_methods, (PyObject *) s, name);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000960}
961
Guido van Rossum30a685f1991-06-27 15:51:29 +0000962
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000963static PyObject *
964BUILD_FUNC_DEF_1(PySocketSock_repr,PySocketSockObject *,s)
965{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000966 char buf[512];
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000967 sprintf(buf,
968 "<socket object, fd=%d, family=%d, type=%d, protocol=%d>",
969 s->sock_fd, s->sock_family, s->sock_type, s->sock_proto);
Guido van Rossumdc1c64a1996-02-27 15:21:19 +0000970 return PyString_FromString(buf);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000971}
972
973
Guido van Rossumb6775db1994-08-01 11:34:53 +0000974/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000975
Guido van Rossum73624e91994-10-10 17:59:00 +0000976static PyTypeObject PySocketSock_Type = {
Guido van Rossum55558541996-05-23 22:54:50 +0000977 PyObject_HEAD_INIT(0) /* Must fill in type value later */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000978 0,
979 "socket",
Guido van Rossum73624e91994-10-10 17:59:00 +0000980 sizeof(PySocketSockObject),
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000981 0,
Guido van Rossum73624e91994-10-10 17:59:00 +0000982 (destructor)PySocketSock_dealloc, /*tp_dealloc*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000983 0, /*tp_print*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000984 (getattrfunc)PySocketSock_getattr, /*tp_getattr*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000985 0, /*tp_setattr*/
986 0, /*tp_compare*/
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000987 (reprfunc)PySocketSock_repr, /*tp_repr*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000988 0, /*tp_as_number*/
989 0, /*tp_as_sequence*/
990 0, /*tp_as_mapping*/
991};
992
Guido van Rossum30a685f1991-06-27 15:51:29 +0000993
Guido van Rossum81194471991-07-27 21:42:02 +0000994/* Python interface to gethostname(). */
995
996/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000997static PyObject *
998BUILD_FUNC_DEF_2(PySocket_gethostname,PyObject *,self, PyObject *,args)
Guido van Rossum81194471991-07-27 21:42:02 +0000999{
1000 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001001 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +00001002 if (!PyArg_NoArgs(args))
Guido van Rossum81194471991-07-27 21:42:02 +00001003 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001004 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001005 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00001006 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001007 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001008 return PySocket_Err();
Guido van Rossum81194471991-07-27 21:42:02 +00001009 buf[sizeof buf - 1] = '\0';
Guido van Rossum73624e91994-10-10 17:59:00 +00001010 return PyString_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00001011}
Guido van Rossumff4949e1992-08-05 19:58:53 +00001012
1013
Guido van Rossum30a685f1991-06-27 15:51:29 +00001014/* Python interface to gethostbyname(name). */
1015
1016/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001017static PyObject *
1018BUILD_FUNC_DEF_2(PySocket_gethostbyname,PyObject *,self, PyObject *,args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001019{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001020 char *name;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001021 struct sockaddr_in addrbuf;
Guido van Rossum73624e91994-10-10 17:59:00 +00001022 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00001023 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001024 if (setipaddr(name, &addrbuf) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001025 return NULL;
1026 return makeipaddr(&addrbuf);
1027}
1028
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001029/* Python interface to gethostbyaddr(IP). */
1030
1031/*ARGSUSED*/
1032static PyObject *
1033BUILD_FUNC_DEF_2(PySocket_gethostbyaddr,PyObject *,self, PyObject *, args)
1034{
1035 struct sockaddr_in addr;
1036 char *ip_num;
1037 struct hostent *h;
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00001038 char **pch;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001039 PyObject *rtn_tuple = (PyObject *)NULL;
1040 PyObject *name_list = (PyObject *)NULL;
1041 PyObject *addr_list = (PyObject *)NULL;
1042 PyObject *tmp;
1043
1044 if (!PyArg_Parse(args, "s", &ip_num))
1045 return NULL;
1046 if (setipaddr(ip_num, &addr) < 0)
1047 return NULL;
1048 h = gethostbyaddr((char *)&addr.sin_addr,
1049 sizeof(addr.sin_addr),
1050 AF_INET);
1051 if (h == NULL) {
Guido van Rossumca9b3231995-01-10 16:30:54 +00001052#ifdef HAVE_HSTRERROR
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001053 /* Let's get real error message to return */
1054 extern int h_errno;
1055 PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno));
1056#else
1057 PyErr_SetString(PySocket_Error, "host not found");
1058#endif
1059 return NULL;
1060 }
1061 if ((name_list = PyList_New(0)) == NULL)
1062 goto err;
1063 if ((addr_list = PyList_New(0)) == NULL)
1064 goto err;
1065 for (pch = h->h_aliases; *pch != NULL; pch++) {
1066 tmp = PyString_FromString(*pch);
1067 if (tmp == NULL)
1068 goto err;
1069 PyList_Append(name_list, tmp);
1070 Py_DECREF(tmp);
1071 }
1072 for (pch = h->h_addr_list; *pch != NULL; pch++) {
1073 memcpy((char *) &addr.sin_addr, *pch, h->h_length);
1074 tmp = makeipaddr(&addr);
1075 if (tmp == NULL)
1076 goto err;
1077 PyList_Append(addr_list, tmp);
1078 Py_DECREF(tmp);
1079 }
1080 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
1081 err:
1082 Py_XDECREF(name_list);
1083 Py_XDECREF(addr_list);
1084 return rtn_tuple;
1085}
1086
Guido van Rossum30a685f1991-06-27 15:51:29 +00001087
1088/* Python interface to getservbyname(name).
1089 This only returns the port number, since the other info is already
1090 known or not useful (like the list of aliases). */
1091
1092/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001093static PyObject *
1094BUILD_FUNC_DEF_2(PySocket_getservbyname,PyObject *,self, PyObject *,args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001095{
Guido van Rossumff4949e1992-08-05 19:58:53 +00001096 char *name, *proto;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001097 struct servent *sp;
Guido van Rossum73624e91994-10-10 17:59:00 +00001098 if (!PyArg_Parse(args, "(ss)", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00001099 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001100 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001101 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00001102 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00001103 if (sp == NULL) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001104 PyErr_SetString(PySocket_Error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00001105 return NULL;
1106 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001107 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00001108}
1109
1110
1111/* Python interface to socket(family, type, proto).
1112 The third (protocol) argument is optional.
1113 Return a new socket object. */
1114
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001115/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001116static PyObject *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001117BUILD_FUNC_DEF_2(PySocket_socket,PyObject *,self, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001118{
Guido van Rossum73624e91994-10-10 17:59:00 +00001119 PySocketSockObject *s;
Guido van Rossum6f489d91996-06-28 20:15:15 +00001120#ifdef MS_WINDOWS
Guido van Rossumd639d4d1996-06-12 04:04:22 +00001121 SOCKET fd;
1122#else
1123 int fd;
1124#endif
Guido van Rossumbe32c891996-06-20 16:25:29 +00001125 int family, type, proto = 0;
1126 if (!PyArg_ParseTuple(args, "ii|i", &family, &type, &proto))
1127 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001128 Py_BEGIN_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001129 fd = socket(family, type, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00001130 Py_END_ALLOW_THREADS
Guido van Rossum6f489d91996-06-28 20:15:15 +00001131#ifdef MS_WINDOWS
Guido van Rossumd639d4d1996-06-12 04:04:22 +00001132 if (fd == INVALID_SOCKET)
1133#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001134 if (fd < 0)
Guido van Rossumd639d4d1996-06-12 04:04:22 +00001135#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001136 return PySocket_Err();
1137 s = PySocketSock_New(fd, family, type, proto);
Guido van Rossum30a685f1991-06-27 15:51:29 +00001138 /* If the object can't be created, don't forget to close the
1139 file descriptor again! */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001140 if (s == NULL)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001141 (void) close(fd);
Guido van Rossum81194471991-07-27 21:42:02 +00001142 /* From now on, ignore SIGPIPE and let the error checking
1143 do the work. */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001144#ifdef SIGPIPE
Guido van Rossum81194471991-07-27 21:42:02 +00001145 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001146#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001147 return (PyObject *) s;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001148}
1149
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001150#ifndef NO_DUP
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001151/* Create a socket object from a numeric file description.
1152 Useful e.g. if stdin is a socket.
1153 Additional arguments as for socket(). */
1154
1155/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001156static PyObject *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001157BUILD_FUNC_DEF_2(PySocket_fromfd,PyObject *,self, PyObject *,args)
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001158{
Guido van Rossum73624e91994-10-10 17:59:00 +00001159 PySocketSockObject *s;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001160 int fd, family, type, proto = 0;
1161 if (!PyArg_ParseTuple(args, "iii|i", &fd, &family, &type, &proto))
1162 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +00001163 /* Dup the fd so it and the socket can be closed independently */
1164 fd = dup(fd);
1165 if (fd < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001166 return PySocket_Err();
1167 s = PySocketSock_New(fd, family, type, proto);
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001168 /* From now on, ignore SIGPIPE and let the error checking
1169 do the work. */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001170#ifdef SIGPIPE
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001171 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001172#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001173 return (PyObject *) s;
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001174}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001175#endif /* NO_DUP */
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001176
Guido van Rossum006bf911996-06-12 04:04:55 +00001177static PyObject *
1178BUILD_FUNC_DEF_2(PySocket_ntohs, PyObject *, self, PyObject *, args)
1179{
1180 int x1, x2;
1181
1182 if (!PyArg_Parse(args, "i", &x1)) {
1183 return NULL;
1184 }
1185 x2 = (int)ntohs((short)x1);
1186 return PyInt_FromLong(x2);
1187}
1188
1189static PyObject *
1190BUILD_FUNC_DEF_2(PySocket_ntohl, PyObject *, self, PyObject *, args)
1191{
1192 int x1, x2;
1193
1194 if (!PyArg_Parse(args, "i", &x1)) {
1195 return NULL;
1196 }
1197 x2 = ntohl(x1);
1198 return PyInt_FromLong(x2);
1199}
1200
1201static PyObject *
1202BUILD_FUNC_DEF_2(PySocket_htons, PyObject *, self, PyObject *, args)
1203{
1204 int x1, x2;
1205
1206 if (!PyArg_Parse(args, "i", &x1)) {
1207 return NULL;
1208 }
1209 x2 = (int)htons((short)x1);
1210 return PyInt_FromLong(x2);
1211}
1212
1213static PyObject *
1214BUILD_FUNC_DEF_2(PySocket_htonl, PyObject *, self, PyObject *, args)
1215{
1216 int x1, x2;
1217
1218 if (!PyArg_Parse(args, "i", &x1)) {
1219 return NULL;
1220 }
1221 x2 = htonl(x1);
1222 return PyInt_FromLong(x2);
1223}
1224
Guido van Rossum30a685f1991-06-27 15:51:29 +00001225/* List of functions exported by this module. */
1226
Guido van Rossum73624e91994-10-10 17:59:00 +00001227static PyMethodDef PySocket_methods[] = {
1228 {"gethostbyname", PySocket_gethostbyname},
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001229 {"gethostbyaddr", PySocket_gethostbyaddr},
Guido van Rossum73624e91994-10-10 17:59:00 +00001230 {"gethostname", PySocket_gethostname},
1231 {"getservbyname", PySocket_getservbyname},
Guido van Rossumbe32c891996-06-20 16:25:29 +00001232 {"socket", PySocket_socket, 1},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001233#ifndef NO_DUP
Guido van Rossumbe32c891996-06-20 16:25:29 +00001234 {"fromfd", PySocket_fromfd, 1},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001235#endif
Guido van Rossum006bf911996-06-12 04:04:55 +00001236 {"ntohs", PySocket_ntohs},
1237 {"ntohl", PySocket_ntohl},
1238 {"htons", PySocket_htons},
1239 {"htonl", PySocket_htonl},
Guido van Rossum30a685f1991-06-27 15:51:29 +00001240 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001241};
1242
Guido van Rossum30a685f1991-06-27 15:51:29 +00001243
1244/* Convenience routine to export an integer value.
1245 For simplicity, errors (which are unlikely anyway) are ignored. */
1246
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001247static void
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001248BUILD_FUNC_DEF_3(insint,PyObject *,d, char *,name, int,value)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001249{
Guido van Rossum73624e91994-10-10 17:59:00 +00001250 PyObject *v = PyInt_FromLong((long) value);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001251 if (v == NULL) {
1252 /* Don't bother reporting this error */
Guido van Rossum73624e91994-10-10 17:59:00 +00001253 PyErr_Clear();
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001254 }
1255 else {
Guido van Rossum73624e91994-10-10 17:59:00 +00001256 PyDict_SetItemString(d, name, v);
1257 Py_DECREF(v);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001258 }
1259}
1260
Guido van Rossum30a685f1991-06-27 15:51:29 +00001261
Guido van Rossum8d665e61996-06-26 18:22:49 +00001262#ifdef MS_WINDOWS
Guido van Rossumbe32c891996-06-20 16:25:29 +00001263
1264/* Additional initialization and cleanup for NT/Windows */
1265
1266static void
1267NTcleanup()
1268{
1269 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00001270}
1271
1272static int
1273NTinit()
1274{
1275 WSADATA WSAData;
1276 int ret;
1277 char buf[100];
1278 ret = WSAStartup(0x0101, &WSAData);
1279 switch (ret) {
1280 case 0: /* no error */
1281 atexit(NTcleanup);
1282 return 1;
1283 case WSASYSNOTREADY:
1284 PyErr_SetString(PyExc_ImportError,
1285 "WSAStartup failed: network not ready");
1286 break;
1287 case WSAVERNOTSUPPORTED:
1288 case WSAEINVAL:
1289 PyErr_SetString(PyExc_ImportError,
1290 "WSAStartup failed: requested version not supported");
1291 break;
1292 default:
1293 sprintf(buf, "WSAStartup failed: error code %d", ret);
1294 PyErr_SetString(PyExc_ImportError, buf);
1295 break;
1296 }
1297 return 0;
1298}
1299
Guido van Rossum8d665e61996-06-26 18:22:49 +00001300#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00001301
1302
Guido van Rossum30a685f1991-06-27 15:51:29 +00001303/* Initialize this module.
1304 This is called when the first 'import socket' is done,
1305 via a table in config.c, if config.c is compiled with USE_SOCKET
Guido van Rossumbe32c891996-06-20 16:25:29 +00001306 defined.
1307
Guido van Rossum8d665e61996-06-26 18:22:49 +00001308 For MS_WINDOWS (which means any Windows variant), this module
Guido van Rossumbe32c891996-06-20 16:25:29 +00001309 is actually called "_socket", and there's a wrapper "socket.py"
1310 which implements some missing functionality (such as makefile(),
1311 dup() and fromfd()). The import of "_socket" may fail with an
1312 ImportError exception if initialization of WINSOCK fails. When
1313 WINSOCK is initialized succesfully, a call to WSACleanup() is
1314 scheduled to be made at exit time. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001315
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001316void
Guido van Rossum8d665e61996-06-26 18:22:49 +00001317#ifdef MS_WINDOWS
Guido van Rossumbe32c891996-06-20 16:25:29 +00001318init_socket()
1319#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001320initsocket()
Guido van Rossumbe32c891996-06-20 16:25:29 +00001321#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001322{
Guido van Rossum73624e91994-10-10 17:59:00 +00001323 PyObject *m, *d;
Guido van Rossum8d665e61996-06-26 18:22:49 +00001324#ifdef MS_WINDOWS
Guido van Rossumbe32c891996-06-20 16:25:29 +00001325 if (!NTinit())
1326 return;
1327 m = Py_InitModule("_socket", PySocket_methods);
1328#else
Guido van Rossum73624e91994-10-10 17:59:00 +00001329 m = Py_InitModule("socket", PySocket_methods);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001330#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001331 d = PyModule_GetDict(m);
1332 PySocket_Error = PyString_FromString("socket.error");
1333 if (PySocket_Error == NULL ||
1334 PyDict_SetItemString(d, "error", PySocket_Error) != 0)
1335 Py_FatalError("can't define socket.error");
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001336 insint(d, "AF_INET", AF_INET);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001337#ifdef AF_UNIX
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001338 insint(d, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001339#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001340 insint(d, "SOCK_STREAM", SOCK_STREAM);
1341 insint(d, "SOCK_DGRAM", SOCK_DGRAM);
1342 insint(d, "SOCK_RAW", SOCK_RAW);
1343 insint(d, "SOCK_SEQPACKET", SOCK_SEQPACKET);
1344 insint(d, "SOCK_RDM", SOCK_RDM);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00001345
1346#ifdef SO_DEBUG
1347 insint(d, "SO_DEBUG", SO_DEBUG);
1348#endif
1349#ifdef SO_ACCEPTCONN
1350 insint(d, "SO_ACCEPTCONN", SO_ACCEPTCONN);
1351#endif
1352#ifdef SO_REUSEADDR
1353 insint(d, "SO_REUSEADDR", SO_REUSEADDR);
1354#endif
1355#ifdef SO_KEEPALIVE
1356 insint(d, "SO_KEEPALIVE", SO_KEEPALIVE);
1357#endif
1358#ifdef SO_DONTROUTE
1359 insint(d, "SO_DONTROUTE", SO_DONTROUTE);
1360#endif
1361#ifdef SO_BROADCAST
1362 insint(d, "SO_BROADCAST", SO_BROADCAST);
1363#endif
1364#ifdef SO_USELOOPBACK
1365 insint(d, "SO_USELOOPBACK", SO_USELOOPBACK);
1366#endif
1367#ifdef SO_LINGER
1368 insint(d, "SO_LINGER", SO_LINGER);
1369#endif
1370#ifdef SO_OOBINLINE
1371 insint(d, "SO_OOBINLINE", SO_OOBINLINE);
1372#endif
1373#ifdef SO_REUSEPORT
1374 insint(d, "SO_REUSEPORT", SO_REUSEPORT);
1375#endif
1376
1377#ifdef SO_SNDBUF
1378 insint(d, "SO_SNDBUF", SO_SNDBUF);
1379#endif
1380#ifdef SO_RCVBUF
1381 insint(d, "SO_RCVBUF", SO_RCVBUF);
1382#endif
1383#ifdef SO_SNDLOWAT
1384 insint(d, "SO_SNDLOWAT", SO_SNDLOWAT);
1385#endif
1386#ifdef SO_RCVLOWAT
1387 insint(d, "SO_RCVLOWAT", SO_RCVLOWAT);
1388#endif
1389#ifdef SO_SNDTIMEO
1390 insint(d, "SO_SNDTIMEO", SO_SNDTIMEO);
1391#endif
1392#ifdef SO_RCVTIMEO
1393 insint(d, "SO_RCVTIMEO", SO_RCVTIMEO);
1394#endif
1395#ifdef SO_ERROR
1396 insint(d, "SO_ERROR", SO_ERROR);
1397#endif
1398#ifdef SO_TYPE
1399 insint(d, "SO_TYPE", SO_TYPE);
1400#endif
1401
1402 /* Maximum number of connections for "listen" */
1403#ifdef SOMAXCONN
1404 insint(d, "SOMAXCONN", SOMAXCONN);
1405#else
1406 insint(d, "SOMAXCONN", 5); /* Common value */
1407#endif
1408
1409 /* Flags for send, recv */
1410#ifdef MSG_OOB
1411 insint(d, "MSG_OOB", MSG_OOB);
1412#endif
1413#ifdef MSG_PEEK
1414 insint(d, "MSG_PEEK", MSG_PEEK);
1415#endif
1416#ifdef MSG_DONTROUTE
1417 insint(d, "MSG_DONTROUTE", MSG_DONTROUTE);
1418#endif
1419#ifdef MSG_EOR
1420 insint(d, "MSG_EOR", MSG_EOR);
1421#endif
1422#ifdef MSG_TRUNC
1423 insint(d, "MSG_TRUNC", MSG_TRUNC);
1424#endif
1425#ifdef MSG_CTRUNC
1426 insint(d, "MSG_CTRUNC", MSG_CTRUNC);
1427#endif
1428#ifdef MSG_WAITALL
1429 insint(d, "MSG_WAITALL", MSG_WAITALL);
1430#endif
1431#ifdef MSG_BTAG
1432 insint(d, "MSG_BTAG", MSG_BTAG);
1433#endif
1434#ifdef MSG_ETAG
1435 insint(d, "MSG_ETAG", MSG_ETAG);
1436#endif
1437
1438 /* Protocol level and numbers, usable for [gs]etsockopt */
1439#ifdef SOL_SOCKET
1440 insint(d, "SOL_SOCKET", SOL_SOCKET);
1441#endif
1442#ifdef IPPROTO_IP
1443 insint(d, "IPPROTO_IP", IPPROTO_IP);
1444#endif
1445#ifdef IPPROTO_ICMP
1446 insint(d, "IPPROTO_ICMP", IPPROTO_ICMP);
1447#endif
1448#ifdef IPPROTO_IGMP
1449 insint(d, "IPPROTO_IGMP", IPPROTO_IGMP);
1450#endif
1451#ifdef IPPROTO_GGP
1452 insint(d, "IPPROTO_GGP", IPPROTO_GGP);
1453#endif
1454#ifdef IPPROTO_TCP
1455 insint(d, "IPPROTO_TCP", IPPROTO_TCP);
1456#endif
1457#ifdef IPPROTO_EGP
1458 insint(d, "IPPROTO_EGP", IPPROTO_EGP);
1459#endif
1460#ifdef IPPROTO_PUP
1461 insint(d, "IPPROTO_PUP", IPPROTO_PUP);
1462#endif
1463#ifdef IPPROTO_UDP
1464 insint(d, "IPPROTO_UDP", IPPROTO_UDP);
1465#endif
1466#ifdef IPPROTO_IDP
1467 insint(d, "IPPROTO_IDP", IPPROTO_IDP);
1468#endif
1469#ifdef IPPROTO_HELLO
1470 insint(d, "IPPROTO_HELLO", IPPROTO_HELLO);
1471#endif
1472#ifdef IPPROTO_ND
1473 insint(d, "IPPROTO_ND", IPPROTO_ND);
1474#endif
1475#ifdef IPPROTO_TP
1476 insint(d, "IPPROTO_TP", IPPROTO_TP);
1477#endif
1478#ifdef IPPROTO_XTP
1479 insint(d, "IPPROTO_XTP", IPPROTO_XTP);
1480#endif
1481#ifdef IPPROTO_EON
1482 insint(d, "IPPROTO_EON", IPPROTO_EON);
1483#endif
1484#ifdef IPPROTO_BIP
1485 insint(d, "IPPROTO_BIP", IPPROTO_BIP);
1486#endif
1487/**/
1488#ifdef IPPROTO_RAW
1489 insint(d, "IPPROTO_RAW", IPPROTO_RAW);
1490#endif
1491#ifdef IPPROTO_MAX
1492 insint(d, "IPPROTO_MAX", IPPROTO_MAX);
1493#endif
1494
1495 /* Some port configuration */
1496#ifdef IPPORT_RESERVED
1497 insint(d, "IPPORT_RESERVED", IPPORT_RESERVED);
1498#else
1499 insint(d, "IPPORT_RESERVED", 1024);
1500#endif
1501#ifdef IPPORT_USERRESERVED
1502 insint(d, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
1503#else
1504 insint(d, "IPPORT_USERRESERVED", 5000);
1505#endif
1506
Guido van Rossum5f05eb41995-02-17 15:11:07 +00001507 /* Some reserved IP v.4 addresses */
1508#ifdef INADDR_ANY
1509 insint(d, "INADDR_ANY", INADDR_ANY);
1510#else
1511 insint(d, "INADDR_ANY", 0x00000000);
1512#endif
1513#ifdef INADDR_BROADCAST
1514 insint(d, "INADDR_BROADCAST", INADDR_BROADCAST);
1515#else
1516 insint(d, "INADDR_BROADCAST", 0xffffffff);
1517#endif
1518#ifdef INADDR_LOOPBACK
1519 insint(d, "INADDR_LOOPBACK", INADDR_LOOPBACK);
1520#else
1521 insint(d, "INADDR_LOOPBACK", 0x7F000001);
1522#endif
1523#ifdef INADDR_UNSPEC_GROUP
1524 insint(d, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
1525#else
1526 insint(d, "INADDR_UNSPEC_GROUP", 0xe0000000);
1527#endif
1528#ifdef INADDR_ALLHOSTS_GROUP
1529 insint(d, "INADDR_ALLHOSTS_GROUP", INADDR_ALLHOSTS_GROUP);
1530#else
1531 insint(d, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
1532#endif
1533#ifdef INADDR_MAX_LOCAL_GROUP
1534 insint(d, "INADDR_MAX_LOCAL_GROUP", INADDR_MAX_LOCAL_GROUP);
1535#else
1536 insint(d, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
1537#endif
1538#ifdef INADDR_NONE
1539 insint(d, "INADDR_NONE", INADDR_NONE);
1540#else
1541 insint(d, "INADDR_NONE", 0xffffffff);
1542#endif
1543
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00001544 /* IP [gs]etsockopt options */
1545#ifdef IP_OPTIONS
1546 insint(d, "IP_OPTIONS", IP_OPTIONS);
1547#endif
1548#ifdef IP_HDRINCL
1549 insint(d, "IP_HDRINCL", IP_HDRINCL);
1550#endif
1551#ifdef IP_TOS
1552 insint(d, "IP_TOS", IP_TOS);
1553#endif
1554#ifdef IP_TTL
1555 insint(d, "IP_TTL", IP_TTL);
1556#endif
1557#ifdef IP_RECVOPTS
1558 insint(d, "IP_RECVOPTS", IP_RECVOPTS);
1559#endif
1560#ifdef IP_RECVRETOPTS
1561 insint(d, "IP_RECVRETOPTS", IP_RECVRETOPTS);
1562#endif
1563#ifdef IP_RECVDSTADDR
1564 insint(d, "IP_RECVDSTADDR", IP_RECVDSTADDR);
1565#endif
1566#ifdef IP_RETOPTS
1567 insint(d, "IP_RETOPTS", IP_RETOPTS);
1568#endif
1569#ifdef IP_MULTICAST_IF
1570 insint(d, "IP_MULTICAST_IF", IP_MULTICAST_IF);
1571#endif
1572#ifdef IP_MULTICAST_TTL
1573 insint(d, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
1574#endif
1575#ifdef IP_MULTICAST_LOOP
1576 insint(d, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
1577#endif
1578#ifdef IP_ADD_MEMBERSHIP
1579 insint(d, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
1580#endif
1581#ifdef IP_DROP_MEMBERSHIP
1582 insint(d, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
1583#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001584}