blob: 32f22db827266a5e87c8455ac409766dfa9cb1ab [file] [log] [blame]
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001/***********************************************************
Guido van Rossumb6775db1994-08-01 11:34:53 +00002Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
Guido van Rossume5372401993-03-16 12:15:04 +00003Amsterdam, The Netherlands.
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
25/* Socket module */
26
27/*
28This module provides an interface to Berkeley socket IPC.
29
30Limitations:
31
Guido van Rossum30a685f1991-06-27 15:51:29 +000032- only AF_INET and AF_UNIX address families are supported
Guido van Rossumb6775db1994-08-01 11:34:53 +000033- no asynchronous I/O (but you can use select() on sockets)
Guido van Rossum81194471991-07-27 21:42:02 +000034- no read/write operations (use send/recv or makefile instead)
Guido van Rossum0e69587d1992-06-05 15:11:30 +000035- setsockopt() and getsockopt() only support integer options
Guido van Rossum6574b3e1991-06-25 21:36:08 +000036
37Interface:
38
Guido van Rossum81194471991-07-27 21:42:02 +000039- socket.gethostname() --> host name (string)
Guido van Rossum30a685f1991-06-27 15:51:29 +000040- socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd')
Guido van Rossum4dd2a7e1991-07-01 18:51:33 +000041- socket.getservbyname(servername, protocolname) --> port number
42- socket.socket(family, type [, proto]) --> new socket object
Guido van Rossum6574b3e1991-06-25 21:36:08 +000043- family and type constants from <socket.h> are accessed as socket.AF_INET etc.
Guido van Rossum6574b3e1991-06-25 21:36:08 +000044- errors are reported as the exception socket.error
45- an Internet socket address is a pair (hostname, port)
46 where hostname can be anything recognized by gethostbyname()
47 (including the dd.dd.dd.dd notation) and port is in host byte order
48- where a hostname is returned, the dd.dd.dd.dd notation is used
49- a UNIX domain socket is a string specifying the pathname
50
Guido van Rossum30a685f1991-06-27 15:51:29 +000051Socket methods:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000052
Guido van Rossum81194471991-07-27 21:42:02 +000053- s.accept() --> new socket object, sockaddr
Guido van Rossum0e69587d1992-06-05 15:11:30 +000054- s.setsockopt(level, optname, flag) --> None
55- s.getsockopt(level, optname) --> flag
Guido van Rossum30a685f1991-06-27 15:51:29 +000056- s.bind(sockaddr) --> None
57- s.connect(sockaddr) --> None
Guido van Rossumc89705d1992-11-26 08:54:07 +000058- s.getsockname() --> sockaddr
59- s.getpeername() --> sockaddr
Guido van Rossum30a685f1991-06-27 15:51:29 +000060- s.listen(n) --> None
61- s.makefile(mode) --> file object
Guido van Rossumeb6b33a1993-05-25 09:38:27 +000062- s.recv(nbytes [,flags]) --> string
Guido van Rossum18c9a4f1993-05-25 12:16:29 +000063- s.recvfrom(nbytes [,flags]) --> string, sockaddr
Guido van Rossumb6775db1994-08-01 11:34:53 +000064- s.send(string [,flags]) --> nbytes
65- s.sendto(string, [flags,] sockaddr) --> nbytes
Guido van Rossum30a685f1991-06-27 15:51:29 +000066- s.shutdown(how) --> None
67- s.close() --> None
68
Guido van Rossum6574b3e1991-06-25 21:36:08 +000069*/
70
71#include "allobjects.h"
72#include "modsupport.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000073#include "ceval.h"
Guido van Rossum6574b3e1991-06-25 21:36:08 +000074
Guido van Rossumb6775db1994-08-01 11:34:53 +000075#include <sys/types.h>
76#include "mytime.h"
Guido van Rossumed233a51992-06-23 09:07:03 +000077
Guido van Rossum81194471991-07-27 21:42:02 +000078#include <signal.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +000079#include <netdb.h>
Guido van Rossum6574b3e1991-06-25 21:36:08 +000080#include <sys/socket.h>
81#include <netinet/in.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +000082#ifdef HAVE_SYS_UN_H
Guido van Rossum6574b3e1991-06-25 21:36:08 +000083#include <sys/un.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +000084#else
85#undef AF_UNIX
Guido van Rossum9575a441993-04-07 14:06:14 +000086#endif
87
Guido van Rossum30a685f1991-06-27 15:51:29 +000088
89/* Global variable holding the exception type for errors detected
90 by this module (but not argument type or memory errors, etc.). */
91
92static object *SocketError;
93
94
95/* Convenience function to raise an error according to errno
96 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +000097
98static object *
99socket_error()
100{
101 return err_errno(SocketError);
102}
103
Guido van Rossum30a685f1991-06-27 15:51:29 +0000104
105/* The object holding a socket. It holds some extra information,
106 like the address family, which is used to decode socket address
107 arguments properly. */
108
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000109typedef struct {
110 OB_HEAD
Guido van Rossum30a685f1991-06-27 15:51:29 +0000111 int sock_fd; /* Socket file descriptor */
112 int sock_family; /* Address family, e.g., AF_INET */
113 int sock_type; /* Socket type, e.g., SOCK_STREAM */
114 int sock_proto; /* Protocol type, usually 0 */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000115} sockobject;
116
Guido van Rossum30a685f1991-06-27 15:51:29 +0000117
118/* A forward reference to the Socktype type object.
119 The Socktype variable contains pointers to various functions,
120 some of which call newsocobject(), which uses Socktype, so
Guido van Rossum54ba21b1991-09-10 14:57:12 +0000121 there has to be a circular reference. */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000122
Guido van Rossumb6775db1994-08-01 11:34:53 +0000123staticforward typeobject Socktype;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000124
Guido van Rossum30a685f1991-06-27 15:51:29 +0000125
126/* Create a new socket object.
127 This just creates the object and initializes it.
128 If the creation fails, return NULL and set an exception (implicit
129 in NEWOBJ()). */
130
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000131static sockobject *
132newsockobject(fd, family, type, proto)
133 int fd, family, type, proto;
134{
135 sockobject *s;
136 s = NEWOBJ(sockobject, &Socktype);
137 if (s != NULL) {
138 s->sock_fd = fd;
139 s->sock_family = family;
140 s->sock_type = type;
141 s->sock_proto = proto;
142 }
143 return s;
144}
145
Guido van Rossum30a685f1991-06-27 15:51:29 +0000146
147/* Convert a string specifying a host name or one of a few symbolic
148 names to a numeric IP address. This usually calls gethostbyname()
149 to do the work; the names "" and "<broadcast>" are special.
150 Return the length (should always be 4 bytes), or negative if
151 an error occurred; then an exception is raised. */
152
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000153static int
Guido van Rossum30a685f1991-06-27 15:51:29 +0000154setipaddr(name, addr_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000155 char *name;
156 struct sockaddr_in *addr_ret;
157{
158 struct hostent *hp;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000159 int d1, d2, d3, d4;
160 char ch;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000161
Guido van Rossum30a685f1991-06-27 15:51:29 +0000162 if (name[0] == '\0') {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000163 addr_ret->sin_addr.s_addr = INADDR_ANY;
164 return 4;
165 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000166 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000167 addr_ret->sin_addr.s_addr = INADDR_BROADCAST;
168 return 4;
169 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000170 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
171 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
172 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
173 addr_ret->sin_addr.s_addr = htonl(
174 ((long) d1 << 24) | ((long) d2 << 16) |
175 ((long) d3 << 8) | ((long) d4 << 0));
176 return 4;
177 }
Guido van Rossumff4949e1992-08-05 19:58:53 +0000178 BGN_SAVE
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000179 hp = gethostbyname(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000180 END_SAVE
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000181 if (hp == NULL) {
182 err_setstr(SocketError, "host not found");
183 return -1;
184 }
185 memcpy((char *) &addr_ret->sin_addr, hp->h_addr, hp->h_length);
186 return hp->h_length;
187}
188
Guido van Rossum30a685f1991-06-27 15:51:29 +0000189
Guido van Rossum30a685f1991-06-27 15:51:29 +0000190/* Create a string object representing an IP address.
191 This is always a string of the form 'dd.dd.dd.dd' (with variable
192 size numbers). */
193
194static object *
195makeipaddr(addr)
196 struct sockaddr_in *addr;
197{
198 long x = ntohl(addr->sin_addr.s_addr);
199 char buf[100];
200 sprintf(buf, "%d.%d.%d.%d",
201 (int) (x>>24) & 0xff, (int) (x>>16) & 0xff,
202 (int) (x>> 8) & 0xff, (int) (x>> 0) & 0xff);
203 return newstringobject(buf);
204}
205
206
207/* Create an object representing the given socket address,
208 suitable for passing it back to bind(), connect() etc.
209 The family field of the sockaddr structure is inspected
210 to determine what kind of address it really is. */
211
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000212/*ARGSUSED*/
213static object *
214makesockaddr(addr, addrlen)
215 struct sockaddr *addr;
216 int addrlen;
217{
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000218 if (addrlen == 0) {
219 /* No address -- may be recvfrom() from known socket */
220 INCREF(None);
221 return None;
222 }
223
Guido van Rossum30a685f1991-06-27 15:51:29 +0000224 switch (addr->sa_family) {
225
226 case AF_INET:
227 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000228 struct sockaddr_in *a = (struct sockaddr_in *) addr;
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000229 object *addr = makeipaddr(a);
230 object *ret = mkvalue("Oi", addr, ntohs(a->sin_port));
231 XDECREF(addr);
232 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000233 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000234
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000236 case AF_UNIX:
237 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000238 struct sockaddr_un *a = (struct sockaddr_un *) addr;
239 return newstringobject(a->sun_path);
240 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241#endif /* AF_UNIX */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000242
243 /* More cases here... */
244
245 default:
246 err_setstr(SocketError, "return unknown socket address type");
247 return NULL;
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000248
Guido van Rossum30a685f1991-06-27 15:51:29 +0000249 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000250}
251
Guido van Rossum30a685f1991-06-27 15:51:29 +0000252
253/* Parse a socket address argument according to the socket object's
254 address family. Return 1 if the address was in the proper format,
255 0 of not. The address is returned through addr_ret, its length
256 through len_ret. */
257
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000258static int
259getsockaddrarg(s, args, addr_ret, len_ret)
260 sockobject *s;
261 object *args;
262 struct sockaddr **addr_ret;
263 int *len_ret;
264{
Guido van Rossum30a685f1991-06-27 15:51:29 +0000265 switch (s->sock_family) {
266
Guido van Rossumb6775db1994-08-01 11:34:53 +0000267#ifdef AF_UNIX
Guido van Rossum30a685f1991-06-27 15:51:29 +0000268 case AF_UNIX:
269 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000270 static struct sockaddr_un addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000271 char *path;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000272 int len;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000273 if (!getargs(args, "s#", &path, &len))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000274 return 0;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000275 if (len > sizeof addr.sun_path) {
Guido van Rossum30a685f1991-06-27 15:51:29 +0000276 err_setstr(SocketError, "AF_UNIX path too long");
277 return 0;
278 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000279 addr.sun_family = AF_UNIX;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000280 memcpy(addr.sun_path, path, len);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000281 *addr_ret = (struct sockaddr *) &addr;
282 *len_ret = len + sizeof addr.sun_family;
283 return 1;
284 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000285#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000286
Guido van Rossum30a685f1991-06-27 15:51:29 +0000287 case AF_INET:
288 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000289 static struct sockaddr_in addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000290 char *host;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000291 int port;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000292 if (!getargs(args, "(si)", &host, &port))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000293 return 0;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000294 if (setipaddr(host, &addr) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000295 return 0;
296 addr.sin_family = AF_INET;
297 addr.sin_port = htons(port);
298 *addr_ret = (struct sockaddr *) &addr;
299 *len_ret = sizeof addr;
300 return 1;
301 }
302
Guido van Rossum30a685f1991-06-27 15:51:29 +0000303 /* More cases here... */
304
305 default:
306 err_setstr(SocketError, "getsockaddrarg: bad family");
307 return 0;
308
309 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000310}
311
Guido van Rossum30a685f1991-06-27 15:51:29 +0000312
Guido van Rossum710e1df1992-06-12 10:39:36 +0000313/* Get the address length according to the socket object's address family.
314 Return 1 if the family is known, 0 otherwise. The length is returned
315 through len_ret. */
316
317static int
318getsockaddrlen(s, len_ret)
319 sockobject *s;
320 int *len_ret;
321{
322 switch (s->sock_family) {
323
Guido van Rossumb6775db1994-08-01 11:34:53 +0000324#ifdef AF_UNIX
Guido van Rossum710e1df1992-06-12 10:39:36 +0000325 case AF_UNIX:
326 {
327 *len_ret = sizeof (struct sockaddr_un);
328 return 1;
329 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000330#endif /* AF_UNIX */
Guido van Rossum710e1df1992-06-12 10:39:36 +0000331
332 case AF_INET:
333 {
334 *len_ret = sizeof (struct sockaddr_in);
335 return 1;
336 }
337
338 /* More cases here... */
339
340 default:
341 err_setstr(SocketError, "getsockaddrarg: bad family");
342 return 0;
343
344 }
345}
346
347
Guido van Rossum30a685f1991-06-27 15:51:29 +0000348/* s.accept() method */
349
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000350static object *
351sock_accept(s, args)
352 sockobject *s;
353 object *args;
354{
355 char addrbuf[256];
356 int addrlen, newfd;
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000357 object *sock, *addr, *res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000358 if (!getnoarg(args))
359 return NULL;
Guido van Rossum710e1df1992-06-12 10:39:36 +0000360 if (!getsockaddrlen(s, &addrlen))
361 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000362 BGN_SAVE
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000363 newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000364 END_SAVE
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000365 if (newfd < 0)
366 return socket_error();
Guido van Rossum30a685f1991-06-27 15:51:29 +0000367 /* Create the new object with unspecified family,
368 to avoid calls to bind() etc. on it. */
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000369 sock = (object *) newsockobject(newfd,
370 s->sock_family,
371 s->sock_type,
372 s->sock_proto);
373 if (sock == NULL)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000374 close(newfd);
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000375 addr = makesockaddr((struct sockaddr *) addrbuf, addrlen);
376 res = mkvalue("OO", sock, addr);
377 XDECREF(sock);
378 XDECREF(addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000379 return res;
380}
381
Guido van Rossum30a685f1991-06-27 15:51:29 +0000382
Guido van Rossume0e5edc1991-10-20 20:22:28 +0000383/* s.allowbroadcast() method */
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000384/* XXX obsolete -- will disappear in next release */
Guido van Rossume0e5edc1991-10-20 20:22:28 +0000385
386static object *
387sock_allowbroadcast(s, args)
388 sockobject *s;
389 object *args;
390{
391 int flag;
392 int res;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000393 if (!getargs(args, "i", &flag))
Guido van Rossume0e5edc1991-10-20 20:22:28 +0000394 return NULL;
395 res = setsockopt(s->sock_fd, SOL_SOCKET, SO_BROADCAST,
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000396 (ANY *)&flag, sizeof flag);
Guido van Rossume0e5edc1991-10-20 20:22:28 +0000397 if (res < 0)
398 return socket_error();
399 INCREF(None);
400 return None;
401}
402
403
Guido van Rossumaee08791992-09-08 09:05:33 +0000404/* s.setsockopt() method.
405 With an integer third argument, sets an integer option.
406 With a string third argument, sets an option from a buffer;
407 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000408
409static object *
410sock_setsockopt(s, args)
411 sockobject *s;
412 object *args;
413{
414 int level;
415 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000416 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +0000417 char *buf;
418 int buflen;
419 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000420
Guido van Rossumaee08791992-09-08 09:05:33 +0000421 if (getargs(args, "(iii)", &level, &optname, &flag)) {
422 buf = (char *) &flag;
423 buflen = sizeof flag;
424 }
425 else {
426 err_clear();
427 if (!getargs(args, "(iis#)", &level, &optname, &buf, &buflen))
428 return NULL;
429 }
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000430 res = setsockopt(s->sock_fd, level, optname, (ANY *)buf, buflen);
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000431 if (res < 0)
432 return socket_error();
433 INCREF(None);
434 return None;
435}
436
437
Guido van Rossumaee08791992-09-08 09:05:33 +0000438/* s.getsockopt() method.
439 With two arguments, retrieves an integer option.
440 With a third integer argument, retrieves a string buffer of that size;
441 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000442
443static object *
444sock_getsockopt(s, args)
445 sockobject *s;
446 object *args;
447{
448 int level;
449 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000450 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +0000451 object *buf;
452 int buflen;
453 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000454
Guido van Rossumaee08791992-09-08 09:05:33 +0000455 if (getargs(args, "(ii)", &level, &optname)) {
456 int flag = 0;
457 int flagsize = sizeof flag;
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000458 res = getsockopt(s->sock_fd, level, optname,
459 (ANY *)&flag, &flagsize);
Guido van Rossumaee08791992-09-08 09:05:33 +0000460 if (res < 0)
461 return socket_error();
462 return newintobject(flag);
463 }
464 err_clear();
465 if (!getargs(args, "(iii)", &level, &optname, &buflen))
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000466 return NULL;
Guido van Rossumaee08791992-09-08 09:05:33 +0000467 if (buflen <= 0 || buflen > 1024) {
468 err_setstr(SocketError, "getsockopt buflen out of range");
469 return NULL;
470 }
471 buf = newsizedstringobject((char *)NULL, buflen);
472 if (buf == NULL)
473 return NULL;
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000474 res = getsockopt(s->sock_fd, level, optname,
475 (ANY *)getstringvalue(buf), &buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +0000476 if (res < 0) {
477 DECREF(buf);
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000478 return socket_error();
Guido van Rossumaee08791992-09-08 09:05:33 +0000479 }
480 resizestring(&buf, buflen);
481 return buf;
Guido van Rossum0e69587d1992-06-05 15:11:30 +0000482}
483
484
Guido van Rossum30a685f1991-06-27 15:51:29 +0000485/* s.bind(sockaddr) method */
486
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000487static object *
488sock_bind(s, args)
489 sockobject *s;
490 object *args;
491{
492 struct sockaddr *addr;
493 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000494 int res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000495 if (!getsockaddrarg(s, args, &addr, &addrlen))
496 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000497 BGN_SAVE
498 res = bind(s->sock_fd, addr, addrlen);
499 END_SAVE
500 if (res < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000501 return socket_error();
502 INCREF(None);
503 return None;
504}
505
Guido van Rossum30a685f1991-06-27 15:51:29 +0000506
507/* s.close() method.
508 Set the file descriptor to -1 so operations tried subsequently
509 will surely fail. */
510
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000511static object *
512sock_close(s, args)
513 sockobject *s;
514 object *args;
515{
516 if (!getnoarg(args))
517 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000518 BGN_SAVE
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000519 (void) close(s->sock_fd);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000520 END_SAVE
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000521 s->sock_fd = -1;
522 INCREF(None);
523 return None;
524}
525
Guido van Rossum30a685f1991-06-27 15:51:29 +0000526
527/* s.connect(sockaddr) method */
528
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000529static object *
530sock_connect(s, args)
531 sockobject *s;
532 object *args;
533{
534 struct sockaddr *addr;
535 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000536 int res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000537 if (!getsockaddrarg(s, args, &addr, &addrlen))
538 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000539 BGN_SAVE
540 res = connect(s->sock_fd, addr, addrlen);
541 END_SAVE
542 if (res < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000543 return socket_error();
544 INCREF(None);
545 return None;
546}
547
Guido van Rossum30a685f1991-06-27 15:51:29 +0000548
Guido van Rossumed233a51992-06-23 09:07:03 +0000549/* s.fileno() method */
550
551static object *
552sock_fileno(s, args)
553 sockobject *s;
554 object *args;
555{
556 if (!getnoarg(args))
557 return NULL;
558 return newintobject((long) s->sock_fd);
559}
560
561
Guido van Rossumc89705d1992-11-26 08:54:07 +0000562/* s.getsockname() method */
563
564static object *
565sock_getsockname(s, args)
566 sockobject *s;
567 object *args;
568{
569 char addrbuf[256];
570 int addrlen, res;
571 if (!getnoarg(args))
572 return NULL;
573 if (!getsockaddrlen(s, &addrlen))
574 return NULL;
575 BGN_SAVE
576 res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
577 END_SAVE
578 if (res < 0)
579 return socket_error();
580 return makesockaddr((struct sockaddr *) addrbuf, addrlen);
581}
582
583
Guido van Rossumb6775db1994-08-01 11:34:53 +0000584#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +0000585/* s.getpeername() method */
586
587static object *
588sock_getpeername(s, args)
589 sockobject *s;
590 object *args;
591{
592 char addrbuf[256];
593 int addrlen, res;
594 if (!getnoarg(args))
595 return NULL;
596 if (!getsockaddrlen(s, &addrlen))
597 return NULL;
598 BGN_SAVE
599 res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
600 END_SAVE
601 if (res < 0)
602 return socket_error();
603 return makesockaddr((struct sockaddr *) addrbuf, addrlen);
604}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000605#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +0000606
607
Guido van Rossum30a685f1991-06-27 15:51:29 +0000608/* s.listen(n) method */
609
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000610static object *
611sock_listen(s, args)
612 sockobject *s;
613 object *args;
614{
615 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000616 int res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000617 if (!getintarg(args, &backlog))
618 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000619 BGN_SAVE
Guido van Rossumb6775db1994-08-01 11:34:53 +0000620 if (backlog < 1)
621 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000622 res = listen(s->sock_fd, backlog);
623 END_SAVE
624 if (res < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000625 return socket_error();
626 INCREF(None);
627 return None;
628}
629
Guido van Rossum30a685f1991-06-27 15:51:29 +0000630
631/* s.makefile(mode) method.
632 Create a new open file object referring to a dupped version of
633 the socket's file descriptor. (The dup() call is necessary so
634 that the open file and socket objects may be closed independent
635 of each other.)
636 The mode argument specifies 'r' or 'w' passed to fdopen(). */
637
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000638static object *
Guido van Rossum30a685f1991-06-27 15:51:29 +0000639sock_makefile(s, args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000640 sockobject *s;
641 object *args;
642{
Guido van Rossum30a685f1991-06-27 15:51:29 +0000643 extern int fclose PROTO((FILE *));
Guido van Rossumff4949e1992-08-05 19:58:53 +0000644 char *mode;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000645 int fd;
646 FILE *fp;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000647 if (!getargs(args, "s", &mode))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000648 return NULL;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000649 if ((fd = dup(s->sock_fd)) < 0 ||
Guido van Rossumff4949e1992-08-05 19:58:53 +0000650 (fp = fdopen(fd, mode)) == NULL)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000651 return socket_error();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000652 return newopenfileobject(fp, "<socket>", mode, fclose);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000653}
654
655
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000656/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000657
658static object *
659sock_recv(s, args)
660 sockobject *s;
661 object *args;
662{
663 int len, n, flags;
664 object *buf;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000665 flags = 0;
666 if (!getargs(args, "i", &len)) {
Guido van Rossum30a685f1991-06-27 15:51:29 +0000667 err_clear();
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000668 if (!getargs(args, "(ii)", &len, &flags))
Guido van Rossum30a685f1991-06-27 15:51:29 +0000669 return NULL;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000670 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000671 buf = newsizedstringobject((char *) 0, len);
672 if (buf == NULL)
673 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000674 BGN_SAVE
Guido van Rossum30a685f1991-06-27 15:51:29 +0000675 n = recv(s->sock_fd, getstringvalue(buf), len, flags);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000676 END_SAVE
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000677 if (n < 0)
678 return socket_error();
679 if (resizestring(&buf, n) < 0)
680 return NULL;
681 return buf;
682}
683
Guido van Rossum30a685f1991-06-27 15:51:29 +0000684
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000685/* s.recvfrom(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000686
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000687static object *
688sock_recvfrom(s, args)
689 sockobject *s;
690 object *args;
691{
692 char addrbuf[256];
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000693 object *buf, *addr, *ret;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000694 int addrlen, len, n, flags;
695 flags = 0;
696 if (!getargs(args, "i", &len)) {
697 err_clear();
698 if (!getargs(args, "(ii)", &len, &flags))
699 return NULL;
700 }
Guido van Rossum18c9a4f1993-05-25 12:16:29 +0000701 if (!getsockaddrlen(s, &addrlen))
702 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000703 buf = newsizedstringobject((char *) 0, len);
Guido van Rossum18c9a4f1993-05-25 12:16:29 +0000704 if (buf == NULL)
705 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000706 BGN_SAVE
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000707 n = recvfrom(s->sock_fd, getstringvalue(buf), len, flags,
Guido van Rossumb376a4a1993-11-23 17:53:17 +0000708 (ANY *)addrbuf, &addrlen);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000709 END_SAVE
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000710 if (n < 0)
711 return socket_error();
712 if (resizestring(&buf, n) < 0)
713 return NULL;
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000714 addr = makesockaddr((struct sockaddr *)addrbuf, addrlen);
715 ret = mkvalue("OO", buf, addr);
716 XDECREF(addr);
717 XDECREF(buf);
718 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000719}
720
Guido van Rossum30a685f1991-06-27 15:51:29 +0000721
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000722/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000723
724static object *
725sock_send(s, args)
726 sockobject *s;
727 object *args;
728{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000729 char *buf;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000730 int len, n, flags;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000731 flags = 0;
Guido van Rossum234f9421993-06-17 12:35:49 +0000732 if (!getargs(args, "s#", &buf, &len)) {
Guido van Rossum30a685f1991-06-27 15:51:29 +0000733 err_clear();
Guido van Rossum234f9421993-06-17 12:35:49 +0000734 if (!getargs(args, "(s#i)", &buf, &len, &flags))
Guido van Rossum30a685f1991-06-27 15:51:29 +0000735 return NULL;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000736 }
Guido van Rossumff4949e1992-08-05 19:58:53 +0000737 BGN_SAVE
738 n = send(s->sock_fd, buf, len, flags);
739 END_SAVE
Guido van Rossum30a685f1991-06-27 15:51:29 +0000740 if (n < 0)
741 return socket_error();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000742 return newintobject((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000743}
744
745
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000746/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000747
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000748static object *
749sock_sendto(s, args)
750 sockobject *s;
751 object *args;
752{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000753 object *addro;
754 char *buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000755 struct sockaddr *addr;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000756 int addrlen, len, n, flags;
757 flags = 0;
758 if (!getargs(args, "(s#O)", &buf, &len, &addro)) {
759 err_clear();
760 if (!getargs(args, "(s#iO)", &buf, &len, &flags, &addro))
761 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000762 }
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000763 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000764 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000765 BGN_SAVE
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000766 n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000767 END_SAVE
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000768 if (n < 0)
769 return socket_error();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000770 return newintobject((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000771}
772
Guido van Rossum30a685f1991-06-27 15:51:29 +0000773
774/* s.shutdown(how) method */
775
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000776static object *
777sock_shutdown(s, args)
778 sockobject *s;
779 object *args;
780{
781 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000782 int res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000783 if (!getintarg(args, &how))
784 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000785 BGN_SAVE
786 res = shutdown(s->sock_fd, how);
787 END_SAVE
788 if (res < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000789 return socket_error();
790 INCREF(None);
791 return None;
792}
793
Guido van Rossum30a685f1991-06-27 15:51:29 +0000794
795/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000796
797static struct methodlist sock_methods[] = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000798 {"accept", (method)sock_accept},
799 {"allowbroadcast", (method)sock_allowbroadcast},
800 {"setsockopt", (method)sock_setsockopt},
801 {"getsockopt", (method)sock_getsockopt},
802 {"bind", (method)sock_bind},
803 {"close", (method)sock_close},
804 {"connect", (method)sock_connect},
805 {"fileno", (method)sock_fileno},
806 {"getsockname", (method)sock_getsockname},
807#ifdef HAVE_GETPEERNAME
808 {"getpeername", (method)sock_getpeername},
Guido van Rossum9575a441993-04-07 14:06:14 +0000809#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000810 {"listen", (method)sock_listen},
811 {"makefile", (method)sock_makefile},
812 {"recv", (method)sock_recv},
813 {"recvfrom", (method)sock_recvfrom},
814 {"send", (method)sock_send},
815 {"sendto", (method)sock_sendto},
816 {"shutdown", (method)sock_shutdown},
817 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000818};
819
Guido van Rossum30a685f1991-06-27 15:51:29 +0000820
821/* Deallocate a socket object in response to the last DECREF().
822 First close the file description. */
823
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000824static void
825sock_dealloc(s)
826 sockobject *s;
827{
828 (void) close(s->sock_fd);
829 DEL(s);
830}
831
Guido van Rossum30a685f1991-06-27 15:51:29 +0000832
833/* Return a socket object's named attribute. */
834
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000835static object *
836sock_getattr(s, name)
837 sockobject *s;
838 char *name;
839{
840 return findmethod(sock_methods, (object *) s, name);
841}
842
Guido van Rossum30a685f1991-06-27 15:51:29 +0000843
Guido van Rossumb6775db1994-08-01 11:34:53 +0000844/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +0000845
Guido van Rossumb6775db1994-08-01 11:34:53 +0000846static typeobject Socktype = {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000847 OB_HEAD_INIT(&Typetype)
848 0,
849 "socket",
850 sizeof(sockobject),
851 0,
Guido van Rossumb6775db1994-08-01 11:34:53 +0000852 (destructor)sock_dealloc, /*tp_dealloc*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000853 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000854 (getattrfunc)sock_getattr, /*tp_getattr*/
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000855 0, /*tp_setattr*/
856 0, /*tp_compare*/
857 0, /*tp_repr*/
858 0, /*tp_as_number*/
859 0, /*tp_as_sequence*/
860 0, /*tp_as_mapping*/
861};
862
Guido van Rossum30a685f1991-06-27 15:51:29 +0000863
Guido van Rossum81194471991-07-27 21:42:02 +0000864/* Python interface to gethostname(). */
865
866/*ARGSUSED*/
867static object *
868socket_gethostname(self, args)
869 object *self;
870 object *args;
871{
872 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000873 int res;
Guido van Rossum81194471991-07-27 21:42:02 +0000874 if (!getnoarg(args))
875 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000876 BGN_SAVE
877 res = gethostname(buf, (int) sizeof buf - 1);
878 END_SAVE
879 if (res < 0)
Guido van Rossum81194471991-07-27 21:42:02 +0000880 return socket_error();
881 buf[sizeof buf - 1] = '\0';
882 return newstringobject(buf);
883}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000884
885
Guido van Rossum30a685f1991-06-27 15:51:29 +0000886/* Python interface to gethostbyname(name). */
887
888/*ARGSUSED*/
889static object *
890socket_gethostbyname(self, args)
891 object *self;
892 object *args;
893{
894 object *name;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000895 struct sockaddr_in addrbuf;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000896 if (!getargs(args, "s", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +0000897 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000898 if (setipaddr(name, &addrbuf) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000899 return NULL;
900 return makeipaddr(&addrbuf);
901}
902
903
904/* Python interface to getservbyname(name).
905 This only returns the port number, since the other info is already
906 known or not useful (like the list of aliases). */
907
908/*ARGSUSED*/
909static object *
910socket_getservbyname(self, args)
911 object *self;
912 object *args;
913{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000914 char *name, *proto;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000915 struct servent *sp;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000916 if (!getargs(args, "(ss)", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +0000917 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000918 BGN_SAVE
919 sp = getservbyname(name, proto);
920 END_SAVE
Guido van Rossum30a685f1991-06-27 15:51:29 +0000921 if (sp == NULL) {
922 err_setstr(SocketError, "service/proto not found");
923 return NULL;
924 }
925 return newintobject((long) ntohs(sp->s_port));
926}
927
928
929/* Python interface to socket(family, type, proto).
930 The third (protocol) argument is optional.
931 Return a new socket object. */
932
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000933/*ARGSUSED*/
934static object *
935socket_socket(self, args)
936 object *self;
937 object *args;
938{
939 sockobject *s;
Guido van Rossum2a7178e1992-12-08 13:38:24 +0000940 int fd, family, type, proto;
941 proto = 0;
942 if (!getargs(args, "(ii)", &family, &type)) {
943 err_clear();
944 if (!getargs(args, "(iii)", &family, &type, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +0000945 return NULL;
946 }
Guido van Rossumff4949e1992-08-05 19:58:53 +0000947 BGN_SAVE
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000948 fd = socket(family, type, proto);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000949 END_SAVE
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000950 if (fd < 0)
951 return socket_error();
952 s = newsockobject(fd, family, type, proto);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000953 /* If the object can't be created, don't forget to close the
954 file descriptor again! */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000955 if (s == NULL)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000956 (void) close(fd);
Guido van Rossum81194471991-07-27 21:42:02 +0000957 /* From now on, ignore SIGPIPE and let the error checking
958 do the work. */
959 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000960 return (object *) s;
961}
962
Guido van Rossum30a685f1991-06-27 15:51:29 +0000963
Guido van Rossum2a7178e1992-12-08 13:38:24 +0000964/* Create a socket object from a numeric file description.
965 Useful e.g. if stdin is a socket.
966 Additional arguments as for socket(). */
967
968/*ARGSUSED*/
969static object *
970socket_fromfd(self, args)
971 object *self;
972 object *args;
973{
974 sockobject *s;
975 int fd, family, type, proto;
976 proto = 0;
977 if (!getargs(args, "(iii)", &fd, &family, &type)) {
978 err_clear();
979 if (!getargs(args, "(iiii)", &fd, &family, &type, &proto))
980 return NULL;
981 }
Guido van Rossum5f59d601992-12-14 16:59:51 +0000982 /* Dup the fd so it and the socket can be closed independently */
983 fd = dup(fd);
984 if (fd < 0)
985 return socket_error();
Guido van Rossum2a7178e1992-12-08 13:38:24 +0000986 s = newsockobject(fd, family, type, proto);
987 /* From now on, ignore SIGPIPE and let the error checking
988 do the work. */
989 (void) signal(SIGPIPE, SIG_IGN);
990 return (object *) s;
991}
992
993
Guido van Rossum30a685f1991-06-27 15:51:29 +0000994/* List of functions exported by this module. */
995
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000996static struct methodlist socket_methods[] = {
Guido van Rossum30a685f1991-06-27 15:51:29 +0000997 {"gethostbyname", socket_gethostbyname},
Guido van Rossum81194471991-07-27 21:42:02 +0000998 {"gethostname", socket_gethostname},
Guido van Rossum30a685f1991-06-27 15:51:29 +0000999 {"getservbyname", socket_getservbyname},
1000 {"socket", socket_socket},
Guido van Rossum2a7178e1992-12-08 13:38:24 +00001001 {"fromfd", socket_fromfd},
Guido van Rossum30a685f1991-06-27 15:51:29 +00001002 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001003};
1004
Guido van Rossum30a685f1991-06-27 15:51:29 +00001005
1006/* Convenience routine to export an integer value.
1007 For simplicity, errors (which are unlikely anyway) are ignored. */
1008
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001009static void
1010insint(d, name, value)
1011 object *d;
1012 char *name;
1013 int value;
1014{
1015 object *v = newintobject((long) value);
1016 if (v == NULL) {
1017 /* Don't bother reporting this error */
1018 err_clear();
1019 }
1020 else {
1021 dictinsert(d, name, v);
1022 DECREF(v);
1023 }
1024}
1025
Guido van Rossum30a685f1991-06-27 15:51:29 +00001026
1027/* Initialize this module.
1028 This is called when the first 'import socket' is done,
1029 via a table in config.c, if config.c is compiled with USE_SOCKET
1030 defined. */
1031
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001032void
1033initsocket()
1034{
1035 object *m, *d;
1036
1037 m = initmodule("socket", socket_methods);
1038 d = getmoduledict(m);
1039 SocketError = newstringobject("socket.error");
1040 if (SocketError == NULL || dictinsert(d, "error", SocketError) != 0)
1041 fatal("can't define socket.error");
1042 insint(d, "AF_INET", AF_INET);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001043#ifdef AF_UNIX
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001044 insint(d, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001045#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001046 insint(d, "SOCK_STREAM", SOCK_STREAM);
1047 insint(d, "SOCK_DGRAM", SOCK_DGRAM);
1048 insint(d, "SOCK_RAW", SOCK_RAW);
1049 insint(d, "SOCK_SEQPACKET", SOCK_SEQPACKET);
1050 insint(d, "SOCK_RDM", SOCK_RDM);
1051}