annotate Modules/socketmodule.c @ 892:11ea669c231e legacy-trunk

Documented new contents of lib5.tex
author Guido van Rossum <guido@python.org>
date Wed, 03 Jun 1992 17:56:44 +0000
parents 0cd197947d81
children 6459e2f21bf6
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 /***********************************************************
795
0cd197947d81 Copyright for 1992 added
Guido van Rossum <guido@python.org>
parents: 729
diff changeset
2 Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
294
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;
729
82dd081f380d Lint fixes.
Guido van Rossum <guido@python.org>
parents: 641
diff changeset
378 if (!getnoarg(args))
82dd081f380d Lint fixes.
Guido van Rossum <guido@python.org>
parents: 641
diff changeset
379 return NULL;
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
380 timeout.tv_sec = 0;
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
381 timeout.tv_usec = 0;
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
382 FD_ZERO(&readers);
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
383 FD_SET(s->sock_fd, &readers);
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
384 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
385 if (n < 0)
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
386 return socket_error();
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
387 return newintobject((long) (n != 0));
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
388 }
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
389
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
390
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
391 /* s.bind(sockaddr) method */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
392
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 sock_bind(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 struct sockaddr *addr;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 int addrlen;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 if (!getsockaddrarg(s, args, &addr, &addrlen))
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 return NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 if (bind(s->sock_fd, addr, addrlen) < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 return socket_error();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 INCREF(None);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
405 return None;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
406 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
407
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
408
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
409 /* s.close() method.
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
410 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
411 will surely fail. */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
412
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 sock_close(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
415 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
416 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 if (!getnoarg(args))
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 return NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 (void) close(s->sock_fd);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 s->sock_fd = -1;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
422 INCREF(None);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 return None;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
425
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
426
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
427 /* s.connect(sockaddr) method */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
428
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 sock_connect(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
432 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
434 struct sockaddr *addr;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 int addrlen;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
436 if (!getsockaddrarg(s, args, &addr, &addrlen))
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 return NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
438 if (connect(s->sock_fd, addr, addrlen) < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 return socket_error();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
440 INCREF(None);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 return None;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
443
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
444
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
445 /* s.listen(n) method */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
446
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 sock_listen(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
449 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
450 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
452 int backlog;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 if (!getintarg(args, &backlog))
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 return NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
455 if (listen(s->sock_fd, backlog) < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 return socket_error();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 INCREF(None);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
458 return None;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
459 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
460
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
461
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
462 /* s.makefile(mode) method.
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
463 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
464 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
465 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
466 of each other.)
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
467 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
468
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 static object *
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
470 sock_makefile(s, args)
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
472 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
473 {
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
474 extern int fclose PROTO((FILE *));
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
475 object *mode;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
476 int fd;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
477 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
478 if (!getStrarg(args, &mode))
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
479 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
480 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
481 (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
482 return socket_error();
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
483 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
484 }
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
485
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 /* s.recv(nbytes) method */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
488
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
489 static object *
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
490 sock_recv(s, args)
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
491 sockobject *s;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
492 object *args;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
493 {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
494 int len, n, flags;
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
495 object *buf;
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
496 if (!getintintarg(args, &len, &flags)) {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
497 err_clear();
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
498 if (!getintarg(args, &len))
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
499 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
500 flags = 0;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
501 }
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
502 buf = newsizedstringobject((char *) 0, len);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
503 if (buf == NULL)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
504 return NULL;
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
505 n = recv(s->sock_fd, getstringvalue(buf), len, flags);
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
506 if (n < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
507 return socket_error();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
508 if (resizestring(&buf, n) < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
509 return NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
510 return buf;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
511 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
512
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
513
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
514 /* s.recvfrom(nbytes) method */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
515
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
516 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
517 sock_recvfrom(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
518 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
519 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
520 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 char addrbuf[256];
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
522 object *buf;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 int addrlen, len, n;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
524 if (!getintarg(args, &len))
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
525 return NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
526 buf = newsizedstringobject((char *) 0, len);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
527 addrlen = sizeof addrbuf;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
528 n = recvfrom(s->sock_fd, getstringvalue(buf), len, 0,
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
529 addrbuf, &addrlen);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
530 if (n < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
531 return socket_error();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 if (resizestring(&buf, n) < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
533 return NULL;
729
82dd081f380d Lint fixes.
Guido van Rossum <guido@python.org>
parents: 641
diff changeset
534 return makepair(buf,
82dd081f380d Lint fixes.
Guido van Rossum <guido@python.org>
parents: 641
diff changeset
535 makesockaddr((struct sockaddr *)addrbuf, addrlen));
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
536 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
537
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
538
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
539 /* s.send(data) method */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
540
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
541 static object *
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
542 sock_send(s, args)
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
543 sockobject *s;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
544 object *args;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
545 {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
546 object *buf;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
547 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
548 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
549 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
550 if (!getStrarg(args, &buf))
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
551 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
552 flags = 0;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
553 }
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
554 len = getstringsize(buf);
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
555 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
556 if (n < 0)
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
557 return socket_error();
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
558 INCREF(None);
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
559 return None;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
560 }
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
561
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
562
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
563 /* s.sendto(data, sockaddr) method */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
564
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
565 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
566 sock_sendto(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
567 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
568 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
569 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
570 object *buf;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
571 struct sockaddr *addr;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
572 int addrlen, len, n;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
573 if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2) {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
574 err_badarg();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
575 return NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
576 }
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
577 if (!getStrarg(gettupleitem(args, 0), &buf) ||
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
578 !getsockaddrarg(s, gettupleitem(args, 1), &addr, &addrlen))
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
579 return NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
580 len = getstringsize(buf);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
581 n = sendto(s->sock_fd, getstringvalue(buf), len, 0,
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
582 addr, addrlen);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
583 if (n < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
584 return socket_error();
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
585 INCREF(None);
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
586 return None;
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
587 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
588
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
589
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
590 /* s.shutdown(how) method */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
591
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
592 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
593 sock_shutdown(s, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
594 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
595 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
596 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
597 int how;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
598 if (!getintarg(args, &how))
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
599 return NULL;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
600 if (shutdown(s->sock_fd, how) < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
601 return socket_error();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
602 INCREF(None);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
603 return None;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
604 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
605
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
606
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
607 /* List of methods for socket objects */
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
608
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
609 static struct methodlist sock_methods[] = {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
610 {"accept", sock_accept},
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
611 {"avail", sock_avail},
396
c262f421a44c Added allowbroadcast()
Guido van Rossum <guido@python.org>
parents: 374
diff changeset
612 {"allowbroadcast", sock_allowbroadcast},
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
613 {"bind", sock_bind},
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 {"close", sock_close},
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 {"connect", sock_connect},
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
616 {"listen", sock_listen},
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
617 {"makefile", sock_makefile},
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
618 {"recv", sock_recv},
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
619 {"recvfrom", sock_recvfrom},
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
620 {"send", sock_send},
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
621 {"sendto", sock_sendto},
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
622 {"shutdown", sock_shutdown},
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
623 {NULL, NULL} /* sentinel */
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
624 };
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
625
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
626
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
627 /* 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
628 First close the file description. */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
629
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
630 static void
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
631 sock_dealloc(s)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
632 sockobject *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 (void) close(s->sock_fd);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
635 DEL(s);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
636 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
637
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
638
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
639 /* 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
640
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
641 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
642 sock_getattr(s, name)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
643 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
644 char *name;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
645 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
646 return findmethod(sock_methods, (object *) s, name);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
647 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
648
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
649
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
650 /* 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
651 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
652 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
653
374
70a18a6689d4 Make Socktype global so old and new compilers will like it.
Guido van Rossum <guido@python.org>
parents: 324
diff changeset
654 typeobject Socktype = {
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
655 OB_HEAD_INIT(&Typetype)
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 "socket",
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
658 sizeof(sockobject),
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
659 0,
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
660 sock_dealloc, /*tp_dealloc*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
661 0, /*tp_print*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
662 sock_getattr, /*tp_getattr*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
663 0, /*tp_setattr*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
664 0, /*tp_compare*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
665 0, /*tp_repr*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
666 0, /*tp_as_number*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
667 0, /*tp_as_sequence*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
668 0, /*tp_as_mapping*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
669 };
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
670
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
671
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
672 /* Python interface to gethostname(). */
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
673
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
674 /*ARGSUSED*/
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
675 static object *
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
676 socket_gethostname(self, args)
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
677 object *self;
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
678 object *args;
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
679 {
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
680 char buf[1024];
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
681 if (!getnoarg(args))
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
682 return NULL;
729
82dd081f380d Lint fixes.
Guido van Rossum <guido@python.org>
parents: 641
diff changeset
683 if (gethostname(buf, (int) sizeof buf - 1) < 0)
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
684 return socket_error();
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
685 buf[sizeof buf - 1] = '\0';
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
686 return newstringobject(buf);
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
687 }
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
688 /* Python interface to gethostbyname(name). */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
689
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
690 /*ARGSUSED*/
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
691 static object *
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
692 socket_gethostbyname(self, args)
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
693 object *self;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
694 object *args;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
695 {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
696 object *name;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
697 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
698 if (!getStrarg(args, &name))
295
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 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
701 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
702 return makeipaddr(&addrbuf);
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
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
705
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
706 /* Python interface to getservbyname(name).
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
707 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
708 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
709
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
710 /*ARGSUSED*/
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
711 static object *
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
712 socket_getservbyname(self, args)
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
713 object *self;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
714 object *args;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
715 {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
716 object *name, *proto;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
717 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
718 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
719 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
720 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
721 if (sp == NULL) {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
722 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
723 return NULL;
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 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
726 }
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
727
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
728
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
729 /* 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
730 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
731 Return a new socket object. */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
732
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
733 /*ARGSUSED*/
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
734 static object *
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
735 socket_socket(self, args)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
736 object *self;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
737 object *args;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
738 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
739 sockobject *s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
740 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
741 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
742 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
743 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
744 }
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
745 else {
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
746 if (!getintintarg(args, &family, &type))
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
747 return NULL;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
748 proto = 0;
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
749 }
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
750 fd = socket(family, type, proto);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
751 if (fd < 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
752 return socket_error();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
753 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
754 /* 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
755 file descriptor again! */
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
756 if (s == NULL)
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
757 (void) close(fd);
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
758 /* 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
759 do the work. */
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
760 (void) signal(SIGPIPE, SIG_IGN);
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
761 return (object *) s;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
762 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
763
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
764
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
765 /* 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
766
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
767 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
768 {"gethostbyname", socket_gethostbyname},
324
a81020d505bf Add gethostname() and s.avail().
Guido van Rossum <guido@python.org>
parents: 309
diff changeset
769 {"gethostname", socket_gethostname},
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
770 {"getservbyname", socket_getservbyname},
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
771 {"socket", socket_socket},
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
772 {NULL, NULL} /* Sentinel */
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
773 };
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
774
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
775
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
776 /* 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
777 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
778
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
779 static void
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
780 insint(d, name, value)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
781 object *d;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
782 char *name;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
783 int value;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
784 {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
785 object *v = newintobject((long) value);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
786 if (v == NULL) {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
787 /* Don't bother reporting this error */
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
788 err_clear();
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
789 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
790 else {
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
791 dictinsert(d, name, v);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
792 DECREF(v);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
793 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
794 }
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
795
295
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
796
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
797 /* Initialize this module.
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
798 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
799 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
800 defined. */
d7a38dece86a Many changes to the interface, and added comments.
Guido van Rossum <guido@python.org>
parents: 294
diff changeset
801
294
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
802 void
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
803 initsocket()
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 object *m, *d;
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
806
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
807 m = initmodule("socket", socket_methods);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
808 d = getmoduledict(m);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
809 SocketError = newstringobject("socket.error");
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
810 if (SocketError == NULL || dictinsert(d, "error", SocketError) != 0)
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
811 fatal("can't define socket.error");
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
812 insint(d, "AF_INET", AF_INET);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
813 insint(d, "AF_UNIX", AF_UNIX);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
814 insint(d, "SOCK_STREAM", SOCK_STREAM);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
815 insint(d, "SOCK_DGRAM", SOCK_DGRAM);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
816 insint(d, "SOCK_RAW", SOCK_RAW);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
817 insint(d, "SOCK_SEQPACKET", SOCK_SEQPACKET);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
818 insint(d, "SOCK_RDM", SOCK_RDM);
8938b3132caa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
819 }