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