blob: 48ce55a08ff3851f8eed434c3f39c027da411203 [file] [log] [blame]
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum6574b3e1991-06-25 21:36:08 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum6574b3e1991-06-25 21:36:08 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum6574b3e1991-06-25 21:36:08 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum6574b3e1991-06-25 21:36:08 +000029
30******************************************************************/
31
32/* Socket module */
33
34/*
35This module provides an interface to Berkeley socket IPC.
36
37Limitations:
38
Guido van Rossum30a685f1991-06-27 15:51:29 +000039- only AF_INET and AF_UNIX address families are supported
Guido van Rossum81194471991-07-27 21:42:02 +000040- no read/write operations (use send/recv or makefile instead)
Guido van Rossumbe32c891996-06-20 16:25:29 +000041- additional restrictions apply on Windows
Guido van Rossum6574b3e1991-06-25 21:36:08 +000042
Guido van Rossum27e177d1995-03-16 15:43:47 +000043Module interface:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000044
Guido van Rossum27e177d1995-03-16 15:43:47 +000045- socket.error: exception raised for socket specific errors
Guido van Rossum30a685f1991-06-27 15:51:29 +000046- socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd')
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000047- socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...])
Guido van Rossum27e177d1995-03-16 15:43:47 +000048- socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com')
Guido van Rossum25405c71996-12-19 16:42:52 +000049- socket.getprotobyname(protocolname) --> protocol number
Guido van Rossum27e177d1995-03-16 15:43:47 +000050- socket.getservbyname(servicename, protocolname) --> port number
Guido van Rossum4dd2a7e1991-07-01 18:51:33 +000051- socket.socket(family, type [, proto]) --> new socket object
Guido van Rossum006bf911996-06-12 04:04:55 +000052- socket.ntohs(16 bit value) --> new int object
53- socket.ntohl(32 bit value) --> new int object
54- socket.htons(16 bit value) --> new int object
55- socket.htonl(32 bit value) --> new int object
Guido van Rossum27e177d1995-03-16 15:43:47 +000056- socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h>
Guido van Rossum6574b3e1991-06-25 21:36:08 +000057- an Internet socket address is a pair (hostname, port)
58 where hostname can be anything recognized by gethostbyname()
59 (including the dd.dd.dd.dd notation) and port is in host byte order
60- where a hostname is returned, the dd.dd.dd.dd notation is used
Guido van Rossum27e177d1995-03-16 15:43:47 +000061- a UNIX domain socket address is a string specifying the pathname
Guido van Rossum6574b3e1991-06-25 21:36:08 +000062
Guido van Rossum30a685f1991-06-27 15:51:29 +000063Socket methods:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000064
Guido van Rossum81194471991-07-27 21:42:02 +000065- s.accept() --> new socket object, sockaddr
Guido van Rossum27e177d1995-03-16 15:43:47 +000066- s.bind(sockaddr) --> None
67- s.close() --> None
68- s.connect(sockaddr) --> None
69- s.fileno() --> file descriptor
Guido van Rossumbe32c891996-06-20 16:25:29 +000070- s.dup() --> same as socket.fromfd(os.dup(s.fileno(), ...)
Guido van Rossumc89705d1992-11-26 08:54:07 +000071- s.getpeername() --> sockaddr
Guido van Rossum27e177d1995-03-16 15:43:47 +000072- s.getsockname() --> sockaddr
73- s.getsockopt(level, optname[, buflen]) --> int or string
74- s.listen(backlog) --> None
Guido van Rossum6b144911995-03-14 15:05:13 +000075- s.makefile([mode[, bufsize]]) --> file object
Guido van Rossum27e177d1995-03-16 15:43:47 +000076- s.recv(buflen [,flags]) --> string
77- s.recvfrom(buflen [,flags]) --> string, sockaddr
Guido van Rossumb6775db1994-08-01 11:34:53 +000078- s.send(string [,flags]) --> nbytes
79- s.sendto(string, [flags,] sockaddr) --> nbytes
Guido van Rossum27e177d1995-03-16 15:43:47 +000080- s.setblocking(0 | 1) --> None
81- s.setsockopt(level, optname, value) --> None
82- s.shutdown(how) --> None
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000083- repr(s) --> "<socket object, fd=%d, family=%d, type=%d, protocol=%d>"
Guido van Rossum30a685f1991-06-27 15:51:29 +000084
Guido van Rossum6574b3e1991-06-25 21:36:08 +000085*/
86
Guido van Rossum73624e91994-10-10 17:59:00 +000087#include "Python.h"
Guido van Rossum6574b3e1991-06-25 21:36:08 +000088
Guido van Rossuma376cc51996-12-05 23:43:35 +000089#ifdef HAVE_UNISTD_H
90#include <unistd.h>
91#endif
92
Guido van Rossumfb4130d1996-12-10 15:17:08 +000093#ifndef MS_WINDOWS
94extern int gethostname(); /* For Solaris, at least */
95#endif
96
Guido van Rossumb6775db1994-08-01 11:34:53 +000097#include <sys/types.h>
98#include "mytime.h"
Guido van Rossumed233a51992-06-23 09:07:03 +000099
Guido van Rossum81194471991-07-27 21:42:02 +0000100#include <signal.h>
Guido van Rossum6f489d91996-06-28 20:15:15 +0000101#ifndef MS_WINDOWS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000102#include <netdb.h>
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000103#include <sys/socket.h>
104#include <netinet/in.h>
Guido van Rossume4485b01994-09-07 14:32:49 +0000105#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000106#else
107#include <winsock.h>
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000108#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000109#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000110#ifdef HAVE_SYS_UN_H
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000111#include <sys/un.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000112#else
113#undef AF_UNIX
Guido van Rossum9575a441993-04-07 14:06:14 +0000114#endif
115
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000116#ifndef O_NDELAY
117#define O_NDELAY O_NONBLOCK /* For QNX only? */
118#endif
119
Jack Jansen508537b1996-02-14 15:57:45 +0000120#ifdef USE_GUSI
121/* fdopen() isn't declared in stdio.h (sigh) */
122#include <GUSI.h>
123#endif
124
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000125
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000126/* Here we have some hacks to choose between K&R or ANSI style function
127 definitions. For NT to build this as an extension module (ie, DLL)
128 it must be compiled by the C++ compiler, as it takes the address of
129 a static data item exported from the main Python DLL.
130*/
Guido van Rossum8d665e61996-06-26 18:22:49 +0000131#ifdef MS_WINDOWS
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000132/* seem to be a few differences in the API */
133#define close closesocket
Guido van Rossumbe32c891996-06-20 16:25:29 +0000134#define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000135#define FORCE_ANSI_FUNC_DEFS
136#endif
137
138#ifdef FORCE_ANSI_FUNC_DEFS
139#define BUILD_FUNC_DEF_1( fnname, arg1type, arg1name ) \
140fnname( arg1type arg1name )
141
142#define BUILD_FUNC_DEF_2( fnname, arg1type, arg1name, arg2type, arg2name ) \
143fnname( arg1type arg1name, arg2type arg2name )
144
145#define BUILD_FUNC_DEF_3( fnname, arg1type, arg1name, arg2type, arg2name , arg3type, arg3name ) \
146fnname( arg1type arg1name, arg2type arg2name, arg3type arg3name )
147
148#define BUILD_FUNC_DEF_4( fnname, arg1type, arg1name, arg2type, arg2name , arg3type, arg3name, arg4type, arg4name ) \
149fnname( arg1type arg1name, arg2type arg2name, arg3type arg3name, arg4type arg4name )
150
151#else /* !FORCE_ANSI_FN_DEFS */
152#define BUILD_FUNC_DEF_1( fnname, arg1type, arg1name ) \
153fnname( arg1name ) \
154 arg1type arg1name;
155
156#define BUILD_FUNC_DEF_2( fnname, arg1type, arg1name, arg2type, arg2name ) \
157fnname( arg1name, arg2name ) \
158 arg1type arg1name; \
159 arg2type arg2name;
160
161#define BUILD_FUNC_DEF_3( fnname, arg1type, arg1name, arg2type, arg2name, arg3type, arg3name ) \
162fnname( arg1name, arg2name, arg3name ) \
163 arg1type arg1name; \
164 arg2type arg2name; \
165 arg3type arg3name;
166
167#define BUILD_FUNC_DEF_4( fnname, arg1type, arg1name, arg2type, arg2name, arg3type, arg3name, arg4type, arg4name ) \
168fnname( arg1name, arg2name, arg3name, arg4name ) \
169 arg1type arg1name; \
170 arg2type arg2name; \
171 arg3type arg3name; \
172 arg4type arg4name;
173
174#endif /* !FORCE_ANSI_FN_DEFS */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000175
176/* Global variable holding the exception type for errors detected
177 by this module (but not argument type or memory errors, etc.). */
178
Guido van Rossum73624e91994-10-10 17:59:00 +0000179static PyObject *PySocket_Error;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000180
181
182/* Convenience function to raise an error according to errno
183 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000184
Guido van Rossum73624e91994-10-10 17:59:00 +0000185static PyObject *
186PySocket_Err()
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000187{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000188#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000189 if (WSAGetLastError()) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000190 PyObject *v;
191 v = Py_BuildValue("(is)", WSAGetLastError(), "winsock error");
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000192 if (v != NULL) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000193 PyErr_SetObject(PySocket_Error, v);
194 Py_DECREF(v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000195 }
196 return NULL;
197 }
198 else
199#endif
Guido van Rossum73624e91994-10-10 17:59:00 +0000200 return PyErr_SetFromErrno(PySocket_Error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000201}
202
Guido van Rossum30a685f1991-06-27 15:51:29 +0000203
204/* The object holding a socket. It holds some extra information,
205 like the address family, which is used to decode socket address
206 arguments properly. */
207
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000208typedef struct {
Guido van Rossum73624e91994-10-10 17:59:00 +0000209 PyObject_HEAD
Guido van Rossum30a685f1991-06-27 15:51:29 +0000210 int sock_fd; /* Socket file descriptor */
211 int sock_family; /* Address family, e.g., AF_INET */
212 int sock_type; /* Socket type, e.g., SOCK_STREAM */
213 int sock_proto; /* Protocol type, usually 0 */
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000214 union sock_addr {
215 struct sockaddr_in in;
216#ifdef AF_UNIX
217 struct sockaddr_un un;
218#endif
219 } sock_addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000220} PySocketSockObject;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000221
Guido van Rossum30a685f1991-06-27 15:51:29 +0000222
223/* A forward reference to the Socktype type object.
224 The Socktype variable contains pointers to various functions,
Guido van Rossum73624e91994-10-10 17:59:00 +0000225 some of which call newsockobject(), which uses Socktype, so
Guido van Rossum54ba21b1991-09-10 14:57:12 +0000226 there has to be a circular reference. */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000227
Guido van Rossum73624e91994-10-10 17:59:00 +0000228staticforward PyTypeObject PySocketSock_Type;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000229
Guido van Rossum30a685f1991-06-27 15:51:29 +0000230
231/* Create a new socket object.
232 This just creates the object and initializes it.
233 If the creation fails, return NULL and set an exception (implicit
234 in NEWOBJ()). */
235
Guido van Rossum73624e91994-10-10 17:59:00 +0000236static PySocketSockObject *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000237BUILD_FUNC_DEF_4(PySocketSock_New,int,fd, int,family, int,type, int,proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000238{
Guido van Rossum73624e91994-10-10 17:59:00 +0000239 PySocketSockObject *s;
Guido van Rossum55558541996-05-23 22:54:50 +0000240 PySocketSock_Type.ob_type = &PyType_Type;
Guido van Rossum73624e91994-10-10 17:59:00 +0000241 s = PyObject_NEW(PySocketSockObject, &PySocketSock_Type);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000242 if (s != NULL) {
243 s->sock_fd = fd;
244 s->sock_family = family;
245 s->sock_type = type;
246 s->sock_proto = proto;
247 }
248 return s;
249}
250
Guido van Rossum30a685f1991-06-27 15:51:29 +0000251
252/* Convert a string specifying a host name or one of a few symbolic
253 names to a numeric IP address. This usually calls gethostbyname()
254 to do the work; the names "" and "<broadcast>" are special.
255 Return the length (should always be 4 bytes), or negative if
256 an error occurred; then an exception is raised. */
257
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000258static int
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000259BUILD_FUNC_DEF_2(setipaddr, char*,name, struct sockaddr_in *,addr_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000260{
261 struct hostent *hp;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000262 int d1, d2, d3, d4;
263 char ch;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000264#ifdef HAVE_GETHOSTBYNAME_R
265 struct hostent hp_allocated;
266 char buf[1001];
267 int buf_len = (sizeof buf) - 1;
268 int errnop;
269#endif /* HAVE_GETHOSTBYNAME_R */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000270
Guido van Rossuma376cc51996-12-05 23:43:35 +0000271 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
Guido van Rossum30a685f1991-06-27 15:51:29 +0000272 if (name[0] == '\0') {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000273 addr_ret->sin_addr.s_addr = INADDR_ANY;
274 return 4;
275 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000276 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000277 addr_ret->sin_addr.s_addr = INADDR_BROADCAST;
278 return 4;
279 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000280 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
281 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
282 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
283 addr_ret->sin_addr.s_addr = htonl(
284 ((long) d1 << 24) | ((long) d2 << 16) |
285 ((long) d3 << 8) | ((long) d4 << 0));
286 return 4;
287 }
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000288#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum73624e91994-10-10 17:59:00 +0000289 Py_BEGIN_ALLOW_THREADS
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000290 hp = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum73624e91994-10-10 17:59:00 +0000291 Py_END_ALLOW_THREADS
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000292#else /* not HAVE_GETHOSTBYNAME_R */
293 hp = gethostbyname(name);
294#endif /* HAVE_GETHOSTBYNAME_R */
295
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000296 if (hp == NULL) {
Guido van Rossumca9b3231995-01-10 16:30:54 +0000297#ifdef HAVE_HSTRERROR
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000298 /* Let's get real error message to return */
299 extern int h_errno;
300 PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno));
301#else
Guido van Rossum73624e91994-10-10 17:59:00 +0000302 PyErr_SetString(PySocket_Error, "host not found");
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000303#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000304 return -1;
305 }
306 memcpy((char *) &addr_ret->sin_addr, hp->h_addr, hp->h_length);
307 return hp->h_length;
308}
309
Guido van Rossum30a685f1991-06-27 15:51:29 +0000310
Guido van Rossum30a685f1991-06-27 15:51:29 +0000311/* Create a string object representing an IP address.
312 This is always a string of the form 'dd.dd.dd.dd' (with variable
313 size numbers). */
314
Guido van Rossum73624e91994-10-10 17:59:00 +0000315static PyObject *
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000316BUILD_FUNC_DEF_1(makeipaddr, struct sockaddr_in *,addr)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000317{
318 long x = ntohl(addr->sin_addr.s_addr);
319 char buf[100];
320 sprintf(buf, "%d.%d.%d.%d",
321 (int) (x>>24) & 0xff, (int) (x>>16) & 0xff,
322 (int) (x>> 8) & 0xff, (int) (x>> 0) & 0xff);
Guido van Rossum73624e91994-10-10 17:59:00 +0000323 return PyString_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000324}
325
326
327/* Create an object representing the given socket address,
328 suitable for passing it back to bind(), connect() etc.
329 The family field of the sockaddr structure is inspected
330 to determine what kind of address it really is. */
331
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000332/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000333static PyObject *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000334BUILD_FUNC_DEF_2(makesockaddr,struct sockaddr *,addr, int,addrlen)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000335{
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000336 if (addrlen == 0) {
337 /* No address -- may be recvfrom() from known socket */
Guido van Rossum73624e91994-10-10 17:59:00 +0000338 Py_INCREF(Py_None);
339 return Py_None;
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000340 }
341
Guido van Rossum30a685f1991-06-27 15:51:29 +0000342 switch (addr->sa_family) {
343
344 case AF_INET:
345 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000346 struct sockaddr_in *a = (struct sockaddr_in *) addr;
Barry Warsaw752300b1997-01-03 17:18:10 +0000347 PyObject *addrobj = makeipaddr(a);
348 PyObject *ret = NULL;
349 if (addrobj) {
350 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
351 Py_DECREF(addrobj);
352 }
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000353 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000354 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000355
Guido van Rossumb6775db1994-08-01 11:34:53 +0000356#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000357 case AF_UNIX:
358 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000359 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000360 return PyString_FromString(a->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000361 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000362#endif /* AF_UNIX */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000363
364 /* More cases here... */
365
366 default:
Guido van Rossumaa948df1997-05-07 17:41:48 +0000367 /* If we don't know the address family, don't raise an
368 exception -- return it as a tuple. */
369 return Py_BuildValue("is#",
370 addr->sa_family,
371 addr->sa_data,
372 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000373
Guido van Rossum30a685f1991-06-27 15:51:29 +0000374 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000375}
376
Guido van Rossum30a685f1991-06-27 15:51:29 +0000377
378/* Parse a socket address argument according to the socket object's
379 address family. Return 1 if the address was in the proper format,
380 0 of not. The address is returned through addr_ret, its length
381 through len_ret. */
382
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000383static int
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000384BUILD_FUNC_DEF_4(
Guido van Rossum73624e91994-10-10 17:59:00 +0000385getsockaddrarg,PySocketSockObject *,s, PyObject *,args, struct sockaddr **,addr_ret, int *,len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000386{
Guido van Rossum30a685f1991-06-27 15:51:29 +0000387 switch (s->sock_family) {
388
Guido van Rossumb6775db1994-08-01 11:34:53 +0000389#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000390 case AF_UNIX:
391 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000392 struct sockaddr_un* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000393 char *path;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000394 int len;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000395 addr = (struct sockaddr_un* )&(s->sock_addr).un;
Guido van Rossum73624e91994-10-10 17:59:00 +0000396 if (!PyArg_Parse(args, "s#", &path, &len))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000397 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000398 if (len > sizeof addr->sun_path) {
Barry Warsaw752300b1997-01-03 17:18:10 +0000399 PyErr_SetString(PySocket_Error,
400 "AF_UNIX path too long");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000401 return 0;
402 }
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000403 addr->sun_family = AF_UNIX;
404 memcpy(addr->sun_path, path, len);
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000405 addr->sun_path[len] = 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000406 *addr_ret = (struct sockaddr *) addr;
Guido van Rossum65af28a1996-06-11 18:36:33 +0000407 *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000408 return 1;
409 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000410#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000411
Guido van Rossum30a685f1991-06-27 15:51:29 +0000412 case AF_INET:
413 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000414 struct sockaddr_in* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000415 char *host;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000416 int port;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000417 addr=(struct sockaddr_in*)&(s->sock_addr).in;
Guido van Rossum73624e91994-10-10 17:59:00 +0000418 if (!PyArg_Parse(args, "(si)", &host, &port))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000419 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000420 if (setipaddr(host, addr) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000421 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000422 addr->sin_family = AF_INET;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000423 addr->sin_port = htons((short)port);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000424 *addr_ret = (struct sockaddr *) addr;
425 *len_ret = sizeof *addr;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000426 return 1;
427 }
428
Guido van Rossum30a685f1991-06-27 15:51:29 +0000429 /* More cases here... */
430
431 default:
Guido van Rossum73624e91994-10-10 17:59:00 +0000432 PyErr_SetString(PySocket_Error, "getsockaddrarg: bad family");
Guido van Rossum30a685f1991-06-27 15:51:29 +0000433 return 0;
434
435 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000436}
437
Guido van Rossum30a685f1991-06-27 15:51:29 +0000438
Guido van Rossum710e1df1992-06-12 10:39:36 +0000439/* Get the address length according to the socket object's address family.
440 Return 1 if the family is known, 0 otherwise. The length is returned
441 through len_ret. */
442
443static int
Guido van Rossum73624e91994-10-10 17:59:00 +0000444BUILD_FUNC_DEF_2(getsockaddrlen,PySocketSockObject *,s, int *,len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +0000445{
446 switch (s->sock_family) {
447
Guido van Rossumb6775db1994-08-01 11:34:53 +0000448#ifdef AF_UNIX
Guido van Rossum710e1df1992-06-12 10:39:36 +0000449 case AF_UNIX:
450 {
451 *len_ret = sizeof (struct sockaddr_un);
452 return 1;
453 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000454#endif /* AF_UNIX */
Guido van Rossum710e1df1992-06-12 10:39:36 +0000455
456 case AF_INET:
457 {
458 *len_ret = sizeof (struct sockaddr_in);
459 return 1;
460 }
461
462 /* More cases here... */
463
464 default:
Guido van Rossum73624e91994-10-10 17:59:00 +0000465 PyErr_SetString(PySocket_Error, "getsockaddrarg: bad family");
Guido van Rossum710e1df1992-06-12 10:39:36 +0000466 return 0;
467
468 }
469}
470
471
Guido van Rossum30a685f1991-06-27 15:51:29 +0000472/* s.accept() method */
473
Guido van Rossum73624e91994-10-10 17:59:00 +0000474static PyObject *
475BUILD_FUNC_DEF_2(PySocketSock_accept,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000476{
477 char addrbuf[256];
478 int addrlen, newfd;
Barry Warsaw752300b1997-01-03 17:18:10 +0000479 PyObject *sock = NULL;
480 PyObject *addr = NULL;
481 PyObject *res = NULL;
482
Guido van Rossum73624e91994-10-10 17:59:00 +0000483 if (!PyArg_NoArgs(args))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000484 return NULL;
Guido van Rossum710e1df1992-06-12 10:39:36 +0000485 if (!getsockaddrlen(s, &addrlen))
486 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000487 Py_BEGIN_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000488 newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000489 Py_END_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000490 if (newfd < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000491 return PySocket_Err();
Barry Warsaw752300b1997-01-03 17:18:10 +0000492
Guido van Rossum30a685f1991-06-27 15:51:29 +0000493 /* Create the new object with unspecified family,
494 to avoid calls to bind() etc. on it. */
Guido van Rossum73624e91994-10-10 17:59:00 +0000495 sock = (PyObject *) PySocketSock_New(newfd,
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000496 s->sock_family,
497 s->sock_type,
498 s->sock_proto);
Barry Warsaw752300b1997-01-03 17:18:10 +0000499 if (sock == NULL) {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000500 close(newfd);
Barry Warsaw752300b1997-01-03 17:18:10 +0000501 goto finally;
502 }
503 if (!(addr = makesockaddr((struct sockaddr *) addrbuf, addrlen)))
504 goto finally;
505
506 if (!(res = Py_BuildValue("OO", sock, addr)))
507 goto finally;
508
509 finally:
Guido van Rossum73624e91994-10-10 17:59:00 +0000510 Py_XDECREF(sock);
511 Py_XDECREF(addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000512 return res;
513}
514
Guido van Rossum30a685f1991-06-27 15:51:29 +0000515
Guido van Rossume4485b01994-09-07 14:32:49 +0000516/* s.setblocking(1 | 0) method */
517
Guido van Rossum73624e91994-10-10 17:59:00 +0000518static PyObject *
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000519BUILD_FUNC_DEF_2(PySocketSock_setblocking,PySocketSockObject*,s,PyObject*,args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000520{
521 int block;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000522#ifndef MS_WINDOWS
Guido van Rossume4485b01994-09-07 14:32:49 +0000523 int delay_flag;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000524#endif
Guido van Rossumfe28fa01997-05-15 19:09:26 +0000525 if (!PyArg_Parse(args, "i", &block))
Guido van Rossume4485b01994-09-07 14:32:49 +0000526 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000527 Py_BEGIN_ALLOW_THREADS
Guido van Rossum6f489d91996-06-28 20:15:15 +0000528#ifndef MS_WINDOWS
Guido van Rossume4485b01994-09-07 14:32:49 +0000529 delay_flag = fcntl (s->sock_fd, F_GETFL, 0);
530 if (block)
531 delay_flag &= (~O_NDELAY);
532 else
533 delay_flag |= O_NDELAY;
534 fcntl (s->sock_fd, F_SETFL, delay_flag);
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000535#else
536 block = !block;
537 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
538#endif
Guido van Rossum73624e91994-10-10 17:59:00 +0000539 Py_END_ALLOW_THREADS
Guido van Rossume4485b01994-09-07 14:32:49 +0000540
Guido van Rossum73624e91994-10-10 17:59:00 +0000541 Py_INCREF(Py_None);
542 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000543}
Guido van Rossume4485b01994-09-07 14:32:49 +0000544
545
Guido van Rossumaee08791992-09-08 09:05:33 +0000546/* s.setsockopt() method.
547 With an integer third argument, sets an integer option.
548 With a string third argument, sets an option from a buffer;
549 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000550
Guido van Rossum73624e91994-10-10 17:59:00 +0000551static PyObject *
552BUILD_FUNC_DEF_2(PySocketSock_setsockopt,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000553{
554 int level;
555 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000556 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +0000557 char *buf;
558 int buflen;
559 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000560
Guido van Rossum73624e91994-10-10 17:59:00 +0000561 if (PyArg_Parse(args, "(iii)", &level, &optname, &flag)) {
Guido van Rossumaee08791992-09-08 09:05:33 +0000562 buf = (char *) &flag;
563 buflen = sizeof flag;
564 }
565 else {
Guido van Rossum73624e91994-10-10 17:59:00 +0000566 PyErr_Clear();
Barry Warsaw752300b1997-01-03 17:18:10 +0000567 if (!PyArg_Parse(args, "(iis#)", &level, &optname,
568 &buf, &buflen))
Guido van Rossumaee08791992-09-08 09:05:33 +0000569 return NULL;
570 }
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000571 res = setsockopt(s->sock_fd, level, optname, (ANY *)buf, buflen);
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000572 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000573 return PySocket_Err();
574 Py_INCREF(Py_None);
575 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000576}
577
578
Guido van Rossumaee08791992-09-08 09:05:33 +0000579/* s.getsockopt() method.
580 With two arguments, retrieves an integer option.
581 With a third integer argument, retrieves a string buffer of that size;
582 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000583
Guido van Rossum73624e91994-10-10 17:59:00 +0000584static PyObject *
585BUILD_FUNC_DEF_2(PySocketSock_getsockopt,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000586{
587 int level;
588 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000589 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +0000590 PyObject *buf;
Guido van Rossumbe32c891996-06-20 16:25:29 +0000591 int buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000592
Guido van Rossumbe32c891996-06-20 16:25:29 +0000593 if (!PyArg_ParseTuple(args, "ii|i", &level, &optname, &buflen))
594 return NULL;
595
596 if (buflen == 0) {
Guido van Rossumaee08791992-09-08 09:05:33 +0000597 int flag = 0;
598 int flagsize = sizeof flag;
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000599 res = getsockopt(s->sock_fd, level, optname,
600 (ANY *)&flag, &flagsize);
Guido van Rossumaee08791992-09-08 09:05:33 +0000601 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000602 return PySocket_Err();
603 return PyInt_FromLong(flag);
Guido van Rossumaee08791992-09-08 09:05:33 +0000604 }
Guido van Rossumaee08791992-09-08 09:05:33 +0000605 if (buflen <= 0 || buflen > 1024) {
Barry Warsaw752300b1997-01-03 17:18:10 +0000606 PyErr_SetString(PySocket_Error,
607 "getsockopt buflen out of range");
Guido van Rossumaee08791992-09-08 09:05:33 +0000608 return NULL;
609 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000610 buf = PyString_FromStringAndSize((char *)NULL, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000611 if (buf == NULL)
612 return NULL;
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000613 res = getsockopt(s->sock_fd, level, optname,
Guido van Rossuma597dde1995-01-10 20:56:29 +0000614 (ANY *)PyString_AsString(buf), &buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000615 if (res < 0) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000616 Py_DECREF(buf);
617 return PySocket_Err();
Guido van Rossumaee08791992-09-08 09:05:33 +0000618 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000619 _PyString_Resize(&buf, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000620 return buf;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000621}
622
623
Guido van Rossum30a685f1991-06-27 15:51:29 +0000624/* s.bind(sockaddr) method */
625
Guido van Rossum73624e91994-10-10 17:59:00 +0000626static PyObject *
627BUILD_FUNC_DEF_2(PySocketSock_bind,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000628{
629 struct sockaddr *addr;
630 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000631 int res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000632 if (!getsockaddrarg(s, args, &addr, &addrlen))
633 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000634 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000635 res = bind(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000636 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000637 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000638 return PySocket_Err();
639 Py_INCREF(Py_None);
640 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000641}
642
Guido van Rossum30a685f1991-06-27 15:51:29 +0000643
644/* s.close() method.
645 Set the file descriptor to -1 so operations tried subsequently
646 will surely fail. */
647
Guido van Rossum73624e91994-10-10 17:59:00 +0000648static PyObject *
649BUILD_FUNC_DEF_2(PySocketSock_close,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000650{
Guido van Rossum73624e91994-10-10 17:59:00 +0000651 if (!PyArg_NoArgs(args))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000652 return NULL;
Guido van Rossum08481461996-10-12 14:07:22 +0000653 if (s->sock_fd != -1) {
654 Py_BEGIN_ALLOW_THREADS
655 (void) close(s->sock_fd);
656 Py_END_ALLOW_THREADS
657 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000658 s->sock_fd = -1;
Guido van Rossum73624e91994-10-10 17:59:00 +0000659 Py_INCREF(Py_None);
660 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000661}
662
Guido van Rossum30a685f1991-06-27 15:51:29 +0000663
664/* s.connect(sockaddr) method */
665
Guido van Rossum73624e91994-10-10 17:59:00 +0000666static PyObject *
667BUILD_FUNC_DEF_2(PySocketSock_connect,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000668{
669 struct sockaddr *addr;
670 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000671 int res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000672 if (!getsockaddrarg(s, args, &addr, &addrlen))
673 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000674 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000675 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000676 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000677 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000678 return PySocket_Err();
679 Py_INCREF(Py_None);
680 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000681}
682
Guido van Rossum30a685f1991-06-27 15:51:29 +0000683
Guido van Rossumed233a51992-06-23 09:07:03 +0000684/* s.fileno() method */
685
Guido van Rossum73624e91994-10-10 17:59:00 +0000686static PyObject *
687BUILD_FUNC_DEF_2(PySocketSock_fileno,PySocketSockObject *,s, PyObject *,args)
Guido van Rossumed233a51992-06-23 09:07:03 +0000688{
Guido van Rossum73624e91994-10-10 17:59:00 +0000689 if (!PyArg_NoArgs(args))
Guido van Rossumed233a51992-06-23 09:07:03 +0000690 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000691 return PyInt_FromLong((long) s->sock_fd);
Guido van Rossumed233a51992-06-23 09:07:03 +0000692}
693
694
Guido van Rossumbe32c891996-06-20 16:25:29 +0000695#ifndef NO_DUP
696/* s.dup() method */
697
698static PyObject *
699BUILD_FUNC_DEF_2(PySocketSock_dup,PySocketSockObject *,s, PyObject *,args)
700{
701 int newfd;
702 PyObject *sock;
703 if (!PyArg_NoArgs(args))
704 return NULL;
705 newfd = dup(s->sock_fd);
706 if (newfd < 0)
707 return PySocket_Err();
708 sock = (PyObject *) PySocketSock_New(newfd,
Barry Warsaw752300b1997-01-03 17:18:10 +0000709 s->sock_family,
710 s->sock_type,
711 s->sock_proto);
Guido van Rossumbe32c891996-06-20 16:25:29 +0000712 if (sock == NULL)
713 close(newfd);
714 return sock;
715}
716#endif
717
718
Guido van Rossumc89705d1992-11-26 08:54:07 +0000719/* s.getsockname() method */
720
Guido van Rossum73624e91994-10-10 17:59:00 +0000721static PyObject *
722BUILD_FUNC_DEF_2(PySocketSock_getsockname,PySocketSockObject *,s, PyObject *,args)
Guido van Rossumc89705d1992-11-26 08:54:07 +0000723{
724 char addrbuf[256];
725 int addrlen, res;
Guido van Rossum73624e91994-10-10 17:59:00 +0000726 if (!PyArg_NoArgs(args))
Guido van Rossumc89705d1992-11-26 08:54:07 +0000727 return NULL;
728 if (!getsockaddrlen(s, &addrlen))
729 return NULL;
Guido van Rossumca42b161996-01-12 01:36:05 +0000730 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000731 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +0000732 res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000733 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +0000734 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000735 return PySocket_Err();
Guido van Rossumc89705d1992-11-26 08:54:07 +0000736 return makesockaddr((struct sockaddr *) addrbuf, addrlen);
737}
738
739
Guido van Rossumb6775db1994-08-01 11:34:53 +0000740#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +0000741/* s.getpeername() method */
742
Guido van Rossum73624e91994-10-10 17:59:00 +0000743static PyObject *
744BUILD_FUNC_DEF_2(PySocketSock_getpeername,PySocketSockObject *,s, PyObject *,args)
Guido van Rossumc89705d1992-11-26 08:54:07 +0000745{
746 char addrbuf[256];
747 int addrlen, res;
Guido van Rossum73624e91994-10-10 17:59:00 +0000748 if (!PyArg_NoArgs(args))
Guido van Rossumc89705d1992-11-26 08:54:07 +0000749 return NULL;
750 if (!getsockaddrlen(s, &addrlen))
751 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000752 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +0000753 res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000754 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +0000755 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000756 return PySocket_Err();
Guido van Rossumc89705d1992-11-26 08:54:07 +0000757 return makesockaddr((struct sockaddr *) addrbuf, addrlen);
758}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000759#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +0000760
761
Guido van Rossum30a685f1991-06-27 15:51:29 +0000762/* s.listen(n) method */
763
Guido van Rossum73624e91994-10-10 17:59:00 +0000764static PyObject *
765BUILD_FUNC_DEF_2(PySocketSock_listen,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000766{
767 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000768 int res;
Guido van Rossumfe28fa01997-05-15 19:09:26 +0000769 if (!PyArg_Parse(args, "i", &backlog))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000770 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000771 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000772 if (backlog < 1)
773 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000774 res = listen(s->sock_fd, backlog);
Guido van Rossum73624e91994-10-10 17:59:00 +0000775 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000776 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000777 return PySocket_Err();
778 Py_INCREF(Py_None);
779 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000780}
781
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000782#ifndef NO_DUP
Guido van Rossum30a685f1991-06-27 15:51:29 +0000783/* s.makefile(mode) method.
784 Create a new open file object referring to a dupped version of
785 the socket's file descriptor. (The dup() call is necessary so
786 that the open file and socket objects may be closed independent
787 of each other.)
788 The mode argument specifies 'r' or 'w' passed to fdopen(). */
789
Guido van Rossum73624e91994-10-10 17:59:00 +0000790static PyObject *
791BUILD_FUNC_DEF_2(PySocketSock_makefile,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000792{
Guido van Rossuma597dde1995-01-10 20:56:29 +0000793 extern int fclose Py_PROTO((FILE *));
Guido van Rossum6b144911995-03-14 15:05:13 +0000794 char *mode = "r";
795 int bufsize = -1;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000796 int fd;
797 FILE *fp;
Guido van Rossum6b144911995-03-14 15:05:13 +0000798 PyObject *f;
799
800 if (!PyArg_ParseTuple(args, "|si", &mode, &bufsize))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000801 return NULL;
Guido van Rossum8d665e61996-06-26 18:22:49 +0000802#ifdef MS_WIN32
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000803 if ( ((fd = _open_osfhandle( s->sock_fd, _O_BINARY )) < 0) ||
804 ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL)) {
805#else
Guido van Rossum30a685f1991-06-27 15:51:29 +0000806 if ((fd = dup(s->sock_fd)) < 0 ||
Guido van Rossum6b144911995-03-14 15:05:13 +0000807 (fp = fdopen(fd, mode)) == NULL) {
Guido van Rossum30b6b2b1995-06-14 22:28:08 +0000808#endif
Guido van Rossum6b144911995-03-14 15:05:13 +0000809 if (fd >= 0)
810 close(fd);
Guido van Rossum73624e91994-10-10 17:59:00 +0000811 return PySocket_Err();
Guido van Rossum6b144911995-03-14 15:05:13 +0000812 }
813 f = PyFile_FromFile(fp, "<socket>", mode, fclose);
814 if (f != NULL)
815 PyFile_SetBufSize(f, bufsize);
816 return f;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000817}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000818#endif /* NO_DUP */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000819
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000820/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000821
Guido van Rossum73624e91994-10-10 17:59:00 +0000822static PyObject *
823BUILD_FUNC_DEF_2(PySocketSock_recv,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000824{
Guido van Rossumbe32c891996-06-20 16:25:29 +0000825 int len, n, flags = 0;
Guido van Rossum73624e91994-10-10 17:59:00 +0000826 PyObject *buf;
Guido van Rossumbe32c891996-06-20 16:25:29 +0000827 if (!PyArg_ParseTuple(args, "i|i", &len, &flags))
828 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000829 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000830 if (buf == NULL)
831 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000832 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma597dde1995-01-10 20:56:29 +0000833 n = recv(s->sock_fd, PyString_AsString(buf), len, flags);
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;
841 return buf;
842}
843
Guido van Rossum30a685f1991-06-27 15:51:29 +0000844
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000845/* s.recvfrom(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000846
Guido van Rossum73624e91994-10-10 17:59:00 +0000847static PyObject *
848BUILD_FUNC_DEF_2(PySocketSock_recvfrom,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000849{
850 char addrbuf[256];
Barry Warsaw752300b1997-01-03 17:18:10 +0000851 PyObject *buf = NULL;
852 PyObject *addr = NULL;
853 PyObject *ret = NULL;
854
Guido van Rossumbe32c891996-06-20 16:25:29 +0000855 int addrlen, len, n, flags = 0;
856 if (!PyArg_ParseTuple(args, "i|i", &len, &flags))
857 return NULL;
Guido van Rossum18c9a4f1993-05-25 12:16:29 +0000858 if (!getsockaddrlen(s, &addrlen))
859 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000860 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum18c9a4f1993-05-25 12:16:29 +0000861 if (buf == NULL)
862 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000863 Py_BEGIN_ALLOW_THREADS
864 n = recvfrom(s->sock_fd, PyString_AsString(buf), len, flags,
Guido van Rossum8d665e61996-06-26 18:22:49 +0000865#ifndef MS_WINDOWS
Barry Warsaw752300b1997-01-03 17:18:10 +0000866 (ANY *)addrbuf, &addrlen
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000867#else
Barry Warsaw752300b1997-01-03 17:18:10 +0000868 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000869#endif
Barry Warsaw752300b1997-01-03 17:18:10 +0000870 );
Guido van Rossum73624e91994-10-10 17:59:00 +0000871 Py_END_ALLOW_THREADS
Guido van Rossum7c53b771995-09-13 18:39:47 +0000872 if (n < 0) {
873 Py_DECREF(buf);
Guido van Rossum73624e91994-10-10 17:59:00 +0000874 return PySocket_Err();
Guido van Rossum7c53b771995-09-13 18:39:47 +0000875 }
Guido van Rossum53a9bf81996-06-11 18:35:24 +0000876 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000877 return NULL;
Barry Warsaw752300b1997-01-03 17:18:10 +0000878
879 if (!(addr = makesockaddr((struct sockaddr *)addrbuf, addrlen)))
880 goto finally;
881
Guido van Rossum73624e91994-10-10 17:59:00 +0000882 ret = Py_BuildValue("OO", buf, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +0000883 finally:
Guido van Rossum73624e91994-10-10 17:59:00 +0000884 Py_XDECREF(addr);
885 Py_XDECREF(buf);
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000886 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000887}
888
Guido van Rossum30a685f1991-06-27 15:51:29 +0000889
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000890/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000891
Guido van Rossum73624e91994-10-10 17:59:00 +0000892static PyObject *
893BUILD_FUNC_DEF_2(PySocketSock_send,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000894{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000895 char *buf;
Guido van Rossumbe32c891996-06-20 16:25:29 +0000896 int len, n, flags = 0;
897 if (!PyArg_ParseTuple(args, "s#|i", &buf, &len, &flags))
898 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000899 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000900 n = send(s->sock_fd, buf, len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +0000901 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +0000902 if (n < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000903 return PySocket_Err();
904 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000905}
906
907
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000908/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000909
Guido van Rossum73624e91994-10-10 17:59:00 +0000910static PyObject *
911BUILD_FUNC_DEF_2(PySocketSock_sendto,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000912{
Guido van Rossum73624e91994-10-10 17:59:00 +0000913 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000914 char *buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000915 struct sockaddr *addr;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000916 int addrlen, len, n, flags;
917 flags = 0;
Guido van Rossum73624e91994-10-10 17:59:00 +0000918 if (!PyArg_Parse(args, "(s#O)", &buf, &len, &addro)) {
919 PyErr_Clear();
920 if (!PyArg_Parse(args, "(s#iO)", &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000921 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000922 }
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000923 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000924 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000925 Py_BEGIN_ALLOW_THREADS
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000926 n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +0000927 Py_END_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000928 if (n < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000929 return PySocket_Err();
930 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000931}
932
Guido van Rossum30a685f1991-06-27 15:51:29 +0000933
934/* s.shutdown(how) method */
935
Guido van Rossum73624e91994-10-10 17:59:00 +0000936static PyObject *
937BUILD_FUNC_DEF_2(PySocketSock_shutdown,PySocketSockObject *,s, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000938{
939 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000940 int res;
Guido van Rossumfe28fa01997-05-15 19:09:26 +0000941 if (!PyArg_Parse(args, "i", &how))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000942 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +0000943 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000944 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +0000945 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000946 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +0000947 return PySocket_Err();
948 Py_INCREF(Py_None);
949 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000950}
951
Guido van Rossum30a685f1991-06-27 15:51:29 +0000952
953/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000954
Guido van Rossum73624e91994-10-10 17:59:00 +0000955static PyMethodDef PySocketSock_methods[] = {
956 {"accept", (PyCFunction)PySocketSock_accept},
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000957 {"setblocking", (PyCFunction)PySocketSock_setblocking},
Guido van Rossum73624e91994-10-10 17:59:00 +0000958 {"setsockopt", (PyCFunction)PySocketSock_setsockopt},
Guido van Rossumbe32c891996-06-20 16:25:29 +0000959 {"getsockopt", (PyCFunction)PySocketSock_getsockopt, 1},
Guido van Rossum73624e91994-10-10 17:59:00 +0000960 {"bind", (PyCFunction)PySocketSock_bind},
961 {"close", (PyCFunction)PySocketSock_close},
962 {"connect", (PyCFunction)PySocketSock_connect},
963 {"fileno", (PyCFunction)PySocketSock_fileno},
Guido van Rossumbe32c891996-06-20 16:25:29 +0000964#ifndef NO_DUP
965 {"dup", (PyCFunction)PySocketSock_dup},
966#endif
Guido van Rossum73624e91994-10-10 17:59:00 +0000967 {"getsockname", (PyCFunction)PySocketSock_getsockname},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000968#ifdef HAVE_GETPEERNAME
Guido van Rossum73624e91994-10-10 17:59:00 +0000969 {"getpeername", (PyCFunction)PySocketSock_getpeername},
Guido van Rossum9575a441993-04-07 14:06:14 +0000970#endif
Guido van Rossum73624e91994-10-10 17:59:00 +0000971 {"listen", (PyCFunction)PySocketSock_listen},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000972#ifndef NO_DUP
Guido van Rossum6b144911995-03-14 15:05:13 +0000973 {"makefile", (PyCFunction)PySocketSock_makefile, 1},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000974#endif
Guido van Rossumbe32c891996-06-20 16:25:29 +0000975 {"recv", (PyCFunction)PySocketSock_recv, 1},
976 {"recvfrom", (PyCFunction)PySocketSock_recvfrom, 1},
977 {"send", (PyCFunction)PySocketSock_send, 1},
Guido van Rossum73624e91994-10-10 17:59:00 +0000978 {"sendto", (PyCFunction)PySocketSock_sendto},
979 {"shutdown", (PyCFunction)PySocketSock_shutdown},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000980 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000981};
982
Guido van Rossum30a685f1991-06-27 15:51:29 +0000983
Guido van Rossum73624e91994-10-10 17:59:00 +0000984/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +0000985 First close the file description. */
986
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000987static void
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000988BUILD_FUNC_DEF_1(PySocketSock_dealloc,PySocketSockObject *,s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000989{
990 (void) close(s->sock_fd);
Guido van Rossum73624e91994-10-10 17:59:00 +0000991 PyMem_DEL(s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000992}
993
Guido van Rossum30a685f1991-06-27 15:51:29 +0000994
995/* Return a socket object's named attribute. */
996
Guido van Rossum73624e91994-10-10 17:59:00 +0000997static PyObject *
998BUILD_FUNC_DEF_2(PySocketSock_getattr,PySocketSockObject *,s, char *,name)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000999{
Guido van Rossum73624e91994-10-10 17:59:00 +00001000 return Py_FindMethod(PySocketSock_methods, (PyObject *) s, name);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001001}
1002
Guido van Rossum30a685f1991-06-27 15:51:29 +00001003
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001004static PyObject *
1005BUILD_FUNC_DEF_1(PySocketSock_repr,PySocketSockObject *,s)
1006{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001007 char buf[512];
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001008 sprintf(buf,
1009 "<socket object, fd=%d, family=%d, type=%d, protocol=%d>",
1010 s->sock_fd, s->sock_family, s->sock_type, s->sock_proto);
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00001011 return PyString_FromString(buf);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001012}
1013
1014
Guido van Rossumb6775db1994-08-01 11:34:53 +00001015/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001016
Guido van Rossum73624e91994-10-10 17:59:00 +00001017static PyTypeObject PySocketSock_Type = {
Guido van Rossum55558541996-05-23 22:54:50 +00001018 PyObject_HEAD_INIT(0) /* Must fill in type value later */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001019 0,
1020 "socket",
Guido van Rossum73624e91994-10-10 17:59:00 +00001021 sizeof(PySocketSockObject),
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001022 0,
Guido van Rossum73624e91994-10-10 17:59:00 +00001023 (destructor)PySocketSock_dealloc, /*tp_dealloc*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001024 0, /*tp_print*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001025 (getattrfunc)PySocketSock_getattr, /*tp_getattr*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001026 0, /*tp_setattr*/
1027 0, /*tp_compare*/
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001028 (reprfunc)PySocketSock_repr, /*tp_repr*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001029 0, /*tp_as_number*/
1030 0, /*tp_as_sequence*/
1031 0, /*tp_as_mapping*/
1032};
1033
Guido van Rossum30a685f1991-06-27 15:51:29 +00001034
Guido van Rossum81194471991-07-27 21:42:02 +00001035/* Python interface to gethostname(). */
1036
1037/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001038static PyObject *
1039BUILD_FUNC_DEF_2(PySocket_gethostname,PyObject *,self, PyObject *,args)
Guido van Rossum81194471991-07-27 21:42:02 +00001040{
1041 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001042 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +00001043 if (!PyArg_NoArgs(args))
Guido van Rossum81194471991-07-27 21:42:02 +00001044 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001045 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001046 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00001047 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001048 if (res < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001049 return PySocket_Err();
Guido van Rossum81194471991-07-27 21:42:02 +00001050 buf[sizeof buf - 1] = '\0';
Guido van Rossum73624e91994-10-10 17:59:00 +00001051 return PyString_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00001052}
Guido van Rossumff4949e1992-08-05 19:58:53 +00001053
1054
Guido van Rossum30a685f1991-06-27 15:51:29 +00001055/* Python interface to gethostbyname(name). */
1056
1057/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001058static PyObject *
1059BUILD_FUNC_DEF_2(PySocket_gethostbyname,PyObject *,self, PyObject *,args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001060{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001061 char *name;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001062 struct sockaddr_in addrbuf;
Guido van Rossum73624e91994-10-10 17:59:00 +00001063 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00001064 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001065 if (setipaddr(name, &addrbuf) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001066 return NULL;
1067 return makeipaddr(&addrbuf);
1068}
1069
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001070/* Python interface to gethostbyaddr(IP). */
1071
1072/*ARGSUSED*/
1073static PyObject *
1074BUILD_FUNC_DEF_2(PySocket_gethostbyaddr,PyObject *,self, PyObject *, args)
1075{
1076 struct sockaddr_in addr;
1077 char *ip_num;
1078 struct hostent *h;
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00001079 char **pch;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001080 PyObject *rtn_tuple = (PyObject *)NULL;
1081 PyObject *name_list = (PyObject *)NULL;
1082 PyObject *addr_list = (PyObject *)NULL;
1083 PyObject *tmp;
1084
1085 if (!PyArg_Parse(args, "s", &ip_num))
1086 return NULL;
1087 if (setipaddr(ip_num, &addr) < 0)
1088 return NULL;
1089 h = gethostbyaddr((char *)&addr.sin_addr,
1090 sizeof(addr.sin_addr),
1091 AF_INET);
1092 if (h == NULL) {
Guido van Rossumca9b3231995-01-10 16:30:54 +00001093#ifdef HAVE_HSTRERROR
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001094 /* Let's get real error message to return */
1095 extern int h_errno;
1096 PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno));
1097#else
1098 PyErr_SetString(PySocket_Error, "host not found");
1099#endif
1100 return NULL;
1101 }
1102 if ((name_list = PyList_New(0)) == NULL)
1103 goto err;
1104 if ((addr_list = PyList_New(0)) == NULL)
1105 goto err;
1106 for (pch = h->h_aliases; *pch != NULL; pch++) {
Barry Warsaw752300b1997-01-03 17:18:10 +00001107 int status;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001108 tmp = PyString_FromString(*pch);
1109 if (tmp == NULL)
1110 goto err;
Barry Warsaw752300b1997-01-03 17:18:10 +00001111 status = PyList_Append(name_list, tmp);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001112 Py_DECREF(tmp);
Barry Warsaw752300b1997-01-03 17:18:10 +00001113 if (status)
1114 goto err;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001115 }
1116 for (pch = h->h_addr_list; *pch != NULL; pch++) {
Barry Warsaw752300b1997-01-03 17:18:10 +00001117 int status;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001118 memcpy((char *) &addr.sin_addr, *pch, h->h_length);
1119 tmp = makeipaddr(&addr);
1120 if (tmp == NULL)
1121 goto err;
Barry Warsaw752300b1997-01-03 17:18:10 +00001122 status = PyList_Append(addr_list, tmp);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001123 Py_DECREF(tmp);
Barry Warsaw752300b1997-01-03 17:18:10 +00001124 if (status)
1125 goto err;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001126 }
1127 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
1128 err:
1129 Py_XDECREF(name_list);
1130 Py_XDECREF(addr_list);
1131 return rtn_tuple;
1132}
1133
Guido van Rossum30a685f1991-06-27 15:51:29 +00001134
1135/* Python interface to getservbyname(name).
1136 This only returns the port number, since the other info is already
1137 known or not useful (like the list of aliases). */
1138
1139/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001140static PyObject *
1141BUILD_FUNC_DEF_2(PySocket_getservbyname,PyObject *,self, PyObject *,args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001142{
Guido van Rossumff4949e1992-08-05 19:58:53 +00001143 char *name, *proto;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001144 struct servent *sp;
Guido van Rossum73624e91994-10-10 17:59:00 +00001145 if (!PyArg_Parse(args, "(ss)", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00001146 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001147 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001148 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00001149 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00001150 if (sp == NULL) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001151 PyErr_SetString(PySocket_Error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00001152 return NULL;
1153 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001154 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00001155}
1156
1157
Guido van Rossum3901d851996-12-19 16:35:04 +00001158/* Python interface to getprotobyname(name).
1159 This only returns the protocol number, since the other info is
1160 already known or not useful (like the list of aliases). */
1161
1162/*ARGSUSED*/
1163static PyObject *
1164BUILD_FUNC_DEF_2(PySocket_getprotobyname,PyObject *,self, PyObject *,args)
1165{
1166 char *name;
1167 struct protoent *sp;
1168 if (!PyArg_Parse(args, "s", &name))
1169 return NULL;
1170 Py_BEGIN_ALLOW_THREADS
1171 sp = getprotobyname(name);
1172 Py_END_ALLOW_THREADS
1173 if (sp == NULL) {
1174 PyErr_SetString(PySocket_Error, "protocol not found");
1175 return NULL;
1176 }
1177 return PyInt_FromLong((long) sp->p_proto);
1178}
1179
1180
Guido van Rossum30a685f1991-06-27 15:51:29 +00001181/* Python interface to socket(family, type, proto).
1182 The third (protocol) argument is optional.
1183 Return a new socket object. */
1184
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001185/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001186static PyObject *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001187BUILD_FUNC_DEF_2(PySocket_socket,PyObject *,self, PyObject *,args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001188{
Guido van Rossum73624e91994-10-10 17:59:00 +00001189 PySocketSockObject *s;
Guido van Rossum6f489d91996-06-28 20:15:15 +00001190#ifdef MS_WINDOWS
Guido van Rossumd639d4d1996-06-12 04:04:22 +00001191 SOCKET fd;
1192#else
1193 int fd;
1194#endif
Guido van Rossumbe32c891996-06-20 16:25:29 +00001195 int family, type, proto = 0;
1196 if (!PyArg_ParseTuple(args, "ii|i", &family, &type, &proto))
1197 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001198 Py_BEGIN_ALLOW_THREADS
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001199 fd = socket(family, type, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00001200 Py_END_ALLOW_THREADS
Guido van Rossum6f489d91996-06-28 20:15:15 +00001201#ifdef MS_WINDOWS
Guido van Rossumd639d4d1996-06-12 04:04:22 +00001202 if (fd == INVALID_SOCKET)
1203#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001204 if (fd < 0)
Guido van Rossumd639d4d1996-06-12 04:04:22 +00001205#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001206 return PySocket_Err();
1207 s = PySocketSock_New(fd, family, type, proto);
Guido van Rossum30a685f1991-06-27 15:51:29 +00001208 /* If the object can't be created, don't forget to close the
1209 file descriptor again! */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001210 if (s == NULL)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001211 (void) close(fd);
Guido van Rossum81194471991-07-27 21:42:02 +00001212 /* From now on, ignore SIGPIPE and let the error checking
1213 do the work. */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001214#ifdef SIGPIPE
Guido van Rossum81194471991-07-27 21:42:02 +00001215 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001216#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001217 return (PyObject *) s;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001218}
1219
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001220#ifndef NO_DUP
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001221/* Create a socket object from a numeric file description.
1222 Useful e.g. if stdin is a socket.
1223 Additional arguments as for socket(). */
1224
1225/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001226static PyObject *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001227BUILD_FUNC_DEF_2(PySocket_fromfd,PyObject *,self, PyObject *,args)
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001228{
Guido van Rossum73624e91994-10-10 17:59:00 +00001229 PySocketSockObject *s;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001230 int fd, family, type, proto = 0;
1231 if (!PyArg_ParseTuple(args, "iii|i", &fd, &family, &type, &proto))
1232 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +00001233 /* Dup the fd so it and the socket can be closed independently */
1234 fd = dup(fd);
1235 if (fd < 0)
Guido van Rossum73624e91994-10-10 17:59:00 +00001236 return PySocket_Err();
1237 s = PySocketSock_New(fd, family, type, proto);
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001238 /* From now on, ignore SIGPIPE and let the error checking
1239 do the work. */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001240#ifdef SIGPIPE
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001241 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001242#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001243 return (PyObject *) s;
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001244}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001245#endif /* NO_DUP */
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001246
Guido van Rossum006bf911996-06-12 04:04:55 +00001247static PyObject *
1248BUILD_FUNC_DEF_2(PySocket_ntohs, PyObject *, self, PyObject *, args)
1249{
1250 int x1, x2;
1251
1252 if (!PyArg_Parse(args, "i", &x1)) {
1253 return NULL;
1254 }
1255 x2 = (int)ntohs((short)x1);
1256 return PyInt_FromLong(x2);
1257}
1258
1259static PyObject *
1260BUILD_FUNC_DEF_2(PySocket_ntohl, PyObject *, self, PyObject *, args)
1261{
1262 int x1, x2;
1263
1264 if (!PyArg_Parse(args, "i", &x1)) {
1265 return NULL;
1266 }
1267 x2 = ntohl(x1);
1268 return PyInt_FromLong(x2);
1269}
1270
1271static PyObject *
1272BUILD_FUNC_DEF_2(PySocket_htons, PyObject *, self, PyObject *, args)
1273{
1274 int x1, x2;
1275
1276 if (!PyArg_Parse(args, "i", &x1)) {
1277 return NULL;
1278 }
1279 x2 = (int)htons((short)x1);
1280 return PyInt_FromLong(x2);
1281}
1282
1283static PyObject *
1284BUILD_FUNC_DEF_2(PySocket_htonl, PyObject *, self, PyObject *, args)
1285{
1286 int x1, x2;
1287
1288 if (!PyArg_Parse(args, "i", &x1)) {
1289 return NULL;
1290 }
1291 x2 = htonl(x1);
1292 return PyInt_FromLong(x2);
1293}
1294
Guido van Rossum30a685f1991-06-27 15:51:29 +00001295/* List of functions exported by this module. */
1296
Guido van Rossum73624e91994-10-10 17:59:00 +00001297static PyMethodDef PySocket_methods[] = {
1298 {"gethostbyname", PySocket_gethostbyname},
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001299 {"gethostbyaddr", PySocket_gethostbyaddr},
Guido van Rossum73624e91994-10-10 17:59:00 +00001300 {"gethostname", PySocket_gethostname},
1301 {"getservbyname", PySocket_getservbyname},
Guido van Rossum3901d851996-12-19 16:35:04 +00001302 {"getprotobyname", PySocket_getprotobyname},
Guido van Rossumbe32c891996-06-20 16:25:29 +00001303 {"socket", PySocket_socket, 1},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001304#ifndef NO_DUP
Guido van Rossumbe32c891996-06-20 16:25:29 +00001305 {"fromfd", PySocket_fromfd, 1},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001306#endif
Guido van Rossum006bf911996-06-12 04:04:55 +00001307 {"ntohs", PySocket_ntohs},
1308 {"ntohl", PySocket_ntohl},
1309 {"htons", PySocket_htons},
1310 {"htonl", PySocket_htonl},
Guido van Rossum30a685f1991-06-27 15:51:29 +00001311 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001312};
1313
Guido van Rossum30a685f1991-06-27 15:51:29 +00001314
1315/* Convenience routine to export an integer value.
Barry Warsaw752300b1997-01-03 17:18:10 +00001316 *
1317 * Since this function is called only from initsocket/init_socket(), any
1318 * errors trigger a fatal exception.
1319 */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001320static void
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001321BUILD_FUNC_DEF_3(insint,PyObject *,d, char *,name, int,value)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001322{
Guido van Rossum73624e91994-10-10 17:59:00 +00001323 PyObject *v = PyInt_FromLong((long) value);
Barry Warsaw752300b1997-01-03 17:18:10 +00001324 if (!v || PyDict_SetItemString(d, name, v))
1325 Py_FatalError("can't initialize socket module");
1326
1327 Py_DECREF(v);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001328}
1329
Guido van Rossum30a685f1991-06-27 15:51:29 +00001330
Guido van Rossum8d665e61996-06-26 18:22:49 +00001331#ifdef MS_WINDOWS
Guido van Rossumbe32c891996-06-20 16:25:29 +00001332
1333/* Additional initialization and cleanup for NT/Windows */
1334
1335static void
1336NTcleanup()
1337{
1338 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00001339}
1340
1341static int
1342NTinit()
1343{
1344 WSADATA WSAData;
1345 int ret;
1346 char buf[100];
1347 ret = WSAStartup(0x0101, &WSAData);
1348 switch (ret) {
1349 case 0: /* no error */
1350 atexit(NTcleanup);
1351 return 1;
1352 case WSASYSNOTREADY:
1353 PyErr_SetString(PyExc_ImportError,
1354 "WSAStartup failed: network not ready");
1355 break;
1356 case WSAVERNOTSUPPORTED:
1357 case WSAEINVAL:
1358 PyErr_SetString(PyExc_ImportError,
1359 "WSAStartup failed: requested version not supported");
1360 break;
1361 default:
1362 sprintf(buf, "WSAStartup failed: error code %d", ret);
1363 PyErr_SetString(PyExc_ImportError, buf);
1364 break;
1365 }
1366 return 0;
1367}
1368
Guido van Rossum8d665e61996-06-26 18:22:49 +00001369#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00001370
1371
Guido van Rossum30a685f1991-06-27 15:51:29 +00001372/* Initialize this module.
1373 This is called when the first 'import socket' is done,
1374 via a table in config.c, if config.c is compiled with USE_SOCKET
Guido van Rossumbe32c891996-06-20 16:25:29 +00001375 defined.
1376
Guido van Rossum8d665e61996-06-26 18:22:49 +00001377 For MS_WINDOWS (which means any Windows variant), this module
Guido van Rossumbe32c891996-06-20 16:25:29 +00001378 is actually called "_socket", and there's a wrapper "socket.py"
1379 which implements some missing functionality (such as makefile(),
1380 dup() and fromfd()). The import of "_socket" may fail with an
1381 ImportError exception if initialization of WINSOCK fails. When
1382 WINSOCK is initialized succesfully, a call to WSACleanup() is
1383 scheduled to be made at exit time. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001384
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001385void
Guido van Rossum8d665e61996-06-26 18:22:49 +00001386#ifdef MS_WINDOWS
Guido van Rossumbe32c891996-06-20 16:25:29 +00001387init_socket()
1388#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001389initsocket()
Guido van Rossumbe32c891996-06-20 16:25:29 +00001390#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001391{
Guido van Rossum73624e91994-10-10 17:59:00 +00001392 PyObject *m, *d;
Guido van Rossum8d665e61996-06-26 18:22:49 +00001393#ifdef MS_WINDOWS
Guido van Rossumbe32c891996-06-20 16:25:29 +00001394 if (!NTinit())
1395 return;
1396 m = Py_InitModule("_socket", PySocket_methods);
1397#else
Guido van Rossum73624e91994-10-10 17:59:00 +00001398 m = Py_InitModule("socket", PySocket_methods);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001399#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00001400 d = PyModule_GetDict(m);
1401 PySocket_Error = PyString_FromString("socket.error");
1402 if (PySocket_Error == NULL ||
1403 PyDict_SetItemString(d, "error", PySocket_Error) != 0)
1404 Py_FatalError("can't define socket.error");
Guido van Rossum3b4b6fc1997-05-21 14:37:37 +00001405 PySocketSock_Type.ob_type = &PyType_Type;
1406 Py_INCREF(&PySocketSock_Type);
Guido van Rossum91ba64d1997-06-02 22:18:09 +00001407 if (PyDict_SetItemString(d, "SocketType",
Guido van Rossum3b4b6fc1997-05-21 14:37:37 +00001408 (PyObject *)&PySocketSock_Type) != 0)
Guido van Rossum91ba64d1997-06-02 22:18:09 +00001409 Py_FatalError("can't define socket.SocketType");
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001410 insint(d, "AF_INET", AF_INET);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001411#ifdef AF_UNIX
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001412 insint(d, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001413#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001414 insint(d, "SOCK_STREAM", SOCK_STREAM);
1415 insint(d, "SOCK_DGRAM", SOCK_DGRAM);
1416 insint(d, "SOCK_RAW", SOCK_RAW);
1417 insint(d, "SOCK_SEQPACKET", SOCK_SEQPACKET);
1418 insint(d, "SOCK_RDM", SOCK_RDM);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00001419
1420#ifdef SO_DEBUG
1421 insint(d, "SO_DEBUG", SO_DEBUG);
1422#endif
1423#ifdef SO_ACCEPTCONN
1424 insint(d, "SO_ACCEPTCONN", SO_ACCEPTCONN);
1425#endif
1426#ifdef SO_REUSEADDR
1427 insint(d, "SO_REUSEADDR", SO_REUSEADDR);
1428#endif
1429#ifdef SO_KEEPALIVE
1430 insint(d, "SO_KEEPALIVE", SO_KEEPALIVE);
1431#endif
1432#ifdef SO_DONTROUTE
1433 insint(d, "SO_DONTROUTE", SO_DONTROUTE);
1434#endif
1435#ifdef SO_BROADCAST
1436 insint(d, "SO_BROADCAST", SO_BROADCAST);
1437#endif
1438#ifdef SO_USELOOPBACK
1439 insint(d, "SO_USELOOPBACK", SO_USELOOPBACK);
1440#endif
1441#ifdef SO_LINGER
1442 insint(d, "SO_LINGER", SO_LINGER);
1443#endif
1444#ifdef SO_OOBINLINE
1445 insint(d, "SO_OOBINLINE", SO_OOBINLINE);
1446#endif
1447#ifdef SO_REUSEPORT
1448 insint(d, "SO_REUSEPORT", SO_REUSEPORT);
1449#endif
1450
1451#ifdef SO_SNDBUF
1452 insint(d, "SO_SNDBUF", SO_SNDBUF);
1453#endif
1454#ifdef SO_RCVBUF
1455 insint(d, "SO_RCVBUF", SO_RCVBUF);
1456#endif
1457#ifdef SO_SNDLOWAT
1458 insint(d, "SO_SNDLOWAT", SO_SNDLOWAT);
1459#endif
1460#ifdef SO_RCVLOWAT
1461 insint(d, "SO_RCVLOWAT", SO_RCVLOWAT);
1462#endif
1463#ifdef SO_SNDTIMEO
1464 insint(d, "SO_SNDTIMEO", SO_SNDTIMEO);
1465#endif
1466#ifdef SO_RCVTIMEO
1467 insint(d, "SO_RCVTIMEO", SO_RCVTIMEO);
1468#endif
1469#ifdef SO_ERROR
1470 insint(d, "SO_ERROR", SO_ERROR);
1471#endif
1472#ifdef SO_TYPE
1473 insint(d, "SO_TYPE", SO_TYPE);
1474#endif
1475
1476 /* Maximum number of connections for "listen" */
1477#ifdef SOMAXCONN
1478 insint(d, "SOMAXCONN", SOMAXCONN);
1479#else
1480 insint(d, "SOMAXCONN", 5); /* Common value */
1481#endif
1482
1483 /* Flags for send, recv */
1484#ifdef MSG_OOB
1485 insint(d, "MSG_OOB", MSG_OOB);
1486#endif
1487#ifdef MSG_PEEK
1488 insint(d, "MSG_PEEK", MSG_PEEK);
1489#endif
1490#ifdef MSG_DONTROUTE
1491 insint(d, "MSG_DONTROUTE", MSG_DONTROUTE);
1492#endif
1493#ifdef MSG_EOR
1494 insint(d, "MSG_EOR", MSG_EOR);
1495#endif
1496#ifdef MSG_TRUNC
1497 insint(d, "MSG_TRUNC", MSG_TRUNC);
1498#endif
1499#ifdef MSG_CTRUNC
1500 insint(d, "MSG_CTRUNC", MSG_CTRUNC);
1501#endif
1502#ifdef MSG_WAITALL
1503 insint(d, "MSG_WAITALL", MSG_WAITALL);
1504#endif
1505#ifdef MSG_BTAG
1506 insint(d, "MSG_BTAG", MSG_BTAG);
1507#endif
1508#ifdef MSG_ETAG
1509 insint(d, "MSG_ETAG", MSG_ETAG);
1510#endif
1511
1512 /* Protocol level and numbers, usable for [gs]etsockopt */
1513#ifdef SOL_SOCKET
1514 insint(d, "SOL_SOCKET", SOL_SOCKET);
1515#endif
1516#ifdef IPPROTO_IP
1517 insint(d, "IPPROTO_IP", IPPROTO_IP);
1518#endif
1519#ifdef IPPROTO_ICMP
1520 insint(d, "IPPROTO_ICMP", IPPROTO_ICMP);
1521#endif
1522#ifdef IPPROTO_IGMP
1523 insint(d, "IPPROTO_IGMP", IPPROTO_IGMP);
1524#endif
1525#ifdef IPPROTO_GGP
1526 insint(d, "IPPROTO_GGP", IPPROTO_GGP);
1527#endif
1528#ifdef IPPROTO_TCP
1529 insint(d, "IPPROTO_TCP", IPPROTO_TCP);
1530#endif
1531#ifdef IPPROTO_EGP
1532 insint(d, "IPPROTO_EGP", IPPROTO_EGP);
1533#endif
1534#ifdef IPPROTO_PUP
1535 insint(d, "IPPROTO_PUP", IPPROTO_PUP);
1536#endif
1537#ifdef IPPROTO_UDP
1538 insint(d, "IPPROTO_UDP", IPPROTO_UDP);
1539#endif
1540#ifdef IPPROTO_IDP
1541 insint(d, "IPPROTO_IDP", IPPROTO_IDP);
1542#endif
1543#ifdef IPPROTO_HELLO
1544 insint(d, "IPPROTO_HELLO", IPPROTO_HELLO);
1545#endif
1546#ifdef IPPROTO_ND
1547 insint(d, "IPPROTO_ND", IPPROTO_ND);
1548#endif
1549#ifdef IPPROTO_TP
1550 insint(d, "IPPROTO_TP", IPPROTO_TP);
1551#endif
1552#ifdef IPPROTO_XTP
1553 insint(d, "IPPROTO_XTP", IPPROTO_XTP);
1554#endif
1555#ifdef IPPROTO_EON
1556 insint(d, "IPPROTO_EON", IPPROTO_EON);
1557#endif
1558#ifdef IPPROTO_BIP
1559 insint(d, "IPPROTO_BIP", IPPROTO_BIP);
1560#endif
1561/**/
1562#ifdef IPPROTO_RAW
1563 insint(d, "IPPROTO_RAW", IPPROTO_RAW);
1564#endif
1565#ifdef IPPROTO_MAX
1566 insint(d, "IPPROTO_MAX", IPPROTO_MAX);
1567#endif
1568
1569 /* Some port configuration */
1570#ifdef IPPORT_RESERVED
1571 insint(d, "IPPORT_RESERVED", IPPORT_RESERVED);
1572#else
1573 insint(d, "IPPORT_RESERVED", 1024);
1574#endif
1575#ifdef IPPORT_USERRESERVED
1576 insint(d, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
1577#else
1578 insint(d, "IPPORT_USERRESERVED", 5000);
1579#endif
1580
Guido van Rossum5f05eb41995-02-17 15:11:07 +00001581 /* Some reserved IP v.4 addresses */
1582#ifdef INADDR_ANY
1583 insint(d, "INADDR_ANY", INADDR_ANY);
1584#else
1585 insint(d, "INADDR_ANY", 0x00000000);
1586#endif
1587#ifdef INADDR_BROADCAST
1588 insint(d, "INADDR_BROADCAST", INADDR_BROADCAST);
1589#else
1590 insint(d, "INADDR_BROADCAST", 0xffffffff);
1591#endif
1592#ifdef INADDR_LOOPBACK
1593 insint(d, "INADDR_LOOPBACK", INADDR_LOOPBACK);
1594#else
1595 insint(d, "INADDR_LOOPBACK", 0x7F000001);
1596#endif
1597#ifdef INADDR_UNSPEC_GROUP
1598 insint(d, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
1599#else
1600 insint(d, "INADDR_UNSPEC_GROUP", 0xe0000000);
1601#endif
1602#ifdef INADDR_ALLHOSTS_GROUP
1603 insint(d, "INADDR_ALLHOSTS_GROUP", INADDR_ALLHOSTS_GROUP);
1604#else
1605 insint(d, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
1606#endif
1607#ifdef INADDR_MAX_LOCAL_GROUP
1608 insint(d, "INADDR_MAX_LOCAL_GROUP", INADDR_MAX_LOCAL_GROUP);
1609#else
1610 insint(d, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
1611#endif
1612#ifdef INADDR_NONE
1613 insint(d, "INADDR_NONE", INADDR_NONE);
1614#else
1615 insint(d, "INADDR_NONE", 0xffffffff);
1616#endif
1617
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00001618 /* IP [gs]etsockopt options */
1619#ifdef IP_OPTIONS
1620 insint(d, "IP_OPTIONS", IP_OPTIONS);
1621#endif
1622#ifdef IP_HDRINCL
1623 insint(d, "IP_HDRINCL", IP_HDRINCL);
1624#endif
1625#ifdef IP_TOS
1626 insint(d, "IP_TOS", IP_TOS);
1627#endif
1628#ifdef IP_TTL
1629 insint(d, "IP_TTL", IP_TTL);
1630#endif
1631#ifdef IP_RECVOPTS
1632 insint(d, "IP_RECVOPTS", IP_RECVOPTS);
1633#endif
1634#ifdef IP_RECVRETOPTS
1635 insint(d, "IP_RECVRETOPTS", IP_RECVRETOPTS);
1636#endif
1637#ifdef IP_RECVDSTADDR
1638 insint(d, "IP_RECVDSTADDR", IP_RECVDSTADDR);
1639#endif
1640#ifdef IP_RETOPTS
1641 insint(d, "IP_RETOPTS", IP_RETOPTS);
1642#endif
1643#ifdef IP_MULTICAST_IF
1644 insint(d, "IP_MULTICAST_IF", IP_MULTICAST_IF);
1645#endif
1646#ifdef IP_MULTICAST_TTL
1647 insint(d, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
1648#endif
1649#ifdef IP_MULTICAST_LOOP
1650 insint(d, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
1651#endif
1652#ifdef IP_ADD_MEMBERSHIP
1653 insint(d, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
1654#endif
1655#ifdef IP_DROP_MEMBERSHIP
1656 insint(d, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
1657#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001658}