annotate Modules/socketmodule.c @ 641:b162b27400bc legacy-trunk

Cal get*Str*arg() instead of get*str*arg(), until the code is fixed.
author Guido van Rossum <guido@python.org>
date Mon, 27 Jan 1992 16:49:54 +0000
parents a5f0b8390c87
children 82dd081f380d
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 /***********************************************************
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 Netherlands.
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
641
b162b27400bc Cal get*Str*arg() instead of get*str*arg(), until the code is fixed.
Guido van Rossum <guido@python.org>
parents: 454
diff changeset
27 /* XXX Ought to fix getStr*arg calls to use getargs(args, "s#", ...) */
b162b27400bc Cal get*Str*arg() instead of get*str*arg(), until the code is fixed.
Guido van Rossum <guido@python.org>
parents: 454
diff changeset
28
294
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 This module provides an interface to Berkeley socket IPC.
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 Limitations:
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
34 - 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
35 - no asynchronous I/O (but read polling: avail)
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
36 - no read/write operations (use send/recv or makefile instead)
309
382323f92fce Connection sockets now have the proper family/type/proto values.
Guido van Rossum <guido@python.org>
parents: 295
diff changeset
37 - no flags on sendto/recvfrom operations
396
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
38 - no general setsockopt() call (but see s.allowbroadcast())
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 Interface:
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
42 - 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
43 - 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
44 - 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
45 - socket.socket(family, type [, proto]) --> new socket object
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 - 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
47 - errors are reported as the exception socket.error
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 - an Internet socket address is a pair (hostname, port)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 where hostname can be anything recognized by gethostbyname()
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 (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
51 - 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
52 - a UNIX domain socket is a string specifying the pathname
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
54 Socket methods:
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
56 - s.accept() --> new socket object, sockaddr
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
57 - s.avail() --> boolean
396
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
58 - s.allowbroadcast(boolean) --> None
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
59 - s.bind(sockaddr) --> None
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
60 - s.connect(sockaddr) --> None
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
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
63 - s.recv(nbytes) --> string
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
64 - s.recvfrom(nbytes) --> string, sockaddr
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
65 - s.send(string) --> None
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
66 - s.sendto(string, sockaddr) --> None
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"
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
75 #include <signal.h>
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 #include <sys/types.h>
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 #include <sys/socket.h>
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
78 #include <sys/time.h> /* Needed for struct timeval */
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 #include <netinet/in.h>
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 #include <sys/un.h>
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 #include <netdb.h>
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
82 #ifdef _AIX /* I *think* this works */
454
a5f0b8390c87 AIX fix.
Guido van Rossum <guido@python.org>
parents: 396
diff changeset
83 /* AIX defines fd_set in a separate file. Sigh... */
a5f0b8390c87 AIX fix.
Guido van Rossum <guido@python.org>
parents: 396
diff changeset
84 #include <sys/select.h>
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
85 #endif
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
87
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
88 /* 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
89 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
90
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
91 static object *SocketError;
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
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
94 /* 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
95 and return a NULL pointer from a function. */
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 socket_error()
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 return err_errno(SocketError);
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
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
103
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
104 /* 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
105 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
106 arguments properly. */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
107
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 typedef struct {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 OB_HEAD
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
110 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
111 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
112 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
113 int sock_proto; /* Protocol type, usually 0 */
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 } sockobject;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
115
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
116
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
117 /* 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
118 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
119 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
120 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
121
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 extern typeobject Socktype; /* Forward */
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
124
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
125 /* Create a new socket object.
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
126 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
127 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
128 in NEWOBJ()). */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
129
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 static sockobject *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 newsockobject(fd, family, type, proto)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 int fd, family, type, proto;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 s = NEWOBJ(sockobject, &Socktype);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 if (s != NULL) {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 s->sock_fd = fd;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 s->sock_family = family;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 s->sock_type = type;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 s->sock_proto = proto;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 return s;
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
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
145
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
146 /* 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
147 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
148 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
149 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
150 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
151
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 static int
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
153 setipaddr(name, addr_ret)
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 char *name;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 struct sockaddr_in *addr_ret;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 struct hostent *hp;
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
158 int d1, d2, d3, d4;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
159 char ch;
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
160
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
161 if (name[0] == '\0') {
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 addr_ret->sin_addr.s_addr = INADDR_ANY;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 return 4;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 }
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
165 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 addr_ret->sin_addr.s_addr = INADDR_BROADCAST;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 return 4;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 }
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
169 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
170 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
171 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
172 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
173 ((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
174 ((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
175 return 4;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
176 }
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 hp = gethostbyname(name);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 if (hp == NULL) {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 err_setstr(SocketError, "host not found");
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 return -1;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 memcpy((char *) &addr_ret->sin_addr, hp->h_addr, hp->h_length);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 return hp->h_length;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
185
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
186
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
187 /* Generally useful convenience function to create a tuple from two
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
188 objects. This eats references to the objects; if either is NULL
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
189 it destroys the other and returns NULL without raising an exception
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
190 (assuming the function that was called to create the argument must
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
191 have raised an exception and returned NULL). */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
192
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 makepair(a, b)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 object *a, *b;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 object *pair = NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 if (a == NULL || b == NULL || (pair = newtupleobject(2)) == NULL) {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 XDECREF(a);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 XDECREF(b);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 return NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 settupleitem(pair, 0, a);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 settupleitem(pair, 1, b);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 return pair;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
207
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
208
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
209 /* 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
210 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
211 size numbers). */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
212
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
213 static object *
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
214 makeipaddr(addr)
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
215 struct sockaddr_in *addr;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
216 {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
217 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
218 char buf[100];
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
219 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
220 (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
221 (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
222 return newstringobject(buf);
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
223 }
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
224
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
225
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
226 /* 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
227 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
228 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
229 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
230
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 /*ARGSUSED*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 makesockaddr(addr, addrlen)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 struct sockaddr *addr;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 int addrlen;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 {
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
237 switch (addr->sa_family) {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
238
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
239 case AF_INET:
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
240 {
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 struct sockaddr_in *a = (struct sockaddr_in *) addr;
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
242 return makepair(makeipaddr(a),
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 newintobject((long) ntohs(a->sin_port)));
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 }
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
245
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
246 case AF_UNIX:
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
247 {
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 struct sockaddr_un *a = (struct sockaddr_un *) addr;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 return newstringobject(a->sun_path);
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 /* More cases here... */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
253
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
254 default:
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
255 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
256 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
257 }
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
259
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
260
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
261 /* 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
262 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
263 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
264 through len_ret. */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
265
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 static int
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 getsockaddrarg(s, args, addr_ret, len_ret)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 struct sockaddr **addr_ret;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 int *len_ret;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 {
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
273 switch (s->sock_family) {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
274
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
275 case AF_UNIX:
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 static struct sockaddr_un addr;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 object *path;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 int len;
641
b162b27400bc Cal get*Str*arg() instead of get*str*arg(), until the code is fixed.
Guido van Rossum <guido@python.org>
parents: 454
diff changeset
280 if (!getStrarg(args, &path))
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 return 0;
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
282 if ((len = getstringsize(path)) > sizeof addr.sun_path) {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
283 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
284 return 0;
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 addr.sun_family = AF_UNIX;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 memcpy(addr.sun_path, getstringvalue(path), len);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 *addr_ret = (struct sockaddr *) &addr;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 *len_ret = len + sizeof addr.sun_family;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 return 1;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
292
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
293 case AF_INET:
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
294 {
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 static struct sockaddr_in addr;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 object *host;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 int port;
641
b162b27400bc Cal get*Str*arg() instead of get*str*arg(), until the code is fixed.
Guido van Rossum <guido@python.org>
parents: 454
diff changeset
298 if (!getStrintarg(args, &host, &port))
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 return 0;
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
300 if (setipaddr(getstringvalue(host), &addr) < 0)
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 return 0;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 addr.sin_family = AF_INET;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 addr.sin_port = htons(port);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 *addr_ret = (struct sockaddr *) &addr;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 *len_ret = sizeof addr;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 return 1;
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 /* More cases here... */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
310
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
311 default:
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
312 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
313 return 0;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
314
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
315 }
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
317
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
318
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
319 /* s.accept() method */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
320
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 sock_accept(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 char addrbuf[256];
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 int addrlen, newfd;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 object *res;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 if (!getnoarg(args))
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 return NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 addrlen = sizeof addrbuf;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 if (newfd < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 return socket_error();
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
335 /* 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
336 to avoid calls to bind() etc. on it. */
309
382323f92fce Connection sockets now have the proper family/type/proto values.
Guido van Rossum <guido@python.org>
parents: 295
diff changeset
337 res = makepair((object *) newsockobject(newfd,
382323f92fce Connection sockets now have the proper family/type/proto values.
Guido van Rossum <guido@python.org>
parents: 295
diff changeset
338 s->sock_family,
382323f92fce Connection sockets now have the proper family/type/proto values.
Guido van Rossum <guido@python.org>
parents: 295
diff changeset
339 s->sock_type,
382323f92fce Connection sockets now have the proper family/type/proto values.
Guido van Rossum <guido@python.org>
parents: 295
diff changeset
340 s->sock_proto),
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 makesockaddr((struct sockaddr *) addrbuf, addrlen));
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 if (res == NULL)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 close(newfd);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 return res;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
346
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
347
396
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
348 /* s.allowbroadcast() method */
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
349
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
350 static object *
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
351 sock_allowbroadcast(s, args)
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
352 sockobject *s;
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
353 object *args;
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
354 {
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
355 int flag;
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
356 int res;
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
357 if (!getintarg(args, &flag))
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
358 return NULL;
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
359 res = setsockopt(s->sock_fd, SOL_SOCKET, SO_BROADCAST,
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
360 &flag, sizeof flag);
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
361 if (res < 0)
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
362 return socket_error();
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
363 INCREF(None);
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
364 return None;
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
365 }
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
366
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
367
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
368 /* s.avail() method */
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
369
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
370 static object *
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
371 sock_avail(s, args)
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
372 sockobject *s;
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
373 object *args;
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
374 {
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
375 struct timeval timeout;
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
376 fd_set readers;
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
377 int n;
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
378 timeout.tv_sec = 0;
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
379 timeout.tv_usec = 0;
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
380 FD_ZERO(&readers);
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
381 FD_SET(s->sock_fd, &readers);
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
382 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
383 if (n < 0)
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
384 return socket_error();
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
385 return newintobject((long) (n != 0));
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
386 }
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
387
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
388
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
389 /* s.bind(sockaddr) method */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
390
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 sock_bind(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 struct sockaddr *addr;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 int addrlen;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 if (!getsockaddrarg(s, args, &addr, &addrlen))
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 return NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 if (bind(s->sock_fd, addr, addrlen) < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 return socket_error();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 INCREF(None);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 return None;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
405
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
406
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
407 /* s.close() method.
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
408 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
409 will surely fail. */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
410
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 sock_close(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
415 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
416 if (!getnoarg(args))
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 return NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 (void) close(s->sock_fd);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 s->sock_fd = -1;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 INCREF(None);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 return None;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
422 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
423
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
424
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
425 /* s.connect(sockaddr) method */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
426
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
427 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
428 sock_connect(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
432 struct sockaddr *addr;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 int addrlen;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
434 if (!getsockaddrarg(s, args, &addr, &addrlen))
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 return NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
436 if (connect(s->sock_fd, addr, addrlen) < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 return socket_error();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
438 INCREF(None);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 return None;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
440 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
441
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
442
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
443 /* s.listen(n) method */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
444
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
445 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
446 sock_listen(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
449 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
450 int backlog;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 if (!getintarg(args, &backlog))
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
452 return NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 if (listen(s->sock_fd, backlog) < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 return socket_error();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
455 INCREF(None);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 return None;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
458
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
459
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
460 /* s.makefile(mode) method.
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
461 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
462 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
463 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
464 of each other.)
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
465 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
466
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
467 static object *
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
468 sock_makefile(s, args)
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 {
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
472 extern int fclose PROTO((FILE *));
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
473 object *mode;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
474 int fd;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
475 FILE *fp;
641
b162b27400bc Cal get*Str*arg() instead of get*str*arg(), until the code is fixed.
Guido van Rossum <guido@python.org>
parents: 454
diff changeset
476 if (!getStrarg(args, &mode))
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
477 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
478 if ((fd = dup(s->sock_fd)) < 0 ||
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
479 (fp = fdopen(fd, getstringvalue(mode))) == NULL)
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
480 return socket_error();
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
481 return newopenfileobject(fp, "<socket>", getstringvalue(mode), fclose);
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
482 }
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
483
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
484
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
485 /* s.recv(nbytes) method */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
486
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
487 static object *
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
488 sock_recv(s, args)
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
489 sockobject *s;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
490 object *args;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
491 {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
492 int len, n, flags;
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
493 object *buf;
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
494 if (!getintintarg(args, &len, &flags)) {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
495 err_clear();
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
496 if (!getintarg(args, &len))
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
497 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
498 flags = 0;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
499 }
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
500 buf = newsizedstringobject((char *) 0, len);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
501 if (buf == NULL)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
502 return NULL;
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
503 n = recv(s->sock_fd, getstringvalue(buf), len, flags);
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
504 if (n < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
505 return socket_error();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
506 if (resizestring(&buf, n) < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
507 return NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
508 return buf;
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
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
511
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
512 /* s.recvfrom(nbytes) method */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
513
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
514 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
515 sock_recvfrom(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
516 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
517 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
518 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
519 char addrbuf[256];
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
520 object *buf;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 int addrlen, len, n;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
522 if (!getintarg(args, &len))
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 return NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
524 buf = newsizedstringobject((char *) 0, len);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
525 addrlen = sizeof addrbuf;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
526 n = recvfrom(s->sock_fd, getstringvalue(buf), len, 0,
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
527 addrbuf, &addrlen);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
528 if (n < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
529 return socket_error();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
530 if (resizestring(&buf, n) < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
531 return NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 return makepair(buf, makesockaddr(addrbuf, addrlen));
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
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
535
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
536 /* s.send(data) method */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
537
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
538 static object *
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
539 sock_send(s, args)
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
540 sockobject *s;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
541 object *args;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
542 {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
543 object *buf;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
544 int len, n, flags;
641
b162b27400bc Cal get*Str*arg() instead of get*str*arg(), until the code is fixed.
Guido van Rossum <guido@python.org>
parents: 454
diff changeset
545 if (!getStrintarg(args, &buf, &flags)) {
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
546 err_clear();
641
b162b27400bc Cal get*Str*arg() instead of get*str*arg(), until the code is fixed.
Guido van Rossum <guido@python.org>
parents: 454
diff changeset
547 if (!getStrarg(args, &buf))
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
548 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
549 flags = 0;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
550 }
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
551 len = getstringsize(buf);
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
552 n = send(s->sock_fd, getstringvalue(buf), len, flags);
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
553 if (n < 0)
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
554 return socket_error();
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
555 INCREF(None);
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
556 return None;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
557 }
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
558
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
559
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
560 /* s.sendto(data, sockaddr) method */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
561
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
562 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
563 sock_sendto(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
564 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
565 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
566 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
567 object *buf;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
568 struct sockaddr *addr;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
569 int addrlen, len, n;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
570 if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2) {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
571 err_badarg();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
572 return NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
573 }
641
b162b27400bc Cal get*Str*arg() instead of get*str*arg(), until the code is fixed.
Guido van Rossum <guido@python.org>
parents: 454
diff changeset
574 if (!getStrarg(gettupleitem(args, 0), &buf) ||
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
575 !getsockaddrarg(s, gettupleitem(args, 1), &addr, &addrlen))
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
576 return NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
577 len = getstringsize(buf);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
578 n = sendto(s->sock_fd, getstringvalue(buf), len, 0,
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
579 addr, addrlen);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
580 if (n < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
581 return socket_error();
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
582 INCREF(None);
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
583 return None;
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
584 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
585
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
586
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
587 /* s.shutdown(how) method */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
588
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
589 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
590 sock_shutdown(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
591 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
592 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
593 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
594 int how;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
595 if (!getintarg(args, &how))
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
596 return NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
597 if (shutdown(s->sock_fd, how) < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
598 return socket_error();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
599 INCREF(None);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
600 return None;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
601 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
602
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
603
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
604 /* List of methods for socket objects */
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
605
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
606 static struct methodlist sock_methods[] = {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
607 {"accept", sock_accept},
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
608 {"avail", sock_avail},
396
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
609 {"allowbroadcast", sock_allowbroadcast},
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
610 {"bind", sock_bind},
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
611 {"close", sock_close},
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
612 {"connect", sock_connect},
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
613 {"listen", sock_listen},
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
614 {"makefile", sock_makefile},
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
615 {"recv", sock_recv},
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
616 {"recvfrom", sock_recvfrom},
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
617 {"send", sock_send},
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
618 {"sendto", sock_sendto},
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
619 {"shutdown", sock_shutdown},
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
620 {NULL, NULL} /* sentinel */
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
621 };
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
622
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
623
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
624 /* 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
625 First close the file description. */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
626
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
627 static void
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
628 sock_dealloc(s)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
629 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
630 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
631 (void) close(s->sock_fd);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
632 DEL(s);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
633 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
634
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
635
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
636 /* 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
637
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
638 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
639 sock_getattr(s, name)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
640 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
641 char *name;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
642 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
643 return findmethod(sock_methods, (object *) s, name);
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 /* 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
648 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
649 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
650
374
70a18a6689d4 Make Socktype global so old and new compilers will like it.
Guido van Rossum <guido@python.org>
parents: 324
diff changeset
651 typeobject Socktype = {
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
652 OB_HEAD_INIT(&Typetype)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
653 0,
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
654 "socket",
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
655 sizeof(sockobject),
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
656 0,
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
657 sock_dealloc, /*tp_dealloc*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
658 0, /*tp_print*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
659 sock_getattr, /*tp_getattr*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
660 0, /*tp_setattr*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
661 0, /*tp_compare*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
662 0, /*tp_repr*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
663 0, /*tp_as_number*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
664 0, /*tp_as_sequence*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
665 0, /*tp_as_mapping*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
666 };
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
667
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
668
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
669 /* Python interface to gethostname(). */
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
670
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
671 /*ARGSUSED*/
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
672 static object *
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
673 socket_gethostname(self, args)
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
674 object *self;
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
675 object *args;
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
676 {
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
677 char buf[1024];
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
678 if (!getnoarg(args))
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
679 return NULL;
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
680 if (gethostname(buf, sizeof buf - 1) < 0)
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
681 return socket_error();
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
682 buf[sizeof buf - 1] = '\0';
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
683 return newstringobject(buf);
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
684 }
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
685 /* Python interface to gethostbyname(name). */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
686
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
687 /*ARGSUSED*/
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
688 static object *
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
689 socket_gethostbyname(self, args)
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
690 object *self;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
691 object *args;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
692 {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
693 object *name;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
694 struct hostent *hp;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
695 struct sockaddr_in addrbuf;
641
b162b27400bc Cal get*Str*arg() instead of get*str*arg(), until the code is fixed.
Guido van Rossum <guido@python.org>
parents: 454
diff changeset
696 if (!getStrarg(args, &name))
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
697 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
698 if (setipaddr(getstringvalue(name), &addrbuf) < 0)
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
699 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
700 return makeipaddr(&addrbuf);
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
701 }
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
702
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
703
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
704 /* Python interface to getservbyname(name).
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
705 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
706 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
707
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
708 /*ARGSUSED*/
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
709 static object *
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
710 socket_getservbyname(self, args)
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
711 object *self;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
712 object *args;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
713 {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
714 object *name, *proto;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
715 struct servent *sp;
641
b162b27400bc Cal get*Str*arg() instead of get*str*arg(), until the code is fixed.
Guido van Rossum <guido@python.org>
parents: 454
diff changeset
716 if (!getStrStrarg(args, &name, &proto))
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
717 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
718 sp = getservbyname(getstringvalue(name), getstringvalue(proto));
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
719 if (sp == NULL) {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
720 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
721 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
722 }
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
723 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
724 }
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
725
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
726
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
727 /* 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
728 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
729 Return a new socket object. */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
730
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
731 /*ARGSUSED*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
732 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
733 socket_socket(self, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
734 object *self;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
735 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
736 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
737 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
738 int family, type, proto, fd;
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
739 if (args != NULL && is_tupleobject(args) && gettuplesize(args) == 3) {
309
382323f92fce Connection sockets now have the proper family/type/proto values.
Guido van Rossum <guido@python.org>
parents: 295
diff changeset
740 if (!getintintintarg(args, &family, &type, &proto))
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
741 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
742 }
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
743 else {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
744 if (!getintintarg(args, &family, &type))
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
745 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
746 proto = 0;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
747 }
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
748 fd = socket(family, type, proto);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
749 if (fd < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
750 return socket_error();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
751 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
752 /* 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
753 file descriptor again! */
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
754 if (s == NULL)
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
755 (void) close(fd);
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
756 /* 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
757 do the work. */
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
758 (void) signal(SIGPIPE, SIG_IGN);
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
759 return (object *) s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
760 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
761
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
762
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
763 /* 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
764
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
765 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
766 {"gethostbyname", socket_gethostbyname},
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
767 {"gethostname", socket_gethostname},
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
768 {"getservbyname", socket_getservbyname},
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
769 {"socket", socket_socket},
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
770 {NULL, NULL} /* Sentinel */
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
771 };
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
772
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
773
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
774 /* 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
775 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
776
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
777 static void
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
778 insint(d, name, value)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
779 object *d;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
780 char *name;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
781 int value;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
782 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
783 object *v = newintobject((long) value);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
784 if (v == NULL) {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
785 /* Don't bother reporting this error */
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
786 err_clear();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
787 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
788 else {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
789 dictinsert(d, name, v);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
790 DECREF(v);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
791 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
792 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
793
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
794
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
795 /* Initialize this module.
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
796 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
797 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
798 defined. */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
799
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
800 void
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
801 initsocket()
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
802 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
803 object *m, *d;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
804
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
805 m = initmodule("socket", socket_methods);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
806 d = getmoduledict(m);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
807 SocketError = newstringobject("socket.error");
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
808 if (SocketError == NULL || dictinsert(d, "error", SocketError) != 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
809 fatal("can't define socket.error");
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
810 insint(d, "AF_INET", AF_INET);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
811 insint(d, "AF_UNIX", AF_UNIX);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
812 insint(d, "SOCK_STREAM", SOCK_STREAM);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
813 insint(d, "SOCK_DGRAM", SOCK_DGRAM);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
814 insint(d, "SOCK_RAW", SOCK_RAW);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
815 insint(d, "SOCK_SEQPACKET", SOCK_SEQPACKET);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
816 insint(d, "SOCK_RDM", SOCK_RDM);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
817 }