Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame^] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame^] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame^] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame^] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* Socket module */ |
| 33 | |
| 34 | /* |
| 35 | This module provides an interface to Berkeley socket IPC. |
| 36 | |
| 37 | Limitations: |
| 38 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 39 | - only AF_INET and AF_UNIX address families are supported |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 40 | - no read/write operations (use send/recv or makefile instead) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 41 | - additional restrictions apply on Windows |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 43 | Module interface: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 45 | - socket.error: exception raised for socket specific errors |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 46 | - socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd') |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 47 | - socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...]) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 48 | - socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com') |
| 49 | - socket.getservbyname(servicename, protocolname) --> port number |
Guido van Rossum | 4dd2a7e | 1991-07-01 18:51:33 +0000 | [diff] [blame] | 50 | - socket.socket(family, type [, proto]) --> new socket object |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 51 | - socket.ntohs(16 bit value) --> new int object |
| 52 | - socket.ntohl(32 bit value) --> new int object |
| 53 | - socket.htons(16 bit value) --> new int object |
| 54 | - socket.htonl(32 bit value) --> new int object |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 55 | - socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h> |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 56 | - an Internet socket address is a pair (hostname, port) |
| 57 | where hostname can be anything recognized by gethostbyname() |
| 58 | (including the dd.dd.dd.dd notation) and port is in host byte order |
| 59 | - where a hostname is returned, the dd.dd.dd.dd notation is used |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 60 | - a UNIX domain socket address is a string specifying the pathname |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 62 | Socket methods: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 64 | - s.accept() --> new socket object, sockaddr |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 65 | - s.bind(sockaddr) --> None |
| 66 | - s.close() --> None |
| 67 | - s.connect(sockaddr) --> None |
| 68 | - s.fileno() --> file descriptor |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 69 | - s.dup() --> same as socket.fromfd(os.dup(s.fileno(), ...) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 70 | - s.getpeername() --> sockaddr |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 71 | - s.getsockname() --> sockaddr |
| 72 | - s.getsockopt(level, optname[, buflen]) --> int or string |
| 73 | - s.listen(backlog) --> None |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 74 | - s.makefile([mode[, bufsize]]) --> file object |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 75 | - s.recv(buflen [,flags]) --> string |
| 76 | - s.recvfrom(buflen [,flags]) --> string, sockaddr |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 77 | - s.send(string [,flags]) --> nbytes |
| 78 | - s.sendto(string, [flags,] sockaddr) --> nbytes |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 79 | - s.setblocking(0 | 1) --> None |
| 80 | - s.setsockopt(level, optname, value) --> None |
| 81 | - s.shutdown(how) --> None |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 82 | - repr(s) --> "<socket object, fd=%d, family=%d, type=%d, protocol=%d>" |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 84 | */ |
| 85 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 86 | #include "Python.h" |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 87 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 88 | #include <sys/types.h> |
| 89 | #include "mytime.h" |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 90 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 91 | #include <signal.h> |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 92 | #ifndef MS_WINDOWS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 93 | #include <netdb.h> |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 94 | #include <sys/socket.h> |
| 95 | #include <netinet/in.h> |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 96 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 97 | #else |
| 98 | #include <winsock.h> |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 99 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 100 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 101 | #ifdef HAVE_SYS_UN_H |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 102 | #include <sys/un.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 103 | #else |
| 104 | #undef AF_UNIX |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 105 | #endif |
| 106 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 107 | #ifndef O_NDELAY |
| 108 | #define O_NDELAY O_NONBLOCK /* For QNX only? */ |
| 109 | #endif |
| 110 | |
Jack Jansen | 508537b | 1996-02-14 15:57:45 +0000 | [diff] [blame] | 111 | #ifdef USE_GUSI |
| 112 | /* fdopen() isn't declared in stdio.h (sigh) */ |
| 113 | #include <GUSI.h> |
| 114 | #endif |
| 115 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 116 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 117 | /* Here we have some hacks to choose between K&R or ANSI style function |
| 118 | definitions. For NT to build this as an extension module (ie, DLL) |
| 119 | it must be compiled by the C++ compiler, as it takes the address of |
| 120 | a static data item exported from the main Python DLL. |
| 121 | */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 122 | #ifdef MS_WINDOWS |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 123 | /* seem to be a few differences in the API */ |
| 124 | #define close closesocket |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 125 | #define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 126 | #define FORCE_ANSI_FUNC_DEFS |
| 127 | #endif |
| 128 | |
| 129 | #ifdef FORCE_ANSI_FUNC_DEFS |
| 130 | #define BUILD_FUNC_DEF_1( fnname, arg1type, arg1name ) \ |
| 131 | fnname( arg1type arg1name ) |
| 132 | |
| 133 | #define BUILD_FUNC_DEF_2( fnname, arg1type, arg1name, arg2type, arg2name ) \ |
| 134 | fnname( arg1type arg1name, arg2type arg2name ) |
| 135 | |
| 136 | #define BUILD_FUNC_DEF_3( fnname, arg1type, arg1name, arg2type, arg2name , arg3type, arg3name ) \ |
| 137 | fnname( arg1type arg1name, arg2type arg2name, arg3type arg3name ) |
| 138 | |
| 139 | #define BUILD_FUNC_DEF_4( fnname, arg1type, arg1name, arg2type, arg2name , arg3type, arg3name, arg4type, arg4name ) \ |
| 140 | fnname( arg1type arg1name, arg2type arg2name, arg3type arg3name, arg4type arg4name ) |
| 141 | |
| 142 | #else /* !FORCE_ANSI_FN_DEFS */ |
| 143 | #define BUILD_FUNC_DEF_1( fnname, arg1type, arg1name ) \ |
| 144 | fnname( arg1name ) \ |
| 145 | arg1type arg1name; |
| 146 | |
| 147 | #define BUILD_FUNC_DEF_2( fnname, arg1type, arg1name, arg2type, arg2name ) \ |
| 148 | fnname( arg1name, arg2name ) \ |
| 149 | arg1type arg1name; \ |
| 150 | arg2type arg2name; |
| 151 | |
| 152 | #define BUILD_FUNC_DEF_3( fnname, arg1type, arg1name, arg2type, arg2name, arg3type, arg3name ) \ |
| 153 | fnname( arg1name, arg2name, arg3name ) \ |
| 154 | arg1type arg1name; \ |
| 155 | arg2type arg2name; \ |
| 156 | arg3type arg3name; |
| 157 | |
| 158 | #define BUILD_FUNC_DEF_4( fnname, arg1type, arg1name, arg2type, arg2name, arg3type, arg3name, arg4type, arg4name ) \ |
| 159 | fnname( arg1name, arg2name, arg3name, arg4name ) \ |
| 160 | arg1type arg1name; \ |
| 161 | arg2type arg2name; \ |
| 162 | arg3type arg3name; \ |
| 163 | arg4type arg4name; |
| 164 | |
| 165 | #endif /* !FORCE_ANSI_FN_DEFS */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 166 | |
| 167 | /* Global variable holding the exception type for errors detected |
| 168 | by this module (but not argument type or memory errors, etc.). */ |
| 169 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 170 | static PyObject *PySocket_Error; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 171 | |
| 172 | |
| 173 | /* Convenience function to raise an error according to errno |
| 174 | and return a NULL pointer from a function. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 175 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 176 | static PyObject * |
| 177 | PySocket_Err() |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 178 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 179 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 180 | if (WSAGetLastError()) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 181 | PyObject *v; |
| 182 | v = Py_BuildValue("(is)", WSAGetLastError(), "winsock error"); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 183 | if (v != NULL) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 184 | PyErr_SetObject(PySocket_Error, v); |
| 185 | Py_DECREF(v); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 186 | } |
| 187 | return NULL; |
| 188 | } |
| 189 | else |
| 190 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 191 | return PyErr_SetFromErrno(PySocket_Error); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 194 | |
| 195 | /* The object holding a socket. It holds some extra information, |
| 196 | like the address family, which is used to decode socket address |
| 197 | arguments properly. */ |
| 198 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 199 | typedef struct { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 200 | PyObject_HEAD |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 201 | int sock_fd; /* Socket file descriptor */ |
| 202 | int sock_family; /* Address family, e.g., AF_INET */ |
| 203 | int sock_type; /* Socket type, e.g., SOCK_STREAM */ |
| 204 | int sock_proto; /* Protocol type, usually 0 */ |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 205 | union sock_addr { |
| 206 | struct sockaddr_in in; |
| 207 | #ifdef AF_UNIX |
| 208 | struct sockaddr_un un; |
| 209 | #endif |
| 210 | } sock_addr; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 211 | } PySocketSockObject; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 212 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 213 | |
| 214 | /* A forward reference to the Socktype type object. |
| 215 | The Socktype variable contains pointers to various functions, |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 216 | some of which call newsockobject(), which uses Socktype, so |
Guido van Rossum | 54ba21b | 1991-09-10 14:57:12 +0000 | [diff] [blame] | 217 | there has to be a circular reference. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 218 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 219 | staticforward PyTypeObject PySocketSock_Type; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 220 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 221 | |
| 222 | /* Create a new socket object. |
| 223 | This just creates the object and initializes it. |
| 224 | If the creation fails, return NULL and set an exception (implicit |
| 225 | in NEWOBJ()). */ |
| 226 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 227 | static PySocketSockObject * |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 228 | BUILD_FUNC_DEF_4(PySocketSock_New,int,fd, int,family, int,type, int,proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 229 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 230 | PySocketSockObject *s; |
Guido van Rossum | 5555854 | 1996-05-23 22:54:50 +0000 | [diff] [blame] | 231 | PySocketSock_Type.ob_type = &PyType_Type; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 232 | s = PyObject_NEW(PySocketSockObject, &PySocketSock_Type); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 233 | if (s != NULL) { |
| 234 | s->sock_fd = fd; |
| 235 | s->sock_family = family; |
| 236 | s->sock_type = type; |
| 237 | s->sock_proto = proto; |
| 238 | } |
| 239 | return s; |
| 240 | } |
| 241 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 242 | |
| 243 | /* Convert a string specifying a host name or one of a few symbolic |
| 244 | names to a numeric IP address. This usually calls gethostbyname() |
| 245 | to do the work; the names "" and "<broadcast>" are special. |
| 246 | Return the length (should always be 4 bytes), or negative if |
| 247 | an error occurred; then an exception is raised. */ |
| 248 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 249 | static int |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 250 | 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] | 251 | { |
| 252 | struct hostent *hp; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 253 | int d1, d2, d3, d4; |
| 254 | char ch; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 255 | #ifdef HAVE_GETHOSTBYNAME_R |
| 256 | struct hostent hp_allocated; |
| 257 | char buf[1001]; |
| 258 | int buf_len = (sizeof buf) - 1; |
| 259 | int errnop; |
| 260 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 261 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 262 | if (name[0] == '\0') { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 263 | addr_ret->sin_addr.s_addr = INADDR_ANY; |
| 264 | return 4; |
| 265 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 266 | if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 267 | addr_ret->sin_addr.s_addr = INADDR_BROADCAST; |
| 268 | return 4; |
| 269 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 270 | if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 && |
| 271 | 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 && |
| 272 | 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) { |
| 273 | addr_ret->sin_addr.s_addr = htonl( |
| 274 | ((long) d1 << 24) | ((long) d2 << 16) | |
| 275 | ((long) d3 << 8) | ((long) d4 << 0)); |
| 276 | return 4; |
| 277 | } |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 278 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 279 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 280 | hp = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 281 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 282 | #else /* not HAVE_GETHOSTBYNAME_R */ |
| 283 | hp = gethostbyname(name); |
| 284 | #endif /* HAVE_GETHOSTBYNAME_R */ |
| 285 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 286 | if (hp == NULL) { |
Guido van Rossum | ca9b323 | 1995-01-10 16:30:54 +0000 | [diff] [blame] | 287 | #ifdef HAVE_HSTRERROR |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 288 | /* Let's get real error message to return */ |
| 289 | extern int h_errno; |
| 290 | PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno)); |
| 291 | #else |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 292 | PyErr_SetString(PySocket_Error, "host not found"); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 293 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 294 | return -1; |
| 295 | } |
| 296 | memcpy((char *) &addr_ret->sin_addr, hp->h_addr, hp->h_length); |
| 297 | return hp->h_length; |
| 298 | } |
| 299 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 300 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 301 | /* Create a string object representing an IP address. |
| 302 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 303 | size numbers). */ |
| 304 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 305 | static PyObject * |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 306 | BUILD_FUNC_DEF_1(makeipaddr, struct sockaddr_in *,addr) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 307 | { |
| 308 | long x = ntohl(addr->sin_addr.s_addr); |
| 309 | char buf[100]; |
| 310 | sprintf(buf, "%d.%d.%d.%d", |
| 311 | (int) (x>>24) & 0xff, (int) (x>>16) & 0xff, |
| 312 | (int) (x>> 8) & 0xff, (int) (x>> 0) & 0xff); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 313 | return PyString_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | |
| 317 | /* Create an object representing the given socket address, |
| 318 | suitable for passing it back to bind(), connect() etc. |
| 319 | The family field of the sockaddr structure is inspected |
| 320 | to determine what kind of address it really is. */ |
| 321 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 322 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 323 | static PyObject * |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 324 | BUILD_FUNC_DEF_2(makesockaddr,struct sockaddr *,addr, int,addrlen) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 325 | { |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 326 | if (addrlen == 0) { |
| 327 | /* No address -- may be recvfrom() from known socket */ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 328 | Py_INCREF(Py_None); |
| 329 | return Py_None; |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 332 | switch (addr->sa_family) { |
| 333 | |
| 334 | case AF_INET: |
| 335 | { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 336 | struct sockaddr_in *a = (struct sockaddr_in *) addr; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 337 | PyObject *addr = makeipaddr(a); |
| 338 | PyObject *ret = Py_BuildValue("Oi", addr, ntohs(a->sin_port)); |
| 339 | Py_XDECREF(addr); |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 340 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 341 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 342 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 343 | #ifdef AF_UNIX |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 344 | case AF_UNIX: |
| 345 | { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 346 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 347 | return PyString_FromString(a->sun_path); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 348 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 349 | #endif /* AF_UNIX */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 350 | |
| 351 | /* More cases here... */ |
| 352 | |
| 353 | default: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 354 | PyErr_SetString(PySocket_Error, "return unknown socket address type"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 355 | return NULL; |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 356 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 357 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 360 | |
| 361 | /* Parse a socket address argument according to the socket object's |
| 362 | address family. Return 1 if the address was in the proper format, |
| 363 | 0 of not. The address is returned through addr_ret, its length |
| 364 | through len_ret. */ |
| 365 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 366 | static int |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 367 | BUILD_FUNC_DEF_4( |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 368 | getsockaddrarg,PySocketSockObject *,s, PyObject *,args, struct sockaddr **,addr_ret, int *,len_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 369 | { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 370 | switch (s->sock_family) { |
| 371 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 372 | #ifdef AF_UNIX |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 373 | case AF_UNIX: |
| 374 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 375 | struct sockaddr_un* addr; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 376 | char *path; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 377 | int len; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 378 | addr = (struct sockaddr_un* )&(s->sock_addr).un; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 379 | if (!PyArg_Parse(args, "s#", &path, &len)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 380 | return 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 381 | if (len > sizeof addr->sun_path) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 382 | PyErr_SetString(PySocket_Error, "AF_UNIX path too long"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 383 | return 0; |
| 384 | } |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 385 | addr->sun_family = AF_UNIX; |
| 386 | memcpy(addr->sun_path, path, len); |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 387 | addr->sun_path[len] = 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 388 | *addr_ret = (struct sockaddr *) addr; |
Guido van Rossum | 65af28a | 1996-06-11 18:36:33 +0000 | [diff] [blame] | 389 | *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 390 | return 1; |
| 391 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 392 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 393 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 394 | case AF_INET: |
| 395 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 396 | struct sockaddr_in* addr; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 397 | char *host; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 398 | int port; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 399 | addr=(struct sockaddr_in*)&(s->sock_addr).in; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 400 | if (!PyArg_Parse(args, "(si)", &host, &port)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 401 | return 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 402 | if (setipaddr(host, addr) < 0) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 403 | return 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 404 | addr->sin_family = AF_INET; |
| 405 | addr->sin_port = htons(port); |
| 406 | *addr_ret = (struct sockaddr *) addr; |
| 407 | *len_ret = sizeof *addr; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 408 | return 1; |
| 409 | } |
| 410 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 411 | /* More cases here... */ |
| 412 | |
| 413 | default: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 414 | PyErr_SetString(PySocket_Error, "getsockaddrarg: bad family"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 415 | return 0; |
| 416 | |
| 417 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 420 | |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 421 | /* Get the address length according to the socket object's address family. |
| 422 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 423 | through len_ret. */ |
| 424 | |
| 425 | static int |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 426 | BUILD_FUNC_DEF_2(getsockaddrlen,PySocketSockObject *,s, int *,len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 427 | { |
| 428 | switch (s->sock_family) { |
| 429 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 430 | #ifdef AF_UNIX |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 431 | case AF_UNIX: |
| 432 | { |
| 433 | *len_ret = sizeof (struct sockaddr_un); |
| 434 | return 1; |
| 435 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 436 | #endif /* AF_UNIX */ |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 437 | |
| 438 | case AF_INET: |
| 439 | { |
| 440 | *len_ret = sizeof (struct sockaddr_in); |
| 441 | return 1; |
| 442 | } |
| 443 | |
| 444 | /* More cases here... */ |
| 445 | |
| 446 | default: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 447 | PyErr_SetString(PySocket_Error, "getsockaddrarg: bad family"); |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 448 | return 0; |
| 449 | |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 454 | /* s.accept() method */ |
| 455 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 456 | static PyObject * |
| 457 | BUILD_FUNC_DEF_2(PySocketSock_accept,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 458 | { |
| 459 | char addrbuf[256]; |
| 460 | int addrlen, newfd; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 461 | PyObject *sock, *addr, *res; |
| 462 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 463 | return NULL; |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 464 | if (!getsockaddrlen(s, &addrlen)) |
| 465 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 466 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 467 | newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 468 | Py_END_ALLOW_THREADS |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 469 | if (newfd < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 470 | return PySocket_Err(); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 471 | /* Create the new object with unspecified family, |
| 472 | to avoid calls to bind() etc. on it. */ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 473 | sock = (PyObject *) PySocketSock_New(newfd, |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 474 | s->sock_family, |
| 475 | s->sock_type, |
| 476 | s->sock_proto); |
| 477 | if (sock == NULL) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 478 | close(newfd); |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 479 | addr = makesockaddr((struct sockaddr *) addrbuf, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 480 | res = Py_BuildValue("OO", sock, addr); |
| 481 | Py_XDECREF(sock); |
| 482 | Py_XDECREF(addr); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 483 | return res; |
| 484 | } |
| 485 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 486 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 487 | /* s.setblocking(1 | 0) method */ |
| 488 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 489 | static PyObject * |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 490 | BUILD_FUNC_DEF_2(PySocketSock_setblocking,PySocketSockObject*,s,PyObject*,args) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 491 | { |
| 492 | int block; |
| 493 | int delay_flag; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 494 | if (!PyArg_GetInt(args, &block)) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 495 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 496 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 497 | #ifndef MS_WINDOWS |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 498 | delay_flag = fcntl (s->sock_fd, F_GETFL, 0); |
| 499 | if (block) |
| 500 | delay_flag &= (~O_NDELAY); |
| 501 | else |
| 502 | delay_flag |= O_NDELAY; |
| 503 | fcntl (s->sock_fd, F_SETFL, delay_flag); |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 504 | #else |
| 505 | block = !block; |
| 506 | ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); |
| 507 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 508 | Py_END_ALLOW_THREADS |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 509 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 510 | Py_INCREF(Py_None); |
| 511 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 512 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 513 | |
| 514 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 515 | /* s.setsockopt() method. |
| 516 | With an integer third argument, sets an integer option. |
| 517 | With a string third argument, sets an option from a buffer; |
| 518 | use optional built-in module 'struct' to encode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 519 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 520 | static PyObject * |
| 521 | BUILD_FUNC_DEF_2(PySocketSock_setsockopt,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 522 | { |
| 523 | int level; |
| 524 | int optname; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 525 | int res; |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 526 | char *buf; |
| 527 | int buflen; |
| 528 | int flag; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 529 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 530 | if (PyArg_Parse(args, "(iii)", &level, &optname, &flag)) { |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 531 | buf = (char *) &flag; |
| 532 | buflen = sizeof flag; |
| 533 | } |
| 534 | else { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 535 | PyErr_Clear(); |
| 536 | if (!PyArg_Parse(args, "(iis#)", &level, &optname, &buf, &buflen)) |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 537 | return NULL; |
| 538 | } |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 539 | res = setsockopt(s->sock_fd, level, optname, (ANY *)buf, buflen); |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 540 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 541 | return PySocket_Err(); |
| 542 | Py_INCREF(Py_None); |
| 543 | return Py_None; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 547 | /* s.getsockopt() method. |
| 548 | With two arguments, retrieves an integer option. |
| 549 | With a third integer argument, retrieves a string buffer of that size; |
| 550 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 551 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 552 | static PyObject * |
| 553 | BUILD_FUNC_DEF_2(PySocketSock_getsockopt,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 554 | { |
| 555 | int level; |
| 556 | int optname; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 557 | int res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 558 | PyObject *buf; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 559 | int buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 560 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 561 | if (!PyArg_ParseTuple(args, "ii|i", &level, &optname, &buflen)) |
| 562 | return NULL; |
| 563 | |
| 564 | if (buflen == 0) { |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 565 | int flag = 0; |
| 566 | int flagsize = sizeof flag; |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 567 | res = getsockopt(s->sock_fd, level, optname, |
| 568 | (ANY *)&flag, &flagsize); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 569 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 570 | return PySocket_Err(); |
| 571 | return PyInt_FromLong(flag); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 572 | } |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 573 | if (buflen <= 0 || buflen > 1024) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 574 | PyErr_SetString(PySocket_Error, "getsockopt buflen out of range"); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 575 | return NULL; |
| 576 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 577 | buf = PyString_FromStringAndSize((char *)NULL, buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 578 | if (buf == NULL) |
| 579 | return NULL; |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 580 | res = getsockopt(s->sock_fd, level, optname, |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 581 | (ANY *)PyString_AsString(buf), &buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 582 | if (res < 0) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 583 | Py_DECREF(buf); |
| 584 | return PySocket_Err(); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 585 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 586 | _PyString_Resize(&buf, buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 587 | return buf; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 591 | /* s.bind(sockaddr) method */ |
| 592 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 593 | static PyObject * |
| 594 | BUILD_FUNC_DEF_2(PySocketSock_bind,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 595 | { |
| 596 | struct sockaddr *addr; |
| 597 | int addrlen; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 598 | int res; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 599 | if (!getsockaddrarg(s, args, &addr, &addrlen)) |
| 600 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 601 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 602 | res = bind(s->sock_fd, addr, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 603 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 604 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 605 | return PySocket_Err(); |
| 606 | Py_INCREF(Py_None); |
| 607 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 610 | |
| 611 | /* s.close() method. |
| 612 | Set the file descriptor to -1 so operations tried subsequently |
| 613 | will surely fail. */ |
| 614 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 615 | static PyObject * |
| 616 | BUILD_FUNC_DEF_2(PySocketSock_close,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 617 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 618 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 619 | return NULL; |
Guido van Rossum | 0848146 | 1996-10-12 14:07:22 +0000 | [diff] [blame] | 620 | if (s->sock_fd != -1) { |
| 621 | Py_BEGIN_ALLOW_THREADS |
| 622 | (void) close(s->sock_fd); |
| 623 | Py_END_ALLOW_THREADS |
| 624 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 625 | s->sock_fd = -1; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 626 | Py_INCREF(Py_None); |
| 627 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 630 | |
| 631 | /* s.connect(sockaddr) method */ |
| 632 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 633 | static PyObject * |
| 634 | BUILD_FUNC_DEF_2(PySocketSock_connect,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 635 | { |
| 636 | struct sockaddr *addr; |
| 637 | int addrlen; |
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 (!getsockaddrarg(s, args, &addr, &addrlen)) |
| 640 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 641 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 642 | res = connect(s->sock_fd, addr, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 643 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 644 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 645 | return PySocket_Err(); |
| 646 | Py_INCREF(Py_None); |
| 647 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 650 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 651 | /* s.fileno() method */ |
| 652 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 653 | static PyObject * |
| 654 | BUILD_FUNC_DEF_2(PySocketSock_fileno,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 655 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 656 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 657 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 658 | return PyInt_FromLong((long) s->sock_fd); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 662 | #ifndef NO_DUP |
| 663 | /* s.dup() method */ |
| 664 | |
| 665 | static PyObject * |
| 666 | BUILD_FUNC_DEF_2(PySocketSock_dup,PySocketSockObject *,s, PyObject *,args) |
| 667 | { |
| 668 | int newfd; |
| 669 | PyObject *sock; |
| 670 | if (!PyArg_NoArgs(args)) |
| 671 | return NULL; |
| 672 | newfd = dup(s->sock_fd); |
| 673 | if (newfd < 0) |
| 674 | return PySocket_Err(); |
| 675 | sock = (PyObject *) PySocketSock_New(newfd, |
| 676 | s->sock_family, |
| 677 | s->sock_type, |
| 678 | s->sock_proto); |
| 679 | if (sock == NULL) |
| 680 | close(newfd); |
| 681 | return sock; |
| 682 | } |
| 683 | #endif |
| 684 | |
| 685 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 686 | /* s.getsockname() method */ |
| 687 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 688 | static PyObject * |
| 689 | BUILD_FUNC_DEF_2(PySocketSock_getsockname,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 690 | { |
| 691 | char addrbuf[256]; |
| 692 | int addrlen, res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 693 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 694 | return NULL; |
| 695 | if (!getsockaddrlen(s, &addrlen)) |
| 696 | return NULL; |
Guido van Rossum | ca42b16 | 1996-01-12 01:36:05 +0000 | [diff] [blame] | 697 | memset(addrbuf, 0, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 698 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 699 | res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 700 | Py_END_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 701 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 702 | return PySocket_Err(); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 703 | return makesockaddr((struct sockaddr *) addrbuf, addrlen); |
| 704 | } |
| 705 | |
| 706 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 707 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 708 | /* s.getpeername() method */ |
| 709 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 710 | static PyObject * |
| 711 | BUILD_FUNC_DEF_2(PySocketSock_getpeername,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 712 | { |
| 713 | char addrbuf[256]; |
| 714 | int addrlen, res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 715 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 716 | return NULL; |
| 717 | if (!getsockaddrlen(s, &addrlen)) |
| 718 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 719 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 720 | res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 721 | Py_END_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 722 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 723 | return PySocket_Err(); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 724 | return makesockaddr((struct sockaddr *) addrbuf, addrlen); |
| 725 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 726 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 727 | |
| 728 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 729 | /* s.listen(n) method */ |
| 730 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 731 | static PyObject * |
| 732 | BUILD_FUNC_DEF_2(PySocketSock_listen,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 733 | { |
| 734 | int backlog; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 735 | int res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 736 | if (!PyArg_GetInt(args, &backlog)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 737 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 738 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 739 | if (backlog < 1) |
| 740 | backlog = 1; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 741 | res = listen(s->sock_fd, backlog); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 742 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 743 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 744 | return PySocket_Err(); |
| 745 | Py_INCREF(Py_None); |
| 746 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 749 | #ifndef NO_DUP |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 750 | /* s.makefile(mode) method. |
| 751 | Create a new open file object referring to a dupped version of |
| 752 | the socket's file descriptor. (The dup() call is necessary so |
| 753 | that the open file and socket objects may be closed independent |
| 754 | of each other.) |
| 755 | The mode argument specifies 'r' or 'w' passed to fdopen(). */ |
| 756 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 757 | static PyObject * |
| 758 | BUILD_FUNC_DEF_2(PySocketSock_makefile,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 759 | { |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 760 | extern int fclose Py_PROTO((FILE *)); |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 761 | char *mode = "r"; |
| 762 | int bufsize = -1; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 763 | int fd; |
| 764 | FILE *fp; |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 765 | PyObject *f; |
| 766 | |
| 767 | if (!PyArg_ParseTuple(args, "|si", &mode, &bufsize)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 768 | return NULL; |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 769 | #ifdef MS_WIN32 |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 770 | if ( ((fd = _open_osfhandle( s->sock_fd, _O_BINARY )) < 0) || |
| 771 | ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL)) { |
| 772 | #else |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 773 | if ((fd = dup(s->sock_fd)) < 0 || |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 774 | (fp = fdopen(fd, mode)) == NULL) { |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 775 | #endif |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 776 | if (fd >= 0) |
| 777 | close(fd); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 778 | return PySocket_Err(); |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 779 | } |
| 780 | f = PyFile_FromFile(fp, "<socket>", mode, fclose); |
| 781 | if (f != NULL) |
| 782 | PyFile_SetBufSize(f, bufsize); |
| 783 | return f; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 784 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 785 | #endif /* NO_DUP */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 786 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 787 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 788 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 789 | static PyObject * |
| 790 | BUILD_FUNC_DEF_2(PySocketSock_recv,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 791 | { |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 792 | int len, n, flags = 0; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 793 | PyObject *buf; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 794 | if (!PyArg_ParseTuple(args, "i|i", &len, &flags)) |
| 795 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 796 | buf = PyString_FromStringAndSize((char *) 0, len); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 797 | if (buf == NULL) |
| 798 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 799 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 800 | n = recv(s->sock_fd, PyString_AsString(buf), len, flags); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 801 | Py_END_ALLOW_THREADS |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 802 | if (n < 0) { |
| 803 | Py_DECREF(buf); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 804 | return PySocket_Err(); |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 805 | } |
Guido van Rossum | 53a9bf8 | 1996-06-11 18:35:24 +0000 | [diff] [blame] | 806 | if (n != len && _PyString_Resize(&buf, n) < 0) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 807 | return NULL; |
| 808 | return buf; |
| 809 | } |
| 810 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 811 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 812 | /* s.recvfrom(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 813 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 814 | static PyObject * |
| 815 | BUILD_FUNC_DEF_2(PySocketSock_recvfrom,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 816 | { |
| 817 | char addrbuf[256]; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 818 | PyObject *buf, *addr, *ret; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 819 | int addrlen, len, n, flags = 0; |
| 820 | if (!PyArg_ParseTuple(args, "i|i", &len, &flags)) |
| 821 | return NULL; |
Guido van Rossum | 18c9a4f | 1993-05-25 12:16:29 +0000 | [diff] [blame] | 822 | if (!getsockaddrlen(s, &addrlen)) |
| 823 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 824 | buf = PyString_FromStringAndSize((char *) 0, len); |
Guido van Rossum | 18c9a4f | 1993-05-25 12:16:29 +0000 | [diff] [blame] | 825 | if (buf == NULL) |
| 826 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 827 | Py_BEGIN_ALLOW_THREADS |
| 828 | n = recvfrom(s->sock_fd, PyString_AsString(buf), len, flags, |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 829 | #ifndef MS_WINDOWS |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 830 | (ANY *)addrbuf, &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 831 | #else |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 832 | (struct sockaddr *)addrbuf, &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 833 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 834 | Py_END_ALLOW_THREADS |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 835 | if (n < 0) { |
| 836 | Py_DECREF(buf); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 837 | return PySocket_Err(); |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 838 | } |
Guido van Rossum | 53a9bf8 | 1996-06-11 18:35:24 +0000 | [diff] [blame] | 839 | if (n != len && _PyString_Resize(&buf, n) < 0) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 840 | return NULL; |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 841 | addr = makesockaddr((struct sockaddr *)addrbuf, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 842 | ret = Py_BuildValue("OO", buf, addr); |
| 843 | Py_XDECREF(addr); |
| 844 | Py_XDECREF(buf); |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 845 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 848 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 849 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 850 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 851 | static PyObject * |
| 852 | BUILD_FUNC_DEF_2(PySocketSock_send,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 853 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 854 | char *buf; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 855 | int len, n, flags = 0; |
| 856 | if (!PyArg_ParseTuple(args, "s#|i", &buf, &len, &flags)) |
| 857 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 858 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 859 | n = send(s->sock_fd, buf, len, flags); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 860 | Py_END_ALLOW_THREADS |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 861 | if (n < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 862 | return PySocket_Err(); |
| 863 | return PyInt_FromLong((long)n); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 867 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 868 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 869 | static PyObject * |
| 870 | BUILD_FUNC_DEF_2(PySocketSock_sendto,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 871 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 872 | PyObject *addro; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 873 | char *buf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 874 | struct sockaddr *addr; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 875 | int addrlen, len, n, flags; |
| 876 | flags = 0; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 877 | if (!PyArg_Parse(args, "(s#O)", &buf, &len, &addro)) { |
| 878 | PyErr_Clear(); |
| 879 | if (!PyArg_Parse(args, "(s#iO)", &buf, &len, &flags, &addro)) |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 880 | return NULL; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 881 | } |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 882 | if (!getsockaddrarg(s, addro, &addr, &addrlen)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 883 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 884 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 885 | n = sendto(s->sock_fd, buf, len, flags, addr, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 886 | Py_END_ALLOW_THREADS |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 887 | if (n < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 888 | return PySocket_Err(); |
| 889 | return PyInt_FromLong((long)n); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 892 | |
| 893 | /* s.shutdown(how) method */ |
| 894 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 895 | static PyObject * |
| 896 | BUILD_FUNC_DEF_2(PySocketSock_shutdown,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 897 | { |
| 898 | int how; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 899 | int res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 900 | if (!PyArg_GetInt(args, &how)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 901 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 902 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 903 | res = shutdown(s->sock_fd, how); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 904 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 905 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 906 | return PySocket_Err(); |
| 907 | Py_INCREF(Py_None); |
| 908 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 911 | |
| 912 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 913 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 914 | static PyMethodDef PySocketSock_methods[] = { |
| 915 | {"accept", (PyCFunction)PySocketSock_accept}, |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 916 | {"setblocking", (PyCFunction)PySocketSock_setblocking}, |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 917 | {"setsockopt", (PyCFunction)PySocketSock_setsockopt}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 918 | {"getsockopt", (PyCFunction)PySocketSock_getsockopt, 1}, |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 919 | {"bind", (PyCFunction)PySocketSock_bind}, |
| 920 | {"close", (PyCFunction)PySocketSock_close}, |
| 921 | {"connect", (PyCFunction)PySocketSock_connect}, |
| 922 | {"fileno", (PyCFunction)PySocketSock_fileno}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 923 | #ifndef NO_DUP |
| 924 | {"dup", (PyCFunction)PySocketSock_dup}, |
| 925 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 926 | {"getsockname", (PyCFunction)PySocketSock_getsockname}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 927 | #ifdef HAVE_GETPEERNAME |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 928 | {"getpeername", (PyCFunction)PySocketSock_getpeername}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 929 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 930 | {"listen", (PyCFunction)PySocketSock_listen}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 931 | #ifndef NO_DUP |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 932 | {"makefile", (PyCFunction)PySocketSock_makefile, 1}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 933 | #endif |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 934 | {"recv", (PyCFunction)PySocketSock_recv, 1}, |
| 935 | {"recvfrom", (PyCFunction)PySocketSock_recvfrom, 1}, |
| 936 | {"send", (PyCFunction)PySocketSock_send, 1}, |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 937 | {"sendto", (PyCFunction)PySocketSock_sendto}, |
| 938 | {"shutdown", (PyCFunction)PySocketSock_shutdown}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 939 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 940 | }; |
| 941 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 942 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 943 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 944 | First close the file description. */ |
| 945 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 946 | static void |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 947 | BUILD_FUNC_DEF_1(PySocketSock_dealloc,PySocketSockObject *,s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 948 | { |
| 949 | (void) close(s->sock_fd); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 950 | PyMem_DEL(s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 953 | |
| 954 | /* Return a socket object's named attribute. */ |
| 955 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 956 | static PyObject * |
| 957 | BUILD_FUNC_DEF_2(PySocketSock_getattr,PySocketSockObject *,s, char *,name) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 958 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 959 | return Py_FindMethod(PySocketSock_methods, (PyObject *) s, name); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 962 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 963 | static PyObject * |
| 964 | BUILD_FUNC_DEF_1(PySocketSock_repr,PySocketSockObject *,s) |
| 965 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 966 | char buf[512]; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 967 | sprintf(buf, |
| 968 | "<socket object, fd=%d, family=%d, type=%d, protocol=%d>", |
| 969 | s->sock_fd, s->sock_family, s->sock_type, s->sock_proto); |
Guido van Rossum | dc1c64a | 1996-02-27 15:21:19 +0000 | [diff] [blame] | 970 | return PyString_FromString(buf); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 974 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 975 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 976 | static PyTypeObject PySocketSock_Type = { |
Guido van Rossum | 5555854 | 1996-05-23 22:54:50 +0000 | [diff] [blame] | 977 | PyObject_HEAD_INIT(0) /* Must fill in type value later */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 978 | 0, |
| 979 | "socket", |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 980 | sizeof(PySocketSockObject), |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 981 | 0, |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 982 | (destructor)PySocketSock_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 983 | 0, /*tp_print*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 984 | (getattrfunc)PySocketSock_getattr, /*tp_getattr*/ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 985 | 0, /*tp_setattr*/ |
| 986 | 0, /*tp_compare*/ |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 987 | (reprfunc)PySocketSock_repr, /*tp_repr*/ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 988 | 0, /*tp_as_number*/ |
| 989 | 0, /*tp_as_sequence*/ |
| 990 | 0, /*tp_as_mapping*/ |
| 991 | }; |
| 992 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 993 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 994 | /* Python interface to gethostname(). */ |
| 995 | |
| 996 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 997 | static PyObject * |
| 998 | BUILD_FUNC_DEF_2(PySocket_gethostname,PyObject *,self, PyObject *,args) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 999 | { |
| 1000 | char buf[1024]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1001 | int res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1002 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1003 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1004 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1005 | res = gethostname(buf, (int) sizeof buf - 1); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1006 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1007 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1008 | return PySocket_Err(); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1009 | buf[sizeof buf - 1] = '\0'; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1010 | return PyString_FromString(buf); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1011 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1012 | |
| 1013 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1014 | /* Python interface to gethostbyname(name). */ |
| 1015 | |
| 1016 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1017 | static PyObject * |
| 1018 | BUILD_FUNC_DEF_2(PySocket_gethostbyname,PyObject *,self, PyObject *,args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1019 | { |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1020 | char *name; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1021 | struct sockaddr_in addrbuf; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1022 | if (!PyArg_Parse(args, "s", &name)) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1023 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1024 | if (setipaddr(name, &addrbuf) < 0) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1025 | return NULL; |
| 1026 | return makeipaddr(&addrbuf); |
| 1027 | } |
| 1028 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1029 | /* Python interface to gethostbyaddr(IP). */ |
| 1030 | |
| 1031 | /*ARGSUSED*/ |
| 1032 | static PyObject * |
| 1033 | BUILD_FUNC_DEF_2(PySocket_gethostbyaddr,PyObject *,self, PyObject *, args) |
| 1034 | { |
| 1035 | struct sockaddr_in addr; |
| 1036 | char *ip_num; |
| 1037 | struct hostent *h; |
Guido van Rossum | dc1c64a | 1996-02-27 15:21:19 +0000 | [diff] [blame] | 1038 | char **pch; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1039 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 1040 | PyObject *name_list = (PyObject *)NULL; |
| 1041 | PyObject *addr_list = (PyObject *)NULL; |
| 1042 | PyObject *tmp; |
| 1043 | |
| 1044 | if (!PyArg_Parse(args, "s", &ip_num)) |
| 1045 | return NULL; |
| 1046 | if (setipaddr(ip_num, &addr) < 0) |
| 1047 | return NULL; |
| 1048 | h = gethostbyaddr((char *)&addr.sin_addr, |
| 1049 | sizeof(addr.sin_addr), |
| 1050 | AF_INET); |
| 1051 | if (h == NULL) { |
Guido van Rossum | ca9b323 | 1995-01-10 16:30:54 +0000 | [diff] [blame] | 1052 | #ifdef HAVE_HSTRERROR |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1053 | /* Let's get real error message to return */ |
| 1054 | extern int h_errno; |
| 1055 | PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno)); |
| 1056 | #else |
| 1057 | PyErr_SetString(PySocket_Error, "host not found"); |
| 1058 | #endif |
| 1059 | return NULL; |
| 1060 | } |
| 1061 | if ((name_list = PyList_New(0)) == NULL) |
| 1062 | goto err; |
| 1063 | if ((addr_list = PyList_New(0)) == NULL) |
| 1064 | goto err; |
| 1065 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 1066 | tmp = PyString_FromString(*pch); |
| 1067 | if (tmp == NULL) |
| 1068 | goto err; |
| 1069 | PyList_Append(name_list, tmp); |
| 1070 | Py_DECREF(tmp); |
| 1071 | } |
| 1072 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 1073 | memcpy((char *) &addr.sin_addr, *pch, h->h_length); |
| 1074 | tmp = makeipaddr(&addr); |
| 1075 | if (tmp == NULL) |
| 1076 | goto err; |
| 1077 | PyList_Append(addr_list, tmp); |
| 1078 | Py_DECREF(tmp); |
| 1079 | } |
| 1080 | rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list); |
| 1081 | err: |
| 1082 | Py_XDECREF(name_list); |
| 1083 | Py_XDECREF(addr_list); |
| 1084 | return rtn_tuple; |
| 1085 | } |
| 1086 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1087 | |
| 1088 | /* Python interface to getservbyname(name). |
| 1089 | This only returns the port number, since the other info is already |
| 1090 | known or not useful (like the list of aliases). */ |
| 1091 | |
| 1092 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1093 | static PyObject * |
| 1094 | BUILD_FUNC_DEF_2(PySocket_getservbyname,PyObject *,self, PyObject *,args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1095 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1096 | char *name, *proto; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1097 | struct servent *sp; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1098 | if (!PyArg_Parse(args, "(ss)", &name, &proto)) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1099 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1100 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1101 | sp = getservbyname(name, proto); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1102 | Py_END_ALLOW_THREADS |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1103 | if (sp == NULL) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1104 | PyErr_SetString(PySocket_Error, "service/proto not found"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1105 | return NULL; |
| 1106 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1107 | return PyInt_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | |
| 1111 | /* Python interface to socket(family, type, proto). |
| 1112 | The third (protocol) argument is optional. |
| 1113 | Return a new socket object. */ |
| 1114 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1115 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1116 | static PyObject * |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1117 | BUILD_FUNC_DEF_2(PySocket_socket,PyObject *,self, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1118 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1119 | PySocketSockObject *s; |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 1120 | #ifdef MS_WINDOWS |
Guido van Rossum | d639d4d | 1996-06-12 04:04:22 +0000 | [diff] [blame] | 1121 | SOCKET fd; |
| 1122 | #else |
| 1123 | int fd; |
| 1124 | #endif |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1125 | int family, type, proto = 0; |
| 1126 | if (!PyArg_ParseTuple(args, "ii|i", &family, &type, &proto)) |
| 1127 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1128 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1129 | fd = socket(family, type, proto); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1130 | Py_END_ALLOW_THREADS |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 1131 | #ifdef MS_WINDOWS |
Guido van Rossum | d639d4d | 1996-06-12 04:04:22 +0000 | [diff] [blame] | 1132 | if (fd == INVALID_SOCKET) |
| 1133 | #else |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1134 | if (fd < 0) |
Guido van Rossum | d639d4d | 1996-06-12 04:04:22 +0000 | [diff] [blame] | 1135 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1136 | return PySocket_Err(); |
| 1137 | s = PySocketSock_New(fd, family, type, proto); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1138 | /* If the object can't be created, don't forget to close the |
| 1139 | file descriptor again! */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1140 | if (s == NULL) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1141 | (void) close(fd); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1142 | /* From now on, ignore SIGPIPE and let the error checking |
| 1143 | do the work. */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1144 | #ifdef SIGPIPE |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1145 | (void) signal(SIGPIPE, SIG_IGN); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1146 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1147 | return (PyObject *) s; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1150 | #ifndef NO_DUP |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1151 | /* Create a socket object from a numeric file description. |
| 1152 | Useful e.g. if stdin is a socket. |
| 1153 | Additional arguments as for socket(). */ |
| 1154 | |
| 1155 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1156 | static PyObject * |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1157 | BUILD_FUNC_DEF_2(PySocket_fromfd,PyObject *,self, PyObject *,args) |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1158 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1159 | PySocketSockObject *s; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1160 | int fd, family, type, proto = 0; |
| 1161 | if (!PyArg_ParseTuple(args, "iii|i", &fd, &family, &type, &proto)) |
| 1162 | return NULL; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1163 | /* Dup the fd so it and the socket can be closed independently */ |
| 1164 | fd = dup(fd); |
| 1165 | if (fd < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1166 | return PySocket_Err(); |
| 1167 | s = PySocketSock_New(fd, family, type, proto); |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1168 | /* From now on, ignore SIGPIPE and let the error checking |
| 1169 | do the work. */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1170 | #ifdef SIGPIPE |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1171 | (void) signal(SIGPIPE, SIG_IGN); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1172 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1173 | return (PyObject *) s; |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1174 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1175 | #endif /* NO_DUP */ |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1176 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 1177 | static PyObject * |
| 1178 | BUILD_FUNC_DEF_2(PySocket_ntohs, PyObject *, self, PyObject *, args) |
| 1179 | { |
| 1180 | int x1, x2; |
| 1181 | |
| 1182 | if (!PyArg_Parse(args, "i", &x1)) { |
| 1183 | return NULL; |
| 1184 | } |
| 1185 | x2 = (int)ntohs((short)x1); |
| 1186 | return PyInt_FromLong(x2); |
| 1187 | } |
| 1188 | |
| 1189 | static PyObject * |
| 1190 | BUILD_FUNC_DEF_2(PySocket_ntohl, PyObject *, self, PyObject *, args) |
| 1191 | { |
| 1192 | int x1, x2; |
| 1193 | |
| 1194 | if (!PyArg_Parse(args, "i", &x1)) { |
| 1195 | return NULL; |
| 1196 | } |
| 1197 | x2 = ntohl(x1); |
| 1198 | return PyInt_FromLong(x2); |
| 1199 | } |
| 1200 | |
| 1201 | static PyObject * |
| 1202 | BUILD_FUNC_DEF_2(PySocket_htons, PyObject *, self, PyObject *, args) |
| 1203 | { |
| 1204 | int x1, x2; |
| 1205 | |
| 1206 | if (!PyArg_Parse(args, "i", &x1)) { |
| 1207 | return NULL; |
| 1208 | } |
| 1209 | x2 = (int)htons((short)x1); |
| 1210 | return PyInt_FromLong(x2); |
| 1211 | } |
| 1212 | |
| 1213 | static PyObject * |
| 1214 | BUILD_FUNC_DEF_2(PySocket_htonl, PyObject *, self, PyObject *, args) |
| 1215 | { |
| 1216 | int x1, x2; |
| 1217 | |
| 1218 | if (!PyArg_Parse(args, "i", &x1)) { |
| 1219 | return NULL; |
| 1220 | } |
| 1221 | x2 = htonl(x1); |
| 1222 | return PyInt_FromLong(x2); |
| 1223 | } |
| 1224 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1225 | /* List of functions exported by this module. */ |
| 1226 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1227 | static PyMethodDef PySocket_methods[] = { |
| 1228 | {"gethostbyname", PySocket_gethostbyname}, |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1229 | {"gethostbyaddr", PySocket_gethostbyaddr}, |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1230 | {"gethostname", PySocket_gethostname}, |
| 1231 | {"getservbyname", PySocket_getservbyname}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1232 | {"socket", PySocket_socket, 1}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1233 | #ifndef NO_DUP |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1234 | {"fromfd", PySocket_fromfd, 1}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1235 | #endif |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 1236 | {"ntohs", PySocket_ntohs}, |
| 1237 | {"ntohl", PySocket_ntohl}, |
| 1238 | {"htons", PySocket_htons}, |
| 1239 | {"htonl", PySocket_htonl}, |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1240 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1241 | }; |
| 1242 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1243 | |
| 1244 | /* Convenience routine to export an integer value. |
| 1245 | For simplicity, errors (which are unlikely anyway) are ignored. */ |
| 1246 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1247 | static void |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1248 | BUILD_FUNC_DEF_3(insint,PyObject *,d, char *,name, int,value) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1249 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1250 | PyObject *v = PyInt_FromLong((long) value); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1251 | if (v == NULL) { |
| 1252 | /* Don't bother reporting this error */ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1253 | PyErr_Clear(); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1254 | } |
| 1255 | else { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1256 | PyDict_SetItemString(d, name, v); |
| 1257 | Py_DECREF(v); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1258 | } |
| 1259 | } |
| 1260 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1261 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1262 | #ifdef MS_WINDOWS |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1263 | |
| 1264 | /* Additional initialization and cleanup for NT/Windows */ |
| 1265 | |
| 1266 | static void |
| 1267 | NTcleanup() |
| 1268 | { |
| 1269 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | static int |
| 1273 | NTinit() |
| 1274 | { |
| 1275 | WSADATA WSAData; |
| 1276 | int ret; |
| 1277 | char buf[100]; |
| 1278 | ret = WSAStartup(0x0101, &WSAData); |
| 1279 | switch (ret) { |
| 1280 | case 0: /* no error */ |
| 1281 | atexit(NTcleanup); |
| 1282 | return 1; |
| 1283 | case WSASYSNOTREADY: |
| 1284 | PyErr_SetString(PyExc_ImportError, |
| 1285 | "WSAStartup failed: network not ready"); |
| 1286 | break; |
| 1287 | case WSAVERNOTSUPPORTED: |
| 1288 | case WSAEINVAL: |
| 1289 | PyErr_SetString(PyExc_ImportError, |
| 1290 | "WSAStartup failed: requested version not supported"); |
| 1291 | break; |
| 1292 | default: |
| 1293 | sprintf(buf, "WSAStartup failed: error code %d", ret); |
| 1294 | PyErr_SetString(PyExc_ImportError, buf); |
| 1295 | break; |
| 1296 | } |
| 1297 | return 0; |
| 1298 | } |
| 1299 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1300 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1301 | |
| 1302 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1303 | /* Initialize this module. |
| 1304 | This is called when the first 'import socket' is done, |
| 1305 | via a table in config.c, if config.c is compiled with USE_SOCKET |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1306 | defined. |
| 1307 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1308 | For MS_WINDOWS (which means any Windows variant), this module |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1309 | is actually called "_socket", and there's a wrapper "socket.py" |
| 1310 | which implements some missing functionality (such as makefile(), |
| 1311 | dup() and fromfd()). The import of "_socket" may fail with an |
| 1312 | ImportError exception if initialization of WINSOCK fails. When |
| 1313 | WINSOCK is initialized succesfully, a call to WSACleanup() is |
| 1314 | scheduled to be made at exit time. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1315 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1316 | void |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1317 | #ifdef MS_WINDOWS |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1318 | init_socket() |
| 1319 | #else |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1320 | initsocket() |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1321 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1322 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1323 | PyObject *m, *d; |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1324 | #ifdef MS_WINDOWS |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1325 | if (!NTinit()) |
| 1326 | return; |
| 1327 | m = Py_InitModule("_socket", PySocket_methods); |
| 1328 | #else |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1329 | m = Py_InitModule("socket", PySocket_methods); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1330 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1331 | d = PyModule_GetDict(m); |
| 1332 | PySocket_Error = PyString_FromString("socket.error"); |
| 1333 | if (PySocket_Error == NULL || |
| 1334 | PyDict_SetItemString(d, "error", PySocket_Error) != 0) |
| 1335 | Py_FatalError("can't define socket.error"); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1336 | insint(d, "AF_INET", AF_INET); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1337 | #ifdef AF_UNIX |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1338 | insint(d, "AF_UNIX", AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1339 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1340 | insint(d, "SOCK_STREAM", SOCK_STREAM); |
| 1341 | insint(d, "SOCK_DGRAM", SOCK_DGRAM); |
| 1342 | insint(d, "SOCK_RAW", SOCK_RAW); |
| 1343 | insint(d, "SOCK_SEQPACKET", SOCK_SEQPACKET); |
| 1344 | insint(d, "SOCK_RDM", SOCK_RDM); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 1345 | |
| 1346 | #ifdef SO_DEBUG |
| 1347 | insint(d, "SO_DEBUG", SO_DEBUG); |
| 1348 | #endif |
| 1349 | #ifdef SO_ACCEPTCONN |
| 1350 | insint(d, "SO_ACCEPTCONN", SO_ACCEPTCONN); |
| 1351 | #endif |
| 1352 | #ifdef SO_REUSEADDR |
| 1353 | insint(d, "SO_REUSEADDR", SO_REUSEADDR); |
| 1354 | #endif |
| 1355 | #ifdef SO_KEEPALIVE |
| 1356 | insint(d, "SO_KEEPALIVE", SO_KEEPALIVE); |
| 1357 | #endif |
| 1358 | #ifdef SO_DONTROUTE |
| 1359 | insint(d, "SO_DONTROUTE", SO_DONTROUTE); |
| 1360 | #endif |
| 1361 | #ifdef SO_BROADCAST |
| 1362 | insint(d, "SO_BROADCAST", SO_BROADCAST); |
| 1363 | #endif |
| 1364 | #ifdef SO_USELOOPBACK |
| 1365 | insint(d, "SO_USELOOPBACK", SO_USELOOPBACK); |
| 1366 | #endif |
| 1367 | #ifdef SO_LINGER |
| 1368 | insint(d, "SO_LINGER", SO_LINGER); |
| 1369 | #endif |
| 1370 | #ifdef SO_OOBINLINE |
| 1371 | insint(d, "SO_OOBINLINE", SO_OOBINLINE); |
| 1372 | #endif |
| 1373 | #ifdef SO_REUSEPORT |
| 1374 | insint(d, "SO_REUSEPORT", SO_REUSEPORT); |
| 1375 | #endif |
| 1376 | |
| 1377 | #ifdef SO_SNDBUF |
| 1378 | insint(d, "SO_SNDBUF", SO_SNDBUF); |
| 1379 | #endif |
| 1380 | #ifdef SO_RCVBUF |
| 1381 | insint(d, "SO_RCVBUF", SO_RCVBUF); |
| 1382 | #endif |
| 1383 | #ifdef SO_SNDLOWAT |
| 1384 | insint(d, "SO_SNDLOWAT", SO_SNDLOWAT); |
| 1385 | #endif |
| 1386 | #ifdef SO_RCVLOWAT |
| 1387 | insint(d, "SO_RCVLOWAT", SO_RCVLOWAT); |
| 1388 | #endif |
| 1389 | #ifdef SO_SNDTIMEO |
| 1390 | insint(d, "SO_SNDTIMEO", SO_SNDTIMEO); |
| 1391 | #endif |
| 1392 | #ifdef SO_RCVTIMEO |
| 1393 | insint(d, "SO_RCVTIMEO", SO_RCVTIMEO); |
| 1394 | #endif |
| 1395 | #ifdef SO_ERROR |
| 1396 | insint(d, "SO_ERROR", SO_ERROR); |
| 1397 | #endif |
| 1398 | #ifdef SO_TYPE |
| 1399 | insint(d, "SO_TYPE", SO_TYPE); |
| 1400 | #endif |
| 1401 | |
| 1402 | /* Maximum number of connections for "listen" */ |
| 1403 | #ifdef SOMAXCONN |
| 1404 | insint(d, "SOMAXCONN", SOMAXCONN); |
| 1405 | #else |
| 1406 | insint(d, "SOMAXCONN", 5); /* Common value */ |
| 1407 | #endif |
| 1408 | |
| 1409 | /* Flags for send, recv */ |
| 1410 | #ifdef MSG_OOB |
| 1411 | insint(d, "MSG_OOB", MSG_OOB); |
| 1412 | #endif |
| 1413 | #ifdef MSG_PEEK |
| 1414 | insint(d, "MSG_PEEK", MSG_PEEK); |
| 1415 | #endif |
| 1416 | #ifdef MSG_DONTROUTE |
| 1417 | insint(d, "MSG_DONTROUTE", MSG_DONTROUTE); |
| 1418 | #endif |
| 1419 | #ifdef MSG_EOR |
| 1420 | insint(d, "MSG_EOR", MSG_EOR); |
| 1421 | #endif |
| 1422 | #ifdef MSG_TRUNC |
| 1423 | insint(d, "MSG_TRUNC", MSG_TRUNC); |
| 1424 | #endif |
| 1425 | #ifdef MSG_CTRUNC |
| 1426 | insint(d, "MSG_CTRUNC", MSG_CTRUNC); |
| 1427 | #endif |
| 1428 | #ifdef MSG_WAITALL |
| 1429 | insint(d, "MSG_WAITALL", MSG_WAITALL); |
| 1430 | #endif |
| 1431 | #ifdef MSG_BTAG |
| 1432 | insint(d, "MSG_BTAG", MSG_BTAG); |
| 1433 | #endif |
| 1434 | #ifdef MSG_ETAG |
| 1435 | insint(d, "MSG_ETAG", MSG_ETAG); |
| 1436 | #endif |
| 1437 | |
| 1438 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 1439 | #ifdef SOL_SOCKET |
| 1440 | insint(d, "SOL_SOCKET", SOL_SOCKET); |
| 1441 | #endif |
| 1442 | #ifdef IPPROTO_IP |
| 1443 | insint(d, "IPPROTO_IP", IPPROTO_IP); |
| 1444 | #endif |
| 1445 | #ifdef IPPROTO_ICMP |
| 1446 | insint(d, "IPPROTO_ICMP", IPPROTO_ICMP); |
| 1447 | #endif |
| 1448 | #ifdef IPPROTO_IGMP |
| 1449 | insint(d, "IPPROTO_IGMP", IPPROTO_IGMP); |
| 1450 | #endif |
| 1451 | #ifdef IPPROTO_GGP |
| 1452 | insint(d, "IPPROTO_GGP", IPPROTO_GGP); |
| 1453 | #endif |
| 1454 | #ifdef IPPROTO_TCP |
| 1455 | insint(d, "IPPROTO_TCP", IPPROTO_TCP); |
| 1456 | #endif |
| 1457 | #ifdef IPPROTO_EGP |
| 1458 | insint(d, "IPPROTO_EGP", IPPROTO_EGP); |
| 1459 | #endif |
| 1460 | #ifdef IPPROTO_PUP |
| 1461 | insint(d, "IPPROTO_PUP", IPPROTO_PUP); |
| 1462 | #endif |
| 1463 | #ifdef IPPROTO_UDP |
| 1464 | insint(d, "IPPROTO_UDP", IPPROTO_UDP); |
| 1465 | #endif |
| 1466 | #ifdef IPPROTO_IDP |
| 1467 | insint(d, "IPPROTO_IDP", IPPROTO_IDP); |
| 1468 | #endif |
| 1469 | #ifdef IPPROTO_HELLO |
| 1470 | insint(d, "IPPROTO_HELLO", IPPROTO_HELLO); |
| 1471 | #endif |
| 1472 | #ifdef IPPROTO_ND |
| 1473 | insint(d, "IPPROTO_ND", IPPROTO_ND); |
| 1474 | #endif |
| 1475 | #ifdef IPPROTO_TP |
| 1476 | insint(d, "IPPROTO_TP", IPPROTO_TP); |
| 1477 | #endif |
| 1478 | #ifdef IPPROTO_XTP |
| 1479 | insint(d, "IPPROTO_XTP", IPPROTO_XTP); |
| 1480 | #endif |
| 1481 | #ifdef IPPROTO_EON |
| 1482 | insint(d, "IPPROTO_EON", IPPROTO_EON); |
| 1483 | #endif |
| 1484 | #ifdef IPPROTO_BIP |
| 1485 | insint(d, "IPPROTO_BIP", IPPROTO_BIP); |
| 1486 | #endif |
| 1487 | /**/ |
| 1488 | #ifdef IPPROTO_RAW |
| 1489 | insint(d, "IPPROTO_RAW", IPPROTO_RAW); |
| 1490 | #endif |
| 1491 | #ifdef IPPROTO_MAX |
| 1492 | insint(d, "IPPROTO_MAX", IPPROTO_MAX); |
| 1493 | #endif |
| 1494 | |
| 1495 | /* Some port configuration */ |
| 1496 | #ifdef IPPORT_RESERVED |
| 1497 | insint(d, "IPPORT_RESERVED", IPPORT_RESERVED); |
| 1498 | #else |
| 1499 | insint(d, "IPPORT_RESERVED", 1024); |
| 1500 | #endif |
| 1501 | #ifdef IPPORT_USERRESERVED |
| 1502 | insint(d, "IPPORT_USERRESERVED", IPPORT_USERRESERVED); |
| 1503 | #else |
| 1504 | insint(d, "IPPORT_USERRESERVED", 5000); |
| 1505 | #endif |
| 1506 | |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 1507 | /* Some reserved IP v.4 addresses */ |
| 1508 | #ifdef INADDR_ANY |
| 1509 | insint(d, "INADDR_ANY", INADDR_ANY); |
| 1510 | #else |
| 1511 | insint(d, "INADDR_ANY", 0x00000000); |
| 1512 | #endif |
| 1513 | #ifdef INADDR_BROADCAST |
| 1514 | insint(d, "INADDR_BROADCAST", INADDR_BROADCAST); |
| 1515 | #else |
| 1516 | insint(d, "INADDR_BROADCAST", 0xffffffff); |
| 1517 | #endif |
| 1518 | #ifdef INADDR_LOOPBACK |
| 1519 | insint(d, "INADDR_LOOPBACK", INADDR_LOOPBACK); |
| 1520 | #else |
| 1521 | insint(d, "INADDR_LOOPBACK", 0x7F000001); |
| 1522 | #endif |
| 1523 | #ifdef INADDR_UNSPEC_GROUP |
| 1524 | insint(d, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP); |
| 1525 | #else |
| 1526 | insint(d, "INADDR_UNSPEC_GROUP", 0xe0000000); |
| 1527 | #endif |
| 1528 | #ifdef INADDR_ALLHOSTS_GROUP |
| 1529 | insint(d, "INADDR_ALLHOSTS_GROUP", INADDR_ALLHOSTS_GROUP); |
| 1530 | #else |
| 1531 | insint(d, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
| 1532 | #endif |
| 1533 | #ifdef INADDR_MAX_LOCAL_GROUP |
| 1534 | insint(d, "INADDR_MAX_LOCAL_GROUP", INADDR_MAX_LOCAL_GROUP); |
| 1535 | #else |
| 1536 | insint(d, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
| 1537 | #endif |
| 1538 | #ifdef INADDR_NONE |
| 1539 | insint(d, "INADDR_NONE", INADDR_NONE); |
| 1540 | #else |
| 1541 | insint(d, "INADDR_NONE", 0xffffffff); |
| 1542 | #endif |
| 1543 | |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 1544 | /* IP [gs]etsockopt options */ |
| 1545 | #ifdef IP_OPTIONS |
| 1546 | insint(d, "IP_OPTIONS", IP_OPTIONS); |
| 1547 | #endif |
| 1548 | #ifdef IP_HDRINCL |
| 1549 | insint(d, "IP_HDRINCL", IP_HDRINCL); |
| 1550 | #endif |
| 1551 | #ifdef IP_TOS |
| 1552 | insint(d, "IP_TOS", IP_TOS); |
| 1553 | #endif |
| 1554 | #ifdef IP_TTL |
| 1555 | insint(d, "IP_TTL", IP_TTL); |
| 1556 | #endif |
| 1557 | #ifdef IP_RECVOPTS |
| 1558 | insint(d, "IP_RECVOPTS", IP_RECVOPTS); |
| 1559 | #endif |
| 1560 | #ifdef IP_RECVRETOPTS |
| 1561 | insint(d, "IP_RECVRETOPTS", IP_RECVRETOPTS); |
| 1562 | #endif |
| 1563 | #ifdef IP_RECVDSTADDR |
| 1564 | insint(d, "IP_RECVDSTADDR", IP_RECVDSTADDR); |
| 1565 | #endif |
| 1566 | #ifdef IP_RETOPTS |
| 1567 | insint(d, "IP_RETOPTS", IP_RETOPTS); |
| 1568 | #endif |
| 1569 | #ifdef IP_MULTICAST_IF |
| 1570 | insint(d, "IP_MULTICAST_IF", IP_MULTICAST_IF); |
| 1571 | #endif |
| 1572 | #ifdef IP_MULTICAST_TTL |
| 1573 | insint(d, "IP_MULTICAST_TTL", IP_MULTICAST_TTL); |
| 1574 | #endif |
| 1575 | #ifdef IP_MULTICAST_LOOP |
| 1576 | insint(d, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP); |
| 1577 | #endif |
| 1578 | #ifdef IP_ADD_MEMBERSHIP |
| 1579 | insint(d, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP); |
| 1580 | #endif |
| 1581 | #ifdef IP_DROP_MEMBERSHIP |
| 1582 | insint(d, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP); |
| 1583 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1584 | } |