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