annotate Modules/socketmodule.c @ 1590:ac2e5cc29949 legacy-trunk

Added tests for missing host to open_http and open_gopher
author Guido van Rossum <guido@python.org>
date Mon, 18 Apr 1994 09:39:56 +0000
parents c914b10bf2ed
children 1940892d2fe3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /***********************************************************
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1207
diff changeset
2 Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1207
diff changeset
3 Amsterdam, The Netherlands.
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 All Rights Reserved
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 documentation for any purpose and without fee is hereby granted,
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 provided that the above copyright notice appear in all copies and that
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 both that copyright notice and this permission notice appear in
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 Centrum or CWI not be used in advertising or publicity pertaining to
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 distribution of the software without specific, written prior permission.
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 ******************************************************************/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 /* Socket module */
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 /*
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 This module provides an interface to Berkeley socket IPC.
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 Limitations:
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
32 - only AF_INET and AF_UNIX address families are supported
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
33 - no asynchronous I/O (but read polling: avail)
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
34 - no read/write operations (use send/recv or makefile instead)
896
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
35 - setsockopt() and getsockopt() only support integer options
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 Interface:
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
39 - socket.gethostname() --> host name (string)
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
40 - socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd')
309
382323f92fce Connection sockets now have the proper family/type/proto values.
Guido van Rossum <guido@python.org>
parents: 295
diff changeset
41 - socket.getservbyname(servername, protocolname) --> port number
382323f92fce Connection sockets now have the proper family/type/proto values.
Guido van Rossum <guido@python.org>
parents: 295
diff changeset
42 - socket.socket(family, type [, proto]) --> new socket object
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 - family and type constants from <socket.h> are accessed as socket.AF_INET etc.
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 - errors are reported as the exception socket.error
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 - an Internet socket address is a pair (hostname, port)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 where hostname can be anything recognized by gethostbyname()
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 (including the dd.dd.dd.dd notation) and port is in host byte order
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 - where a hostname is returned, the dd.dd.dd.dd notation is used
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 - a UNIX domain socket is a string specifying the pathname
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
51 Socket methods:
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
53 - s.accept() --> new socket object, sockaddr
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
54 - s.avail() --> boolean
896
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
55 - s.setsockopt(level, optname, flag) --> None
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
56 - s.getsockopt(level, optname) --> flag
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
57 - s.bind(sockaddr) --> None
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
58 - s.connect(sockaddr) --> None
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
59 - s.getsockname() --> sockaddr
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
60 - s.getpeername() --> sockaddr
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
61 - s.listen(n) --> None
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
62 - s.makefile(mode) --> file object
1293
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
63 - s.recv(nbytes [,flags]) --> string
1295
4773d5c3b5a4 * socketmodule.c: fix long-standing bug in recvfrom() -- addrlen
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
64 - s.recvfrom(nbytes [,flags]) --> string, sockaddr
1293
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
65 - s.send(string [,flags]) --> None
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
66 - s.sendto(string, [flags,] sockaddr) --> None
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
67 - s.shutdown(how) --> None
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
68 - s.close() --> None
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
69
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 */
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 #include "allobjects.h"
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 #include "modsupport.h"
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
74 #include "ceval.h"
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 900
diff changeset
76 #include "myselect.h" /* Implies <sys/types.h>, <sys/time.h>, <sys/param.h> */
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 900
diff changeset
77
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
78 #include <signal.h>
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 #include <sys/socket.h>
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 #include <netinet/in.h>
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 #include <sys/un.h>
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 #include <netdb.h>
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
1271
2919b448e385 * Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
84 #ifdef i860
2919b448e385 * Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
85 /* Cray APP doesn't have getpeername() */
2919b448e385 * Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
86 #define NO_GETPEERNAME
2919b448e385 * Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
87 #endif
2919b448e385 * Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
88
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
89
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
90 /* Global variable holding the exception type for errors detected
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
91 by this module (but not argument type or memory errors, etc.). */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
92
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
93 static object *SocketError;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
94
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
95
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
96 /* Convenience function to raise an error according to errno
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
97 and return a NULL pointer from a function. */
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 socket_error()
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 return err_errno(SocketError);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
104
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
105
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
106 /* The object holding a socket. It holds some extra information,
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
107 like the address family, which is used to decode socket address
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
108 arguments properly. */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
109
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 typedef struct {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 OB_HEAD
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
112 int sock_fd; /* Socket file descriptor */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
113 int sock_family; /* Address family, e.g., AF_INET */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
114 int sock_type; /* Socket type, e.g., SOCK_STREAM */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
115 int sock_proto; /* Protocol type, usually 0 */
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 } sockobject;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
117
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
118
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
119 /* A forward reference to the Socktype type object.
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
120 The Socktype variable contains pointers to various functions,
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
121 some of which call newsocobject(), which uses Socktype, so
374
70a18a6689d4 Make Socktype global so old and new compilers will like it.
Guido van Rossum <guido@python.org>
parents: 324
diff changeset
122 there has to be a circular reference. */
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
123
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 extern typeobject Socktype; /* Forward */
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
125
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
126
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
127 /* Create a new socket object.
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
128 This just creates the object and initializes it.
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
129 If the creation fails, return NULL and set an exception (implicit
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
130 in NEWOBJ()). */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
131
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 static sockobject *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 newsockobject(fd, family, type, proto)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 int fd, family, type, proto;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 s = NEWOBJ(sockobject, &Socktype);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 if (s != NULL) {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 s->sock_fd = fd;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 s->sock_family = family;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 s->sock_type = type;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 s->sock_proto = proto;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 return s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
146
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
147
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
148 /* Convert a string specifying a host name or one of a few symbolic
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
149 names to a numeric IP address. This usually calls gethostbyname()
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
150 to do the work; the names "" and "<broadcast>" are special.
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
151 Return the length (should always be 4 bytes), or negative if
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
152 an error occurred; then an exception is raised. */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
153
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 static int
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
155 setipaddr(name, addr_ret)
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 char *name;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 struct sockaddr_in *addr_ret;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 struct hostent *hp;
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
160 int d1, d2, d3, d4;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
161 char ch;
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
162
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
163 if (name[0] == '\0') {
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 addr_ret->sin_addr.s_addr = INADDR_ANY;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 return 4;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 }
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
167 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 addr_ret->sin_addr.s_addr = INADDR_BROADCAST;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 return 4;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 }
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
171 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
172 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
173 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
174 addr_ret->sin_addr.s_addr = htonl(
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
175 ((long) d1 << 24) | ((long) d2 << 16) |
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
176 ((long) d3 << 8) | ((long) d4 << 0));
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
177 return 4;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
178 }
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
179 BGN_SAVE
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 hp = gethostbyname(name);
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
181 END_SAVE
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 if (hp == NULL) {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 err_setstr(SocketError, "host not found");
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 return -1;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 memcpy((char *) &addr_ret->sin_addr, hp->h_addr, hp->h_length);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 return hp->h_length;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
189
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
190
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
191 /* Create a string object representing an IP address.
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
192 This is always a string of the form 'dd.dd.dd.dd' (with variable
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
193 size numbers). */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
194
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
195 static object *
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
196 makeipaddr(addr)
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
197 struct sockaddr_in *addr;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
198 {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
199 long x = ntohl(addr->sin_addr.s_addr);
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
200 char buf[100];
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
201 sprintf(buf, "%d.%d.%d.%d",
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
202 (int) (x>>24) & 0xff, (int) (x>>16) & 0xff,
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
203 (int) (x>> 8) & 0xff, (int) (x>> 0) & 0xff);
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
204 return newstringobject(buf);
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
205 }
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
206
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
207
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
208 /* Create an object representing the given socket address,
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
209 suitable for passing it back to bind(), connect() etc.
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
210 The family field of the sockaddr structure is inspected
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
211 to determine what kind of address it really is. */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
212
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 /*ARGSUSED*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 makesockaddr(addr, addrlen)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 struct sockaddr *addr;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 int addrlen;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 {
925
61143bf03b44 Sometimes, recvfrom() returns an empty address; this is not an error.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
219 if (addrlen == 0) {
61143bf03b44 Sometimes, recvfrom() returns an empty address; this is not an error.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
220 /* No address -- may be recvfrom() from known socket */
61143bf03b44 Sometimes, recvfrom() returns an empty address; this is not an error.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
221 INCREF(None);
61143bf03b44 Sometimes, recvfrom() returns an empty address; this is not an error.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
222 return None;
61143bf03b44 Sometimes, recvfrom() returns an empty address; this is not an error.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
223 }
61143bf03b44 Sometimes, recvfrom() returns an empty address; this is not an error.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
224
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
225 switch (addr->sa_family) {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
226
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
227 case AF_INET:
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
228 {
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 struct sockaddr_in *a = (struct sockaddr_in *) addr;
1207
b564bd4e57e1 * ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents: 1132
diff changeset
230 object *addr = makeipaddr(a);
b564bd4e57e1 * ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents: 1132
diff changeset
231 object *ret = mkvalue("Oi", addr, ntohs(a->sin_port));
b564bd4e57e1 * ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents: 1132
diff changeset
232 XDECREF(addr);
b564bd4e57e1 * ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents: 1132
diff changeset
233 return ret;
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 }
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
235
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
236 case AF_UNIX:
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
237 {
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 struct sockaddr_un *a = (struct sockaddr_un *) addr;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 return newstringobject(a->sun_path);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 }
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
241
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
242 /* More cases here... */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
243
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
244 default:
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
245 err_setstr(SocketError, "return unknown socket address type");
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
246 return NULL;
925
61143bf03b44 Sometimes, recvfrom() returns an empty address; this is not an error.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
247
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
248 }
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
250
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
251
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
252 /* Parse a socket address argument according to the socket object's
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
253 address family. Return 1 if the address was in the proper format,
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
254 0 of not. The address is returned through addr_ret, its length
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
255 through len_ret. */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
256
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 static int
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 getsockaddrarg(s, args, addr_ret, len_ret)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 struct sockaddr **addr_ret;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 int *len_ret;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 {
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
264 switch (s->sock_family) {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
265
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
266 case AF_UNIX:
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
267 {
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 static struct sockaddr_un addr;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
269 char *path;
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 int len;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
271 if (!getargs(args, "s#", &path, &len))
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 return 0;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
273 if (len > sizeof addr.sun_path) {
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
274 err_setstr(SocketError, "AF_UNIX path too long");
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
275 return 0;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
276 }
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 addr.sun_family = AF_UNIX;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
278 memcpy(addr.sun_path, path, len);
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 *addr_ret = (struct sockaddr *) &addr;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 *len_ret = len + sizeof addr.sun_family;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 return 1;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
283
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
284 case AF_INET:
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
285 {
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 static struct sockaddr_in addr;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
287 char *host;
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 int port;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
289 if (!getargs(args, "(si)", &host, &port))
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 return 0;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
291 if (setipaddr(host, &addr) < 0)
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 return 0;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 addr.sin_family = AF_INET;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 addr.sin_port = htons(port);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 *addr_ret = (struct sockaddr *) &addr;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 *len_ret = sizeof addr;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 return 1;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
299
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
300 /* More cases here... */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
301
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
302 default:
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
303 err_setstr(SocketError, "getsockaddrarg: bad family");
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
304 return 0;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
305
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
306 }
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
308
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
309
900
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
310 /* Get the address length according to the socket object's address family.
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
311 Return 1 if the family is known, 0 otherwise. The length is returned
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
312 through len_ret. */
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
313
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
314 static int
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
315 getsockaddrlen(s, len_ret)
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
316 sockobject *s;
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
317 int *len_ret;
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
318 {
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
319 switch (s->sock_family) {
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
320
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
321 case AF_UNIX:
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
322 {
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
323 *len_ret = sizeof (struct sockaddr_un);
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
324 return 1;
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
325 }
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
326
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
327 case AF_INET:
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
328 {
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
329 *len_ret = sizeof (struct sockaddr_in);
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
330 return 1;
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
331 }
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
332
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
333 /* More cases here... */
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
334
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
335 default:
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
336 err_setstr(SocketError, "getsockaddrarg: bad family");
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
337 return 0;
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
338
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
339 }
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
340 }
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
341
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
342
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
343 /* s.accept() method */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
344
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 sock_accept(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 char addrbuf[256];
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 int addrlen, newfd;
1207
b564bd4e57e1 * ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents: 1132
diff changeset
352 object *sock, *addr, *res;
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 if (!getnoarg(args))
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 return NULL;
900
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
355 if (!getsockaddrlen(s, &addrlen))
58a7438c2e7e Some UNIX types want the exact size of the address structure
Guido van Rossum <guido@python.org>
parents: 896
diff changeset
356 return NULL;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
357 BGN_SAVE
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
359 END_SAVE
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 if (newfd < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 return socket_error();
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
362 /* Create the new object with unspecified family,
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
363 to avoid calls to bind() etc. on it. */
1207
b564bd4e57e1 * ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents: 1132
diff changeset
364 sock = (object *) newsockobject(newfd,
b564bd4e57e1 * ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents: 1132
diff changeset
365 s->sock_family,
b564bd4e57e1 * ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents: 1132
diff changeset
366 s->sock_type,
b564bd4e57e1 * ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents: 1132
diff changeset
367 s->sock_proto);
b564bd4e57e1 * ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents: 1132
diff changeset
368 if (sock == NULL)
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 close(newfd);
1207
b564bd4e57e1 * ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents: 1132
diff changeset
370 addr = makesockaddr((struct sockaddr *) addrbuf, addrlen);
b564bd4e57e1 * ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents: 1132
diff changeset
371 res = mkvalue("OO", sock, addr);
b564bd4e57e1 * ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents: 1132
diff changeset
372 XDECREF(sock);
b564bd4e57e1 * ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents: 1132
diff changeset
373 XDECREF(addr);
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 return res;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
376
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
377
396
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
378 /* s.allowbroadcast() method */
896
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
379 /* XXX obsolete -- will disappear in next release */
396
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
380
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
381 static object *
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
382 sock_allowbroadcast(s, args)
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
383 sockobject *s;
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
384 object *args;
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
385 {
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
386 int flag;
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
387 int res;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
388 if (!getargs(args, "i", &flag))
396
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
389 return NULL;
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
390 res = setsockopt(s->sock_fd, SOL_SOCKET, SO_BROADCAST,
1413
c914b10bf2ed * timemodule.c: Add hack for Solaris 2.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
391 (ANY *)&flag, sizeof flag);
396
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
392 if (res < 0)
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
393 return socket_error();
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
394 INCREF(None);
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
395 return None;
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
396 }
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
397
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
398
1039
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
399 /* s.setsockopt() method.
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
400 With an integer third argument, sets an integer option.
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
401 With a string third argument, sets an option from a buffer;
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
402 use optional built-in module 'struct' to encode the string. */
896
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
403
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
404 static object *
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
405 sock_setsockopt(s, args)
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
406 sockobject *s;
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
407 object *args;
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
408 {
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
409 int level;
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
410 int optname;
1039
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
411 int res;
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
412 char *buf;
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
413 int buflen;
896
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
414 int flag;
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
415
1039
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
416 if (getargs(args, "(iii)", &level, &optname, &flag)) {
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
417 buf = (char *) &flag;
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
418 buflen = sizeof flag;
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
419 }
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
420 else {
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
421 err_clear();
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
422 if (!getargs(args, "(iis#)", &level, &optname, &buf, &buflen))
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
423 return NULL;
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
424 }
1413
c914b10bf2ed * timemodule.c: Add hack for Solaris 2.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
425 res = setsockopt(s->sock_fd, level, optname, (ANY *)buf, buflen);
896
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
426 if (res < 0)
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
427 return socket_error();
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
428 INCREF(None);
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
429 return None;
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
430 }
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
431
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
432
1039
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
433 /* s.getsockopt() method.
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
434 With two arguments, retrieves an integer option.
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
435 With a third integer argument, retrieves a string buffer of that size;
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
436 use optional built-in module 'struct' to decode the string. */
896
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
437
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
438 static object *
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
439 sock_getsockopt(s, args)
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
440 sockobject *s;
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
441 object *args;
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
442 {
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
443 int level;
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
444 int optname;
1039
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
445 int res;
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
446 object *buf;
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
447 int buflen;
896
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
448 int flag;
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
449
1039
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
450 if (getargs(args, "(ii)", &level, &optname)) {
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
451 int flag = 0;
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
452 int flagsize = sizeof flag;
1413
c914b10bf2ed * timemodule.c: Add hack for Solaris 2.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
453 res = getsockopt(s->sock_fd, level, optname,
c914b10bf2ed * timemodule.c: Add hack for Solaris 2.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
454 (ANY *)&flag, &flagsize);
1039
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
455 if (res < 0)
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
456 return socket_error();
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
457 return newintobject(flag);
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
458 }
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
459 err_clear();
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
460 if (!getargs(args, "(iii)", &level, &optname, &buflen))
896
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
461 return NULL;
1039
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
462 if (buflen <= 0 || buflen > 1024) {
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
463 err_setstr(SocketError, "getsockopt buflen out of range");
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
464 return NULL;
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
465 }
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
466 buf = newsizedstringobject((char *)NULL, buflen);
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
467 if (buf == NULL)
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
468 return NULL;
1413
c914b10bf2ed * timemodule.c: Add hack for Solaris 2.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
469 res = getsockopt(s->sock_fd, level, optname,
c914b10bf2ed * timemodule.c: Add hack for Solaris 2.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
470 (ANY *)getstringvalue(buf), &buflen);
1039
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
471 if (res < 0) {
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
472 DECREF(buf);
896
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
473 return socket_error();
1039
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
474 }
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
475 resizestring(&buf, buflen);
6aa0ee14ac2e Made struct a standard built-in module; added string args to {set,get}sockopt;
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
476 return buf;
896
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
477 }
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
478
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
479
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
480 /* s.avail() method */
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
481
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
482 static object *
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
483 sock_avail(s, args)
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
484 sockobject *s;
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
485 object *args;
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
486 {
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
487 struct timeval timeout;
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
488 fd_set readers;
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
489 int n;
729
82dd081f380d Lint fixes.
Guido van Rossum <guido@python.org>
parents: 641
diff changeset
490 if (!getnoarg(args))
82dd081f380d Lint fixes.
Guido van Rossum <guido@python.org>
parents: 641
diff changeset
491 return NULL;
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
492 timeout.tv_sec = 0;
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
493 timeout.tv_usec = 0;
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
494 FD_ZERO(&readers);
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
495 FD_SET(s->sock_fd, &readers);
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
496 n = select(s->sock_fd+1, &readers, (fd_set *)0, (fd_set *)0, &timeout);
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
497 if (n < 0)
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
498 return socket_error();
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
499 return newintobject((long) (n != 0));
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
500 }
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
501
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
502
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
503 /* s.bind(sockaddr) method */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
504
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
505 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
506 sock_bind(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
507 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
508 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
509 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
510 struct sockaddr *addr;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
511 int addrlen;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
512 int res;
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
513 if (!getsockaddrarg(s, args, &addr, &addrlen))
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
514 return NULL;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
515 BGN_SAVE
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
516 res = bind(s->sock_fd, addr, addrlen);
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
517 END_SAVE
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
518 if (res < 0)
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
519 return socket_error();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
520 INCREF(None);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 return None;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
522 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
523
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
524
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
525 /* s.close() method.
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
526 Set the file descriptor to -1 so operations tried subsequently
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
527 will surely fail. */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
528
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
529 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
530 sock_close(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
531 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
533 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
534 if (!getnoarg(args))
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
535 return NULL;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
536 BGN_SAVE
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
537 (void) close(s->sock_fd);
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
538 END_SAVE
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
539 s->sock_fd = -1;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
540 INCREF(None);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
541 return None;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
542 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
543
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
544
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
545 /* s.connect(sockaddr) method */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
546
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
547 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
548 sock_connect(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
549 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
550 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
551 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 struct sockaddr *addr;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
553 int addrlen;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
554 int res;
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
555 if (!getsockaddrarg(s, args, &addr, &addrlen))
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
556 return NULL;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
557 BGN_SAVE
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
558 res = connect(s->sock_fd, addr, addrlen);
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
559 END_SAVE
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
560 if (res < 0)
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
561 return socket_error();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
562 INCREF(None);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
563 return None;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
564 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
565
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
566
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 900
diff changeset
567 /* s.fileno() method */
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 900
diff changeset
568
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 900
diff changeset
569 static object *
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 900
diff changeset
570 sock_fileno(s, args)
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 900
diff changeset
571 sockobject *s;
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 900
diff changeset
572 object *args;
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 900
diff changeset
573 {
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 900
diff changeset
574 if (!getnoarg(args))
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 900
diff changeset
575 return NULL;
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 900
diff changeset
576 return newintobject((long) s->sock_fd);
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 900
diff changeset
577 }
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 900
diff changeset
578
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 900
diff changeset
579
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
580 /* s.getsockname() method */
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
581
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
582 static object *
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
583 sock_getsockname(s, args)
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
584 sockobject *s;
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
585 object *args;
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
586 {
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
587 char addrbuf[256];
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
588 int addrlen, res;
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
589 if (!getnoarg(args))
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
590 return NULL;
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
591 if (!getsockaddrlen(s, &addrlen))
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
592 return NULL;
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
593 BGN_SAVE
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
594 res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
595 END_SAVE
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
596 if (res < 0)
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
597 return socket_error();
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
598 return makesockaddr((struct sockaddr *) addrbuf, addrlen);
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
599 }
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
600
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
601
1271
2919b448e385 * Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
602 #ifndef NO_GETPEERNAME
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
603 /* s.getpeername() method */
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
604
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
605 static object *
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
606 sock_getpeername(s, args)
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
607 sockobject *s;
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
608 object *args;
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
609 {
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
610 char addrbuf[256];
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
611 int addrlen, res;
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
612 if (!getnoarg(args))
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
613 return NULL;
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
614 if (!getsockaddrlen(s, &addrlen))
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
615 return NULL;
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
616 BGN_SAVE
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
617 res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
618 END_SAVE
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
619 if (res < 0)
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
620 return socket_error();
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
621 return makesockaddr((struct sockaddr *) addrbuf, addrlen);
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
622 }
1271
2919b448e385 * Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
623 #endif
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
624
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
625
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
626 /* s.listen(n) method */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
627
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
628 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
629 sock_listen(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
630 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
631 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
632 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
633 int backlog;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
634 int res;
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
635 if (!getintarg(args, &backlog))
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
636 return NULL;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
637 BGN_SAVE
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
638 res = listen(s->sock_fd, backlog);
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
639 END_SAVE
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
640 if (res < 0)
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
641 return socket_error();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
642 INCREF(None);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
643 return None;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
644 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
645
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
646
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
647 /* s.makefile(mode) method.
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
648 Create a new open file object referring to a dupped version of
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
649 the socket's file descriptor. (The dup() call is necessary so
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
650 that the open file and socket objects may be closed independent
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
651 of each other.)
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
652 The mode argument specifies 'r' or 'w' passed to fdopen(). */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
653
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
654 static object *
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
655 sock_makefile(s, args)
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
656 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
657 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
658 {
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
659 extern int fclose PROTO((FILE *));
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
660 char *mode;
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
661 int fd;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
662 FILE *fp;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
663 if (!getargs(args, "s", &mode))
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
664 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
665 if ((fd = dup(s->sock_fd)) < 0 ||
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
666 (fp = fdopen(fd, mode)) == NULL)
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
667 return socket_error();
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
668 return newopenfileobject(fp, "<socket>", mode, fclose);
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
669 }
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
670
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
671
1293
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
672 /* s.recv(nbytes [,flags]) method */
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
673
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
674 static object *
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
675 sock_recv(s, args)
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
676 sockobject *s;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
677 object *args;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
678 {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
679 int len, n, flags;
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
680 object *buf;
1293
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
681 flags = 0;
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
682 if (!getargs(args, "i", &len)) {
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
683 err_clear();
1293
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
684 if (!getargs(args, "(ii)", &len, &flags))
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
685 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
686 }
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
687 buf = newsizedstringobject((char *) 0, len);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
688 if (buf == NULL)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
689 return NULL;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
690 BGN_SAVE
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
691 n = recv(s->sock_fd, getstringvalue(buf), len, flags);
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
692 END_SAVE
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
693 if (n < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
694 return socket_error();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
695 if (resizestring(&buf, n) < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
696 return NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
697 return buf;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
698 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
699
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
700
1293
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
701 /* s.recvfrom(nbytes [,flags]) method */
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
702
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
703 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
704 sock_recvfrom(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
705 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
706 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
707 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
708 char addrbuf[256];
1207
b564bd4e57e1 * ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents: 1132
diff changeset
709 object *buf, *addr, *ret;
1293
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
710 int addrlen, len, n, flags;
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
711 flags = 0;
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
712 if (!getargs(args, "i", &len)) {
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
713 err_clear();
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
714 if (!getargs(args, "(ii)", &len, &flags))
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
715 return NULL;
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
716 }
1295
4773d5c3b5a4 * socketmodule.c: fix long-standing bug in recvfrom() -- addrlen
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
717 if (!getsockaddrlen(s, &addrlen))
4773d5c3b5a4 * socketmodule.c: fix long-standing bug in recvfrom() -- addrlen
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
718 return NULL;
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
719 buf = newsizedstringobject((char *) 0, len);
1295
4773d5c3b5a4 * socketmodule.c: fix long-standing bug in recvfrom() -- addrlen
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
720 if (buf == NULL)
4773d5c3b5a4 * socketmodule.c: fix long-standing bug in recvfrom() -- addrlen
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
721 return NULL;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
722 BGN_SAVE
1293
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
723 n = recvfrom(s->sock_fd, getstringvalue(buf), len, flags,
1413
c914b10bf2ed * timemodule.c: Add hack for Solaris 2.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
724 (ANY *)addrbuf, &addrlen);
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
725 END_SAVE
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
726 if (n < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
727 return socket_error();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
728 if (resizestring(&buf, n) < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
729 return NULL;
1207
b564bd4e57e1 * ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents: 1132
diff changeset
730 addr = makesockaddr((struct sockaddr *)addrbuf, addrlen);
b564bd4e57e1 * ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents: 1132
diff changeset
731 ret = mkvalue("OO", buf, addr);
b564bd4e57e1 * ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents: 1132
diff changeset
732 XDECREF(addr);
b564bd4e57e1 * ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents: 1132
diff changeset
733 XDECREF(buf);
b564bd4e57e1 * ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents: 1132
diff changeset
734 return ret;
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
735 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
736
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
737
1293
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
738 /* s.send(data [,flags]) method */
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
739
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
740 static object *
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
741 sock_send(s, args)
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
742 sockobject *s;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
743 object *args;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
744 {
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
745 char *buf;
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
746 int len, n, flags;
1293
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
747 flags = 0;
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1295
diff changeset
748 if (!getargs(args, "s#", &buf, &len)) {
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
749 err_clear();
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1295
diff changeset
750 if (!getargs(args, "(s#i)", &buf, &len, &flags))
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
751 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
752 }
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
753 BGN_SAVE
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
754 n = send(s->sock_fd, buf, len, flags);
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
755 END_SAVE
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
756 if (n < 0)
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
757 return socket_error();
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
758 INCREF(None);
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
759 return None;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
760 }
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
761
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
762
1293
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
763 /* s.sendto(data, [flags,] sockaddr) method */
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
764
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
765 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
766 sock_sendto(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
767 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
768 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
769 {
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
770 object *addro;
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
771 char *buf;
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
772 struct sockaddr *addr;
1293
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
773 int addrlen, len, n, flags;
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
774 flags = 0;
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
775 if (!getargs(args, "(s#O)", &buf, &len, &addro)) {
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
776 err_clear();
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
777 if (!getargs(args, "(s#iO)", &buf, &len, &flags, &addro))
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
778 return NULL;
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
779 }
1293
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
780 if (!getsockaddrarg(s, addro, &addr, &addrlen))
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
781 return NULL;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
782 BGN_SAVE
1293
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
783 n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
784 END_SAVE
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
785 if (n < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
786 return socket_error();
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
787 INCREF(None);
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
788 return None;
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
789 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
790
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
791
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
792 /* s.shutdown(how) method */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
793
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
794 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
795 sock_shutdown(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
796 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
797 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
798 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
799 int how;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
800 int res;
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
801 if (!getintarg(args, &how))
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
802 return NULL;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
803 BGN_SAVE
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
804 res = shutdown(s->sock_fd, how);
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
805 END_SAVE
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
806 if (res < 0)
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
807 return socket_error();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
808 INCREF(None);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
809 return None;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
810 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
811
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
812
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
813 /* List of methods for socket objects */
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
814
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
815 static struct methodlist sock_methods[] = {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
816 {"accept", sock_accept},
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
817 {"avail", sock_avail},
396
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
818 {"allowbroadcast", sock_allowbroadcast},
896
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
819 {"setsockopt", sock_setsockopt},
6459e2f21bf6 Added {get,set}sockopt(). allowbroadcast is now obsolete.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
820 {"getsockopt", sock_getsockopt},
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
821 {"bind", sock_bind},
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
822 {"close", sock_close},
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
823 {"connect", sock_connect},
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 900
diff changeset
824 {"fileno", sock_fileno},
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
825 {"getsockname", sock_getsockname},
1271
2919b448e385 * Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
826 #ifndef NO_GETPEERNAME
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1039
diff changeset
827 {"getpeername", sock_getpeername},
1271
2919b448e385 * Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
828 #endif
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
829 {"listen", sock_listen},
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
830 {"makefile", sock_makefile},
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
831 {"recv", sock_recv},
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
832 {"recvfrom", sock_recvfrom},
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
833 {"send", sock_send},
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
834 {"sendto", sock_sendto},
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
835 {"shutdown", sock_shutdown},
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
836 {NULL, NULL} /* sentinel */
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
837 };
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
838
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
839
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
840 /* Deallocate a socket object in response to the last DECREF().
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
841 First close the file description. */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
842
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
843 static void
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
844 sock_dealloc(s)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
845 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
846 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
847 (void) close(s->sock_fd);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
848 DEL(s);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
849 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
850
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
851
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
852 /* Return a socket object's named attribute. */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
853
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
854 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
855 sock_getattr(s, name)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
856 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
857 char *name;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
858 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
859 return findmethod(sock_methods, (object *) s, name);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
860 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
861
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
862
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
863 /* Type object for socket objects.
374
70a18a6689d4 Make Socktype global so old and new compilers will like it.
Guido van Rossum <guido@python.org>
parents: 324
diff changeset
864 XXX This should be static, but some compilers don't grok the
70a18a6689d4 Make Socktype global so old and new compilers will like it.
Guido van Rossum <guido@python.org>
parents: 324
diff changeset
865 XXX forward reference to it in that case... */
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
866
374
70a18a6689d4 Make Socktype global so old and new compilers will like it.
Guido van Rossum <guido@python.org>
parents: 324
diff changeset
867 typeobject Socktype = {
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
868 OB_HEAD_INIT(&Typetype)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
869 0,
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
870 "socket",
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
871 sizeof(sockobject),
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
872 0,
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
873 sock_dealloc, /*tp_dealloc*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
874 0, /*tp_print*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
875 sock_getattr, /*tp_getattr*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
876 0, /*tp_setattr*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
877 0, /*tp_compare*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
878 0, /*tp_repr*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
879 0, /*tp_as_number*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
880 0, /*tp_as_sequence*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
881 0, /*tp_as_mapping*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
882 };
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
883
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
884
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
885 /* Python interface to gethostname(). */
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
886
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
887 /*ARGSUSED*/
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
888 static object *
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
889 socket_gethostname(self, args)
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
890 object *self;
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
891 object *args;
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
892 {
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
893 char buf[1024];
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
894 int res;
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
895 if (!getnoarg(args))
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
896 return NULL;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
897 BGN_SAVE
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
898 res = gethostname(buf, (int) sizeof buf - 1);
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
899 END_SAVE
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
900 if (res < 0)
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
901 return socket_error();
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
902 buf[sizeof buf - 1] = '\0';
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
903 return newstringobject(buf);
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
904 }
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
905
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
906
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
907 /* Python interface to gethostbyname(name). */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
908
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
909 /*ARGSUSED*/
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
910 static object *
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
911 socket_gethostbyname(self, args)
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
912 object *self;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
913 object *args;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
914 {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
915 object *name;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
916 struct sockaddr_in addrbuf;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
917 if (!getargs(args, "s", &name))
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
918 return NULL;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
919 if (setipaddr(name, &addrbuf) < 0)
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
920 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
921 return makeipaddr(&addrbuf);
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
922 }
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
923
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
924
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
925 /* Python interface to getservbyname(name).
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
926 This only returns the port number, since the other info is already
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
927 known or not useful (like the list of aliases). */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
928
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
929 /*ARGSUSED*/
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
930 static object *
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
931 socket_getservbyname(self, args)
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
932 object *self;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
933 object *args;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
934 {
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
935 char *name, *proto;
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
936 struct servent *sp;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
937 if (!getargs(args, "(ss)", &name, &proto))
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
938 return NULL;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
939 BGN_SAVE
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
940 sp = getservbyname(name, proto);
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
941 END_SAVE
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
942 if (sp == NULL) {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
943 err_setstr(SocketError, "service/proto not found");
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
944 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
945 }
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
946 return newintobject((long) ntohs(sp->s_port));
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
947 }
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
948
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
949
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
950 /* Python interface to socket(family, type, proto).
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
951 The third (protocol) argument is optional.
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
952 Return a new socket object. */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
953
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
954 /*ARGSUSED*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
955 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
956 socket_socket(self, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
957 object *self;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
958 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
959 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
960 sockobject *s;
1099
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
961 int fd, family, type, proto;
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
962 proto = 0;
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
963 if (!getargs(args, "(ii)", &family, &type)) {
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
964 err_clear();
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
965 if (!getargs(args, "(iii)", &family, &type, &proto))
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
966 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
967 }
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
968 BGN_SAVE
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
969 fd = socket(family, type, proto);
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 925
diff changeset
970 END_SAVE
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
971 if (fd < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
972 return socket_error();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
973 s = newsockobject(fd, family, type, proto);
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
974 /* If the object can't be created, don't forget to close the
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
975 file descriptor again! */
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
976 if (s == NULL)
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
977 (void) close(fd);
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
978 /* From now on, ignore SIGPIPE and let the error checking
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
979 do the work. */
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
980 (void) signal(SIGPIPE, SIG_IGN);
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
981 return (object *) s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
982 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
983
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
984
1099
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
985 /* Create a socket object from a numeric file description.
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
986 Useful e.g. if stdin is a socket.
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
987 Additional arguments as for socket(). */
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
988
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
989 /*ARGSUSED*/
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
990 static object *
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
991 socket_fromfd(self, args)
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
992 object *self;
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
993 object *args;
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
994 {
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
995 sockobject *s;
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
996 int fd, family, type, proto;
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
997 proto = 0;
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
998 if (!getargs(args, "(iii)", &fd, &family, &type)) {
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
999 err_clear();
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
1000 if (!getargs(args, "(iiii)", &fd, &family, &type, &proto))
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
1001 return NULL;
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
1002 }
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1099
diff changeset
1003 /* Dup the fd so it and the socket can be closed independently */
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1099
diff changeset
1004 fd = dup(fd);
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1099
diff changeset
1005 if (fd < 0)
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1099
diff changeset
1006 return socket_error();
1099
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
1007 s = newsockobject(fd, family, type, proto);
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
1008 /* From now on, ignore SIGPIPE and let the error checking
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
1009 do the work. */
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
1010 (void) signal(SIGPIPE, SIG_IGN);
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
1011 return (object *) s;
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
1012 }
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
1013
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
1014
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
1015 /* List of functions exported by this module. */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
1016
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1017 static struct methodlist socket_methods[] = {
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
1018 {"gethostbyname", socket_gethostbyname},
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
1019 {"gethostname", socket_gethostname},
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
1020 {"getservbyname", socket_getservbyname},
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
1021 {"socket", socket_socket},
1099
3dd7b8255619 socketmodule.c: added socket.fromfd(fd, family, type, [proto]);
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
1022 {"fromfd", socket_fromfd},
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
1023 {NULL, NULL} /* Sentinel */
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1024 };
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1025
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
1026
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
1027 /* Convenience routine to export an integer value.
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
1028 For simplicity, errors (which are unlikely anyway) are ignored. */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
1029
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1030 static void
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1031 insint(d, name, value)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1032 object *d;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1033 char *name;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1034 int value;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1035 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1036 object *v = newintobject((long) value);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1037 if (v == NULL) {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1038 /* Don't bother reporting this error */
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1039 err_clear();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1040 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1041 else {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1042 dictinsert(d, name, v);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1043 DECREF(v);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1044 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1045 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1046
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
1047
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
1048 /* Initialize this module.
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
1049 This is called when the first 'import socket' is done,
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
1050 via a table in config.c, if config.c is compiled with USE_SOCKET
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
1051 defined. */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
1052
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1053 void
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1054 initsocket()
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1055 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1056 object *m, *d;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1057
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1058 m = initmodule("socket", socket_methods);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1059 d = getmoduledict(m);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1060 SocketError = newstringobject("socket.error");
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1061 if (SocketError == NULL || dictinsert(d, "error", SocketError) != 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1062 fatal("can't define socket.error");
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1063 insint(d, "AF_INET", AF_INET);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1064 insint(d, "AF_UNIX", AF_UNIX);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1065 insint(d, "SOCK_STREAM", SOCK_STREAM);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1066 insint(d, "SOCK_DGRAM", SOCK_DGRAM);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1067 insint(d, "SOCK_RAW", SOCK_RAW);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1068 insint(d, "SOCK_SEQPACKET", SOCK_SEQPACKET);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1069 insint(d, "SOCK_RDM", SOCK_RDM);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1070 }