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