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