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') |
Guido van Rossum | 25405c7 | 1996-12-19 16:42:52 +0000 | [diff] [blame] | 49 | - socket.getprotobyname(protocolname) --> protocol number |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 50 | - socket.getservbyname(servicename, protocolname) --> port number |
Guido van Rossum | 4dd2a7e | 1991-07-01 18:51:33 +0000 | [diff] [blame] | 51 | - socket.socket(family, type [, proto]) --> new socket object |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 52 | - socket.ntohs(16 bit value) --> new int object |
| 53 | - socket.ntohl(32 bit value) --> new int object |
| 54 | - socket.htons(16 bit value) --> new int object |
| 55 | - socket.htonl(32 bit value) --> new int object |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 56 | - socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 57 | - socket.inet_aton(IP address) -> 32-bit packed IP representation |
| 58 | - socket.inet_ntoa(packed IP) -> IP address string |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 59 | - an Internet socket address is a pair (hostname, port) |
| 60 | where hostname can be anything recognized by gethostbyname() |
| 61 | (including the dd.dd.dd.dd notation) and port is in host byte order |
| 62 | - 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] | 63 | - a UNIX domain socket address is a string specifying the pathname |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 65 | Socket methods: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 67 | - s.accept() --> new socket object, sockaddr |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 68 | - s.bind(sockaddr) --> None |
| 69 | - s.close() --> None |
| 70 | - s.connect(sockaddr) --> None |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 71 | - s.connect_ex(sockaddr) --> 0 or errno (handy for e.g. async connect) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 72 | - s.fileno() --> file descriptor |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 73 | - s.dup() --> same as socket.fromfd(os.dup(s.fileno(), ...) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 74 | - s.getpeername() --> sockaddr |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 75 | - s.getsockname() --> sockaddr |
| 76 | - s.getsockopt(level, optname[, buflen]) --> int or string |
| 77 | - s.listen(backlog) --> None |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 78 | - s.makefile([mode[, bufsize]]) --> file object |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 79 | - s.recv(buflen [,flags]) --> string |
| 80 | - s.recvfrom(buflen [,flags]) --> string, sockaddr |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 81 | - s.send(string [,flags]) --> nbytes |
| 82 | - s.sendto(string, [flags,] sockaddr) --> nbytes |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 83 | - s.setblocking(0 | 1) --> None |
| 84 | - s.setsockopt(level, optname, value) --> None |
| 85 | - s.shutdown(how) --> None |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 86 | - 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] | 87 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 88 | */ |
| 89 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 90 | #include "Python.h" |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 91 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 92 | /* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure |
| 93 | script doesn't get this right, so we hardcode some platform checks below. |
| 94 | On the other hand, not all Linux versions agree, so there the settings |
| 95 | computed by the configure script are needed! */ |
| 96 | |
| 97 | #ifndef linux |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 98 | #undef HAVE_GETHOSTBYNAME_R_3_ARG |
| 99 | #undef HAVE_GETHOSTBYNAME_R_5_ARG |
| 100 | #undef HAVE_GETHOSTBYNAME_R_6_ARG |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 101 | #endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 102 | |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 103 | #ifndef WITH_THREAD |
| 104 | #undef HAVE_GETHOSTBYNAME_R |
| 105 | #endif |
| 106 | |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 107 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 108 | #if defined(_AIX) || defined(__osf__) |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 109 | #define HAVE_GETHOSTBYNAME_R_3_ARG |
| 110 | #elif defined(__sun__) || defined(__sgi) |
| 111 | #define HAVE_GETHOSTBYNAME_R_5_ARG |
| 112 | #elif defined(linux) |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 113 | /* Rely on the configure script */ |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 114 | #else |
| 115 | #undef HAVE_GETHOSTBYNAME_R |
| 116 | #endif |
| 117 | #endif |
| 118 | |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 119 | #if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && !defined(MS_WINDOWS) |
| 120 | #define USE_GETHOSTBYNAME_LOCK |
| 121 | #endif |
| 122 | |
| 123 | #ifdef USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 124 | #include "pythread.h" |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 125 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 126 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 127 | #ifdef HAVE_UNISTD_H |
| 128 | #include <unistd.h> |
| 129 | #endif |
| 130 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 131 | #if !defined(MS_WINDOWS) && !defined(PYOS_OS2) && !defined(__BEOS__) |
Guido van Rossum | fb4130d | 1996-12-10 15:17:08 +0000 | [diff] [blame] | 132 | extern int gethostname(); /* For Solaris, at least */ |
| 133 | #endif |
| 134 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 135 | #if defined(PYCC_VACPP) |
| 136 | #include <types.h> |
| 137 | #include <io.h> |
| 138 | #include <sys/ioctl.h> |
| 139 | #include <utils.h> |
| 140 | #include <ctype.h> |
| 141 | #endif |
| 142 | |
| 143 | #if defined(PYOS_OS2) |
| 144 | #define INCL_DOS |
| 145 | #define INCL_DOSERRORS |
| 146 | #define INCL_NOPMAPI |
| 147 | #include <os2.h> |
| 148 | #endif |
| 149 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 150 | #if defined(__BEOS__) |
| 151 | /* It's in the libs, but not the headers... - [cjh] */ |
| 152 | int shutdown( int, int ); |
| 153 | #endif |
| 154 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 155 | #include <sys/types.h> |
| 156 | #include "mytime.h" |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 157 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 158 | #include <signal.h> |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 159 | #ifndef MS_WINDOWS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 160 | #include <netdb.h> |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 161 | #include <sys/socket.h> |
| 162 | #include <netinet/in.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 163 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 164 | /* Headers needed for inet_ntoa() and inet_addr() */ |
| 165 | #ifdef __BEOS__ |
| 166 | #include <net/netdb.h> |
| 167 | #else |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 168 | #include <arpa/inet.h> |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 169 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 170 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 171 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 172 | #else |
| 173 | #include <winsock.h> |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 174 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 175 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 176 | #ifdef HAVE_SYS_UN_H |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 177 | #include <sys/un.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 178 | #else |
| 179 | #undef AF_UNIX |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 180 | #endif |
| 181 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 182 | #ifndef O_NDELAY |
| 183 | #define O_NDELAY O_NONBLOCK /* For QNX only? */ |
| 184 | #endif |
| 185 | |
Jack Jansen | 508537b | 1996-02-14 15:57:45 +0000 | [diff] [blame] | 186 | #ifdef USE_GUSI |
| 187 | /* fdopen() isn't declared in stdio.h (sigh) */ |
| 188 | #include <GUSI.h> |
| 189 | #endif |
| 190 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 191 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 192 | /* Here we have some hacks to choose between K&R or ANSI style function |
| 193 | definitions. For NT to build this as an extension module (ie, DLL) |
| 194 | it must be compiled by the C++ compiler, as it takes the address of |
| 195 | a static data item exported from the main Python DLL. |
| 196 | */ |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 197 | #if defined(MS_WINDOWS) || defined(__BEOS__) |
| 198 | /* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 199 | /* seem to be a few differences in the API */ |
| 200 | #define close closesocket |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 201 | #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] | 202 | #define FORCE_ANSI_FUNC_DEFS |
| 203 | #endif |
| 204 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 205 | #if defined(PYOS_OS2) |
| 206 | #define close soclose |
| 207 | #define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */ |
| 208 | #define FORCE_ANSI_FUNC_DEFS |
| 209 | #endif |
| 210 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 211 | #ifdef FORCE_ANSI_FUNC_DEFS |
| 212 | #define BUILD_FUNC_DEF_1( fnname, arg1type, arg1name ) \ |
| 213 | fnname( arg1type arg1name ) |
| 214 | |
| 215 | #define BUILD_FUNC_DEF_2( fnname, arg1type, arg1name, arg2type, arg2name ) \ |
| 216 | fnname( arg1type arg1name, arg2type arg2name ) |
| 217 | |
| 218 | #define BUILD_FUNC_DEF_3( fnname, arg1type, arg1name, arg2type, arg2name , arg3type, arg3name ) \ |
| 219 | fnname( arg1type arg1name, arg2type arg2name, arg3type arg3name ) |
| 220 | |
| 221 | #define BUILD_FUNC_DEF_4( fnname, arg1type, arg1name, arg2type, arg2name , arg3type, arg3name, arg4type, arg4name ) \ |
| 222 | fnname( arg1type arg1name, arg2type arg2name, arg3type arg3name, arg4type arg4name ) |
| 223 | |
| 224 | #else /* !FORCE_ANSI_FN_DEFS */ |
| 225 | #define BUILD_FUNC_DEF_1( fnname, arg1type, arg1name ) \ |
| 226 | fnname( arg1name ) \ |
| 227 | arg1type arg1name; |
| 228 | |
| 229 | #define BUILD_FUNC_DEF_2( fnname, arg1type, arg1name, arg2type, arg2name ) \ |
| 230 | fnname( arg1name, arg2name ) \ |
| 231 | arg1type arg1name; \ |
| 232 | arg2type arg2name; |
| 233 | |
| 234 | #define BUILD_FUNC_DEF_3( fnname, arg1type, arg1name, arg2type, arg2name, arg3type, arg3name ) \ |
| 235 | fnname( arg1name, arg2name, arg3name ) \ |
| 236 | arg1type arg1name; \ |
| 237 | arg2type arg2name; \ |
| 238 | arg3type arg3name; |
| 239 | |
| 240 | #define BUILD_FUNC_DEF_4( fnname, arg1type, arg1name, arg2type, arg2name, arg3type, arg3name, arg4type, arg4name ) \ |
| 241 | fnname( arg1name, arg2name, arg3name, arg4name ) \ |
| 242 | arg1type arg1name; \ |
| 243 | arg2type arg2name; \ |
| 244 | arg3type arg3name; \ |
| 245 | arg4type arg4name; |
| 246 | |
| 247 | #endif /* !FORCE_ANSI_FN_DEFS */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 248 | |
| 249 | /* Global variable holding the exception type for errors detected |
| 250 | by this module (but not argument type or memory errors, etc.). */ |
| 251 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 252 | static PyObject *PySocket_Error; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 253 | |
| 254 | |
| 255 | /* Convenience function to raise an error according to errno |
| 256 | and return a NULL pointer from a function. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 257 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 258 | static PyObject * |
| 259 | PySocket_Err() |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 260 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 261 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 262 | if (WSAGetLastError()) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 263 | PyObject *v; |
| 264 | v = Py_BuildValue("(is)", WSAGetLastError(), "winsock error"); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 265 | if (v != NULL) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 266 | PyErr_SetObject(PySocket_Error, v); |
| 267 | Py_DECREF(v); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 268 | } |
| 269 | return NULL; |
| 270 | } |
| 271 | else |
| 272 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 273 | |
| 274 | #if defined(PYOS_OS2) |
| 275 | if (sock_errno() != NO_ERROR) { |
| 276 | APIRET rc; |
| 277 | ULONG msglen; |
| 278 | char outbuf[100]; |
| 279 | int myerrorcode = sock_errno(); |
| 280 | |
| 281 | /* Retrieve Socket-Related Error Message from MPTN.MSG File */ |
| 282 | rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf), |
| 283 | myerrorcode - SOCBASEERR + 26, "mptn.msg", &msglen); |
| 284 | if (rc == NO_ERROR) { |
| 285 | PyObject *v; |
| 286 | |
| 287 | outbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 288 | if (strlen(outbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 289 | char *lastc = &outbuf[ strlen(outbuf)-1 ]; |
| 290 | while (lastc > outbuf && isspace(*lastc)) |
| 291 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 292 | } |
| 293 | v = Py_BuildValue("(is)", myerrorcode, outbuf); |
| 294 | if (v != NULL) { |
| 295 | PyErr_SetObject(PySocket_Error, v); |
| 296 | Py_DECREF(v); |
| 297 | } |
| 298 | return NULL; |
| 299 | } |
| 300 | } |
| 301 | #endif |
| 302 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 303 | return PyErr_SetFromErrno(PySocket_Error); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 306 | |
| 307 | /* The object holding a socket. It holds some extra information, |
| 308 | like the address family, which is used to decode socket address |
| 309 | arguments properly. */ |
| 310 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 311 | typedef struct { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 312 | PyObject_HEAD |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 313 | int sock_fd; /* Socket file descriptor */ |
| 314 | int sock_family; /* Address family, e.g., AF_INET */ |
| 315 | int sock_type; /* Socket type, e.g., SOCK_STREAM */ |
| 316 | int sock_proto; /* Protocol type, usually 0 */ |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 317 | union sock_addr { |
| 318 | struct sockaddr_in in; |
| 319 | #ifdef AF_UNIX |
| 320 | struct sockaddr_un un; |
| 321 | #endif |
| 322 | } sock_addr; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 323 | } PySocketSockObject; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 324 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 325 | |
| 326 | /* A forward reference to the Socktype type object. |
| 327 | The Socktype variable contains pointers to various functions, |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 328 | some of which call newsockobject(), which uses Socktype, so |
Guido van Rossum | 54ba21b | 1991-09-10 14:57:12 +0000 | [diff] [blame] | 329 | there has to be a circular reference. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 330 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 331 | staticforward PyTypeObject PySocketSock_Type; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 332 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 333 | |
| 334 | /* Create a new socket object. |
| 335 | This just creates the object and initializes it. |
| 336 | If the creation fails, return NULL and set an exception (implicit |
| 337 | in NEWOBJ()). */ |
| 338 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 339 | static PySocketSockObject * |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 340 | 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] | 341 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 342 | PySocketSockObject *s; |
Guido van Rossum | 5555854 | 1996-05-23 22:54:50 +0000 | [diff] [blame] | 343 | PySocketSock_Type.ob_type = &PyType_Type; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 344 | s = PyObject_NEW(PySocketSockObject, &PySocketSock_Type); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 345 | if (s != NULL) { |
| 346 | s->sock_fd = fd; |
| 347 | s->sock_family = family; |
| 348 | s->sock_type = type; |
| 349 | s->sock_proto = proto; |
| 350 | } |
| 351 | return s; |
| 352 | } |
| 353 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 354 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 355 | /* Lock to allow python interpreter to continue, but only allow one |
| 356 | thread to be in gethostbyname */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 357 | #ifdef USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 358 | PyThread_type_lock gethostbyname_lock; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 359 | #endif |
| 360 | |
| 361 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 362 | /* Convert a string specifying a host name or one of a few symbolic |
| 363 | names to a numeric IP address. This usually calls gethostbyname() |
| 364 | to do the work; the names "" and "<broadcast>" are special. |
| 365 | Return the length (should always be 4 bytes), or negative if |
| 366 | an error occurred; then an exception is raised. */ |
| 367 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 368 | static int |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 369 | 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] | 370 | { |
| 371 | struct hostent *hp; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 372 | int d1, d2, d3, d4; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 373 | int h_length; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 374 | char ch; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 375 | #ifdef HAVE_GETHOSTBYNAME_R |
| 376 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 377 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
| 378 | struct hostent_data data; |
| 379 | #else |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 380 | char buf[1001]; |
| 381 | int buf_len = (sizeof buf) - 1; |
| 382 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 383 | #endif |
| 384 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 385 | int result; |
| 386 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 387 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 388 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 389 | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 390 | if (name[0] == '\0') { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 391 | addr_ret->sin_addr.s_addr = INADDR_ANY; |
| 392 | return 4; |
| 393 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 394 | if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 395 | addr_ret->sin_addr.s_addr = INADDR_BROADCAST; |
| 396 | return 4; |
| 397 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 398 | if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 && |
| 399 | 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 && |
| 400 | 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) { |
| 401 | addr_ret->sin_addr.s_addr = htonl( |
| 402 | ((long) d1 << 24) | ((long) d2 << 16) | |
| 403 | ((long) d3 << 8) | ((long) d4 << 0)); |
| 404 | return 4; |
| 405 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 406 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 407 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 408 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 409 | result = gethostbyname_r(name, &hp_allocated, buf, buf_len, &hp, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 410 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 411 | hp = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 412 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 413 | memset((void *) &data, '\0', sizeof(data)); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 414 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 415 | hp = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 416 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 417 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 418 | #ifdef USE_GETHOSTBYNAME_LOCK |
| 419 | PyThread_acquire_lock(gethostbyname_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 420 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 421 | hp = gethostbyname(name); |
| 422 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 423 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 424 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 425 | if (hp == NULL) { |
Guido van Rossum | ca9b323 | 1995-01-10 16:30:54 +0000 | [diff] [blame] | 426 | #ifdef HAVE_HSTRERROR |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 427 | /* Let's get real error message to return */ |
| 428 | extern int h_errno; |
| 429 | PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno)); |
| 430 | #else |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 431 | PyErr_SetString(PySocket_Error, "host not found"); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 432 | #endif |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 433 | #ifdef USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 434 | PyThread_release_lock(gethostbyname_lock); |
| 435 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 436 | return -1; |
| 437 | } |
| 438 | memcpy((char *) &addr_ret->sin_addr, hp->h_addr, hp->h_length); |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 439 | h_length = hp->h_length; |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 440 | #ifdef USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 441 | PyThread_release_lock(gethostbyname_lock); |
| 442 | #endif |
| 443 | return h_length; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 446 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 447 | /* Create a string object representing an IP address. |
| 448 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 449 | size numbers). */ |
| 450 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 451 | static PyObject * |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 452 | BUILD_FUNC_DEF_1(makeipaddr, struct sockaddr_in *,addr) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 453 | { |
| 454 | long x = ntohl(addr->sin_addr.s_addr); |
| 455 | char buf[100]; |
| 456 | sprintf(buf, "%d.%d.%d.%d", |
| 457 | (int) (x>>24) & 0xff, (int) (x>>16) & 0xff, |
| 458 | (int) (x>> 8) & 0xff, (int) (x>> 0) & 0xff); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 459 | return PyString_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | |
| 463 | /* Create an object representing the given socket address, |
| 464 | suitable for passing it back to bind(), connect() etc. |
| 465 | The family field of the sockaddr structure is inspected |
| 466 | to determine what kind of address it really is. */ |
| 467 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 468 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 469 | static PyObject * |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 470 | BUILD_FUNC_DEF_2(makesockaddr,struct sockaddr *,addr, int,addrlen) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 471 | { |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 472 | if (addrlen == 0) { |
| 473 | /* No address -- may be recvfrom() from known socket */ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 474 | Py_INCREF(Py_None); |
| 475 | return Py_None; |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 478 | #ifdef __BEOS__ |
| 479 | /* XXX: BeOS version of accept() doesn't set family coreectly */ |
| 480 | addr->sa_family = AF_INET; |
| 481 | #endif |
| 482 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 483 | switch (addr->sa_family) { |
| 484 | |
| 485 | case AF_INET: |
| 486 | { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 487 | struct sockaddr_in *a = (struct sockaddr_in *) addr; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 488 | PyObject *addrobj = makeipaddr(a); |
| 489 | PyObject *ret = NULL; |
| 490 | if (addrobj) { |
| 491 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 492 | Py_DECREF(addrobj); |
| 493 | } |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 494 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 495 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 496 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 497 | #ifdef AF_UNIX |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 498 | case AF_UNIX: |
| 499 | { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 500 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 501 | return PyString_FromString(a->sun_path); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 502 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 503 | #endif /* AF_UNIX */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 504 | |
| 505 | /* More cases here... */ |
| 506 | |
| 507 | default: |
Guido van Rossum | aa948df | 1997-05-07 17:41:48 +0000 | [diff] [blame] | 508 | /* If we don't know the address family, don't raise an |
| 509 | exception -- return it as a tuple. */ |
| 510 | return Py_BuildValue("is#", |
| 511 | addr->sa_family, |
| 512 | addr->sa_data, |
| 513 | sizeof(addr->sa_data)); |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 514 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 515 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 518 | |
| 519 | /* Parse a socket address argument according to the socket object's |
| 520 | address family. Return 1 if the address was in the proper format, |
| 521 | 0 of not. The address is returned through addr_ret, its length |
| 522 | through len_ret. */ |
| 523 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 524 | static int |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 525 | BUILD_FUNC_DEF_4( |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 526 | 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] | 527 | { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 528 | switch (s->sock_family) { |
| 529 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 530 | #ifdef AF_UNIX |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 531 | case AF_UNIX: |
| 532 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 533 | struct sockaddr_un* addr; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 534 | char *path; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 535 | int len; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 536 | addr = (struct sockaddr_un* )&(s->sock_addr).un; |
Guido van Rossum | 7e48898 | 1998-10-08 02:25:24 +0000 | [diff] [blame] | 537 | if (!PyArg_Parse(args, "t#", &path, &len)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 538 | return 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 539 | if (len > sizeof addr->sun_path) { |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 540 | PyErr_SetString(PySocket_Error, |
| 541 | "AF_UNIX path too long"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 542 | return 0; |
| 543 | } |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 544 | addr->sun_family = AF_UNIX; |
| 545 | memcpy(addr->sun_path, path, len); |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 546 | addr->sun_path[len] = 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 547 | *addr_ret = (struct sockaddr *) addr; |
Guido van Rossum | 65af28a | 1996-06-11 18:36:33 +0000 | [diff] [blame] | 548 | *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 549 | return 1; |
| 550 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 551 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 552 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 553 | case AF_INET: |
| 554 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 555 | struct sockaddr_in* addr; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 556 | char *host; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 557 | int port; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 558 | addr=(struct sockaddr_in*)&(s->sock_addr).in; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 559 | if (!PyArg_Parse(args, "(si)", &host, &port)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 560 | return 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 561 | if (setipaddr(host, addr) < 0) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 562 | return 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 563 | addr->sin_family = AF_INET; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 564 | addr->sin_port = htons((short)port); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 565 | *addr_ret = (struct sockaddr *) addr; |
| 566 | *len_ret = sizeof *addr; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 567 | return 1; |
| 568 | } |
| 569 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 570 | /* More cases here... */ |
| 571 | |
| 572 | default: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 573 | PyErr_SetString(PySocket_Error, "getsockaddrarg: bad family"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 574 | return 0; |
| 575 | |
| 576 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 579 | |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 580 | /* Get the address length according to the socket object's address family. |
| 581 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 582 | through len_ret. */ |
| 583 | |
| 584 | static int |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 585 | BUILD_FUNC_DEF_2(getsockaddrlen,PySocketSockObject *,s, int *,len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 586 | { |
| 587 | switch (s->sock_family) { |
| 588 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 589 | #ifdef AF_UNIX |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 590 | case AF_UNIX: |
| 591 | { |
| 592 | *len_ret = sizeof (struct sockaddr_un); |
| 593 | return 1; |
| 594 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 595 | #endif /* AF_UNIX */ |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 596 | |
| 597 | case AF_INET: |
| 598 | { |
| 599 | *len_ret = sizeof (struct sockaddr_in); |
| 600 | return 1; |
| 601 | } |
| 602 | |
| 603 | /* More cases here... */ |
| 604 | |
| 605 | default: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 606 | PyErr_SetString(PySocket_Error, "getsockaddrarg: bad family"); |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 607 | return 0; |
| 608 | |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 613 | /* s.accept() method */ |
| 614 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 615 | static PyObject * |
| 616 | BUILD_FUNC_DEF_2(PySocketSock_accept,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 617 | { |
| 618 | char addrbuf[256]; |
| 619 | int addrlen, newfd; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 620 | PyObject *sock = NULL; |
| 621 | PyObject *addr = NULL; |
| 622 | PyObject *res = NULL; |
| 623 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 624 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 625 | return NULL; |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 626 | if (!getsockaddrlen(s, &addrlen)) |
| 627 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 628 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 629 | newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 630 | Py_END_ALLOW_THREADS |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 631 | if (newfd < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 632 | return PySocket_Err(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 633 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 634 | /* Create the new object with unspecified family, |
| 635 | to avoid calls to bind() etc. on it. */ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 636 | sock = (PyObject *) PySocketSock_New(newfd, |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 637 | s->sock_family, |
| 638 | s->sock_type, |
| 639 | s->sock_proto); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 640 | if (sock == NULL) { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 641 | close(newfd); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 642 | goto finally; |
| 643 | } |
| 644 | if (!(addr = makesockaddr((struct sockaddr *) addrbuf, addrlen))) |
| 645 | goto finally; |
| 646 | |
| 647 | if (!(res = Py_BuildValue("OO", sock, addr))) |
| 648 | goto finally; |
| 649 | |
| 650 | finally: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 651 | Py_XDECREF(sock); |
| 652 | Py_XDECREF(addr); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 653 | return res; |
| 654 | } |
| 655 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 656 | static char accept_doc[] = |
| 657 | "accept() -> (socket object, address info)\n\ |
| 658 | \n\ |
| 659 | Wait for an incoming connection. Return a new socket representing the\n\ |
| 660 | connection, and the address of the client. For IP sockets, the address\n\ |
| 661 | info is a pair (hostaddr, port)."; |
| 662 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 663 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 664 | /* s.setblocking(1 | 0) method */ |
| 665 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 666 | static PyObject * |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 667 | BUILD_FUNC_DEF_2(PySocketSock_setblocking,PySocketSockObject*,s,PyObject*,args) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 668 | { |
| 669 | int block; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 670 | #ifndef MS_WINDOWS |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 671 | int delay_flag; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 672 | #endif |
Guido van Rossum | fe28fa0 | 1997-05-15 19:09:26 +0000 | [diff] [blame] | 673 | if (!PyArg_Parse(args, "i", &block)) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 674 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 675 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 676 | #ifdef __BEOS__ |
| 677 | block = !block; |
| 678 | setsockopt( s->sock_fd, SOL_SOCKET, SO_NONBLOCK, |
| 679 | (void *)(&block), sizeof( int ) ); |
| 680 | #else |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 681 | #ifndef MS_WINDOWS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 682 | #ifdef PYOS_OS2 |
| 683 | block = !block; |
| 684 | ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block)); |
| 685 | #else /* !PYOS_OS2 */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 686 | delay_flag = fcntl (s->sock_fd, F_GETFL, 0); |
| 687 | if (block) |
| 688 | delay_flag &= (~O_NDELAY); |
| 689 | else |
| 690 | delay_flag |= O_NDELAY; |
| 691 | fcntl (s->sock_fd, F_SETFL, delay_flag); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 692 | #endif /* !PYOS_OS2 */ |
| 693 | #else /* MS_WINDOWS */ |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 694 | block = !block; |
| 695 | ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 696 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 697 | #endif /* __BEOS__ */ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 698 | Py_END_ALLOW_THREADS |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 699 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 700 | Py_INCREF(Py_None); |
| 701 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 702 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 703 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 704 | static char setblocking_doc[] = |
| 705 | "setblocking(flag)\n\ |
| 706 | \n\ |
| 707 | Set the socket to blocking (flag is true) or non-blocking (false).\n\ |
| 708 | This uses the FIONBIO ioctl with the O_NDELAY flag."; |
| 709 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 710 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 711 | /* s.setsockopt() method. |
| 712 | With an integer third argument, sets an integer option. |
| 713 | With a string third argument, sets an option from a buffer; |
| 714 | use optional built-in module 'struct' to encode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 715 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 716 | static PyObject * |
| 717 | BUILD_FUNC_DEF_2(PySocketSock_setsockopt,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 718 | { |
| 719 | int level; |
| 720 | int optname; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 721 | int res; |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 722 | char *buf; |
| 723 | int buflen; |
| 724 | int flag; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 725 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 726 | if (PyArg_Parse(args, "(iii)", &level, &optname, &flag)) { |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 727 | buf = (char *) &flag; |
| 728 | buflen = sizeof flag; |
| 729 | } |
| 730 | else { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 731 | PyErr_Clear(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 732 | if (!PyArg_Parse(args, "(iis#)", &level, &optname, |
| 733 | &buf, &buflen)) |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 734 | return NULL; |
| 735 | } |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 736 | res = setsockopt(s->sock_fd, level, optname, (ANY *)buf, buflen); |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 737 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 738 | return PySocket_Err(); |
| 739 | Py_INCREF(Py_None); |
| 740 | return Py_None; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 743 | static char setsockopt_doc[] = |
| 744 | "setsockopt(level, option, value)\n\ |
| 745 | \n\ |
| 746 | Set a socket option. See the Unix manual for level and option.\n\ |
| 747 | The value argument can either be an integer or a string."; |
| 748 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 749 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 750 | /* s.getsockopt() method. |
| 751 | With two arguments, retrieves an integer option. |
| 752 | With a third integer argument, retrieves a string buffer of that size; |
| 753 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 754 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 755 | static PyObject * |
| 756 | BUILD_FUNC_DEF_2(PySocketSock_getsockopt,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 757 | { |
| 758 | int level; |
| 759 | int optname; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 760 | int res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 761 | PyObject *buf; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 762 | int buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 763 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 764 | #ifdef __BEOS__ |
| 765 | /* We have incomplete socket support. */ |
| 766 | PyErr_SetString( PySocket_Error, "getsockopt not supported" ); |
| 767 | return NULL; |
| 768 | #else |
| 769 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 770 | if (!PyArg_ParseTuple(args, "ii|i", &level, &optname, &buflen)) |
| 771 | return NULL; |
| 772 | |
| 773 | if (buflen == 0) { |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 774 | int flag = 0; |
| 775 | int flagsize = sizeof flag; |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 776 | res = getsockopt(s->sock_fd, level, optname, |
| 777 | (ANY *)&flag, &flagsize); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 778 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 779 | return PySocket_Err(); |
| 780 | return PyInt_FromLong(flag); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 781 | } |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 782 | if (buflen <= 0 || buflen > 1024) { |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 783 | PyErr_SetString(PySocket_Error, |
| 784 | "getsockopt buflen out of range"); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 785 | return NULL; |
| 786 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 787 | buf = PyString_FromStringAndSize((char *)NULL, buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 788 | if (buf == NULL) |
| 789 | return NULL; |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 790 | res = getsockopt(s->sock_fd, level, optname, |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 791 | (ANY *)PyString_AsString(buf), &buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 792 | if (res < 0) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 793 | Py_DECREF(buf); |
| 794 | return PySocket_Err(); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 795 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 796 | _PyString_Resize(&buf, buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 797 | return buf; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 798 | #endif /* __BEOS__ */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 801 | static char getsockopt_doc[] = |
| 802 | "getsockopt(level, option[, buffersize]) -> value\n\ |
| 803 | \n\ |
| 804 | Get a socket option. See the Unix manual for level and option.\n\ |
| 805 | If a nonzero buffersize argument is given, the return value is a\n\ |
| 806 | string of that length; otherwise it is an integer."; |
| 807 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 808 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 809 | /* s.bind(sockaddr) method */ |
| 810 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 811 | static PyObject * |
| 812 | BUILD_FUNC_DEF_2(PySocketSock_bind,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 813 | { |
| 814 | struct sockaddr *addr; |
| 815 | int addrlen; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 816 | int res; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 817 | if (!getsockaddrarg(s, args, &addr, &addrlen)) |
| 818 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 819 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 820 | res = bind(s->sock_fd, addr, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 821 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 822 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 823 | return PySocket_Err(); |
| 824 | Py_INCREF(Py_None); |
| 825 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 828 | static char bind_doc[] = |
| 829 | "bind(address)\n\ |
| 830 | \n\ |
| 831 | Bind the socket to a local address. For IP sockets, the address is a\n\ |
| 832 | pair (host, port); the host must refer to the local host."; |
| 833 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 834 | |
| 835 | /* s.close() method. |
| 836 | Set the file descriptor to -1 so operations tried subsequently |
| 837 | will surely fail. */ |
| 838 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 839 | static PyObject * |
| 840 | BUILD_FUNC_DEF_2(PySocketSock_close,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 841 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 842 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 843 | return NULL; |
Guido van Rossum | 0848146 | 1996-10-12 14:07:22 +0000 | [diff] [blame] | 844 | if (s->sock_fd != -1) { |
| 845 | Py_BEGIN_ALLOW_THREADS |
| 846 | (void) close(s->sock_fd); |
| 847 | Py_END_ALLOW_THREADS |
| 848 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 849 | s->sock_fd = -1; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 850 | Py_INCREF(Py_None); |
| 851 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 854 | static char close_doc[] = |
| 855 | "close()\n\ |
| 856 | \n\ |
| 857 | Close the socket. It cannot be used after this call."; |
| 858 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 859 | |
| 860 | /* s.connect(sockaddr) method */ |
| 861 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 862 | static PyObject * |
| 863 | BUILD_FUNC_DEF_2(PySocketSock_connect,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 864 | { |
| 865 | struct sockaddr *addr; |
| 866 | int addrlen; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 867 | int res; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 868 | if (!getsockaddrarg(s, args, &addr, &addrlen)) |
| 869 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 870 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 871 | res = connect(s->sock_fd, addr, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 872 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 873 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 874 | return PySocket_Err(); |
| 875 | Py_INCREF(Py_None); |
| 876 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 879 | static char connect_doc[] = |
| 880 | "connect(address)\n\ |
| 881 | \n\ |
| 882 | Connect the socket to a remote address. For IP sockets, the address\n\ |
| 883 | is a pair (host, port)."; |
| 884 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 885 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 886 | /* s.connect_ex(sockaddr) method */ |
| 887 | |
| 888 | static PyObject * |
| 889 | BUILD_FUNC_DEF_2(PySocketSock_connect_ex,PySocketSockObject *,s, PyObject *,args) |
| 890 | { |
| 891 | struct sockaddr *addr; |
| 892 | int addrlen; |
| 893 | int res; |
| 894 | if (!getsockaddrarg(s, args, &addr, &addrlen)) |
| 895 | return NULL; |
| 896 | Py_BEGIN_ALLOW_THREADS |
| 897 | res = connect(s->sock_fd, addr, addrlen); |
| 898 | Py_END_ALLOW_THREADS |
| 899 | if (res != 0) |
| 900 | res = errno; |
| 901 | return PyInt_FromLong((long) res); |
| 902 | } |
| 903 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 904 | static char connect_ex_doc[] = |
| 905 | "connect_ex(address)\n\ |
| 906 | \n\ |
| 907 | This is like connect(address), but returns an error code (the errno value)\n\ |
| 908 | instead of raising an exception when an error occurs."; |
| 909 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 910 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 911 | /* s.fileno() method */ |
| 912 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 913 | static PyObject * |
| 914 | BUILD_FUNC_DEF_2(PySocketSock_fileno,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 915 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 916 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 917 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 918 | return PyInt_FromLong((long) s->sock_fd); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 921 | static char fileno_doc[] = |
| 922 | "fileno() -> integer\n\ |
| 923 | \n\ |
| 924 | Return the integer file descriptor of the socket."; |
| 925 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 926 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 927 | #ifndef NO_DUP |
| 928 | /* s.dup() method */ |
| 929 | |
| 930 | static PyObject * |
| 931 | BUILD_FUNC_DEF_2(PySocketSock_dup,PySocketSockObject *,s, PyObject *,args) |
| 932 | { |
| 933 | int newfd; |
| 934 | PyObject *sock; |
| 935 | if (!PyArg_NoArgs(args)) |
| 936 | return NULL; |
| 937 | newfd = dup(s->sock_fd); |
| 938 | if (newfd < 0) |
| 939 | return PySocket_Err(); |
| 940 | sock = (PyObject *) PySocketSock_New(newfd, |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 941 | s->sock_family, |
| 942 | s->sock_type, |
| 943 | s->sock_proto); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 944 | if (sock == NULL) |
| 945 | close(newfd); |
| 946 | return sock; |
| 947 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 948 | |
| 949 | static char dup_doc[] = |
| 950 | "dup() -> socket object\n\ |
| 951 | \n\ |
| 952 | Return a new socket object connected to the same system resource."; |
| 953 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 954 | #endif |
| 955 | |
| 956 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 957 | /* s.getsockname() method */ |
| 958 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 959 | static PyObject * |
| 960 | BUILD_FUNC_DEF_2(PySocketSock_getsockname,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 961 | { |
| 962 | char addrbuf[256]; |
| 963 | int addrlen, res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 964 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 965 | return NULL; |
| 966 | if (!getsockaddrlen(s, &addrlen)) |
| 967 | return NULL; |
Guido van Rossum | ca42b16 | 1996-01-12 01:36:05 +0000 | [diff] [blame] | 968 | memset(addrbuf, 0, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 969 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 970 | res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 971 | Py_END_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 972 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 973 | return PySocket_Err(); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 974 | return makesockaddr((struct sockaddr *) addrbuf, addrlen); |
| 975 | } |
| 976 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 977 | static char getsockname_doc[] = |
| 978 | "getsockname() -> address info\n\ |
| 979 | \n\ |
| 980 | Return the address of the local endpoint. For IP sockets, the address\n\ |
| 981 | info is a pair (hostaddr, port)."; |
| 982 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 983 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 984 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 985 | /* s.getpeername() method */ |
| 986 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 987 | static PyObject * |
| 988 | BUILD_FUNC_DEF_2(PySocketSock_getpeername,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 989 | { |
| 990 | char addrbuf[256]; |
| 991 | int addrlen, res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 992 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 993 | return NULL; |
| 994 | if (!getsockaddrlen(s, &addrlen)) |
| 995 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 996 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 997 | res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 998 | Py_END_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 999 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1000 | return PySocket_Err(); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1001 | return makesockaddr((struct sockaddr *) addrbuf, addrlen); |
| 1002 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1003 | |
| 1004 | static char getpeername_doc[] = |
| 1005 | "getpeername() -> address info\n\ |
| 1006 | \n\ |
| 1007 | Return the address of the remote endpoint. For IP sockets, the address\n\ |
| 1008 | info is a pair (hostaddr, port)."; |
| 1009 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1010 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1011 | |
| 1012 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1013 | /* s.listen(n) method */ |
| 1014 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1015 | static PyObject * |
| 1016 | BUILD_FUNC_DEF_2(PySocketSock_listen,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1017 | { |
| 1018 | int backlog; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1019 | int res; |
Guido van Rossum | fe28fa0 | 1997-05-15 19:09:26 +0000 | [diff] [blame] | 1020 | if (!PyArg_Parse(args, "i", &backlog)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1021 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1022 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1023 | if (backlog < 1) |
| 1024 | backlog = 1; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1025 | res = listen(s->sock_fd, backlog); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1026 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1027 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1028 | return PySocket_Err(); |
| 1029 | Py_INCREF(Py_None); |
| 1030 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1033 | static char listen_doc[] = |
| 1034 | "listen(backlog)\n\ |
| 1035 | \n\ |
| 1036 | Enable a server to accept connections. The backlog argument must be at\n\ |
| 1037 | least 1; it specifies the number of unaccepted connection that the system\n\ |
| 1038 | will allow before refusing new connections."; |
| 1039 | |
| 1040 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1041 | #ifndef NO_DUP |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1042 | /* s.makefile(mode) method. |
| 1043 | Create a new open file object referring to a dupped version of |
| 1044 | the socket's file descriptor. (The dup() call is necessary so |
| 1045 | that the open file and socket objects may be closed independent |
| 1046 | of each other.) |
| 1047 | The mode argument specifies 'r' or 'w' passed to fdopen(). */ |
| 1048 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1049 | static PyObject * |
| 1050 | BUILD_FUNC_DEF_2(PySocketSock_makefile,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1051 | { |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 1052 | extern int fclose Py_PROTO((FILE *)); |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 1053 | char *mode = "r"; |
| 1054 | int bufsize = -1; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1055 | int fd; |
| 1056 | FILE *fp; |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 1057 | PyObject *f; |
| 1058 | |
| 1059 | if (!PyArg_ParseTuple(args, "|si", &mode, &bufsize)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1060 | return NULL; |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1061 | #ifdef MS_WIN32 |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1062 | if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) || |
| 1063 | ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL)) |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 1064 | #else |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1065 | if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL) |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 1066 | #endif |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1067 | { |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 1068 | if (fd >= 0) |
| 1069 | close(fd); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1070 | return PySocket_Err(); |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 1071 | } |
| 1072 | f = PyFile_FromFile(fp, "<socket>", mode, fclose); |
| 1073 | if (f != NULL) |
| 1074 | PyFile_SetBufSize(f, bufsize); |
| 1075 | return f; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1076 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1077 | |
| 1078 | static char makefile_doc[] = |
| 1079 | "makefile([mode[, buffersize]]) -> file object\n\ |
| 1080 | \n\ |
| 1081 | Return a regular file object corresponding to the socket.\n\ |
| 1082 | The mode and buffersize arguments are as for the built-in open() function."; |
| 1083 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1084 | #endif /* NO_DUP */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1085 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1086 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1087 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1088 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1089 | static PyObject * |
| 1090 | BUILD_FUNC_DEF_2(PySocketSock_recv,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1091 | { |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1092 | int len, n, flags = 0; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1093 | PyObject *buf; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1094 | if (!PyArg_ParseTuple(args, "i|i", &len, &flags)) |
| 1095 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1096 | buf = PyString_FromStringAndSize((char *) 0, len); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1097 | if (buf == NULL) |
| 1098 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1099 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 1100 | n = recv(s->sock_fd, PyString_AsString(buf), len, flags); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1101 | Py_END_ALLOW_THREADS |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 1102 | if (n < 0) { |
| 1103 | Py_DECREF(buf); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1104 | return PySocket_Err(); |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 1105 | } |
Guido van Rossum | 53a9bf8 | 1996-06-11 18:35:24 +0000 | [diff] [blame] | 1106 | if (n != len && _PyString_Resize(&buf, n) < 0) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1107 | return NULL; |
| 1108 | return buf; |
| 1109 | } |
| 1110 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1111 | static char recv_doc[] = |
| 1112 | "recv(buffersize[, flags]) -> data\n\ |
| 1113 | \n\ |
| 1114 | Receive up to buffersize bytes from the socket. For the optional flags\n\ |
| 1115 | argument, see the Unix manual. When no data is available, block until\n\ |
| 1116 | at least one byte is available or until the remote end is closed. When\n\ |
| 1117 | the remote end is closed and all data is read, return the empty string."; |
| 1118 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1119 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1120 | /* s.recvfrom(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1121 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1122 | static PyObject * |
| 1123 | BUILD_FUNC_DEF_2(PySocketSock_recvfrom,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1124 | { |
| 1125 | char addrbuf[256]; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1126 | PyObject *buf = NULL; |
| 1127 | PyObject *addr = NULL; |
| 1128 | PyObject *ret = NULL; |
| 1129 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1130 | int addrlen, len, n, flags = 0; |
| 1131 | if (!PyArg_ParseTuple(args, "i|i", &len, &flags)) |
| 1132 | return NULL; |
Guido van Rossum | 18c9a4f | 1993-05-25 12:16:29 +0000 | [diff] [blame] | 1133 | if (!getsockaddrlen(s, &addrlen)) |
| 1134 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1135 | buf = PyString_FromStringAndSize((char *) 0, len); |
Guido van Rossum | 18c9a4f | 1993-05-25 12:16:29 +0000 | [diff] [blame] | 1136 | if (buf == NULL) |
| 1137 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1138 | Py_BEGIN_ALLOW_THREADS |
| 1139 | n = recvfrom(s->sock_fd, PyString_AsString(buf), len, flags, |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1140 | #ifndef MS_WINDOWS |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 1141 | #if defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1142 | (struct sockaddr *)addrbuf, &addrlen |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 1143 | #else |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1144 | (ANY *)addrbuf, &addrlen |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 1145 | #endif |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1146 | #else |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1147 | (struct sockaddr *)addrbuf, &addrlen |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1148 | #endif |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1149 | ); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1150 | Py_END_ALLOW_THREADS |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 1151 | if (n < 0) { |
| 1152 | Py_DECREF(buf); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1153 | return PySocket_Err(); |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 1154 | } |
Guido van Rossum | 53a9bf8 | 1996-06-11 18:35:24 +0000 | [diff] [blame] | 1155 | if (n != len && _PyString_Resize(&buf, n) < 0) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1156 | return NULL; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1157 | |
| 1158 | if (!(addr = makesockaddr((struct sockaddr *)addrbuf, addrlen))) |
| 1159 | goto finally; |
| 1160 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1161 | ret = Py_BuildValue("OO", buf, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1162 | finally: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1163 | Py_XDECREF(addr); |
| 1164 | Py_XDECREF(buf); |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 1165 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1168 | static char recvfrom_doc[] = |
| 1169 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
| 1170 | \n\ |
| 1171 | Like recv(buffersize, flags) but also return the sender's address info."; |
| 1172 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1173 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1174 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1175 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1176 | static PyObject * |
| 1177 | BUILD_FUNC_DEF_2(PySocketSock_send,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1178 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1179 | char *buf; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1180 | int len, n, flags = 0; |
| 1181 | if (!PyArg_ParseTuple(args, "s#|i", &buf, &len, &flags)) |
| 1182 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1183 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1184 | n = send(s->sock_fd, buf, len, flags); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1185 | Py_END_ALLOW_THREADS |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1186 | if (n < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1187 | return PySocket_Err(); |
| 1188 | return PyInt_FromLong((long)n); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1191 | static char send_doc[] = |
| 1192 | "send(data[, flags])\n\ |
| 1193 | \n\ |
| 1194 | Send a data string to the socket. For the optional flags\n\ |
| 1195 | argument, see the Unix manual."; |
| 1196 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1197 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1198 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1199 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1200 | static PyObject * |
| 1201 | BUILD_FUNC_DEF_2(PySocketSock_sendto,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1202 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1203 | PyObject *addro; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1204 | char *buf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1205 | struct sockaddr *addr; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1206 | int addrlen, len, n, flags; |
| 1207 | flags = 0; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1208 | if (!PyArg_Parse(args, "(s#O)", &buf, &len, &addro)) { |
| 1209 | PyErr_Clear(); |
| 1210 | if (!PyArg_Parse(args, "(s#iO)", &buf, &len, &flags, &addro)) |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1211 | return NULL; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1212 | } |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1213 | if (!getsockaddrarg(s, addro, &addr, &addrlen)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1214 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1215 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1216 | n = sendto(s->sock_fd, buf, len, flags, addr, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1217 | Py_END_ALLOW_THREADS |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1218 | if (n < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1219 | return PySocket_Err(); |
| 1220 | return PyInt_FromLong((long)n); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1223 | static char sendto_doc[] = |
| 1224 | "sendto(data[, flags], address)\n\ |
| 1225 | \n\ |
| 1226 | Like send(data, flags) but allows specifying the destination address.\n\ |
| 1227 | For IP sockets, the address is a pair (hostaddr, port)."; |
| 1228 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1229 | |
| 1230 | /* s.shutdown(how) method */ |
| 1231 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1232 | static PyObject * |
| 1233 | BUILD_FUNC_DEF_2(PySocketSock_shutdown,PySocketSockObject *,s, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1234 | { |
| 1235 | int how; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1236 | int res; |
Guido van Rossum | fe28fa0 | 1997-05-15 19:09:26 +0000 | [diff] [blame] | 1237 | if (!PyArg_Parse(args, "i", &how)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1238 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1239 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1240 | res = shutdown(s->sock_fd, how); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1241 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1242 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1243 | return PySocket_Err(); |
| 1244 | Py_INCREF(Py_None); |
| 1245 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1248 | static char shutdown_doc[] = |
| 1249 | "shutdown(flag)\n\ |
| 1250 | \n\ |
| 1251 | Shut down the reading side of the socket (flag == 0), the writing side\n\ |
| 1252 | of the socket (flag == 1), or both ends (flag == 2)."; |
| 1253 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1254 | |
| 1255 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1256 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1257 | static PyMethodDef PySocketSock_methods[] = { |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1258 | {"accept", (PyCFunction)PySocketSock_accept, 0, |
| 1259 | accept_doc}, |
| 1260 | {"bind", (PyCFunction)PySocketSock_bind, 0, |
| 1261 | bind_doc}, |
| 1262 | {"close", (PyCFunction)PySocketSock_close, 0, |
| 1263 | close_doc}, |
| 1264 | {"connect", (PyCFunction)PySocketSock_connect, 0, |
| 1265 | connect_doc}, |
| 1266 | {"connect_ex", (PyCFunction)PySocketSock_connect_ex, 0, |
| 1267 | connect_ex_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1268 | #ifndef NO_DUP |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1269 | {"dup", (PyCFunction)PySocketSock_dup, 0, |
| 1270 | dup_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1271 | #endif |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1272 | {"fileno", (PyCFunction)PySocketSock_fileno, 0, |
| 1273 | fileno_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1274 | #ifdef HAVE_GETPEERNAME |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1275 | {"getpeername", (PyCFunction)PySocketSock_getpeername, 0, |
| 1276 | getpeername_doc}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 1277 | #endif |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1278 | {"getsockname", (PyCFunction)PySocketSock_getsockname, 0, |
| 1279 | getsockname_doc}, |
| 1280 | {"getsockopt", (PyCFunction)PySocketSock_getsockopt, 1, |
| 1281 | getsockopt_doc}, |
| 1282 | {"listen", (PyCFunction)PySocketSock_listen, 0, |
| 1283 | listen_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1284 | #ifndef NO_DUP |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1285 | {"makefile", (PyCFunction)PySocketSock_makefile, 1, |
| 1286 | makefile_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1287 | #endif |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1288 | {"recv", (PyCFunction)PySocketSock_recv, 1, |
| 1289 | recv_doc}, |
| 1290 | {"recvfrom", (PyCFunction)PySocketSock_recvfrom, 1, |
| 1291 | recvfrom_doc}, |
| 1292 | {"send", (PyCFunction)PySocketSock_send, 1, |
| 1293 | send_doc}, |
| 1294 | {"sendto", (PyCFunction)PySocketSock_sendto, 0, |
| 1295 | sendto_doc}, |
| 1296 | {"setblocking", (PyCFunction)PySocketSock_setblocking, 0, |
| 1297 | setblocking_doc}, |
| 1298 | {"setsockopt", (PyCFunction)PySocketSock_setsockopt, 0, |
| 1299 | setsockopt_doc}, |
| 1300 | {"shutdown", (PyCFunction)PySocketSock_shutdown, 0, |
| 1301 | shutdown_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1302 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1303 | }; |
| 1304 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1305 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1306 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1307 | First close the file description. */ |
| 1308 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1309 | static void |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1310 | BUILD_FUNC_DEF_1(PySocketSock_dealloc,PySocketSockObject *,s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1311 | { |
| 1312 | (void) close(s->sock_fd); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1313 | PyMem_DEL(s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1316 | |
| 1317 | /* Return a socket object's named attribute. */ |
| 1318 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1319 | static PyObject * |
| 1320 | BUILD_FUNC_DEF_2(PySocketSock_getattr,PySocketSockObject *,s, char *,name) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1321 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1322 | return Py_FindMethod(PySocketSock_methods, (PyObject *) s, name); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1325 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1326 | static PyObject * |
| 1327 | BUILD_FUNC_DEF_1(PySocketSock_repr,PySocketSockObject *,s) |
| 1328 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1329 | char buf[512]; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1330 | sprintf(buf, |
| 1331 | "<socket object, fd=%d, family=%d, type=%d, protocol=%d>", |
| 1332 | 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] | 1333 | return PyString_FromString(buf); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1337 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1338 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1339 | static PyTypeObject PySocketSock_Type = { |
Guido van Rossum | 5555854 | 1996-05-23 22:54:50 +0000 | [diff] [blame] | 1340 | PyObject_HEAD_INIT(0) /* Must fill in type value later */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1341 | 0, |
| 1342 | "socket", |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1343 | sizeof(PySocketSockObject), |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1344 | 0, |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1345 | (destructor)PySocketSock_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1346 | 0, /*tp_print*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1347 | (getattrfunc)PySocketSock_getattr, /*tp_getattr*/ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1348 | 0, /*tp_setattr*/ |
| 1349 | 0, /*tp_compare*/ |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1350 | (reprfunc)PySocketSock_repr, /*tp_repr*/ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1351 | 0, /*tp_as_number*/ |
| 1352 | 0, /*tp_as_sequence*/ |
| 1353 | 0, /*tp_as_mapping*/ |
| 1354 | }; |
| 1355 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1356 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1357 | /* Python interface to gethostname(). */ |
| 1358 | |
| 1359 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1360 | static PyObject * |
| 1361 | BUILD_FUNC_DEF_2(PySocket_gethostname,PyObject *,self, PyObject *,args) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1362 | { |
| 1363 | char buf[1024]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1364 | int res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1365 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1366 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1367 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1368 | res = gethostname(buf, (int) sizeof buf - 1); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1369 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1370 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1371 | return PySocket_Err(); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1372 | buf[sizeof buf - 1] = '\0'; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1373 | return PyString_FromString(buf); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1374 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1375 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1376 | static char gethostname_doc[] = |
| 1377 | "gethostname() -> string\n\ |
| 1378 | \n\ |
| 1379 | Return the current host name."; |
| 1380 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1381 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1382 | /* Python interface to gethostbyname(name). */ |
| 1383 | |
| 1384 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1385 | static PyObject * |
| 1386 | BUILD_FUNC_DEF_2(PySocket_gethostbyname,PyObject *,self, PyObject *,args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1387 | { |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1388 | char *name; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1389 | struct sockaddr_in addrbuf; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1390 | if (!PyArg_Parse(args, "s", &name)) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1391 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1392 | if (setipaddr(name, &addrbuf) < 0) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1393 | return NULL; |
| 1394 | return makeipaddr(&addrbuf); |
| 1395 | } |
| 1396 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1397 | static char gethostbyname_doc[] = |
| 1398 | "gethostbyname(host) -> address\n\ |
| 1399 | \n\ |
| 1400 | Return the IP address (a string of the form '255.255.255.255') for a host."; |
| 1401 | |
| 1402 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1403 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
| 1404 | |
| 1405 | static PyObject * |
| 1406 | gethost_common(h, addr) |
| 1407 | struct hostent *h; |
| 1408 | struct sockaddr_in *addr; |
| 1409 | { |
| 1410 | char **pch; |
| 1411 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 1412 | PyObject *name_list = (PyObject *)NULL; |
| 1413 | PyObject *addr_list = (PyObject *)NULL; |
| 1414 | PyObject *tmp; |
| 1415 | if (h == NULL) { |
| 1416 | #ifdef HAVE_HSTRERROR |
| 1417 | /* Let's get real error message to return */ |
| 1418 | extern int h_errno; |
| 1419 | PyErr_SetString(PySocket_Error, (char *)hstrerror(h_errno)); |
| 1420 | #else |
| 1421 | PyErr_SetString(PySocket_Error, "host not found"); |
| 1422 | #endif |
| 1423 | return NULL; |
| 1424 | } |
| 1425 | if ((name_list = PyList_New(0)) == NULL) |
| 1426 | goto err; |
| 1427 | if ((addr_list = PyList_New(0)) == NULL) |
| 1428 | goto err; |
| 1429 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 1430 | int status; |
| 1431 | tmp = PyString_FromString(*pch); |
| 1432 | if (tmp == NULL) |
| 1433 | goto err; |
| 1434 | status = PyList_Append(name_list, tmp); |
| 1435 | Py_DECREF(tmp); |
| 1436 | if (status) |
| 1437 | goto err; |
| 1438 | } |
| 1439 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 1440 | int status; |
| 1441 | memcpy((char *) &addr->sin_addr, *pch, h->h_length); |
| 1442 | tmp = makeipaddr(addr); |
| 1443 | if (tmp == NULL) |
| 1444 | goto err; |
| 1445 | status = PyList_Append(addr_list, tmp); |
| 1446 | Py_DECREF(tmp); |
| 1447 | if (status) |
| 1448 | goto err; |
| 1449 | } |
| 1450 | rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list); |
| 1451 | err: |
| 1452 | Py_XDECREF(name_list); |
| 1453 | Py_XDECREF(addr_list); |
| 1454 | return rtn_tuple; |
| 1455 | } |
| 1456 | |
| 1457 | |
| 1458 | /* Python interface to gethostbyname_ex(name). */ |
| 1459 | |
| 1460 | /*ARGSUSED*/ |
| 1461 | static PyObject * |
| 1462 | BUILD_FUNC_DEF_2(PySocket_gethostbyname_ex,PyObject *,self, PyObject *,args) |
| 1463 | { |
| 1464 | char *name; |
| 1465 | struct hostent *h; |
| 1466 | struct sockaddr_in addr; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 1467 | PyObject *ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1468 | #ifdef HAVE_GETHOSTBYNAME_R |
| 1469 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1470 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
| 1471 | struct hostent_data data; |
| 1472 | #else |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1473 | char buf[16384]; |
| 1474 | int buf_len = (sizeof buf) - 1; |
| 1475 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1476 | #endif |
| 1477 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 1478 | int result; |
| 1479 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1480 | #endif /* HAVE_GETHOSTBYNAME_R */ |
| 1481 | if (!PyArg_Parse(args, "s", &name)) |
| 1482 | return NULL; |
| 1483 | if (setipaddr(name, &addr) < 0) |
| 1484 | return NULL; |
| 1485 | Py_BEGIN_ALLOW_THREADS |
| 1486 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1487 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 1488 | result = gethostbyname_r(name, &hp_allocated, buf, buf_len, &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1489 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1490 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1491 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 1492 | memset((void *) &data, '\0', sizeof(data)); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1493 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 1494 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 1495 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1496 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 1497 | #ifdef USE_GETHOSTBYNAME_LOCK |
| 1498 | PyThread_acquire_lock(gethostbyname_lock, 1); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1499 | #endif |
| 1500 | h = gethostbyname(name); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1501 | #endif /* HAVE_GETHOSTBYNAME_R */ |
| 1502 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 1503 | ret = gethost_common(h, &addr); |
| 1504 | #ifdef USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 1505 | PyThread_release_lock(gethostbyname_lock); |
| 1506 | #endif |
| 1507 | return ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | static char ghbn_ex_doc[] = |
| 1511 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
| 1512 | \n\ |
| 1513 | Return the true host name, a list of aliases, and a list of IP addresses,\n\ |
| 1514 | for a host. The host argument is a string giving a host name or IP number."; |
| 1515 | |
| 1516 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1517 | /* Python interface to gethostbyaddr(IP). */ |
| 1518 | |
| 1519 | /*ARGSUSED*/ |
| 1520 | static PyObject * |
| 1521 | BUILD_FUNC_DEF_2(PySocket_gethostbyaddr,PyObject *,self, PyObject *, args) |
| 1522 | { |
| 1523 | struct sockaddr_in addr; |
| 1524 | char *ip_num; |
| 1525 | struct hostent *h; |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 1526 | PyObject *ret; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 1527 | #ifdef HAVE_GETHOSTBYNAME_R |
| 1528 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1529 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
| 1530 | struct hostent_data data; |
| 1531 | #else |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 1532 | char buf[16384]; |
| 1533 | int buf_len = (sizeof buf) - 1; |
| 1534 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1535 | #endif |
| 1536 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 1537 | int result; |
| 1538 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 1539 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1540 | |
| 1541 | if (!PyArg_Parse(args, "s", &ip_num)) |
| 1542 | return NULL; |
| 1543 | if (setipaddr(ip_num, &addr) < 0) |
| 1544 | return NULL; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 1545 | Py_BEGIN_ALLOW_THREADS |
| 1546 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1547 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 1548 | result = gethostbyaddr_r((char *)&addr.sin_addr, |
| 1549 | sizeof(addr.sin_addr), |
| 1550 | AF_INET, &hp_allocated, buf, buf_len, |
| 1551 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1552 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 1553 | h = gethostbyaddr_r((char *)&addr.sin_addr, |
| 1554 | sizeof(addr.sin_addr), |
| 1555 | AF_INET, |
| 1556 | &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1557 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 1558 | memset((void *) &data, '\0', sizeof(data)); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 1559 | result = gethostbyaddr_r((char *)&addr.sin_addr, |
| 1560 | sizeof(addr.sin_addr), |
| 1561 | AF_INET, &hp_allocated, &data); |
| 1562 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 1563 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 1564 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 1565 | #ifdef USE_GETHOSTBYNAME_LOCK |
| 1566 | PyThread_acquire_lock(gethostbyname_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 1567 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1568 | h = gethostbyaddr((char *)&addr.sin_addr, |
| 1569 | sizeof(addr.sin_addr), |
| 1570 | AF_INET); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 1571 | #endif /* HAVE_GETHOSTBYNAME_R */ |
| 1572 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 1573 | ret = gethost_common(h, &addr); |
| 1574 | #ifdef USE_GETHOSTBYNAME_LOCK |
| 1575 | PyThread_release_lock(gethostbyname_lock); |
| 1576 | #endif |
| 1577 | return ret; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1580 | static char gethostbyaddr_doc[] = |
| 1581 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
| 1582 | \n\ |
| 1583 | Return the true host name, a list of aliases, and a list of IP addresses,\n\ |
| 1584 | for a host. The host argument is a string giving a host name or IP number."; |
| 1585 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1586 | |
| 1587 | /* Python interface to getservbyname(name). |
| 1588 | This only returns the port number, since the other info is already |
| 1589 | known or not useful (like the list of aliases). */ |
| 1590 | |
| 1591 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1592 | static PyObject * |
| 1593 | BUILD_FUNC_DEF_2(PySocket_getservbyname,PyObject *,self, PyObject *,args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1594 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1595 | char *name, *proto; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1596 | struct servent *sp; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1597 | if (!PyArg_Parse(args, "(ss)", &name, &proto)) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1598 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1599 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1600 | sp = getservbyname(name, proto); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1601 | Py_END_ALLOW_THREADS |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1602 | if (sp == NULL) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1603 | PyErr_SetString(PySocket_Error, "service/proto not found"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1604 | return NULL; |
| 1605 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1606 | return PyInt_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1609 | static char getservbyname_doc[] = |
| 1610 | "getservbyname(servicename, protocolname) -> integer\n\ |
| 1611 | \n\ |
| 1612 | Return a port number from a service name and protocol name.\n\ |
| 1613 | The protocol name should be 'tcp' or 'udp'."; |
| 1614 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1615 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 1616 | /* Python interface to getprotobyname(name). |
| 1617 | This only returns the protocol number, since the other info is |
| 1618 | already known or not useful (like the list of aliases). */ |
| 1619 | |
| 1620 | /*ARGSUSED*/ |
| 1621 | static PyObject * |
| 1622 | BUILD_FUNC_DEF_2(PySocket_getprotobyname,PyObject *,self, PyObject *,args) |
| 1623 | { |
| 1624 | char *name; |
| 1625 | struct protoent *sp; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1626 | #ifdef __BEOS__ |
| 1627 | /* Not available in BeOS yet. - [cjh] */ |
| 1628 | PyErr_SetString( PySocket_Error, "getprotobyname not supported" ); |
| 1629 | return NULL; |
| 1630 | #else |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 1631 | if (!PyArg_Parse(args, "s", &name)) |
| 1632 | return NULL; |
| 1633 | Py_BEGIN_ALLOW_THREADS |
| 1634 | sp = getprotobyname(name); |
| 1635 | Py_END_ALLOW_THREADS |
| 1636 | if (sp == NULL) { |
| 1637 | PyErr_SetString(PySocket_Error, "protocol not found"); |
| 1638 | return NULL; |
| 1639 | } |
| 1640 | return PyInt_FromLong((long) sp->p_proto); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1641 | #endif |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1644 | static char getprotobyname_doc[] = |
| 1645 | "getprotobyname(name) -> integer\n\ |
| 1646 | \n\ |
| 1647 | Return the protocol number for the named protocol. (Rarely used.)"; |
| 1648 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 1649 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1650 | /* Python interface to socket(family, type, proto). |
| 1651 | The third (protocol) argument is optional. |
| 1652 | Return a new socket object. */ |
| 1653 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1654 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1655 | static PyObject * |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1656 | BUILD_FUNC_DEF_2(PySocket_socket,PyObject *,self, PyObject *,args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1657 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1658 | PySocketSockObject *s; |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 1659 | #ifdef MS_WINDOWS |
Guido van Rossum | d639d4d | 1996-06-12 04:04:22 +0000 | [diff] [blame] | 1660 | SOCKET fd; |
| 1661 | #else |
| 1662 | int fd; |
| 1663 | #endif |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1664 | int family, type, proto = 0; |
| 1665 | if (!PyArg_ParseTuple(args, "ii|i", &family, &type, &proto)) |
| 1666 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1667 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1668 | fd = socket(family, type, proto); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1669 | Py_END_ALLOW_THREADS |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 1670 | #ifdef MS_WINDOWS |
Guido van Rossum | d639d4d | 1996-06-12 04:04:22 +0000 | [diff] [blame] | 1671 | if (fd == INVALID_SOCKET) |
| 1672 | #else |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1673 | if (fd < 0) |
Guido van Rossum | d639d4d | 1996-06-12 04:04:22 +0000 | [diff] [blame] | 1674 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1675 | return PySocket_Err(); |
| 1676 | s = PySocketSock_New(fd, family, type, proto); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1677 | /* If the object can't be created, don't forget to close the |
| 1678 | file descriptor again! */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1679 | if (s == NULL) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1680 | (void) close(fd); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1681 | /* From now on, ignore SIGPIPE and let the error checking |
| 1682 | do the work. */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1683 | #ifdef SIGPIPE |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1684 | (void) signal(SIGPIPE, SIG_IGN); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1685 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1686 | return (PyObject *) s; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1689 | static char socket_doc[] = |
| 1690 | "socket(family, type[, proto]) -> socket object\n\ |
| 1691 | \n\ |
| 1692 | Open a socket of the given type. The family argument specifies the\n\ |
| 1693 | address family; it is normally AF_INET, sometimes AF_UNIX.\n\ |
| 1694 | The type argument specifies whether this is a stream (SOCK_STREAM)\n\ |
| 1695 | or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\ |
| 1696 | specifying the default protocol."; |
| 1697 | |
| 1698 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1699 | #ifndef NO_DUP |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1700 | /* Create a socket object from a numeric file description. |
| 1701 | Useful e.g. if stdin is a socket. |
| 1702 | Additional arguments as for socket(). */ |
| 1703 | |
| 1704 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1705 | static PyObject * |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1706 | BUILD_FUNC_DEF_2(PySocket_fromfd,PyObject *,self, PyObject *,args) |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1707 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1708 | PySocketSockObject *s; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1709 | int fd, family, type, proto = 0; |
| 1710 | if (!PyArg_ParseTuple(args, "iii|i", &fd, &family, &type, &proto)) |
| 1711 | return NULL; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1712 | /* Dup the fd so it and the socket can be closed independently */ |
| 1713 | fd = dup(fd); |
| 1714 | if (fd < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1715 | return PySocket_Err(); |
| 1716 | s = PySocketSock_New(fd, family, type, proto); |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1717 | /* From now on, ignore SIGPIPE and let the error checking |
| 1718 | do the work. */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1719 | #ifdef SIGPIPE |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1720 | (void) signal(SIGPIPE, SIG_IGN); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1721 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1722 | return (PyObject *) s; |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1723 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1724 | |
| 1725 | static char fromfd_doc[] = |
| 1726 | "fromfd(fd, family, type[, proto]) -> socket object\n\ |
| 1727 | \n\ |
| 1728 | Create a socket object from the given file descriptor.\n\ |
| 1729 | The remaining arguments are the same as for socket()."; |
| 1730 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1731 | #endif /* NO_DUP */ |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 1732 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1733 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 1734 | static PyObject * |
| 1735 | BUILD_FUNC_DEF_2(PySocket_ntohs, PyObject *, self, PyObject *, args) |
| 1736 | { |
| 1737 | int x1, x2; |
| 1738 | |
| 1739 | if (!PyArg_Parse(args, "i", &x1)) { |
| 1740 | return NULL; |
| 1741 | } |
| 1742 | x2 = (int)ntohs((short)x1); |
| 1743 | return PyInt_FromLong(x2); |
| 1744 | } |
| 1745 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1746 | static char ntohs_doc[] = |
| 1747 | "ntohs(integer) -> integer\n\ |
| 1748 | \n\ |
| 1749 | Convert a 16-bit integer from network to host byte order."; |
| 1750 | |
| 1751 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 1752 | static PyObject * |
| 1753 | BUILD_FUNC_DEF_2(PySocket_ntohl, PyObject *, self, PyObject *, args) |
| 1754 | { |
| 1755 | int x1, x2; |
| 1756 | |
| 1757 | if (!PyArg_Parse(args, "i", &x1)) { |
| 1758 | return NULL; |
| 1759 | } |
| 1760 | x2 = ntohl(x1); |
| 1761 | return PyInt_FromLong(x2); |
| 1762 | } |
| 1763 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1764 | static char ntohl_doc[] = |
| 1765 | "ntohl(integer) -> integer\n\ |
| 1766 | \n\ |
| 1767 | Convert a 32-bit integer from network to host byte order."; |
| 1768 | |
| 1769 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 1770 | static PyObject * |
| 1771 | BUILD_FUNC_DEF_2(PySocket_htons, PyObject *, self, PyObject *, args) |
| 1772 | { |
| 1773 | int x1, x2; |
| 1774 | |
| 1775 | if (!PyArg_Parse(args, "i", &x1)) { |
| 1776 | return NULL; |
| 1777 | } |
| 1778 | x2 = (int)htons((short)x1); |
| 1779 | return PyInt_FromLong(x2); |
| 1780 | } |
| 1781 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1782 | static char htons_doc[] = |
| 1783 | "htons(integer) -> integer\n\ |
| 1784 | \n\ |
| 1785 | Convert a 16-bit integer from host to network byte order."; |
| 1786 | |
| 1787 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 1788 | static PyObject * |
| 1789 | BUILD_FUNC_DEF_2(PySocket_htonl, PyObject *, self, PyObject *, args) |
| 1790 | { |
| 1791 | int x1, x2; |
| 1792 | |
| 1793 | if (!PyArg_Parse(args, "i", &x1)) { |
| 1794 | return NULL; |
| 1795 | } |
| 1796 | x2 = htonl(x1); |
| 1797 | return PyInt_FromLong(x2); |
| 1798 | } |
| 1799 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1800 | static char htonl_doc[] = |
| 1801 | "htonl(integer) -> integer\n\ |
| 1802 | \n\ |
| 1803 | Convert a 32-bit integer from host to network byte order."; |
| 1804 | |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 1805 | /* |
| 1806 | * socket.inet_aton() and socket.inet_ntoa() functions |
| 1807 | * |
| 1808 | * written 20 Aug 1999 by Ben Gertzfield <che@debian.org> <- blame him! |
| 1809 | * |
| 1810 | */ |
| 1811 | |
| 1812 | static char inet_aton_doc[] = |
| 1813 | "inet_aton(string) -> packed 32-bit IP representation\n\ |
| 1814 | \n\ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 1815 | Convert an IP address in string format (123.45.67.89) to the 32-bit packed\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 1816 | binary format used in low-level network functions."; |
| 1817 | |
| 1818 | static PyObject* |
| 1819 | BUILD_FUNC_DEF_2(PySocket_inet_aton, PyObject *, self, PyObject *, args) |
| 1820 | { |
Guido van Rossum | a2e4855 | 1999-09-09 15:42:59 +0000 | [diff] [blame] | 1821 | #ifndef INADDR_NONE |
| 1822 | #define INADDR_NONE (-1) |
| 1823 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 1824 | |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 1825 | /* Have to use inet_addr() instead */ |
| 1826 | char *ip_addr; |
| 1827 | long packed_addr; |
| 1828 | |
| 1829 | if (!PyArg_Parse(args, "s", &ip_addr)) { |
| 1830 | return NULL; |
| 1831 | } |
| 1832 | |
| 1833 | packed_addr = inet_addr(ip_addr); |
| 1834 | |
| 1835 | if (packed_addr == INADDR_NONE) { /* invalid address */ |
| 1836 | PyErr_SetString(PySocket_Error, |
| 1837 | "illegal IP address string passed to inet_aton"); |
| 1838 | return NULL; |
| 1839 | } |
| 1840 | |
| 1841 | return PyString_FromStringAndSize((char *) &packed_addr, |
| 1842 | sizeof(packed_addr)); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | static char inet_ntoa_doc[] = |
| 1846 | "inet_aton(packed_ip) -> ip_address_string\n\ |
| 1847 | \n\ |
| 1848 | Convert an IP address from 32-bit packed binary format to string format"; |
| 1849 | |
| 1850 | static PyObject* |
| 1851 | BUILD_FUNC_DEF_2(PySocket_inet_ntoa, PyObject *, self, PyObject *, args) |
| 1852 | { |
| 1853 | char *packed_str; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 1854 | int addr_len; |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 1855 | struct in_addr packed_addr; |
| 1856 | |
| 1857 | if (!PyArg_Parse(args, "s#", &packed_str, &addr_len)) { |
| 1858 | return NULL; |
| 1859 | } |
| 1860 | |
| 1861 | if (addr_len != sizeof(packed_addr)) { |
| 1862 | PyErr_SetString(PySocket_Error, |
| 1863 | "packed IP wrong length for inet_ntoa"); |
| 1864 | return NULL; |
| 1865 | } |
| 1866 | |
| 1867 | memcpy(&packed_addr, packed_str, addr_len); |
| 1868 | |
| 1869 | return PyString_FromString(inet_ntoa(packed_addr)); |
| 1870 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1871 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1872 | /* List of functions exported by this module. */ |
| 1873 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1874 | static PyMethodDef PySocket_methods[] = { |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1875 | {"gethostbyname", PySocket_gethostbyname, 0, gethostbyname_doc}, |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1876 | {"gethostbyname_ex", PySocket_gethostbyname_ex, 0, ghbn_ex_doc}, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1877 | {"gethostbyaddr", PySocket_gethostbyaddr, 0, gethostbyaddr_doc}, |
| 1878 | {"gethostname", PySocket_gethostname, 0, gethostname_doc}, |
| 1879 | {"getservbyname", PySocket_getservbyname, 0, getservbyname_doc}, |
| 1880 | {"getprotobyname", PySocket_getprotobyname, 0,getprotobyname_doc}, |
| 1881 | {"socket", PySocket_socket, 1, socket_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1882 | #ifndef NO_DUP |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1883 | {"fromfd", PySocket_fromfd, 1, fromfd_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1884 | #endif |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1885 | {"ntohs", PySocket_ntohs, 0, ntohs_doc}, |
| 1886 | {"ntohl", PySocket_ntohl, 0, ntohl_doc}, |
| 1887 | {"htons", PySocket_htons, 0, htons_doc}, |
| 1888 | {"htonl", PySocket_htonl, 0, htonl_doc}, |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 1889 | {"inet_aton", PySocket_inet_aton, 0, inet_aton_doc}, |
| 1890 | {"inet_ntoa", PySocket_inet_ntoa, 0, inet_ntoa_doc}, |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1891 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1892 | }; |
| 1893 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1894 | |
| 1895 | /* Convenience routine to export an integer value. |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1896 | * |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 1897 | * Errors are silently ignored, for better or for worse... |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1898 | */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1899 | static void |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1900 | BUILD_FUNC_DEF_3(insint,PyObject *,d, char *,name, int,value) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1901 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1902 | PyObject *v = PyInt_FromLong((long) value); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1903 | if (!v || PyDict_SetItemString(d, name, v)) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 1904 | PyErr_Clear(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1905 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 1906 | Py_XDECREF(v); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1909 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1910 | #ifdef MS_WINDOWS |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1911 | |
| 1912 | /* Additional initialization and cleanup for NT/Windows */ |
| 1913 | |
| 1914 | static void |
| 1915 | NTcleanup() |
| 1916 | { |
| 1917 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | static int |
| 1921 | NTinit() |
| 1922 | { |
| 1923 | WSADATA WSAData; |
| 1924 | int ret; |
| 1925 | char buf[100]; |
| 1926 | ret = WSAStartup(0x0101, &WSAData); |
| 1927 | switch (ret) { |
| 1928 | case 0: /* no error */ |
| 1929 | atexit(NTcleanup); |
| 1930 | return 1; |
| 1931 | case WSASYSNOTREADY: |
| 1932 | PyErr_SetString(PyExc_ImportError, |
| 1933 | "WSAStartup failed: network not ready"); |
| 1934 | break; |
| 1935 | case WSAVERNOTSUPPORTED: |
| 1936 | case WSAEINVAL: |
| 1937 | PyErr_SetString(PyExc_ImportError, |
| 1938 | "WSAStartup failed: requested version not supported"); |
| 1939 | break; |
| 1940 | default: |
| 1941 | sprintf(buf, "WSAStartup failed: error code %d", ret); |
| 1942 | PyErr_SetString(PyExc_ImportError, buf); |
| 1943 | break; |
| 1944 | } |
| 1945 | return 0; |
| 1946 | } |
| 1947 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1948 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1949 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1950 | #if defined(PYOS_OS2) |
| 1951 | |
| 1952 | /* Additional initialization and cleanup for OS/2 */ |
| 1953 | |
| 1954 | static void |
| 1955 | OS2cleanup() |
| 1956 | { |
| 1957 | /* No cleanup is necessary for OS/2 Sockets */ |
| 1958 | } |
| 1959 | |
| 1960 | static int |
| 1961 | OS2init() |
| 1962 | { |
| 1963 | char reason[64]; |
| 1964 | int rc = sock_init(); |
| 1965 | |
| 1966 | if (rc == 0) { |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 1967 | atexit(OS2cleanup); |
| 1968 | return 1; /* Indicate Success */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
| 1971 | sprintf(reason, "OS/2 TCP/IP Error# %d", sock_errno()); |
| 1972 | PyErr_SetString(PyExc_ImportError, reason); |
| 1973 | |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 1974 | return 0; /* Indicate Failure */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | #endif /* PYOS_OS2 */ |
| 1978 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1979 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1980 | /* Initialize this module. |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1981 | * This is called when the first 'import socket' is done, |
| 1982 | * via a table in config.c, if config.c is compiled with USE_SOCKET |
| 1983 | * defined. |
| 1984 | * |
| 1985 | * For MS_WINDOWS (which means any Windows variant), this module |
| 1986 | * is actually called "_socket", and there's a wrapper "socket.py" |
| 1987 | * which implements some missing functionality (such as makefile(), |
| 1988 | * dup() and fromfd()). The import of "_socket" may fail with an |
| 1989 | * ImportError exception if initialization of WINSOCK fails. When |
| 1990 | * WINSOCK is initialized succesfully, a call to WSACleanup() is |
| 1991 | * scheduled to be made at exit time. |
| 1992 | * |
| 1993 | * For OS/2, this module is also called "_socket" and uses a wrapper |
| 1994 | * "socket.py" which implements that functionality that is missing |
| 1995 | * when PC operating systems don't put socket descriptors in the |
| 1996 | * operating system's filesystem layer. |
| 1997 | */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1998 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1999 | static char module_doc[] = |
| 2000 | "This module provides socket operations and some related functions.\n\ |
| 2001 | On Unix, it supports IP (Internet Protocol) and Unix domain sockets.\n\ |
| 2002 | On other systems, it only supports IP.\n\ |
| 2003 | \n\ |
| 2004 | Functions:\n\ |
| 2005 | \n\ |
| 2006 | socket() -- create a new socket object\n\ |
| 2007 | fromfd() -- create a socket object from an open file descriptor (*)\n\ |
| 2008 | gethostname() -- return the current hostname\n\ |
| 2009 | gethostbyname() -- map a hostname to its IP number\n\ |
| 2010 | gethostbyaddr() -- map an IP number or hostname to DNS info\n\ |
| 2011 | getservbyname() -- map a service name and a protocol name to a port number\n\ |
| 2012 | getprotobyname() -- mape a protocol name (e.g. 'tcp') to a number\n\ |
| 2013 | ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order\n\ |
| 2014 | htons(), htonl() -- convert 16, 32 bit int from host to network byte order\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 2015 | inet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format\n\ |
| 2016 | inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89)\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2017 | \n\ |
| 2018 | (*) not available on all platforms!)\n\ |
| 2019 | \n\ |
| 2020 | Special objects:\n\ |
| 2021 | \n\ |
| 2022 | SocketType -- type object for socket objects\n\ |
| 2023 | error -- exception raised for I/O errors\n\ |
| 2024 | \n\ |
| 2025 | Integer constants:\n\ |
| 2026 | \n\ |
| 2027 | AF_INET, AF_UNIX -- socket domains (first argument to socket() call)\n\ |
| 2028 | SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)\n\ |
| 2029 | \n\ |
| 2030 | Many other constants may be defined; these may be used in calls to\n\ |
| 2031 | the setsockopt() and getsockopt() methods.\n\ |
| 2032 | "; |
| 2033 | |
| 2034 | static char sockettype_doc[] = |
| 2035 | "A socket represents one endpoint of a network connection.\n\ |
| 2036 | \n\ |
| 2037 | Methods:\n\ |
| 2038 | \n\ |
| 2039 | accept() -- accept a connection, returning new socket and client address\n\ |
| 2040 | bind() -- bind the socket to a local address\n\ |
| 2041 | close() -- close the socket\n\ |
| 2042 | connect() -- connect the socket to a remote address\n\ |
| 2043 | connect_ex() -- connect, return an error code instead of an exception \n\ |
| 2044 | dup() -- return a new socket object identical to the current one (*)\n\ |
| 2045 | fileno() -- return underlying file descriptor\n\ |
| 2046 | getpeername() -- return remote address (*)\n\ |
| 2047 | getsockname() -- return local address\n\ |
| 2048 | getsockopt() -- get socket options\n\ |
| 2049 | listen() -- start listening for incoming connections\n\ |
| 2050 | makefile() -- return a file object corresponding tot the socket (*)\n\ |
| 2051 | recv() -- receive data\n\ |
| 2052 | recvfrom() -- receive data and sender's address\n\ |
| 2053 | send() -- send data\n\ |
| 2054 | sendto() -- send data to a given address\n\ |
| 2055 | setblocking() -- set or clear the blocking I/O flag\n\ |
| 2056 | setsockopt() -- set socket options\n\ |
| 2057 | shutdown() -- shut down traffic in one or both directions\n\ |
| 2058 | \n\ |
| 2059 | (*) not available on all platforms!)"; |
| 2060 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 2061 | DL_EXPORT(void) |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 2062 | #if defined(MS_WINDOWS) || defined(PYOS_OS2) || defined(__BEOS__) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2063 | init_socket() |
| 2064 | #else |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2065 | initsocket() |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2066 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2067 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2068 | PyObject *m, *d; |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2069 | #ifdef MS_WINDOWS |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2070 | if (!NTinit()) |
| 2071 | return; |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2072 | m = Py_InitModule3("_socket", PySocket_methods, module_doc); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2073 | #else |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2074 | #if defined(__TOS_OS2__) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2075 | if (!OS2init()) |
| 2076 | return; |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2077 | m = Py_InitModule3("_socket", PySocket_methods, module_doc); |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2078 | #else |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 2079 | #if defined(__BEOS__) |
| 2080 | m = Py_InitModule3("_socket", PySocket_methods, module_doc); |
| 2081 | #else |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2082 | m = Py_InitModule3("socket", PySocket_methods, module_doc); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 2083 | #endif /* __BEOS__ */ |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2084 | #endif |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2085 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2086 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 2087 | PySocket_Error = PyErr_NewException("socket.error", NULL, NULL); |
| 2088 | if (PySocket_Error == NULL) |
| 2089 | return; |
| 2090 | PyDict_SetItemString(d, "error", PySocket_Error); |
Guido van Rossum | 3b4b6fc | 1997-05-21 14:37:37 +0000 | [diff] [blame] | 2091 | PySocketSock_Type.ob_type = &PyType_Type; |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2092 | PySocketSock_Type.tp_doc = sockettype_doc; |
Guido van Rossum | 3b4b6fc | 1997-05-21 14:37:37 +0000 | [diff] [blame] | 2093 | Py_INCREF(&PySocketSock_Type); |
Guido van Rossum | 91ba64d | 1997-06-02 22:18:09 +0000 | [diff] [blame] | 2094 | if (PyDict_SetItemString(d, "SocketType", |
Guido van Rossum | 3b4b6fc | 1997-05-21 14:37:37 +0000 | [diff] [blame] | 2095 | (PyObject *)&PySocketSock_Type) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 2096 | return; |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 2097 | |
| 2098 | /* Address families (we only support AF_INET and AF_UNIX) */ |
| 2099 | #ifdef AF_UNSPEC |
| 2100 | insint(d, "AF_UNSPEC", AF_UNSPEC); |
| 2101 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2102 | insint(d, "AF_INET", AF_INET); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2103 | #ifdef AF_UNIX |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2104 | insint(d, "AF_UNIX", AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2105 | #endif /* AF_UNIX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 2106 | #ifdef AF_AX25 |
| 2107 | insint(d, "AF_AX25", AF_AX25); /* Amateur Radio AX.25 */ |
| 2108 | #endif |
| 2109 | #ifdef AF_IPX |
| 2110 | insint(d, "AF_IPX", AF_IPX); /* Novell IPX */ |
| 2111 | #endif |
| 2112 | #ifdef AF_APPLETALK |
| 2113 | insint(d, "AF_APPLETALK", AF_APPLETALK); /* Appletalk DDP */ |
| 2114 | #endif |
| 2115 | #ifdef AF_NETROM |
| 2116 | insint(d, "AF_NETROM", AF_NETROM); /* Amateur radio NetROM */ |
| 2117 | #endif |
| 2118 | #ifdef AF_BRIDGE |
| 2119 | insint(d, "AF_BRIDGE", AF_BRIDGE); /* Multiprotocol bridge */ |
| 2120 | #endif |
| 2121 | #ifdef AF_AAL5 |
| 2122 | insint(d, "AF_AAL5", AF_AAL5); /* Reserved for Werner's ATM */ |
| 2123 | #endif |
| 2124 | #ifdef AF_X25 |
| 2125 | insint(d, "AF_X25", AF_X25); /* Reserved for X.25 project */ |
| 2126 | #endif |
| 2127 | #ifdef AF_INET6 |
| 2128 | insint(d, "AF_INET6", AF_INET6); /* IP version 6 */ |
| 2129 | #endif |
| 2130 | #ifdef AF_ROSE |
| 2131 | insint(d, "AF_ROSE", AF_ROSE); /* Amateur Radio X.25 PLP */ |
| 2132 | #endif |
| 2133 | |
| 2134 | /* Socket types */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2135 | insint(d, "SOCK_STREAM", SOCK_STREAM); |
| 2136 | insint(d, "SOCK_DGRAM", SOCK_DGRAM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 2137 | #ifndef __BEOS__ |
| 2138 | /* We have incomplete socket support. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2139 | insint(d, "SOCK_RAW", SOCK_RAW); |
| 2140 | insint(d, "SOCK_SEQPACKET", SOCK_SEQPACKET); |
| 2141 | insint(d, "SOCK_RDM", SOCK_RDM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 2142 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 2143 | |
| 2144 | #ifdef SO_DEBUG |
| 2145 | insint(d, "SO_DEBUG", SO_DEBUG); |
| 2146 | #endif |
| 2147 | #ifdef SO_ACCEPTCONN |
| 2148 | insint(d, "SO_ACCEPTCONN", SO_ACCEPTCONN); |
| 2149 | #endif |
| 2150 | #ifdef SO_REUSEADDR |
| 2151 | insint(d, "SO_REUSEADDR", SO_REUSEADDR); |
| 2152 | #endif |
| 2153 | #ifdef SO_KEEPALIVE |
| 2154 | insint(d, "SO_KEEPALIVE", SO_KEEPALIVE); |
| 2155 | #endif |
| 2156 | #ifdef SO_DONTROUTE |
| 2157 | insint(d, "SO_DONTROUTE", SO_DONTROUTE); |
| 2158 | #endif |
| 2159 | #ifdef SO_BROADCAST |
| 2160 | insint(d, "SO_BROADCAST", SO_BROADCAST); |
| 2161 | #endif |
| 2162 | #ifdef SO_USELOOPBACK |
| 2163 | insint(d, "SO_USELOOPBACK", SO_USELOOPBACK); |
| 2164 | #endif |
| 2165 | #ifdef SO_LINGER |
| 2166 | insint(d, "SO_LINGER", SO_LINGER); |
| 2167 | #endif |
| 2168 | #ifdef SO_OOBINLINE |
| 2169 | insint(d, "SO_OOBINLINE", SO_OOBINLINE); |
| 2170 | #endif |
| 2171 | #ifdef SO_REUSEPORT |
| 2172 | insint(d, "SO_REUSEPORT", SO_REUSEPORT); |
| 2173 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 2174 | #ifdef SO_SNDBUF |
| 2175 | insint(d, "SO_SNDBUF", SO_SNDBUF); |
| 2176 | #endif |
| 2177 | #ifdef SO_RCVBUF |
| 2178 | insint(d, "SO_RCVBUF", SO_RCVBUF); |
| 2179 | #endif |
| 2180 | #ifdef SO_SNDLOWAT |
| 2181 | insint(d, "SO_SNDLOWAT", SO_SNDLOWAT); |
| 2182 | #endif |
| 2183 | #ifdef SO_RCVLOWAT |
| 2184 | insint(d, "SO_RCVLOWAT", SO_RCVLOWAT); |
| 2185 | #endif |
| 2186 | #ifdef SO_SNDTIMEO |
| 2187 | insint(d, "SO_SNDTIMEO", SO_SNDTIMEO); |
| 2188 | #endif |
| 2189 | #ifdef SO_RCVTIMEO |
| 2190 | insint(d, "SO_RCVTIMEO", SO_RCVTIMEO); |
| 2191 | #endif |
| 2192 | #ifdef SO_ERROR |
| 2193 | insint(d, "SO_ERROR", SO_ERROR); |
| 2194 | #endif |
| 2195 | #ifdef SO_TYPE |
| 2196 | insint(d, "SO_TYPE", SO_TYPE); |
| 2197 | #endif |
| 2198 | |
| 2199 | /* Maximum number of connections for "listen" */ |
| 2200 | #ifdef SOMAXCONN |
| 2201 | insint(d, "SOMAXCONN", SOMAXCONN); |
| 2202 | #else |
| 2203 | insint(d, "SOMAXCONN", 5); /* Common value */ |
| 2204 | #endif |
| 2205 | |
| 2206 | /* Flags for send, recv */ |
| 2207 | #ifdef MSG_OOB |
| 2208 | insint(d, "MSG_OOB", MSG_OOB); |
| 2209 | #endif |
| 2210 | #ifdef MSG_PEEK |
| 2211 | insint(d, "MSG_PEEK", MSG_PEEK); |
| 2212 | #endif |
| 2213 | #ifdef MSG_DONTROUTE |
| 2214 | insint(d, "MSG_DONTROUTE", MSG_DONTROUTE); |
| 2215 | #endif |
| 2216 | #ifdef MSG_EOR |
| 2217 | insint(d, "MSG_EOR", MSG_EOR); |
| 2218 | #endif |
| 2219 | #ifdef MSG_TRUNC |
| 2220 | insint(d, "MSG_TRUNC", MSG_TRUNC); |
| 2221 | #endif |
| 2222 | #ifdef MSG_CTRUNC |
| 2223 | insint(d, "MSG_CTRUNC", MSG_CTRUNC); |
| 2224 | #endif |
| 2225 | #ifdef MSG_WAITALL |
| 2226 | insint(d, "MSG_WAITALL", MSG_WAITALL); |
| 2227 | #endif |
| 2228 | #ifdef MSG_BTAG |
| 2229 | insint(d, "MSG_BTAG", MSG_BTAG); |
| 2230 | #endif |
| 2231 | #ifdef MSG_ETAG |
| 2232 | insint(d, "MSG_ETAG", MSG_ETAG); |
| 2233 | #endif |
| 2234 | |
| 2235 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 2236 | #ifdef SOL_SOCKET |
| 2237 | insint(d, "SOL_SOCKET", SOL_SOCKET); |
| 2238 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 2239 | #ifdef SOL_IP |
| 2240 | insint(d, "SOL_IP", SOL_IP); |
| 2241 | #else |
| 2242 | insint(d, "SOL_IP", 0); |
| 2243 | #endif |
| 2244 | #ifdef SOL_IPX |
| 2245 | insint(d, "SOL_IPX", SOL_IPX); |
| 2246 | #endif |
| 2247 | #ifdef SOL_AX25 |
| 2248 | insint(d, "SOL_AX25", SOL_AX25); |
| 2249 | #endif |
| 2250 | #ifdef SOL_ATALK |
| 2251 | insint(d, "SOL_ATALK", SOL_ATALK); |
| 2252 | #endif |
| 2253 | #ifdef SOL_NETROM |
| 2254 | insint(d, "SOL_NETROM", SOL_NETROM); |
| 2255 | #endif |
| 2256 | #ifdef SOL_ROSE |
| 2257 | insint(d, "SOL_ROSE", SOL_ROSE); |
| 2258 | #endif |
| 2259 | #ifdef SOL_TCP |
| 2260 | insint(d, "SOL_TCP", SOL_TCP); |
| 2261 | #else |
| 2262 | insint(d, "SOL_TCP", 6); |
| 2263 | #endif |
| 2264 | #ifdef SOL_UDP |
| 2265 | insint(d, "SOL_UDP", SOL_UDP); |
| 2266 | #else |
| 2267 | insint(d, "SOL_UDP", 17); |
| 2268 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 2269 | #ifdef IPPROTO_IP |
| 2270 | insint(d, "IPPROTO_IP", IPPROTO_IP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 2271 | #else |
| 2272 | insint(d, "IPPROTO_IP", 0); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 2273 | #endif |
| 2274 | #ifdef IPPROTO_ICMP |
| 2275 | insint(d, "IPPROTO_ICMP", IPPROTO_ICMP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 2276 | #else |
| 2277 | insint(d, "IPPROTO_ICMP", 1); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 2278 | #endif |
| 2279 | #ifdef IPPROTO_IGMP |
| 2280 | insint(d, "IPPROTO_IGMP", IPPROTO_IGMP); |
| 2281 | #endif |
| 2282 | #ifdef IPPROTO_GGP |
| 2283 | insint(d, "IPPROTO_GGP", IPPROTO_GGP); |
| 2284 | #endif |
| 2285 | #ifdef IPPROTO_TCP |
| 2286 | insint(d, "IPPROTO_TCP", IPPROTO_TCP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 2287 | #else |
| 2288 | insint(d, "IPPROTO_TCP", 6); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 2289 | #endif |
| 2290 | #ifdef IPPROTO_EGP |
| 2291 | insint(d, "IPPROTO_EGP", IPPROTO_EGP); |
| 2292 | #endif |
| 2293 | #ifdef IPPROTO_PUP |
| 2294 | insint(d, "IPPROTO_PUP", IPPROTO_PUP); |
| 2295 | #endif |
| 2296 | #ifdef IPPROTO_UDP |
| 2297 | insint(d, "IPPROTO_UDP", IPPROTO_UDP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 2298 | #else |
| 2299 | insint(d, "IPPROTO_UDP", 17); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 2300 | #endif |
| 2301 | #ifdef IPPROTO_IDP |
| 2302 | insint(d, "IPPROTO_IDP", IPPROTO_IDP); |
| 2303 | #endif |
| 2304 | #ifdef IPPROTO_HELLO |
| 2305 | insint(d, "IPPROTO_HELLO", IPPROTO_HELLO); |
| 2306 | #endif |
| 2307 | #ifdef IPPROTO_ND |
| 2308 | insint(d, "IPPROTO_ND", IPPROTO_ND); |
| 2309 | #endif |
| 2310 | #ifdef IPPROTO_TP |
| 2311 | insint(d, "IPPROTO_TP", IPPROTO_TP); |
| 2312 | #endif |
| 2313 | #ifdef IPPROTO_XTP |
| 2314 | insint(d, "IPPROTO_XTP", IPPROTO_XTP); |
| 2315 | #endif |
| 2316 | #ifdef IPPROTO_EON |
| 2317 | insint(d, "IPPROTO_EON", IPPROTO_EON); |
| 2318 | #endif |
| 2319 | #ifdef IPPROTO_BIP |
| 2320 | insint(d, "IPPROTO_BIP", IPPROTO_BIP); |
| 2321 | #endif |
| 2322 | /**/ |
| 2323 | #ifdef IPPROTO_RAW |
| 2324 | insint(d, "IPPROTO_RAW", IPPROTO_RAW); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 2325 | #else |
| 2326 | insint(d, "IPPROTO_RAW", 255); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 2327 | #endif |
| 2328 | #ifdef IPPROTO_MAX |
| 2329 | insint(d, "IPPROTO_MAX", IPPROTO_MAX); |
| 2330 | #endif |
| 2331 | |
| 2332 | /* Some port configuration */ |
| 2333 | #ifdef IPPORT_RESERVED |
| 2334 | insint(d, "IPPORT_RESERVED", IPPORT_RESERVED); |
| 2335 | #else |
| 2336 | insint(d, "IPPORT_RESERVED", 1024); |
| 2337 | #endif |
| 2338 | #ifdef IPPORT_USERRESERVED |
| 2339 | insint(d, "IPPORT_USERRESERVED", IPPORT_USERRESERVED); |
| 2340 | #else |
| 2341 | insint(d, "IPPORT_USERRESERVED", 5000); |
| 2342 | #endif |
| 2343 | |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 2344 | /* Some reserved IP v.4 addresses */ |
| 2345 | #ifdef INADDR_ANY |
| 2346 | insint(d, "INADDR_ANY", INADDR_ANY); |
| 2347 | #else |
| 2348 | insint(d, "INADDR_ANY", 0x00000000); |
| 2349 | #endif |
| 2350 | #ifdef INADDR_BROADCAST |
| 2351 | insint(d, "INADDR_BROADCAST", INADDR_BROADCAST); |
| 2352 | #else |
| 2353 | insint(d, "INADDR_BROADCAST", 0xffffffff); |
| 2354 | #endif |
| 2355 | #ifdef INADDR_LOOPBACK |
| 2356 | insint(d, "INADDR_LOOPBACK", INADDR_LOOPBACK); |
| 2357 | #else |
| 2358 | insint(d, "INADDR_LOOPBACK", 0x7F000001); |
| 2359 | #endif |
| 2360 | #ifdef INADDR_UNSPEC_GROUP |
| 2361 | insint(d, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP); |
| 2362 | #else |
| 2363 | insint(d, "INADDR_UNSPEC_GROUP", 0xe0000000); |
| 2364 | #endif |
| 2365 | #ifdef INADDR_ALLHOSTS_GROUP |
| 2366 | insint(d, "INADDR_ALLHOSTS_GROUP", INADDR_ALLHOSTS_GROUP); |
| 2367 | #else |
| 2368 | insint(d, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
| 2369 | #endif |
| 2370 | #ifdef INADDR_MAX_LOCAL_GROUP |
| 2371 | insint(d, "INADDR_MAX_LOCAL_GROUP", INADDR_MAX_LOCAL_GROUP); |
| 2372 | #else |
| 2373 | insint(d, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
| 2374 | #endif |
| 2375 | #ifdef INADDR_NONE |
| 2376 | insint(d, "INADDR_NONE", INADDR_NONE); |
| 2377 | #else |
| 2378 | insint(d, "INADDR_NONE", 0xffffffff); |
| 2379 | #endif |
| 2380 | |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 2381 | /* IP [gs]etsockopt options */ |
| 2382 | #ifdef IP_OPTIONS |
| 2383 | insint(d, "IP_OPTIONS", IP_OPTIONS); |
| 2384 | #endif |
| 2385 | #ifdef IP_HDRINCL |
| 2386 | insint(d, "IP_HDRINCL", IP_HDRINCL); |
| 2387 | #endif |
| 2388 | #ifdef IP_TOS |
| 2389 | insint(d, "IP_TOS", IP_TOS); |
| 2390 | #endif |
| 2391 | #ifdef IP_TTL |
| 2392 | insint(d, "IP_TTL", IP_TTL); |
| 2393 | #endif |
| 2394 | #ifdef IP_RECVOPTS |
| 2395 | insint(d, "IP_RECVOPTS", IP_RECVOPTS); |
| 2396 | #endif |
| 2397 | #ifdef IP_RECVRETOPTS |
| 2398 | insint(d, "IP_RECVRETOPTS", IP_RECVRETOPTS); |
| 2399 | #endif |
| 2400 | #ifdef IP_RECVDSTADDR |
| 2401 | insint(d, "IP_RECVDSTADDR", IP_RECVDSTADDR); |
| 2402 | #endif |
| 2403 | #ifdef IP_RETOPTS |
| 2404 | insint(d, "IP_RETOPTS", IP_RETOPTS); |
| 2405 | #endif |
| 2406 | #ifdef IP_MULTICAST_IF |
| 2407 | insint(d, "IP_MULTICAST_IF", IP_MULTICAST_IF); |
| 2408 | #endif |
| 2409 | #ifdef IP_MULTICAST_TTL |
| 2410 | insint(d, "IP_MULTICAST_TTL", IP_MULTICAST_TTL); |
| 2411 | #endif |
| 2412 | #ifdef IP_MULTICAST_LOOP |
| 2413 | insint(d, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP); |
| 2414 | #endif |
| 2415 | #ifdef IP_ADD_MEMBERSHIP |
| 2416 | insint(d, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP); |
| 2417 | #endif |
| 2418 | #ifdef IP_DROP_MEMBERSHIP |
| 2419 | insint(d, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP); |
| 2420 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 2421 | #ifdef IP_DEFAULT_MULTICAST_TTL |
| 2422 | insint(d, "IP_DEFAULT_MULTICAST_TTL", IP_DEFAULT_MULTICAST_TTL); |
| 2423 | #endif |
| 2424 | #ifdef IP_DEFAULT_MULTICAST_LOOP |
| 2425 | insint(d, "IP_DEFAULT_MULTICAST_LOOP", IP_DEFAULT_MULTICAST_LOOP); |
| 2426 | #endif |
| 2427 | #ifdef IP_MAX_MEMBERSHIPS |
| 2428 | insint(d, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS); |
| 2429 | #endif |
| 2430 | |
| 2431 | /* TCP options */ |
| 2432 | #ifdef TCP_NODELAY |
| 2433 | insint(d, "TCP_NODELAY", TCP_NODELAY); |
| 2434 | #endif |
| 2435 | #ifdef TCP_MAXSEG |
| 2436 | insint(d, "TCP_MAXSEG", TCP_MAXSEG); |
| 2437 | #endif |
| 2438 | |
| 2439 | /* IPX options */ |
| 2440 | #ifdef IPX_TYPE |
| 2441 | insint(d, "IPX_TYPE", IPX_TYPE); |
| 2442 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2443 | |
| 2444 | /* Initialize gethostbyname lock */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 2445 | #ifdef USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 2446 | gethostbyname_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2447 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2448 | } |