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