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