annotate Modules/socketmodule.c @ 396:c262f421a44c legacy-trunk

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