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