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 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 10 | - only AF_INET, AF_INET6 and AF_UNIX address families are supported in a |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 11 | portable manner, though AF_PACKET is supported under Linux. |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 12 | - no read/write operations (use sendall/recv or makefile instead) |
| 13 | - additional restrictions apply on Windows (compensated for by socket.py) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 15 | Module interface: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 17 | - socket.error: exception raised for socket specific errors |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 18 | - socket.gaierror: exception raised for getaddrinfo/getnameinfo errors, |
| 19 | a subclass of socket.error |
| 20 | - socket.herror: exception raised for gethostby* errors, |
| 21 | a subclass of socket.error |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 22 | - socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd') |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 23 | - socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...]) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 24 | - socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com') |
Guido van Rossum | 25405c7 | 1996-12-19 16:42:52 +0000 | [diff] [blame] | 25 | - socket.getprotobyname(protocolname) --> protocol number |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 26 | - socket.getservbyname(servicename, protocolname) --> port number |
Guido van Rossum | 4dd2a7e | 1991-07-01 18:51:33 +0000 | [diff] [blame] | 27 | - socket.socket(family, type [, proto]) --> new socket object |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 28 | - socket.ntohs(16 bit value) --> new int object |
| 29 | - socket.ntohl(32 bit value) --> new int object |
| 30 | - socket.htons(16 bit value) --> new int object |
| 31 | - socket.htonl(32 bit value) --> new int object |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 32 | - socket.getaddrinfo(host, port [, family, socktype, proto, flags]) |
| 33 | --> List of (family, socktype, proto, canonname, sockaddr) |
| 34 | - socket.getnameinfo(sockaddr, flags) --> (host, port) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 35 | - socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 36 | - socket.inet_aton(IP address) -> 32-bit packed IP representation |
| 37 | - socket.inet_ntoa(packed IP) -> IP address string |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 38 | - socket.ssl(socket, keyfile, certfile) -> new ssl object |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 39 | - an Internet socket address is a pair (hostname, port) |
| 40 | where hostname can be anything recognized by gethostbyname() |
| 41 | (including the dd.dd.dd.dd notation) and port is in host byte order |
| 42 | - 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] | 43 | - a UNIX domain socket address is a string specifying the pathname |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 44 | - an AF_PACKET socket address is a tuple containing a string |
| 45 | specifying the ethernet interface and an integer specifying |
| 46 | the Ethernet protocol number to be received. For example: |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 47 | ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple |
| 48 | specify packet-type and ha-type/addr -- these are ignored by |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 49 | networking code, but accepted since they are returned by the |
| 50 | getsockname() method. |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 52 | Socket methods: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 54 | - s.accept() --> new socket object, sockaddr |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 55 | - s.bind(sockaddr) --> None |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 56 | - s.close() --> None |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 57 | - s.connect(sockaddr) --> None |
| 58 | - 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] | 59 | - s.fileno() --> file descriptor |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 60 | - s.dup() --> same as socket.fromfd(os.dup(s.fileno(), ...) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 61 | - s.getpeername() --> sockaddr |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 62 | - s.getsockname() --> sockaddr |
| 63 | - s.getsockopt(level, optname[, buflen]) --> int or string |
| 64 | - s.listen(backlog) --> None |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 65 | - s.makefile([mode[, bufsize]]) --> file object |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 66 | - s.recv(buflen [,flags]) --> string |
| 67 | - s.recvfrom(buflen [,flags]) --> string, sockaddr |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 68 | - s.send(string [,flags]) --> nbytes |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 69 | - s.sendall(string [,flags]) # tries to send everything in a loop |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 70 | - s.sendto(string, [flags,] sockaddr) --> nbytes |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 71 | - s.setblocking(0 | 1) --> None |
| 72 | - s.setsockopt(level, optname, value) --> None |
| 73 | - s.shutdown(how) --> None |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 74 | - 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] | 75 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 76 | */ |
| 77 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 78 | #include "Python.h" |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 80 | /* XXX This is a terrible mess of of platform-dependent preprocessor hacks. |
| 81 | I hope some day someone can clean this up please... */ |
| 82 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 83 | /* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure |
| 84 | script doesn't get this right, so we hardcode some platform checks below. |
| 85 | On the other hand, not all Linux versions agree, so there the settings |
| 86 | computed by the configure script are needed! */ |
| 87 | |
| 88 | #ifndef linux |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 89 | #undef HAVE_GETHOSTBYNAME_R_3_ARG |
| 90 | #undef HAVE_GETHOSTBYNAME_R_5_ARG |
| 91 | #undef HAVE_GETHOSTBYNAME_R_6_ARG |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 92 | #endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 93 | |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 94 | #ifndef WITH_THREAD |
| 95 | #undef HAVE_GETHOSTBYNAME_R |
| 96 | #endif |
| 97 | |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 98 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 99 | #if defined(_AIX) || defined(__osf__) |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 100 | #define HAVE_GETHOSTBYNAME_R_3_ARG |
Martin v. Löwis | 1c07b4b | 2001-10-13 09:00:42 +0000 | [diff] [blame] | 101 | #elif defined(__sun) || defined(__sgi) |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 102 | #define HAVE_GETHOSTBYNAME_R_5_ARG |
| 103 | #elif defined(linux) |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 104 | /* Rely on the configure script */ |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 105 | #else |
| 106 | #undef HAVE_GETHOSTBYNAME_R |
| 107 | #endif |
| 108 | #endif |
| 109 | |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 110 | #if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && !defined(MS_WINDOWS) |
| 111 | #define USE_GETHOSTBYNAME_LOCK |
| 112 | #endif |
| 113 | |
| 114 | #ifdef USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 115 | #include "pythread.h" |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 116 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 117 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 118 | #ifdef HAVE_UNISTD_H |
| 119 | #include <unistd.h> |
| 120 | #endif |
| 121 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 122 | #if defined(PYCC_VACPP) |
| 123 | #include <types.h> |
| 124 | #include <io.h> |
| 125 | #include <sys/ioctl.h> |
| 126 | #include <utils.h> |
| 127 | #include <ctype.h> |
| 128 | #endif |
| 129 | |
| 130 | #if defined(PYOS_OS2) |
| 131 | #define INCL_DOS |
| 132 | #define INCL_DOSERRORS |
| 133 | #define INCL_NOPMAPI |
| 134 | #include <os2.h> |
| 135 | #endif |
| 136 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 137 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 138 | #include <sys/types.h> |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 139 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 140 | #include <signal.h> |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 141 | #ifndef MS_WINDOWS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 142 | #include <netdb.h> |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 143 | #include <sys/socket.h> |
| 144 | #include <netinet/in.h> |
Tim Peters | 603c683 | 2001-11-05 02:45:59 +0000 | [diff] [blame] | 145 | #if !(defined(__BEOS__) || defined(__CYGWIN__) || (defined(PYOS_OS2) && defined(PYCC_VACPP))) |
Andrew M. Kuchling | 52a644c | 2000-03-31 17:24:30 +0000 | [diff] [blame] | 146 | #include <netinet/tcp.h> |
Fred Drake | 11b0936 | 2000-05-16 13:30:12 +0000 | [diff] [blame] | 147 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 148 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 149 | /* Headers needed for inet_ntoa() and inet_addr() */ |
| 150 | #ifdef __BEOS__ |
| 151 | #include <net/netdb.h> |
Tim Peters | 603c683 | 2001-11-05 02:45:59 +0000 | [diff] [blame] | 152 | #elif defined(PYOS_OS2) && defined(PYCC_VACPP) |
| 153 | #include <netdb.h> |
| 154 | typedef size_t socklen_t; |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 155 | #else |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 156 | #ifndef USE_GUSI1 |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 157 | #include <arpa/inet.h> |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 158 | #endif |
Guido van Rossum | 1b6e463 | 1999-11-04 18:22:29 +0000 | [diff] [blame] | 159 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 160 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 161 | #ifndef RISCOS |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 162 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 163 | #else |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 164 | #include <sys/fcntl.h> |
| 165 | #define NO_DUP |
| 166 | int h_errno; /* not used */ |
| 167 | #endif |
| 168 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 169 | #include <winsock.h> |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 170 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 171 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 172 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 173 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 174 | #ifdef HAVE_SYS_UN_H |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 175 | #include <sys/un.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 176 | #else |
| 177 | #undef AF_UNIX |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 178 | #endif |
| 179 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 180 | #ifdef HAVE_NETPACKET_PACKET_H |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 181 | #include <sys/ioctl.h> |
| 182 | #include <net/if.h> |
| 183 | #include <netpacket/packet.h> |
| 184 | #endif |
| 185 | |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 186 | #ifdef HAVE_STDDEF_H |
| 187 | #include <stddef.h> |
| 188 | #endif |
| 189 | |
| 190 | #ifndef offsetof |
| 191 | #define offsetof(type, member) ((size_t)(&((type *)0)->member)) |
| 192 | #endif |
| 193 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 194 | #ifndef O_NDELAY |
| 195 | #define O_NDELAY O_NONBLOCK /* For QNX only? */ |
| 196 | #endif |
| 197 | |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 198 | #ifdef USE_GUSI1 |
Jack Jansen | 508537b | 1996-02-14 15:57:45 +0000 | [diff] [blame] | 199 | /* fdopen() isn't declared in stdio.h (sigh) */ |
| 200 | #include <GUSI.h> |
| 201 | #endif |
| 202 | |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 203 | #include "addrinfo.h" |
| 204 | |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 205 | #ifdef USE_SSL |
Andrew M. Kuchling | 697a0b0 | 2001-01-18 17:41:41 +0000 | [diff] [blame] | 206 | #include "openssl/rsa.h" |
| 207 | #include "openssl/crypto.h" |
| 208 | #include "openssl/x509.h" |
| 209 | #include "openssl/pem.h" |
| 210 | #include "openssl/ssl.h" |
| 211 | #include "openssl/err.h" |
Jeremy Hylton | de80f2e | 2001-10-18 00:28:50 +0000 | [diff] [blame] | 212 | #include "openssl/rand.h" |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 213 | #endif /* USE_SSL */ |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 214 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 215 | #ifndef HAVE_INET_PTON |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 216 | int inet_pton (int af, const char *src, void *dst); |
| 217 | const char *inet_ntop(int af, const void *src, char *dst, socklen_t size); |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 218 | #endif |
| 219 | |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 220 | #ifdef __APPLE__ |
| 221 | /* On OS X, getaddrinfo returns no error indication of lookup |
| 222 | failure, so we must use the emulation instead of the libinfo |
| 223 | implementation. Unfortunately, performing an autoconf test |
| 224 | for this bug would require DNS access for the machine performing |
| 225 | the configuration, which is not acceptable. Therefore, we |
| 226 | determine the bug just by checking for __APPLE__. If this bug |
| 227 | gets ever fixed, perhaps checking for sys/version.h would be |
| 228 | appropriate, which is 10/0 on the system with the bug. */ |
| 229 | #undef HAVE_GETADDRINFO |
| 230 | /* avoid clashes with the C library definition of the symbol. */ |
| 231 | #define getaddrinfo fake_getaddrinfo |
| 232 | #endif |
| 233 | |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 234 | /* I know this is a bad practice, but it is the easiest... */ |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 235 | #if !defined(HAVE_GETADDRINFO) |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 236 | #include "getaddrinfo.c" |
| 237 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 238 | #if !defined(HAVE_GETNAMEINFO) |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 239 | #include "getnameinfo.c" |
| 240 | #endif |
| 241 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 242 | #if defined(MS_WINDOWS) || defined(__BEOS__) |
| 243 | /* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 244 | /* seem to be a few differences in the API */ |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 245 | #define SOCKETCLOSE closesocket |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 246 | #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] | 247 | #endif |
| 248 | |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 249 | /* abstract the socket file descriptor type */ |
| 250 | #ifdef MS_WINDOWS |
| 251 | typedef SOCKET SOCKET_T; |
| 252 | # ifdef MS_WIN64 |
| 253 | # define SIZEOF_SOCKET_T 8 |
| 254 | # else |
| 255 | # define SIZEOF_SOCKET_T 4 |
| 256 | # endif |
| 257 | #else |
| 258 | typedef int SOCKET_T; |
| 259 | # define SIZEOF_SOCKET_T SIZEOF_INT |
| 260 | #endif |
| 261 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 262 | #ifdef MS_WIN32 |
| 263 | # define EAFNOSUPPORT WSAEAFNOSUPPORT |
| 264 | # define snprintf _snprintf |
| 265 | #endif |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 266 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 267 | #if defined(PYOS_OS2) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 268 | #define SOCKETCLOSE soclose |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 269 | #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] | 270 | #endif |
| 271 | |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 272 | #ifndef SOCKETCLOSE |
| 273 | #define SOCKETCLOSE close |
| 274 | #endif |
| 275 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 276 | |
| 277 | /* XXX There's a problem here: *static* functions are not supposed to have |
| 278 | a Py prefix (or use CapitalizedWords). Later... */ |
| 279 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 280 | /* Global variable holding the exception type for errors detected |
| 281 | by this module (but not argument type or memory errors, etc.). */ |
| 282 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 283 | static PyObject *PySocket_Error; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 284 | static PyObject *PyH_Error; |
| 285 | static PyObject *PyGAI_Error; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 286 | |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 287 | #ifdef USE_SSL |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 288 | static PyObject *PySSLErrorObject; |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 289 | #endif /* USE_SSL */ |
| 290 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 291 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 292 | #ifdef RISCOS |
| 293 | /* Global variable which is !=0 if Python is running in a RISC OS taskwindow */ |
| 294 | static int taskwindow; |
| 295 | #endif |
| 296 | |
| 297 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 298 | /* Convenience function to raise an error according to errno |
| 299 | and return a NULL pointer from a function. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 300 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 301 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 302 | PySocket_Err(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 303 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 304 | #ifdef MS_WINDOWS |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 305 | int err_no = WSAGetLastError(); |
| 306 | if (err_no) { |
| 307 | static struct { int no; const char *msg; } *msgp, msgs[] = { |
| 308 | { WSAEINTR, "Interrupted system call" }, |
| 309 | { WSAEBADF, "Bad file descriptor" }, |
| 310 | { WSAEACCES, "Permission denied" }, |
| 311 | { WSAEFAULT, "Bad address" }, |
| 312 | { WSAEINVAL, "Invalid argument" }, |
| 313 | { WSAEMFILE, "Too many open files" }, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 314 | { WSAEWOULDBLOCK, |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 315 | "The socket operation could not complete " |
| 316 | "without blocking" }, |
| 317 | { WSAEINPROGRESS, "Operation now in progress" }, |
| 318 | { WSAEALREADY, "Operation already in progress" }, |
| 319 | { WSAENOTSOCK, "Socket operation on non-socket" }, |
| 320 | { WSAEDESTADDRREQ, "Destination address required" }, |
| 321 | { WSAEMSGSIZE, "Message too long" }, |
| 322 | { WSAEPROTOTYPE, "Protocol wrong type for socket" }, |
| 323 | { WSAENOPROTOOPT, "Protocol not available" }, |
| 324 | { WSAEPROTONOSUPPORT, "Protocol not supported" }, |
| 325 | { WSAESOCKTNOSUPPORT, "Socket type not supported" }, |
| 326 | { WSAEOPNOTSUPP, "Operation not supported" }, |
| 327 | { WSAEPFNOSUPPORT, "Protocol family not supported" }, |
| 328 | { WSAEAFNOSUPPORT, "Address family not supported" }, |
| 329 | { WSAEADDRINUSE, "Address already in use" }, |
| 330 | { WSAEADDRNOTAVAIL, |
| 331 | "Can't assign requested address" }, |
| 332 | { WSAENETDOWN, "Network is down" }, |
| 333 | { WSAENETUNREACH, "Network is unreachable" }, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 334 | { WSAENETRESET, |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 335 | "Network dropped connection on reset" }, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 336 | { WSAECONNABORTED, |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 337 | "Software caused connection abort" }, |
| 338 | { WSAECONNRESET, "Connection reset by peer" }, |
| 339 | { WSAENOBUFS, "No buffer space available" }, |
| 340 | { WSAEISCONN, "Socket is already connected" }, |
| 341 | { WSAENOTCONN, "Socket is not connected" }, |
| 342 | { WSAESHUTDOWN, "Can't send after socket shutdown" }, |
| 343 | { WSAETOOMANYREFS, |
| 344 | "Too many references: can't splice" }, |
| 345 | { WSAETIMEDOUT, "Operation timed out" }, |
| 346 | { WSAECONNREFUSED, "Connection refused" }, |
| 347 | { WSAELOOP, "Too many levels of symbolic links" }, |
| 348 | { WSAENAMETOOLONG, "File name too long" }, |
| 349 | { WSAEHOSTDOWN, "Host is down" }, |
| 350 | { WSAEHOSTUNREACH, "No route to host" }, |
| 351 | { WSAENOTEMPTY, "Directory not empty" }, |
| 352 | { WSAEPROCLIM, "Too many processes" }, |
| 353 | { WSAEUSERS, "Too many users" }, |
| 354 | { WSAEDQUOT, "Disc quota exceeded" }, |
| 355 | { WSAESTALE, "Stale NFS file handle" }, |
| 356 | { WSAEREMOTE, "Too many levels of remote in path" }, |
| 357 | { WSASYSNOTREADY, |
| 358 | "Network subsystem is unvailable" }, |
| 359 | { WSAVERNOTSUPPORTED, |
| 360 | "WinSock version is not supported" }, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 361 | { WSANOTINITIALISED, |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 362 | "Successful WSAStartup() not yet performed" }, |
| 363 | { WSAEDISCON, "Graceful shutdown in progress" }, |
| 364 | /* Resolver errors */ |
| 365 | { WSAHOST_NOT_FOUND, "No such host is known" }, |
| 366 | { WSATRY_AGAIN, "Host not found, or server failed" }, |
| 367 | { WSANO_RECOVERY, |
| 368 | "Unexpected server error encountered" }, |
| 369 | { WSANO_DATA, "Valid name without requested data" }, |
| 370 | { WSANO_ADDRESS, "No address, look for MX record" }, |
| 371 | { 0, NULL } |
| 372 | }; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 373 | PyObject *v; |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 374 | const char *msg = "winsock error"; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 375 | |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 376 | for (msgp = msgs; msgp->msg; msgp++) { |
| 377 | if (err_no == msgp->no) { |
| 378 | msg = msgp->msg; |
| 379 | break; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | v = Py_BuildValue("(is)", err_no, msg); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 384 | if (v != NULL) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 385 | PyErr_SetObject(PySocket_Error, v); |
| 386 | Py_DECREF(v); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 387 | } |
| 388 | return NULL; |
| 389 | } |
| 390 | else |
| 391 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 392 | |
| 393 | #if defined(PYOS_OS2) |
| 394 | if (sock_errno() != NO_ERROR) { |
| 395 | APIRET rc; |
| 396 | ULONG msglen; |
| 397 | char outbuf[100]; |
| 398 | int myerrorcode = sock_errno(); |
| 399 | |
| 400 | /* Retrieve Socket-Related Error Message from MPTN.MSG File */ |
| 401 | rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf), |
| 402 | myerrorcode - SOCBASEERR + 26, "mptn.msg", &msglen); |
| 403 | if (rc == NO_ERROR) { |
| 404 | PyObject *v; |
| 405 | |
| 406 | outbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 407 | if (strlen(outbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 408 | char *lastc = &outbuf[ strlen(outbuf)-1 ]; |
| 409 | while (lastc > outbuf && isspace(*lastc)) |
| 410 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 411 | } |
| 412 | v = Py_BuildValue("(is)", myerrorcode, outbuf); |
| 413 | if (v != NULL) { |
| 414 | PyErr_SetObject(PySocket_Error, v); |
| 415 | Py_DECREF(v); |
| 416 | } |
| 417 | return NULL; |
| 418 | } |
| 419 | } |
| 420 | #endif |
| 421 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 422 | return PyErr_SetFromErrno(PySocket_Error); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 425 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 426 | static PyObject * |
| 427 | PyH_Err(int h_error) |
| 428 | { |
| 429 | PyObject *v; |
| 430 | |
| 431 | #ifdef HAVE_HSTRERROR |
| 432 | v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error)); |
| 433 | #else |
| 434 | v = Py_BuildValue("(is)", h_error, "host not found"); |
| 435 | #endif |
| 436 | if (v != NULL) { |
Martin v. Löwis | 864e9ff | 2001-08-04 22:32:03 +0000 | [diff] [blame] | 437 | PyErr_SetObject(PyH_Error, v); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 438 | Py_DECREF(v); |
| 439 | } |
| 440 | |
| 441 | return NULL; |
| 442 | } |
| 443 | |
| 444 | |
| 445 | static PyObject * |
| 446 | PyGAI_Err(int error) |
| 447 | { |
| 448 | PyObject *v; |
| 449 | |
| 450 | if (error == EAI_SYSTEM) |
| 451 | return PySocket_Err(); |
| 452 | |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 453 | #ifdef HAVE_GAI_STRERROR |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 454 | v = Py_BuildValue("(is)", error, gai_strerror(error)); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 455 | #else |
| 456 | v = Py_BuildValue("(is)", error, "getaddrinfo failed"); |
| 457 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 458 | if (v != NULL) { |
| 459 | PyErr_SetObject(PyGAI_Error, v); |
| 460 | Py_DECREF(v); |
| 461 | } |
| 462 | |
| 463 | return NULL; |
| 464 | } |
| 465 | |
| 466 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 467 | /* The object holding a socket. It holds some extra information, |
| 468 | like the address family, which is used to decode socket address |
| 469 | arguments properly. */ |
| 470 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 471 | typedef struct { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 472 | PyObject_HEAD |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 473 | SOCKET_T sock_fd; /* Socket file descriptor */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 474 | int sock_family; /* Address family, e.g., AF_INET */ |
| 475 | int sock_type; /* Socket type, e.g., SOCK_STREAM */ |
| 476 | int sock_proto; /* Protocol type, usually 0 */ |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 477 | union sock_addr { |
| 478 | struct sockaddr_in in; |
| 479 | #ifdef AF_UNIX |
| 480 | struct sockaddr_un un; |
| 481 | #endif |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 482 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 483 | struct sockaddr_in6 in6; |
| 484 | struct sockaddr_storage storage; |
| 485 | #endif |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 486 | #ifdef HAVE_NETPACKET_PACKET_H |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 487 | struct sockaddr_ll ll; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 488 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 489 | } sock_addr; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 490 | } PySocketSockObject; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 491 | |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 492 | #ifdef USE_SSL |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 493 | |
Jeremy Hylton | ba69936 | 2001-10-11 17:23:34 +0000 | [diff] [blame] | 494 | #define X509_NAME_MAXLEN 256 |
| 495 | |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 496 | typedef struct { |
| 497 | PyObject_HEAD |
| 498 | PySocketSockObject *Socket; /* Socket on which we're layered */ |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 499 | SSL_CTX* ctx; |
| 500 | SSL* ssl; |
| 501 | X509* server_cert; |
| 502 | BIO* sbio; |
Jeremy Hylton | ba69936 | 2001-10-11 17:23:34 +0000 | [diff] [blame] | 503 | char server[X509_NAME_MAXLEN]; |
| 504 | char issuer[X509_NAME_MAXLEN]; |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 505 | |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 506 | } PySSLObject; |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 507 | |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 508 | staticforward PyTypeObject PySSL_Type; |
| 509 | staticforward PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args); |
| 510 | staticforward PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args); |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 511 | |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 512 | #define PySSLObject_Check(v) ((v)->ob_type == &PySSL_Type) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 513 | |
| 514 | #endif /* USE_SSL */ |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 515 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 516 | /* A forward reference to the Socktype type object. |
| 517 | The Socktype variable contains pointers to various functions, |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 518 | some of which call newsockobject(), which uses Socktype, so |
Guido van Rossum | 54ba21b | 1991-09-10 14:57:12 +0000 | [diff] [blame] | 519 | there has to be a circular reference. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 520 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 521 | staticforward PyTypeObject PySocketSock_Type; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 522 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 523 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 524 | /* Initialize a new socket object. */ |
| 525 | |
| 526 | static void |
| 527 | init_sockobject(PySocketSockObject *s, |
| 528 | SOCKET_T fd, int family, int type, int proto) |
| 529 | { |
| 530 | #ifdef RISCOS |
| 531 | int block = 1; |
| 532 | #endif |
| 533 | s->sock_fd = fd; |
| 534 | s->sock_family = family; |
| 535 | s->sock_type = type; |
| 536 | s->sock_proto = proto; |
| 537 | #ifdef RISCOS |
| 538 | if(taskwindow) { |
| 539 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block); |
| 540 | } |
| 541 | #endif |
| 542 | } |
| 543 | |
| 544 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 545 | /* Create a new socket object. |
| 546 | This just creates the object and initializes it. |
| 547 | If the creation fails, return NULL and set an exception (implicit |
| 548 | in NEWOBJ()). */ |
| 549 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 550 | static PySocketSockObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 551 | PySocketSock_New(SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 552 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 553 | PySocketSockObject *s; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 554 | s = (PySocketSockObject *) |
| 555 | PyType_GenericNew(&PySocketSock_Type, NULL, NULL); |
| 556 | if (s != NULL) |
| 557 | init_sockobject(s, fd, family, type, proto); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 558 | return s; |
| 559 | } |
| 560 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 561 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 562 | /* Lock to allow python interpreter to continue, but only allow one |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 563 | thread to be in gethostbyname */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 564 | #ifdef USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 565 | PyThread_type_lock gethostbyname_lock; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 566 | #endif |
| 567 | |
| 568 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 569 | /* Convert a string specifying a host name or one of a few symbolic |
| 570 | names to a numeric IP address. This usually calls gethostbyname() |
| 571 | to do the work; the names "" and "<broadcast>" are special. |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 572 | Return the length (IPv4 should be 4 bytes), or negative if |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 573 | an error occurred; then an exception is raised. */ |
| 574 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 575 | static int |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 576 | setipaddr(char* name, struct sockaddr * addr_ret, int af) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 577 | { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 578 | struct addrinfo hints, *res; |
| 579 | int error; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 580 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 581 | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 582 | if (name[0] == '\0') { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 583 | int siz; |
| 584 | memset(&hints, 0, sizeof(hints)); |
| 585 | hints.ai_family = af; |
| 586 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
| 587 | hints.ai_flags = AI_PASSIVE; |
| 588 | error = getaddrinfo(NULL, "0", &hints, &res); |
| 589 | if (error) { |
| 590 | PyGAI_Err(error); |
| 591 | return -1; |
| 592 | } |
| 593 | switch (res->ai_family) { |
| 594 | case AF_INET: |
| 595 | siz = 4; |
| 596 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 597 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 598 | case AF_INET6: |
| 599 | siz = 16; |
| 600 | break; |
| 601 | #endif |
| 602 | default: |
| 603 | freeaddrinfo(res); |
| 604 | PyErr_SetString(PySocket_Error, |
| 605 | "unsupported address family"); |
| 606 | return -1; |
| 607 | } |
| 608 | if (res->ai_next) { |
Martin v. Löwis | f0b11d2 | 2001-11-07 08:31:03 +0000 | [diff] [blame] | 609 | freeaddrinfo(res); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 610 | PyErr_SetString(PySocket_Error, |
| 611 | "wildcard resolved to multiple address"); |
| 612 | return -1; |
| 613 | } |
| 614 | memcpy(addr_ret, res->ai_addr, res->ai_addrlen); |
| 615 | freeaddrinfo(res); |
| 616 | return siz; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 617 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 618 | if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 619 | struct sockaddr_in *sin; |
| 620 | if (af != PF_INET && af != PF_UNSPEC) { |
| 621 | PyErr_SetString(PySocket_Error, |
| 622 | "address family mismatched"); |
| 623 | return -1; |
| 624 | } |
| 625 | sin = (struct sockaddr_in *)addr_ret; |
| 626 | memset((void *) sin, '\0', sizeof(*sin)); |
| 627 | sin->sin_family = AF_INET; |
| 628 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 629 | sin->sin_len = sizeof(*sin); |
| 630 | #endif |
| 631 | sin->sin_addr.s_addr = INADDR_BROADCAST; |
| 632 | return sizeof(sin->sin_addr); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 633 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 634 | memset(&hints, 0, sizeof(hints)); |
| 635 | hints.ai_family = af; |
| 636 | error = getaddrinfo(name, NULL, &hints, &res); |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 637 | #if defined(__digital__) && defined(__unix__) |
Martin v. Löwis | b8fc972 | 2001-10-24 17:35:46 +0000 | [diff] [blame] | 638 | if (error == EAI_NONAME && af == AF_UNSPEC) { |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 639 | /* On Tru64 V5.1, numeric-to-addr conversion |
Martin v. Löwis | 861a65b | 2001-10-24 14:36:00 +0000 | [diff] [blame] | 640 | fails if no address family is given. Assume IPv4 for now.*/ |
| 641 | hints.ai_family = AF_INET; |
| 642 | error = getaddrinfo(name, NULL, &hints, &res); |
| 643 | } |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 644 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 645 | if (error) { |
| 646 | PyGAI_Err(error); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 647 | return -1; |
| 648 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 649 | memcpy((char *) addr_ret, res->ai_addr, res->ai_addrlen); |
| 650 | freeaddrinfo(res); |
| 651 | switch (addr_ret->sa_family) { |
| 652 | case AF_INET: |
| 653 | return 4; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 654 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 655 | case AF_INET6: |
| 656 | return 16; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 657 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 658 | default: |
| 659 | PyErr_SetString(PySocket_Error, "unknown address family"); |
| 660 | return -1; |
| 661 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 664 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 665 | /* Create a string object representing an IP address. |
| 666 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 667 | size numbers). */ |
| 668 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 669 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 670 | makeipaddr(struct sockaddr *addr, int addrlen) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 671 | { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 672 | char buf[NI_MAXHOST]; |
| 673 | int error; |
| 674 | |
| 675 | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, |
| 676 | NI_NUMERICHOST); |
| 677 | if (error) { |
| 678 | PyGAI_Err(error); |
| 679 | return NULL; |
| 680 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 681 | return PyString_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | |
| 685 | /* Create an object representing the given socket address, |
| 686 | suitable for passing it back to bind(), connect() etc. |
| 687 | The family field of the sockaddr structure is inspected |
| 688 | to determine what kind of address it really is. */ |
| 689 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 690 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 691 | static PyObject * |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 692 | makesockaddr(int sockfd, struct sockaddr *addr, int addrlen) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 693 | { |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 694 | if (addrlen == 0) { |
| 695 | /* No address -- may be recvfrom() from known socket */ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 696 | Py_INCREF(Py_None); |
| 697 | return Py_None; |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 700 | #ifdef __BEOS__ |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 701 | /* XXX: BeOS version of accept() doesn't set family correctly */ |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 702 | addr->sa_family = AF_INET; |
| 703 | #endif |
| 704 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 705 | switch (addr->sa_family) { |
| 706 | |
| 707 | case AF_INET: |
| 708 | { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 709 | struct sockaddr_in *a; |
| 710 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 711 | PyObject *ret = NULL; |
| 712 | if (addrobj) { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 713 | a = (struct sockaddr_in *)addr; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 714 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 715 | Py_DECREF(addrobj); |
| 716 | } |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 717 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 718 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 719 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 720 | #ifdef AF_UNIX |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 721 | case AF_UNIX: |
| 722 | { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 723 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 724 | return PyString_FromString(a->sun_path); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 725 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 726 | #endif /* AF_UNIX */ |
| 727 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 728 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 729 | case AF_INET6: |
| 730 | { |
| 731 | struct sockaddr_in6 *a; |
| 732 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 733 | PyObject *ret = NULL; |
| 734 | if (addrobj) { |
| 735 | a = (struct sockaddr_in6 *)addr; |
| 736 | ret = Py_BuildValue("Oiii", addrobj, ntohs(a->sin6_port), |
| 737 | a->sin6_flowinfo, a->sin6_scope_id); |
| 738 | Py_DECREF(addrobj); |
| 739 | } |
| 740 | return ret; |
| 741 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 742 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 743 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 744 | #ifdef HAVE_NETPACKET_PACKET_H |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 745 | case AF_PACKET: |
| 746 | { |
| 747 | struct sockaddr_ll *a = (struct sockaddr_ll *)addr; |
| 748 | char *ifname = ""; |
| 749 | struct ifreq ifr; |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 750 | /* need to look up interface name give index */ |
| 751 | if (a->sll_ifindex) { |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 752 | ifr.ifr_ifindex = a->sll_ifindex; |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 753 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 754 | ifname = ifr.ifr_name; |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 755 | } |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 756 | return Py_BuildValue("shbhs#", ifname, ntohs(a->sll_protocol), |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 757 | a->sll_pkttype, a->sll_hatype, |
| 758 | a->sll_addr, a->sll_halen); |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 759 | } |
| 760 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 761 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 762 | /* More cases here... */ |
| 763 | |
| 764 | default: |
Guido van Rossum | aa948df | 1997-05-07 17:41:48 +0000 | [diff] [blame] | 765 | /* If we don't know the address family, don't raise an |
| 766 | exception -- return it as a tuple. */ |
| 767 | return Py_BuildValue("is#", |
| 768 | addr->sa_family, |
| 769 | addr->sa_data, |
| 770 | sizeof(addr->sa_data)); |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 771 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 772 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 775 | |
| 776 | /* Parse a socket address argument according to the socket object's |
| 777 | address family. Return 1 if the address was in the proper format, |
| 778 | 0 of not. The address is returned through addr_ret, its length |
| 779 | through len_ret. */ |
| 780 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 781 | static int |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 782 | getsockaddrarg(PySocketSockObject *s, PyObject *args, |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 783 | struct sockaddr **addr_ret, int *len_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 784 | { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 785 | switch (s->sock_family) { |
| 786 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 787 | #ifdef AF_UNIX |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 788 | case AF_UNIX: |
| 789 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 790 | struct sockaddr_un* addr; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 791 | char *path; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 792 | int len; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 793 | addr = (struct sockaddr_un* )&(s->sock_addr).un; |
Guido van Rossum | 7e48898 | 1998-10-08 02:25:24 +0000 | [diff] [blame] | 794 | if (!PyArg_Parse(args, "t#", &path, &len)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 795 | return 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 796 | if (len > sizeof addr->sun_path) { |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 797 | PyErr_SetString(PySocket_Error, |
| 798 | "AF_UNIX path too long"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 799 | return 0; |
| 800 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 801 | addr->sun_family = s->sock_family; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 802 | memcpy(addr->sun_path, path, len); |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 803 | addr->sun_path[len] = 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 804 | *addr_ret = (struct sockaddr *) addr; |
Guido van Rossum | 65af28a | 1996-06-11 18:36:33 +0000 | [diff] [blame] | 805 | *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 806 | return 1; |
| 807 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 808 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 809 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 810 | case AF_INET: |
| 811 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 812 | struct sockaddr_in* addr; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 813 | char *host; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 814 | int port; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 815 | addr=(struct sockaddr_in*)&(s->sock_addr).in; |
Guido van Rossum | e4dad90 | 2000-12-01 13:13:11 +0000 | [diff] [blame] | 816 | if (!PyTuple_Check(args)) { |
| 817 | PyErr_Format(PyExc_TypeError, |
| 818 | "getsockaddrarg: AF_INET address must be tuple, not %.500s", |
| 819 | args->ob_type->tp_name); |
| 820 | return 0; |
| 821 | } |
| 822 | if (!PyArg_ParseTuple(args, "si:getsockaddrarg", &host, &port)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 823 | return 0; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 824 | if (setipaddr(host, (struct sockaddr *)addr, AF_INET) < 0) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 825 | return 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 826 | addr->sin_family = AF_INET; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 827 | addr->sin_port = htons((short)port); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 828 | *addr_ret = (struct sockaddr *) addr; |
| 829 | *len_ret = sizeof *addr; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 830 | return 1; |
| 831 | } |
| 832 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 833 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 834 | case AF_INET6: |
| 835 | { |
| 836 | struct sockaddr_in6* addr; |
| 837 | char *host; |
| 838 | int port, flowinfo, scope_id; |
| 839 | addr = (struct sockaddr_in6*)&(s->sock_addr).in6; |
| 840 | flowinfo = scope_id = 0; |
| 841 | if (!PyArg_ParseTuple(args, "si|ii", &host, &port, &flowinfo, |
| 842 | &scope_id)) { |
| 843 | return 0; |
| 844 | } |
| 845 | if (setipaddr(host, (struct sockaddr *)addr, AF_INET6) < 0) |
| 846 | return 0; |
| 847 | addr->sin6_family = s->sock_family; |
| 848 | addr->sin6_port = htons((short)port); |
| 849 | addr->sin6_flowinfo = flowinfo; |
| 850 | addr->sin6_scope_id = scope_id; |
| 851 | *addr_ret = (struct sockaddr *) addr; |
| 852 | *len_ret = sizeof *addr; |
| 853 | return 1; |
| 854 | } |
| 855 | #endif |
| 856 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 857 | #ifdef HAVE_NETPACKET_PACKET_H |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 858 | case AF_PACKET: |
| 859 | { |
| 860 | struct sockaddr_ll* addr; |
| 861 | struct ifreq ifr; |
| 862 | char *interfaceName; |
| 863 | int protoNumber; |
| 864 | int hatype = 0; |
| 865 | int pkttype = 0; |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 866 | char *haddr; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 867 | |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 868 | if (!PyArg_ParseTuple(args, "si|iis", &interfaceName, |
| 869 | &protoNumber, &pkttype, &hatype, &haddr)) |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 870 | return 0; |
| 871 | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name)); |
| 872 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 873 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
Jeremy Hylton | e2adc6c | 2001-10-11 17:27:58 +0000 | [diff] [blame] | 874 | PySocket_Err(); |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 875 | return 0; |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 876 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 877 | addr = &(s->sock_addr.ll); |
| 878 | addr->sll_family = AF_PACKET; |
| 879 | addr->sll_protocol = htons((short)protoNumber); |
| 880 | addr->sll_ifindex = ifr.ifr_ifindex; |
| 881 | addr->sll_pkttype = pkttype; |
| 882 | addr->sll_hatype = hatype; |
| 883 | *addr_ret = (struct sockaddr *) addr; |
| 884 | *len_ret = sizeof *addr; |
| 885 | return 1; |
| 886 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 887 | #endif |
| 888 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 889 | /* More cases here... */ |
| 890 | |
| 891 | default: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 892 | PyErr_SetString(PySocket_Error, "getsockaddrarg: bad family"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 893 | return 0; |
| 894 | |
| 895 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 898 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 899 | /* Get the address length according to the socket object's address family. |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 900 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 901 | through len_ret. */ |
| 902 | |
| 903 | static int |
Peter Schneider-Kamp | 618e25d | 2000-07-11 23:00:12 +0000 | [diff] [blame] | 904 | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 905 | { |
| 906 | switch (s->sock_family) { |
| 907 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 908 | #ifdef AF_UNIX |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 909 | case AF_UNIX: |
| 910 | { |
| 911 | *len_ret = sizeof (struct sockaddr_un); |
| 912 | return 1; |
| 913 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 914 | #endif /* AF_UNIX */ |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 915 | |
| 916 | case AF_INET: |
| 917 | { |
| 918 | *len_ret = sizeof (struct sockaddr_in); |
| 919 | return 1; |
| 920 | } |
| 921 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 922 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 923 | case AF_INET6: |
| 924 | { |
| 925 | *len_ret = sizeof (struct sockaddr_in6); |
| 926 | return 1; |
| 927 | } |
| 928 | #endif |
| 929 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 930 | #ifdef HAVE_NETPACKET_PACKET_H |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 931 | case AF_PACKET: |
| 932 | { |
| 933 | *len_ret = sizeof (struct sockaddr_ll); |
| 934 | return 1; |
| 935 | } |
| 936 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 937 | |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 938 | /* More cases here... */ |
| 939 | |
| 940 | default: |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 941 | PyErr_SetString(PySocket_Error, "getsockaddrlen: bad family"); |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 942 | return 0; |
| 943 | |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 948 | /* s.accept() method */ |
| 949 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 950 | static PyObject * |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 951 | PySocketSock_accept(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 952 | { |
| 953 | char addrbuf[256]; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 954 | SOCKET_T newfd; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 955 | socklen_t addrlen; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 956 | PyObject *sock = NULL; |
| 957 | PyObject *addr = NULL; |
| 958 | PyObject *res = NULL; |
| 959 | |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 960 | if (!getsockaddrlen(s, &addrlen)) |
| 961 | return NULL; |
Fred Drake | fd16834 | 2001-05-09 19:11:33 +0000 | [diff] [blame] | 962 | memset(addrbuf, 0, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 963 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 964 | newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 965 | Py_END_ALLOW_THREADS |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 966 | #ifdef MS_WINDOWS |
| 967 | if (newfd == INVALID_SOCKET) |
| 968 | #else |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 969 | if (newfd < 0) |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 970 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 971 | return PySocket_Err(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 972 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 973 | /* Create the new object with unspecified family, |
| 974 | to avoid calls to bind() etc. on it. */ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 975 | sock = (PyObject *) PySocketSock_New(newfd, |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 976 | s->sock_family, |
| 977 | s->sock_type, |
| 978 | s->sock_proto); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 979 | if (sock == NULL) { |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 980 | SOCKETCLOSE(newfd); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 981 | goto finally; |
| 982 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 983 | addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf, |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 984 | addrlen); |
| 985 | if (addr == NULL) |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 986 | goto finally; |
| 987 | |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 988 | res = Py_BuildValue("OO", sock, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 989 | |
| 990 | finally: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 991 | Py_XDECREF(sock); |
| 992 | Py_XDECREF(addr); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 993 | return res; |
| 994 | } |
| 995 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 996 | static char accept_doc[] = |
| 997 | "accept() -> (socket object, address info)\n\ |
| 998 | \n\ |
| 999 | Wait for an incoming connection. Return a new socket representing the\n\ |
| 1000 | connection, and the address of the client. For IP sockets, the address\n\ |
| 1001 | info is a pair (hostaddr, port)."; |
| 1002 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1003 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1004 | /* s.setblocking(1 | 0) method */ |
| 1005 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1006 | static PyObject * |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1007 | PySocketSock_setblocking(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1008 | { |
| 1009 | int block; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1010 | #ifndef RISCOS |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 1011 | #ifndef MS_WINDOWS |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1012 | int delay_flag; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 1013 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1014 | #endif |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1015 | block = PyInt_AsLong(arg); |
| 1016 | if (block == -1 && PyErr_Occurred()) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1017 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1018 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1019 | #ifdef __BEOS__ |
| 1020 | block = !block; |
| 1021 | setsockopt( s->sock_fd, SOL_SOCKET, SO_NONBLOCK, |
| 1022 | (void *)(&block), sizeof( int ) ); |
| 1023 | #else |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1024 | #ifndef RISCOS |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 1025 | #ifndef MS_WINDOWS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1026 | #ifdef PYOS_OS2 |
| 1027 | block = !block; |
| 1028 | ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block)); |
| 1029 | #else /* !PYOS_OS2 */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1030 | delay_flag = fcntl (s->sock_fd, F_GETFL, 0); |
| 1031 | if (block) |
| 1032 | delay_flag &= (~O_NDELAY); |
| 1033 | else |
| 1034 | delay_flag |= O_NDELAY; |
| 1035 | fcntl (s->sock_fd, F_SETFL, delay_flag); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1036 | #endif /* !PYOS_OS2 */ |
| 1037 | #else /* MS_WINDOWS */ |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 1038 | block = !block; |
| 1039 | ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1040 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1041 | #endif /* __BEOS__ */ |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1042 | #endif /* RISCOS */ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1043 | Py_END_ALLOW_THREADS |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1044 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1045 | Py_INCREF(Py_None); |
| 1046 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1047 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1048 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1049 | static char setblocking_doc[] = |
| 1050 | "setblocking(flag)\n\ |
| 1051 | \n\ |
| 1052 | Set the socket to blocking (flag is true) or non-blocking (false).\n\ |
| 1053 | This uses the FIONBIO ioctl with the O_NDELAY flag."; |
| 1054 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1055 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1056 | #ifdef RISCOS |
| 1057 | /* s.sleeptaskw(1 | 0) method */ |
| 1058 | |
| 1059 | static PyObject * |
| 1060 | PySocketSock_sleeptaskw(PySocketSockObject *s,PyObject *args) |
| 1061 | { |
| 1062 | int block; |
| 1063 | int delay_flag; |
| 1064 | if (!PyArg_GetInt(args, &block)) |
| 1065 | return NULL; |
| 1066 | Py_BEGIN_ALLOW_THREADS |
| 1067 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block); |
| 1068 | Py_END_ALLOW_THREADS |
| 1069 | |
| 1070 | Py_INCREF(Py_None); |
| 1071 | return Py_None; |
| 1072 | } |
| 1073 | static char sleeptaskw_doc[] = |
| 1074 | "sleeptaskw(flag)\n\ |
| 1075 | \n\ |
| 1076 | Allow sleeps in taskwindows."; |
| 1077 | #endif |
| 1078 | |
| 1079 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1080 | /* s.setsockopt() method. |
| 1081 | With an integer third argument, sets an integer option. |
| 1082 | With a string third argument, sets an option from a buffer; |
| 1083 | use optional built-in module 'struct' to encode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1084 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1085 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1086 | PySocketSock_setsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1087 | { |
| 1088 | int level; |
| 1089 | int optname; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1090 | int res; |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1091 | char *buf; |
| 1092 | int buflen; |
| 1093 | int flag; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1094 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1095 | if (PyArg_ParseTuple(args, "iii:setsockopt", |
| 1096 | &level, &optname, &flag)) { |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1097 | buf = (char *) &flag; |
| 1098 | buflen = sizeof flag; |
| 1099 | } |
| 1100 | else { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1101 | PyErr_Clear(); |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1102 | if (!PyArg_ParseTuple(args, "iis#:setsockopt", |
| 1103 | &level, &optname, &buf, &buflen)) |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1104 | return NULL; |
| 1105 | } |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1106 | res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen); |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1107 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1108 | return PySocket_Err(); |
| 1109 | Py_INCREF(Py_None); |
| 1110 | return Py_None; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1113 | static char setsockopt_doc[] = |
| 1114 | "setsockopt(level, option, value)\n\ |
| 1115 | \n\ |
| 1116 | Set a socket option. See the Unix manual for level and option.\n\ |
| 1117 | The value argument can either be an integer or a string."; |
| 1118 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1119 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1120 | /* s.getsockopt() method. |
| 1121 | With two arguments, retrieves an integer option. |
| 1122 | With a third integer argument, retrieves a string buffer of that size; |
| 1123 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1124 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1125 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1126 | PySocketSock_getsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1127 | { |
| 1128 | int level; |
| 1129 | int optname; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1130 | int res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1131 | PyObject *buf; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1132 | socklen_t buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1133 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1134 | #ifdef __BEOS__ |
Fred Drake | 564a6cc | 2001-05-11 20:12:26 +0000 | [diff] [blame] | 1135 | /* We have incomplete socket support. */ |
| 1136 | PyErr_SetString(PySocket_Error, "getsockopt not supported"); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1137 | return NULL; |
| 1138 | #else |
| 1139 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1140 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt", |
| 1141 | &level, &optname, &buflen)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1142 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1143 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1144 | if (buflen == 0) { |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1145 | int flag = 0; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1146 | socklen_t flagsize = sizeof flag; |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 1147 | res = getsockopt(s->sock_fd, level, optname, |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1148 | (void *)&flag, &flagsize); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1149 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1150 | return PySocket_Err(); |
| 1151 | return PyInt_FromLong(flag); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1152 | } |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1153 | if (buflen <= 0 || buflen > 1024) { |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1154 | PyErr_SetString(PySocket_Error, |
| 1155 | "getsockopt buflen out of range"); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1156 | return NULL; |
| 1157 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1158 | buf = PyString_FromStringAndSize((char *)NULL, buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1159 | if (buf == NULL) |
| 1160 | return NULL; |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 1161 | res = getsockopt(s->sock_fd, level, optname, |
Fred Drake | 564a6cc | 2001-05-11 20:12:26 +0000 | [diff] [blame] | 1162 | (void *)PyString_AS_STRING(buf), &buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1163 | if (res < 0) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1164 | Py_DECREF(buf); |
| 1165 | return PySocket_Err(); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1166 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1167 | _PyString_Resize(&buf, buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1168 | return buf; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1169 | #endif /* __BEOS__ */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1172 | static char getsockopt_doc[] = |
| 1173 | "getsockopt(level, option[, buffersize]) -> value\n\ |
| 1174 | \n\ |
| 1175 | Get a socket option. See the Unix manual for level and option.\n\ |
| 1176 | If a nonzero buffersize argument is given, the return value is a\n\ |
| 1177 | string of that length; otherwise it is an integer."; |
| 1178 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1179 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1180 | /* s.bind(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1181 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1182 | static PyObject * |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1183 | PySocketSock_bind(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1184 | { |
| 1185 | struct sockaddr *addr; |
| 1186 | int addrlen; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1187 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1188 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1189 | if (!getsockaddrarg(s, addro, &addr, &addrlen)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1190 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1191 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1192 | res = bind(s->sock_fd, addr, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1193 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1194 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1195 | return PySocket_Err(); |
| 1196 | Py_INCREF(Py_None); |
| 1197 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1200 | static char bind_doc[] = |
| 1201 | "bind(address)\n\ |
| 1202 | \n\ |
| 1203 | Bind the socket to a local address. For IP sockets, the address is a\n\ |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1204 | pair (host, port); the host must refer to the local host. For raw packet\n\ |
| 1205 | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"; |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1206 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1207 | |
| 1208 | /* s.close() method. |
| 1209 | Set the file descriptor to -1 so operations tried subsequently |
| 1210 | will surely fail. */ |
| 1211 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1212 | static PyObject * |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1213 | PySocketSock_close(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1214 | { |
Guido van Rossum | 20d3fc0 | 2000-12-18 22:23:44 +0000 | [diff] [blame] | 1215 | SOCKET_T fd; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1216 | |
Guido van Rossum | 20d3fc0 | 2000-12-18 22:23:44 +0000 | [diff] [blame] | 1217 | if ((fd = s->sock_fd) != -1) { |
| 1218 | s->sock_fd = -1; |
Guido van Rossum | 0848146 | 1996-10-12 14:07:22 +0000 | [diff] [blame] | 1219 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 20d3fc0 | 2000-12-18 22:23:44 +0000 | [diff] [blame] | 1220 | (void) SOCKETCLOSE(fd); |
Guido van Rossum | 0848146 | 1996-10-12 14:07:22 +0000 | [diff] [blame] | 1221 | Py_END_ALLOW_THREADS |
| 1222 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1223 | Py_INCREF(Py_None); |
| 1224 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1227 | static char close_doc[] = |
| 1228 | "close()\n\ |
| 1229 | \n\ |
| 1230 | Close the socket. It cannot be used after this call."; |
| 1231 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1232 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1233 | /* s.connect(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1234 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1235 | static PyObject * |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1236 | PySocketSock_connect(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1237 | { |
| 1238 | struct sockaddr *addr; |
| 1239 | int addrlen; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1240 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1241 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1242 | if (!getsockaddrarg(s, addro, &addr, &addrlen)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1243 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1244 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1245 | res = connect(s->sock_fd, addr, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1246 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1247 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1248 | return PySocket_Err(); |
| 1249 | Py_INCREF(Py_None); |
| 1250 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1253 | static char connect_doc[] = |
| 1254 | "connect(address)\n\ |
| 1255 | \n\ |
| 1256 | Connect the socket to a remote address. For IP sockets, the address\n\ |
| 1257 | is a pair (host, port)."; |
| 1258 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1259 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1260 | /* s.connect_ex(sockaddr) method */ |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1261 | |
| 1262 | static PyObject * |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1263 | PySocketSock_connect_ex(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1264 | { |
| 1265 | struct sockaddr *addr; |
| 1266 | int addrlen; |
| 1267 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1268 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1269 | if (!getsockaddrarg(s, addro, &addr, &addrlen)) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1270 | return NULL; |
| 1271 | Py_BEGIN_ALLOW_THREADS |
| 1272 | res = connect(s->sock_fd, addr, addrlen); |
| 1273 | Py_END_ALLOW_THREADS |
Tim Peters | c32410a | 2001-10-30 01:26:49 +0000 | [diff] [blame] | 1274 | if (res != 0) { |
| 1275 | #ifdef MS_WINDOWS |
| 1276 | res = WSAGetLastError(); |
| 1277 | #else |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1278 | res = errno; |
Tim Peters | c32410a | 2001-10-30 01:26:49 +0000 | [diff] [blame] | 1279 | #endif |
| 1280 | } |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1281 | return PyInt_FromLong((long) res); |
| 1282 | } |
| 1283 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1284 | static char connect_ex_doc[] = |
| 1285 | "connect_ex(address)\n\ |
| 1286 | \n\ |
| 1287 | This is like connect(address), but returns an error code (the errno value)\n\ |
| 1288 | instead of raising an exception when an error occurs."; |
| 1289 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1290 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 1291 | /* s.fileno() method */ |
| 1292 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1293 | static PyObject * |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1294 | PySocketSock_fileno(PySocketSockObject *s) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 1295 | { |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1296 | #if SIZEOF_SOCKET_T <= SIZEOF_LONG |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1297 | return PyInt_FromLong((long) s->sock_fd); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1298 | #else |
| 1299 | return PyLong_FromLongLong((LONG_LONG)s->sock_fd); |
| 1300 | #endif |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1303 | static char fileno_doc[] = |
| 1304 | "fileno() -> integer\n\ |
| 1305 | \n\ |
| 1306 | Return the integer file descriptor of the socket."; |
| 1307 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 1308 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1309 | #ifndef NO_DUP |
| 1310 | /* s.dup() method */ |
| 1311 | |
| 1312 | static PyObject * |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1313 | PySocketSock_dup(PySocketSockObject *s) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1314 | { |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1315 | SOCKET_T newfd; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1316 | PyObject *sock; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1317 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1318 | newfd = dup(s->sock_fd); |
| 1319 | if (newfd < 0) |
| 1320 | return PySocket_Err(); |
| 1321 | sock = (PyObject *) PySocketSock_New(newfd, |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1322 | s->sock_family, |
| 1323 | s->sock_type, |
| 1324 | s->sock_proto); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1325 | if (sock == NULL) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 1326 | SOCKETCLOSE(newfd); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1327 | return sock; |
| 1328 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1329 | |
| 1330 | static char dup_doc[] = |
| 1331 | "dup() -> socket object\n\ |
| 1332 | \n\ |
| 1333 | Return a new socket object connected to the same system resource."; |
| 1334 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1335 | #endif |
| 1336 | |
| 1337 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1338 | /* s.getsockname() method */ |
| 1339 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1340 | static PyObject * |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1341 | PySocketSock_getsockname(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1342 | { |
| 1343 | char addrbuf[256]; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1344 | int res; |
| 1345 | socklen_t addrlen; |
| 1346 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1347 | if (!getsockaddrlen(s, &addrlen)) |
| 1348 | return NULL; |
Guido van Rossum | ca42b16 | 1996-01-12 01:36:05 +0000 | [diff] [blame] | 1349 | memset(addrbuf, 0, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1350 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1351 | res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1352 | Py_END_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1353 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1354 | return PySocket_Err(); |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1355 | return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1358 | static char getsockname_doc[] = |
| 1359 | "getsockname() -> address info\n\ |
| 1360 | \n\ |
| 1361 | Return the address of the local endpoint. For IP sockets, the address\n\ |
| 1362 | info is a pair (hostaddr, port)."; |
| 1363 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1364 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1365 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1366 | /* s.getpeername() method */ |
| 1367 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1368 | static PyObject * |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1369 | PySocketSock_getpeername(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1370 | { |
| 1371 | char addrbuf[256]; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1372 | int res; |
| 1373 | socklen_t addrlen; |
| 1374 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1375 | if (!getsockaddrlen(s, &addrlen)) |
| 1376 | return NULL; |
Fred Drake | fd16834 | 2001-05-09 19:11:33 +0000 | [diff] [blame] | 1377 | memset(addrbuf, 0, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1378 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1379 | res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1380 | Py_END_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1381 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1382 | return PySocket_Err(); |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1383 | return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1384 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1385 | |
| 1386 | static char getpeername_doc[] = |
| 1387 | "getpeername() -> address info\n\ |
| 1388 | \n\ |
| 1389 | Return the address of the remote endpoint. For IP sockets, the address\n\ |
| 1390 | info is a pair (hostaddr, port)."; |
| 1391 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1392 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1393 | |
| 1394 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1395 | /* s.listen(n) method */ |
| 1396 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1397 | static PyObject * |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1398 | PySocketSock_listen(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1399 | { |
| 1400 | int backlog; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1401 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1402 | |
| 1403 | backlog = PyInt_AsLong(arg); |
| 1404 | if (backlog == -1 && PyErr_Occurred()) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1405 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1406 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1407 | if (backlog < 1) |
| 1408 | backlog = 1; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1409 | res = listen(s->sock_fd, backlog); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1410 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1411 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1412 | return PySocket_Err(); |
| 1413 | Py_INCREF(Py_None); |
| 1414 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1417 | static char listen_doc[] = |
| 1418 | "listen(backlog)\n\ |
| 1419 | \n\ |
| 1420 | Enable a server to accept connections. The backlog argument must be at\n\ |
| 1421 | least 1; it specifies the number of unaccepted connection that the system\n\ |
| 1422 | will allow before refusing new connections."; |
| 1423 | |
| 1424 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1425 | #ifndef NO_DUP |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1426 | /* s.makefile(mode) method. |
| 1427 | Create a new open file object referring to a dupped version of |
| 1428 | the socket's file descriptor. (The dup() call is necessary so |
| 1429 | that the open file and socket objects may be closed independent |
| 1430 | of each other.) |
| 1431 | The mode argument specifies 'r' or 'w' passed to fdopen(). */ |
| 1432 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1433 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1434 | PySocketSock_makefile(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1435 | { |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 1436 | extern int fclose(FILE *); |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 1437 | char *mode = "r"; |
| 1438 | int bufsize = -1; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1439 | #ifdef MS_WIN32 |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 1440 | Py_intptr_t fd; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1441 | #else |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1442 | int fd; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1443 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1444 | FILE *fp; |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 1445 | PyObject *f; |
| 1446 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1447 | if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1448 | return NULL; |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1449 | #ifdef MS_WIN32 |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1450 | if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) || |
| 1451 | ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL)) |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 1452 | #else |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1453 | 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] | 1454 | #endif |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1455 | { |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 1456 | if (fd >= 0) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 1457 | SOCKETCLOSE(fd); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1458 | return PySocket_Err(); |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 1459 | } |
| 1460 | f = PyFile_FromFile(fp, "<socket>", mode, fclose); |
| 1461 | if (f != NULL) |
| 1462 | PyFile_SetBufSize(f, bufsize); |
| 1463 | return f; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1464 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1465 | |
| 1466 | static char makefile_doc[] = |
| 1467 | "makefile([mode[, buffersize]]) -> file object\n\ |
| 1468 | \n\ |
| 1469 | Return a regular file object corresponding to the socket.\n\ |
| 1470 | The mode and buffersize arguments are as for the built-in open() function."; |
| 1471 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1472 | #endif /* NO_DUP */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1473 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1474 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1475 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1476 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1477 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1478 | PySocketSock_recv(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1479 | { |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1480 | int len, n, flags = 0; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1481 | PyObject *buf; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1482 | if (!PyArg_ParseTuple(args, "i|i:recv", &len, &flags)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1483 | return NULL; |
Martin v. Löwis | fba64e1 | 2001-11-19 10:41:26 +0000 | [diff] [blame] | 1484 | if (len < 0) { |
| 1485 | PyErr_SetString(PyExc_ValueError, |
| 1486 | "negative buffersize in connect"); |
| 1487 | return NULL; |
| 1488 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1489 | buf = PyString_FromStringAndSize((char *) 0, len); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1490 | if (buf == NULL) |
| 1491 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1492 | Py_BEGIN_ALLOW_THREADS |
Jeremy Hylton | 1ba6bad | 2001-08-20 19:06:36 +0000 | [diff] [blame] | 1493 | n = recv(s->sock_fd, PyString_AS_STRING(buf), len, flags); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1494 | Py_END_ALLOW_THREADS |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 1495 | if (n < 0) { |
| 1496 | Py_DECREF(buf); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1497 | return PySocket_Err(); |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 1498 | } |
Guido van Rossum | 53a9bf8 | 1996-06-11 18:35:24 +0000 | [diff] [blame] | 1499 | if (n != len && _PyString_Resize(&buf, n) < 0) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1500 | return NULL; |
| 1501 | return buf; |
| 1502 | } |
| 1503 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1504 | static char recv_doc[] = |
| 1505 | "recv(buffersize[, flags]) -> data\n\ |
| 1506 | \n\ |
| 1507 | Receive up to buffersize bytes from the socket. For the optional flags\n\ |
| 1508 | argument, see the Unix manual. When no data is available, block until\n\ |
| 1509 | at least one byte is available or until the remote end is closed. When\n\ |
| 1510 | the remote end is closed and all data is read, return the empty string."; |
| 1511 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1512 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1513 | /* s.recvfrom(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1514 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1515 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1516 | PySocketSock_recvfrom(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1517 | { |
| 1518 | char addrbuf[256]; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1519 | PyObject *buf = NULL; |
| 1520 | PyObject *addr = NULL; |
| 1521 | PyObject *ret = NULL; |
| 1522 | |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1523 | int len, n, flags = 0; |
| 1524 | socklen_t addrlen; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1525 | if (!PyArg_ParseTuple(args, "i|i:recvfrom", &len, &flags)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1526 | return NULL; |
Guido van Rossum | 18c9a4f | 1993-05-25 12:16:29 +0000 | [diff] [blame] | 1527 | if (!getsockaddrlen(s, &addrlen)) |
| 1528 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1529 | buf = PyString_FromStringAndSize((char *) 0, len); |
Guido van Rossum | 18c9a4f | 1993-05-25 12:16:29 +0000 | [diff] [blame] | 1530 | if (buf == NULL) |
| 1531 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1532 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | fd16834 | 2001-05-09 19:11:33 +0000 | [diff] [blame] | 1533 | memset(addrbuf, 0, addrlen); |
| 1534 | n = recvfrom(s->sock_fd, PyString_AS_STRING(buf), len, flags, |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1535 | #ifndef MS_WINDOWS |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 1536 | #if defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1537 | (struct sockaddr *)addrbuf, &addrlen |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 1538 | #else |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1539 | (void *)addrbuf, &addrlen |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 1540 | #endif |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1541 | #else |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1542 | (struct sockaddr *)addrbuf, &addrlen |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1543 | #endif |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1544 | ); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1545 | Py_END_ALLOW_THREADS |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 1546 | if (n < 0) { |
| 1547 | Py_DECREF(buf); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1548 | return PySocket_Err(); |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 1549 | } |
Guido van Rossum | 53a9bf8 | 1996-06-11 18:35:24 +0000 | [diff] [blame] | 1550 | if (n != len && _PyString_Resize(&buf, n) < 0) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1551 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1552 | |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1553 | if (!(addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf, addrlen))) |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1554 | goto finally; |
| 1555 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1556 | ret = Py_BuildValue("OO", buf, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1557 | finally: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1558 | Py_XDECREF(addr); |
| 1559 | Py_XDECREF(buf); |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 1560 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1563 | static char recvfrom_doc[] = |
| 1564 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
| 1565 | \n\ |
| 1566 | Like recv(buffersize, flags) but also return the sender's address info."; |
| 1567 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1568 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1569 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1570 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1571 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1572 | PySocketSock_send(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1573 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1574 | char *buf; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1575 | int len, n, flags = 0; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1576 | if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1577 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1578 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1579 | n = send(s->sock_fd, buf, len, flags); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1580 | Py_END_ALLOW_THREADS |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1581 | if (n < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1582 | return PySocket_Err(); |
| 1583 | return PyInt_FromLong((long)n); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1586 | static char send_doc[] = |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 1587 | "send(data[, flags]) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1588 | \n\ |
| 1589 | Send a data string to the socket. For the optional flags\n\ |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 1590 | argument, see the Unix manual. Return the number of bytes\n\ |
| 1591 | sent; this may be less than len(data) if the network is busy."; |
| 1592 | |
| 1593 | |
| 1594 | /* s.sendall(data [,flags]) method */ |
| 1595 | |
| 1596 | static PyObject * |
| 1597 | PySocketSock_sendall(PySocketSockObject *s, PyObject *args) |
| 1598 | { |
| 1599 | char *buf; |
| 1600 | int len, n, flags = 0, total = 0; |
| 1601 | if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags)) |
| 1602 | return NULL; |
| 1603 | Py_BEGIN_ALLOW_THREADS |
| 1604 | do { |
| 1605 | n = send(s->sock_fd, buf, len, flags); |
| 1606 | if (n < 0) |
| 1607 | break; |
| 1608 | total += n; |
| 1609 | buf += n; |
| 1610 | len -= n; |
| 1611 | } while (len > 0); |
| 1612 | Py_END_ALLOW_THREADS |
| 1613 | if (n < 0) |
| 1614 | return PySocket_Err(); |
| 1615 | Py_INCREF(Py_None); |
| 1616 | return Py_None; |
| 1617 | } |
| 1618 | |
| 1619 | static char sendall_doc[] = |
| 1620 | "sendall(data[, flags])\n\ |
| 1621 | \n\ |
| 1622 | Send a data string to the socket. For the optional flags\n\ |
| 1623 | argument, see the Unix manual. This calls send() repeatedly\n\ |
| 1624 | until all data is sent. If an error occurs, it's impossible\n\ |
| 1625 | to tell how much data has been sent."; |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1626 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1627 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1628 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1629 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1630 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1631 | PySocketSock_sendto(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1632 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1633 | PyObject *addro; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1634 | char *buf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1635 | struct sockaddr *addr; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1636 | int addrlen, len, n, flags; |
| 1637 | flags = 0; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1638 | if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1639 | PyErr_Clear(); |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1640 | if (!PyArg_ParseTuple(args, "s#iO:sendto", |
| 1641 | &buf, &len, &flags, &addro)) |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1642 | return NULL; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1643 | } |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1644 | if (!getsockaddrarg(s, addro, &addr, &addrlen)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1645 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1646 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1647 | n = sendto(s->sock_fd, buf, len, flags, addr, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1648 | Py_END_ALLOW_THREADS |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1649 | if (n < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1650 | return PySocket_Err(); |
| 1651 | return PyInt_FromLong((long)n); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1654 | static char sendto_doc[] = |
| 1655 | "sendto(data[, flags], address)\n\ |
| 1656 | \n\ |
| 1657 | Like send(data, flags) but allows specifying the destination address.\n\ |
| 1658 | For IP sockets, the address is a pair (hostaddr, port)."; |
| 1659 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1660 | |
| 1661 | /* s.shutdown(how) method */ |
| 1662 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1663 | static PyObject * |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1664 | PySocketSock_shutdown(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1665 | { |
| 1666 | int how; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1667 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1668 | |
| 1669 | how = PyInt_AsLong(arg); |
| 1670 | if (how == -1 && PyErr_Occurred()) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1671 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1672 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1673 | res = shutdown(s->sock_fd, how); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1674 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1675 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1676 | return PySocket_Err(); |
| 1677 | Py_INCREF(Py_None); |
| 1678 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1681 | static char shutdown_doc[] = |
| 1682 | "shutdown(flag)\n\ |
| 1683 | \n\ |
| 1684 | Shut down the reading side of the socket (flag == 0), the writing side\n\ |
| 1685 | of the socket (flag == 1), or both ends (flag == 2)."; |
| 1686 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1687 | |
| 1688 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1689 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1690 | static PyMethodDef PySocketSock_methods[] = { |
Guido van Rossum | 17209fc | 2001-10-15 21:12:54 +0000 | [diff] [blame] | 1691 | {"accept", (PyCFunction)PySocketSock_accept, METH_NOARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1692 | accept_doc}, |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1693 | {"bind", (PyCFunction)PySocketSock_bind, METH_O, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1694 | bind_doc}, |
Guido van Rossum | 17209fc | 2001-10-15 21:12:54 +0000 | [diff] [blame] | 1695 | {"close", (PyCFunction)PySocketSock_close, METH_NOARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1696 | close_doc}, |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1697 | {"connect", (PyCFunction)PySocketSock_connect, METH_O, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1698 | connect_doc}, |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1699 | {"connect_ex", (PyCFunction)PySocketSock_connect_ex, METH_O, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1700 | connect_ex_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1701 | #ifndef NO_DUP |
Guido van Rossum | 17209fc | 2001-10-15 21:12:54 +0000 | [diff] [blame] | 1702 | {"dup", (PyCFunction)PySocketSock_dup, METH_NOARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1703 | dup_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1704 | #endif |
Guido van Rossum | 17209fc | 2001-10-15 21:12:54 +0000 | [diff] [blame] | 1705 | {"fileno", (PyCFunction)PySocketSock_fileno, METH_NOARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1706 | fileno_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1707 | #ifdef HAVE_GETPEERNAME |
Guido van Rossum | 17209fc | 2001-10-15 21:12:54 +0000 | [diff] [blame] | 1708 | {"getpeername", (PyCFunction)PySocketSock_getpeername, |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1709 | METH_NOARGS, getpeername_doc}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 1710 | #endif |
Guido van Rossum | 17209fc | 2001-10-15 21:12:54 +0000 | [diff] [blame] | 1711 | {"getsockname", (PyCFunction)PySocketSock_getsockname, |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1712 | METH_NOARGS, getsockname_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1713 | {"getsockopt", (PyCFunction)PySocketSock_getsockopt, METH_VARARGS, |
| 1714 | getsockopt_doc}, |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1715 | {"listen", (PyCFunction)PySocketSock_listen, METH_O, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1716 | listen_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1717 | #ifndef NO_DUP |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1718 | {"makefile", (PyCFunction)PySocketSock_makefile, METH_VARARGS, |
| 1719 | makefile_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1720 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1721 | {"recv", (PyCFunction)PySocketSock_recv, METH_VARARGS, |
| 1722 | recv_doc}, |
| 1723 | {"recvfrom", (PyCFunction)PySocketSock_recvfrom, METH_VARARGS, |
| 1724 | recvfrom_doc}, |
| 1725 | {"send", (PyCFunction)PySocketSock_send, METH_VARARGS, |
| 1726 | send_doc}, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 1727 | {"sendall", (PyCFunction)PySocketSock_sendall, METH_VARARGS, |
| 1728 | sendall_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1729 | {"sendto", (PyCFunction)PySocketSock_sendto, METH_VARARGS, |
| 1730 | sendto_doc}, |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1731 | {"setblocking", (PyCFunction)PySocketSock_setblocking, METH_O, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1732 | setblocking_doc}, |
| 1733 | {"setsockopt", (PyCFunction)PySocketSock_setsockopt, METH_VARARGS, |
| 1734 | setsockopt_doc}, |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1735 | {"shutdown", (PyCFunction)PySocketSock_shutdown, METH_O, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1736 | shutdown_doc}, |
| 1737 | #ifdef RISCOS |
| 1738 | {"sleeptaskw", (PyCFunction)PySocketSock_sleeptaskw, METH_VARARGS, |
| 1739 | sleeptaskw_doc}, |
| 1740 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1741 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1742 | }; |
| 1743 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1744 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1745 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1746 | First close the file description. */ |
| 1747 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1748 | static void |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1749 | PySocketSock_dealloc(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1750 | { |
Guido van Rossum | fa972c9 | 2000-04-10 12:45:45 +0000 | [diff] [blame] | 1751 | if (s->sock_fd != -1) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 1752 | (void) SOCKETCLOSE(s->sock_fd); |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 1753 | s->ob_type->tp_free((PyObject *)s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1756 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1757 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1758 | PySocketSock_repr(PySocketSockObject *s) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1759 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1760 | char buf[512]; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1761 | #if SIZEOF_SOCKET_T > SIZEOF_LONG |
| 1762 | if (s->sock_fd > LONG_MAX) { |
| 1763 | /* this can occur on Win64, and actually there is a special |
| 1764 | ugly printf formatter for decimal pointer length integer |
| 1765 | printing, only bother if necessary*/ |
| 1766 | PyErr_SetString(PyExc_OverflowError, |
| 1767 | "no printf formatter to display the socket descriptor in decimal"); |
| 1768 | return NULL; |
| 1769 | } |
| 1770 | #endif |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 1771 | PyOS_snprintf(buf, sizeof(buf), |
| 1772 | "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>", |
| 1773 | (long)s->sock_fd, s->sock_family, |
| 1774 | s->sock_type, |
| 1775 | s->sock_proto); |
Guido van Rossum | dc1c64a | 1996-02-27 15:21:19 +0000 | [diff] [blame] | 1776 | return PyString_FromString(buf); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 1780 | /* Create a new, uninitialized socket object. */ |
| 1781 | |
| 1782 | static PyObject * |
| 1783 | PySocketSock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1784 | { |
| 1785 | PyObject *new; |
| 1786 | |
| 1787 | new = type->tp_alloc(type, 0); |
| 1788 | if (new != NULL) |
| 1789 | ((PySocketSockObject *)new)->sock_fd = -1; |
| 1790 | return new; |
| 1791 | } |
| 1792 | |
| 1793 | |
| 1794 | /* Initialize a new socket object. */ |
| 1795 | |
| 1796 | /*ARGSUSED*/ |
| 1797 | static int |
| 1798 | PySocketSock_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1799 | { |
| 1800 | PySocketSockObject *s = (PySocketSockObject *)self; |
| 1801 | SOCKET_T fd; |
| 1802 | int family = AF_INET, type = SOCK_STREAM, proto = 0; |
| 1803 | static char *keywords[] = {"family", "type", "proto", 0}; |
| 1804 | |
| 1805 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 1806 | "|iii:socket", keywords, |
| 1807 | &family, &type, &proto)) |
| 1808 | return -1; |
| 1809 | Py_BEGIN_ALLOW_THREADS |
| 1810 | fd = socket(family, type, proto); |
| 1811 | Py_END_ALLOW_THREADS |
| 1812 | #ifdef MS_WINDOWS |
| 1813 | if (fd == INVALID_SOCKET) |
| 1814 | #else |
| 1815 | if (fd < 0) |
| 1816 | #endif |
| 1817 | { |
| 1818 | PySocket_Err(); |
| 1819 | return -1; |
| 1820 | } |
| 1821 | init_sockobject(s, fd, family, type, proto); |
| 1822 | /* From now on, ignore SIGPIPE and let the error checking |
| 1823 | do the work. */ |
| 1824 | #ifdef SIGPIPE |
| 1825 | (void) signal(SIGPIPE, SIG_IGN); |
| 1826 | #endif |
| 1827 | return 0; |
| 1828 | } |
| 1829 | |
| 1830 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1831 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1832 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 1833 | static char socket_doc[] = |
| 1834 | "socket([family[, type[, proto]]]) -> socket object\n\ |
| 1835 | \n\ |
| 1836 | Open a socket of the given type. The family argument specifies the\n\ |
| 1837 | address family; it defaults to AF_INET. The type argument specifies\n\ |
| 1838 | whether this is a stream (SOCK_STREAM, this is the default)\n\ |
| 1839 | or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\ |
| 1840 | specifying the default protocol.\n\ |
| 1841 | \n\ |
| 1842 | A socket represents one endpoint of a network connection.\n\ |
| 1843 | \n\ |
| 1844 | Methods:\n\ |
| 1845 | \n\ |
| 1846 | accept() -- accept a connection, returning new socket and client address\n\ |
| 1847 | bind() -- bind the socket to a local address\n\ |
| 1848 | close() -- close the socket\n\ |
| 1849 | connect() -- connect the socket to a remote address\n\ |
| 1850 | connect_ex() -- connect, return an error code instead of an exception \n\ |
| 1851 | dup() -- return a new socket object identical to the current one (*)\n\ |
| 1852 | fileno() -- return underlying file descriptor\n\ |
| 1853 | getpeername() -- return remote address (*)\n\ |
| 1854 | getsockname() -- return local address\n\ |
| 1855 | getsockopt() -- get socket options\n\ |
| 1856 | listen() -- start listening for incoming connections\n\ |
Andrew M. Kuchling | 3072ecd | 2001-11-28 04:28:31 +0000 | [diff] [blame] | 1857 | makefile() -- return a file object corresponding to the socket (*)\n\ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 1858 | recv() -- receive data\n\ |
| 1859 | recvfrom() -- receive data and sender's address\n\ |
| 1860 | send() -- send data, may not send all of it\n\ |
| 1861 | sendall() -- send all data\n\ |
| 1862 | sendto() -- send data to a given address\n\ |
| 1863 | setblocking() -- set or clear the blocking I/O flag\n\ |
| 1864 | setsockopt() -- set socket options\n\ |
| 1865 | shutdown() -- shut down traffic in one or both directions\n\ |
| 1866 | \n\ |
| 1867 | (*) not available on all platforms!)"; |
| 1868 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1869 | static PyTypeObject PySocketSock_Type = { |
Guido van Rossum | 5555854 | 1996-05-23 22:54:50 +0000 | [diff] [blame] | 1870 | PyObject_HEAD_INIT(0) /* Must fill in type value later */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 1871 | 0, /* ob_size */ |
Guido van Rossum | ca6dfa5 | 2001-10-28 12:31:33 +0000 | [diff] [blame] | 1872 | "_socket.socket", /* tp_name */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 1873 | sizeof(PySocketSockObject), /* tp_basicsize */ |
| 1874 | 0, /* tp_itemsize */ |
| 1875 | (destructor)PySocketSock_dealloc, /* tp_dealloc */ |
| 1876 | 0, /* tp_print */ |
| 1877 | 0, /* tp_getattr */ |
| 1878 | 0, /* tp_setattr */ |
| 1879 | 0, /* tp_compare */ |
| 1880 | (reprfunc)PySocketSock_repr, /* tp_repr */ |
| 1881 | 0, /* tp_as_number */ |
| 1882 | 0, /* tp_as_sequence */ |
| 1883 | 0, /* tp_as_mapping */ |
| 1884 | 0, /* tp_hash */ |
| 1885 | 0, /* tp_call */ |
| 1886 | 0, /* tp_str */ |
Michael W. Hudson | 03f96bd | 2001-11-09 10:06:23 +0000 | [diff] [blame] | 1887 | 0, /* set below */ /* tp_getattro */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 1888 | 0, /* tp_setattro */ |
| 1889 | 0, /* tp_as_buffer */ |
| 1890 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1891 | socket_doc, /* tp_doc */ |
| 1892 | 0, /* tp_traverse */ |
| 1893 | 0, /* tp_clear */ |
| 1894 | 0, /* tp_richcompare */ |
| 1895 | 0, /* tp_weaklistoffset */ |
| 1896 | 0, /* tp_iter */ |
| 1897 | 0, /* tp_iternext */ |
| 1898 | PySocketSock_methods, /* tp_methods */ |
| 1899 | 0, /* tp_members */ |
| 1900 | 0, /* tp_getset */ |
| 1901 | 0, /* tp_base */ |
| 1902 | 0, /* tp_dict */ |
| 1903 | 0, /* tp_descr_get */ |
| 1904 | 0, /* tp_descr_set */ |
| 1905 | 0, /* tp_dictoffset */ |
| 1906 | PySocketSock_init, /* tp_init */ |
Michael W. Hudson | 03f96bd | 2001-11-09 10:06:23 +0000 | [diff] [blame] | 1907 | 0, /* set below */ /* tp_alloc */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 1908 | PySocketSock_new, /* tp_new */ |
Michael W. Hudson | 03f96bd | 2001-11-09 10:06:23 +0000 | [diff] [blame] | 1909 | 0, /* set below */ /* tp_free */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1910 | }; |
| 1911 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1912 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1913 | /* Python interface to gethostname(). */ |
| 1914 | |
| 1915 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1916 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1917 | PySocket_gethostname(PyObject *self, PyObject *args) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1918 | { |
| 1919 | char buf[1024]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1920 | int res; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1921 | if (!PyArg_ParseTuple(args, ":gethostname")) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1922 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1923 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1924 | res = gethostname(buf, (int) sizeof buf - 1); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1925 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1926 | if (res < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1927 | return PySocket_Err(); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1928 | buf[sizeof buf - 1] = '\0'; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1929 | return PyString_FromString(buf); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 1930 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1931 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1932 | static char gethostname_doc[] = |
| 1933 | "gethostname() -> string\n\ |
| 1934 | \n\ |
| 1935 | Return the current host name."; |
| 1936 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1937 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1938 | /* Python interface to gethostbyname(name). */ |
| 1939 | |
| 1940 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1941 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 1942 | PySocket_gethostbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1943 | { |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1944 | char *name; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1945 | struct sockaddr_storage addrbuf; |
| 1946 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1947 | if (!PyArg_ParseTuple(args, "s:gethostbyname", &name)) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1948 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1949 | if (setipaddr(name, (struct sockaddr *)&addrbuf, AF_INET) < 0) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1950 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1951 | return makeipaddr((struct sockaddr *)&addrbuf, |
| 1952 | sizeof(struct sockaddr_in)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1955 | static char gethostbyname_doc[] = |
| 1956 | "gethostbyname(host) -> address\n\ |
| 1957 | \n\ |
| 1958 | Return the IP address (a string of the form '255.255.255.255') for a host."; |
| 1959 | |
| 1960 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1961 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
| 1962 | |
| 1963 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1964 | gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1965 | { |
| 1966 | char **pch; |
| 1967 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 1968 | PyObject *name_list = (PyObject *)NULL; |
| 1969 | PyObject *addr_list = (PyObject *)NULL; |
| 1970 | PyObject *tmp; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1971 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1972 | if (h == NULL) { |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1973 | /* Let's get real error message to return */ |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 1974 | #ifndef RISCOS |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1975 | PyH_Err(h_errno); |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 1976 | #else |
| 1977 | PyErr_SetString(PySocket_Error, "host not found"); |
| 1978 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1979 | return NULL; |
| 1980 | } |
| 1981 | if (h->h_addrtype != af) { |
| 1982 | #ifdef HAVE_STRERROR |
| 1983 | /* Let's get real error message to return */ |
| 1984 | PyErr_SetString(PySocket_Error, (char *)strerror(EAFNOSUPPORT)); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1985 | #else |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1986 | PyErr_SetString(PySocket_Error, |
| 1987 | "Address family not supported by protocol family"); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 1988 | #endif |
| 1989 | return NULL; |
| 1990 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1991 | switch (af) { |
| 1992 | case AF_INET: |
| 1993 | if (alen < sizeof(struct sockaddr_in)) |
| 1994 | return NULL; |
| 1995 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1996 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1997 | case AF_INET6: |
| 1998 | if (alen < sizeof(struct sockaddr_in6)) |
| 1999 | return NULL; |
| 2000 | break; |
| 2001 | #endif |
| 2002 | } |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2003 | if ((name_list = PyList_New(0)) == NULL) |
| 2004 | goto err; |
| 2005 | if ((addr_list = PyList_New(0)) == NULL) |
| 2006 | goto err; |
| 2007 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 2008 | int status; |
| 2009 | tmp = PyString_FromString(*pch); |
| 2010 | if (tmp == NULL) |
| 2011 | goto err; |
| 2012 | status = PyList_Append(name_list, tmp); |
| 2013 | Py_DECREF(tmp); |
| 2014 | if (status) |
| 2015 | goto err; |
| 2016 | } |
| 2017 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 2018 | int status; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2019 | switch (af) { |
| 2020 | case AF_INET: |
| 2021 | { |
| 2022 | struct sockaddr_in sin; |
| 2023 | memset(&sin, 0, sizeof(sin)); |
| 2024 | sin.sin_family = af; |
| 2025 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 2026 | sin.sin_len = sizeof(sin); |
| 2027 | #endif |
| 2028 | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); |
| 2029 | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); |
| 2030 | if (pch == h->h_addr_list && alen >= sizeof(sin)) |
| 2031 | memcpy((char *) addr, &sin, sizeof(sin)); |
| 2032 | break; |
| 2033 | } |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 2034 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2035 | case AF_INET6: |
| 2036 | { |
| 2037 | struct sockaddr_in6 sin6; |
| 2038 | memset(&sin6, 0, sizeof(sin6)); |
| 2039 | sin6.sin6_family = af; |
| 2040 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 2041 | sin6.sin6_len = sizeof(sin6); |
| 2042 | #endif |
| 2043 | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); |
| 2044 | tmp = makeipaddr((struct sockaddr *)&sin6, |
| 2045 | sizeof(sin6)); |
| 2046 | if (pch == h->h_addr_list && alen >= sizeof(sin6)) |
| 2047 | memcpy((char *) addr, &sin6, sizeof(sin6)); |
| 2048 | break; |
| 2049 | } |
| 2050 | #endif |
| 2051 | default: /* can't happen */ |
| 2052 | PyErr_SetString(PySocket_Error, |
| 2053 | "unsupported address family"); |
| 2054 | return NULL; |
| 2055 | } |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2056 | if (tmp == NULL) |
| 2057 | goto err; |
| 2058 | status = PyList_Append(addr_list, tmp); |
| 2059 | Py_DECREF(tmp); |
| 2060 | if (status) |
| 2061 | goto err; |
| 2062 | } |
| 2063 | rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list); |
| 2064 | err: |
| 2065 | Py_XDECREF(name_list); |
| 2066 | Py_XDECREF(addr_list); |
| 2067 | return rtn_tuple; |
| 2068 | } |
| 2069 | |
| 2070 | |
| 2071 | /* Python interface to gethostbyname_ex(name). */ |
| 2072 | |
| 2073 | /*ARGSUSED*/ |
| 2074 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 2075 | PySocket_gethostbyname_ex(PyObject *self, PyObject *args) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2076 | { |
| 2077 | char *name; |
| 2078 | struct hostent *h; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2079 | struct sockaddr_storage addr; |
Martin v. Löwis | 9db2f57 | 2001-07-23 01:30:10 +0000 | [diff] [blame] | 2080 | struct sockaddr *sa; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 2081 | PyObject *ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2082 | #ifdef HAVE_GETHOSTBYNAME_R |
| 2083 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2084 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
| 2085 | struct hostent_data data; |
| 2086 | #else |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2087 | char buf[16384]; |
| 2088 | int buf_len = (sizeof buf) - 1; |
| 2089 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2090 | #endif |
| 2091 | #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] | 2092 | int result; |
| 2093 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2094 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2095 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2096 | if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name)) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2097 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2098 | if (setipaddr(name, (struct sockaddr *)&addr, PF_INET) < 0) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2099 | return NULL; |
| 2100 | Py_BEGIN_ALLOW_THREADS |
| 2101 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2102 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 2103 | result = gethostbyname_r(name, &hp_allocated, buf, buf_len, &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2104 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2105 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2106 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 2107 | memset((void *) &data, '\0', sizeof(data)); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2108 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 2109 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 2110 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2111 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 2112 | #ifdef USE_GETHOSTBYNAME_LOCK |
| 2113 | PyThread_acquire_lock(gethostbyname_lock, 1); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2114 | #endif |
| 2115 | h = gethostbyname(name); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2116 | #endif /* HAVE_GETHOSTBYNAME_R */ |
| 2117 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 9db2f57 | 2001-07-23 01:30:10 +0000 | [diff] [blame] | 2118 | /* Some C libraries would require addr.__ss_family instead of addr.ss_family. |
| 2119 | Therefore, we cast the sockaddr_storage into sockaddr to access sa_family. */ |
| 2120 | sa = (struct sockaddr*)&addr; |
| 2121 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), sa->sa_family); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 2122 | #ifdef USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 2123 | PyThread_release_lock(gethostbyname_lock); |
| 2124 | #endif |
| 2125 | return ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | static char ghbn_ex_doc[] = |
| 2129 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
| 2130 | \n\ |
| 2131 | Return the true host name, a list of aliases, and a list of IP addresses,\n\ |
| 2132 | for a host. The host argument is a string giving a host name or IP number."; |
| 2133 | |
| 2134 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2135 | /* Python interface to gethostbyaddr(IP). */ |
| 2136 | |
| 2137 | /*ARGSUSED*/ |
| 2138 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 2139 | PySocket_gethostbyaddr(PyObject *self, PyObject *args) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2140 | { |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 2141 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2142 | struct sockaddr_storage addr; |
| 2143 | #else |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2144 | struct sockaddr_in addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2145 | #endif |
| 2146 | struct sockaddr *sa = (struct sockaddr *)&addr; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2147 | char *ip_num; |
| 2148 | struct hostent *h; |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 2149 | PyObject *ret; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2150 | #ifdef HAVE_GETHOSTBYNAME_R |
| 2151 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2152 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
| 2153 | struct hostent_data data; |
| 2154 | #else |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2155 | char buf[16384]; |
| 2156 | int buf_len = (sizeof buf) - 1; |
| 2157 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2158 | #endif |
| 2159 | #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] | 2160 | int result; |
| 2161 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2162 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2163 | char *ap; |
| 2164 | int al; |
| 2165 | int af; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2166 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2167 | if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num)) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2168 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2169 | af = PF_UNSPEC; |
| 2170 | if (setipaddr(ip_num, sa, af) < 0) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2171 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2172 | af = sa->sa_family; |
| 2173 | ap = NULL; |
| 2174 | al = 0; |
| 2175 | switch (af) { |
| 2176 | case AF_INET: |
| 2177 | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr; |
| 2178 | al = sizeof(((struct sockaddr_in *)sa)->sin_addr); |
| 2179 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 2180 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2181 | case AF_INET6: |
| 2182 | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr; |
| 2183 | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); |
| 2184 | break; |
| 2185 | #endif |
| 2186 | default: |
| 2187 | PyErr_SetString(PySocket_Error, "unsupported address family"); |
| 2188 | return NULL; |
| 2189 | } |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2190 | Py_BEGIN_ALLOW_THREADS |
| 2191 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2192 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2193 | result = gethostbyaddr_r(ap, al, af, |
| 2194 | &hp_allocated, buf, buf_len, |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 2195 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2196 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2197 | h = gethostbyaddr_r(ap, al, af, |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2198 | &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2199 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 2200 | memset((void *) &data, '\0', sizeof(data)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2201 | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2202 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 2203 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2204 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 2205 | #ifdef USE_GETHOSTBYNAME_LOCK |
| 2206 | PyThread_acquire_lock(gethostbyname_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2207 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2208 | h = gethostbyaddr(ap, al, af); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2209 | #endif /* HAVE_GETHOSTBYNAME_R */ |
| 2210 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2211 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 2212 | #ifdef USE_GETHOSTBYNAME_LOCK |
| 2213 | PyThread_release_lock(gethostbyname_lock); |
| 2214 | #endif |
| 2215 | return ret; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2218 | static char gethostbyaddr_doc[] = |
| 2219 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
| 2220 | \n\ |
| 2221 | Return the true host name, a list of aliases, and a list of IP addresses,\n\ |
| 2222 | for a host. The host argument is a string giving a host name or IP number."; |
| 2223 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2224 | |
| 2225 | /* Python interface to getservbyname(name). |
| 2226 | This only returns the port number, since the other info is already |
| 2227 | known or not useful (like the list of aliases). */ |
| 2228 | |
| 2229 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2230 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 2231 | PySocket_getservbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2232 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2233 | char *name, *proto; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2234 | struct servent *sp; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2235 | if (!PyArg_ParseTuple(args, "ss:getservbyname", &name, &proto)) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2236 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2237 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2238 | sp = getservbyname(name, proto); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2239 | Py_END_ALLOW_THREADS |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2240 | if (sp == NULL) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2241 | PyErr_SetString(PySocket_Error, "service/proto not found"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2242 | return NULL; |
| 2243 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2244 | return PyInt_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2245 | } |
| 2246 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2247 | static char getservbyname_doc[] = |
| 2248 | "getservbyname(servicename, protocolname) -> integer\n\ |
| 2249 | \n\ |
| 2250 | Return a port number from a service name and protocol name.\n\ |
| 2251 | The protocol name should be 'tcp' or 'udp'."; |
| 2252 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2253 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 2254 | /* Python interface to getprotobyname(name). |
| 2255 | This only returns the protocol number, since the other info is |
| 2256 | already known or not useful (like the list of aliases). */ |
| 2257 | |
| 2258 | /*ARGSUSED*/ |
| 2259 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 2260 | PySocket_getprotobyname(PyObject *self, PyObject *args) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 2261 | { |
| 2262 | char *name; |
| 2263 | struct protoent *sp; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 2264 | #ifdef __BEOS__ |
| 2265 | /* Not available in BeOS yet. - [cjh] */ |
| 2266 | PyErr_SetString( PySocket_Error, "getprotobyname not supported" ); |
| 2267 | return NULL; |
| 2268 | #else |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2269 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 2270 | return NULL; |
| 2271 | Py_BEGIN_ALLOW_THREADS |
| 2272 | sp = getprotobyname(name); |
| 2273 | Py_END_ALLOW_THREADS |
| 2274 | if (sp == NULL) { |
| 2275 | PyErr_SetString(PySocket_Error, "protocol not found"); |
| 2276 | return NULL; |
| 2277 | } |
| 2278 | return PyInt_FromLong((long) sp->p_proto); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 2279 | #endif |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2282 | static char getprotobyname_doc[] = |
| 2283 | "getprotobyname(name) -> integer\n\ |
| 2284 | \n\ |
| 2285 | Return the protocol number for the named protocol. (Rarely used.)"; |
| 2286 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 2287 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2288 | #ifndef NO_DUP |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 2289 | /* Create a socket object from a numeric file description. |
| 2290 | Useful e.g. if stdin is a socket. |
| 2291 | Additional arguments as for socket(). */ |
| 2292 | |
| 2293 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2294 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 2295 | PySocket_fromfd(PyObject *self, PyObject *args) |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 2296 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2297 | PySocketSockObject *s; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2298 | SOCKET_T fd; |
| 2299 | int family, type, proto = 0; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2300 | if (!PyArg_ParseTuple(args, "iii|i:fromfd", |
| 2301 | &fd, &family, &type, &proto)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2302 | return NULL; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 2303 | /* Dup the fd so it and the socket can be closed independently */ |
| 2304 | fd = dup(fd); |
| 2305 | if (fd < 0) |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2306 | return PySocket_Err(); |
| 2307 | s = PySocketSock_New(fd, family, type, proto); |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 2308 | /* From now on, ignore SIGPIPE and let the error checking |
| 2309 | do the work. */ |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2310 | #ifdef SIGPIPE |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 2311 | (void) signal(SIGPIPE, SIG_IGN); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2312 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2313 | return (PyObject *) s; |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 2314 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2315 | |
| 2316 | static char fromfd_doc[] = |
| 2317 | "fromfd(fd, family, type[, proto]) -> socket object\n\ |
| 2318 | \n\ |
| 2319 | Create a socket object from the given file descriptor.\n\ |
| 2320 | The remaining arguments are the same as for socket()."; |
| 2321 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2322 | #endif /* NO_DUP */ |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 2323 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2324 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2325 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 2326 | PySocket_ntohs(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2327 | { |
| 2328 | int x1, x2; |
| 2329 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2330 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2331 | return NULL; |
| 2332 | } |
| 2333 | x2 = (int)ntohs((short)x1); |
| 2334 | return PyInt_FromLong(x2); |
| 2335 | } |
| 2336 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2337 | static char ntohs_doc[] = |
| 2338 | "ntohs(integer) -> integer\n\ |
| 2339 | \n\ |
| 2340 | Convert a 16-bit integer from network to host byte order."; |
| 2341 | |
| 2342 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2343 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 2344 | PySocket_ntohl(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2345 | { |
| 2346 | int x1, x2; |
| 2347 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2348 | if (!PyArg_ParseTuple(args, "i:ntohl", &x1)) { |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2349 | return NULL; |
| 2350 | } |
| 2351 | x2 = ntohl(x1); |
| 2352 | return PyInt_FromLong(x2); |
| 2353 | } |
| 2354 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2355 | static char ntohl_doc[] = |
| 2356 | "ntohl(integer) -> integer\n\ |
| 2357 | \n\ |
| 2358 | Convert a 32-bit integer from network to host byte order."; |
| 2359 | |
| 2360 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2361 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 2362 | PySocket_htons(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2363 | { |
| 2364 | int x1, x2; |
| 2365 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2366 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) { |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2367 | return NULL; |
| 2368 | } |
| 2369 | x2 = (int)htons((short)x1); |
| 2370 | return PyInt_FromLong(x2); |
| 2371 | } |
| 2372 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2373 | static char htons_doc[] = |
| 2374 | "htons(integer) -> integer\n\ |
| 2375 | \n\ |
| 2376 | Convert a 16-bit integer from host to network byte order."; |
| 2377 | |
| 2378 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2379 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 2380 | PySocket_htonl(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2381 | { |
| 2382 | int x1, x2; |
| 2383 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2384 | if (!PyArg_ParseTuple(args, "i:htonl", &x1)) { |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2385 | return NULL; |
| 2386 | } |
| 2387 | x2 = htonl(x1); |
| 2388 | return PyInt_FromLong(x2); |
| 2389 | } |
| 2390 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2391 | static char htonl_doc[] = |
| 2392 | "htonl(integer) -> integer\n\ |
| 2393 | \n\ |
| 2394 | Convert a 32-bit integer from host to network byte order."; |
| 2395 | |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 2396 | /* |
| 2397 | * socket.inet_aton() and socket.inet_ntoa() functions |
| 2398 | * |
| 2399 | * written 20 Aug 1999 by Ben Gertzfield <che@debian.org> <- blame him! |
| 2400 | * |
| 2401 | */ |
| 2402 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2403 | static char inet_aton_doc[] = |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 2404 | "inet_aton(string) -> packed 32-bit IP representation\n\ |
| 2405 | \n\ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 2406 | 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] | 2407 | binary format used in low-level network functions."; |
| 2408 | |
| 2409 | static PyObject* |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 2410 | PySocket_inet_aton(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 2411 | { |
Guido van Rossum | a2e4855 | 1999-09-09 15:42:59 +0000 | [diff] [blame] | 2412 | #ifndef INADDR_NONE |
| 2413 | #define INADDR_NONE (-1) |
| 2414 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 2415 | |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 2416 | /* Have to use inet_addr() instead */ |
| 2417 | char *ip_addr; |
Fred Drake | e506529 | 2001-07-19 21:16:41 +0000 | [diff] [blame] | 2418 | unsigned long packed_addr; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 2419 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2420 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) { |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 2421 | return NULL; |
| 2422 | } |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2423 | #ifdef USE_GUSI1 |
Fred Drake | e506529 | 2001-07-19 21:16:41 +0000 | [diff] [blame] | 2424 | packed_addr = inet_addr(ip_addr).s_addr; |
Guido van Rossum | 1b6e463 | 1999-11-04 18:22:29 +0000 | [diff] [blame] | 2425 | #else |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 2426 | packed_addr = inet_addr(ip_addr); |
Guido van Rossum | 1b6e463 | 1999-11-04 18:22:29 +0000 | [diff] [blame] | 2427 | #endif |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 2428 | |
| 2429 | if (packed_addr == INADDR_NONE) { /* invalid address */ |
| 2430 | PyErr_SetString(PySocket_Error, |
| 2431 | "illegal IP address string passed to inet_aton"); |
| 2432 | return NULL; |
| 2433 | } |
| 2434 | |
| 2435 | return PyString_FromStringAndSize((char *) &packed_addr, |
| 2436 | sizeof(packed_addr)); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 2437 | } |
| 2438 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2439 | static char inet_ntoa_doc[] = |
Fred Drake | e066134 | 2000-03-07 14:05:16 +0000 | [diff] [blame] | 2440 | "inet_ntoa(packed_ip) -> ip_address_string\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 2441 | \n\ |
| 2442 | Convert an IP address from 32-bit packed binary format to string format"; |
| 2443 | |
| 2444 | static PyObject* |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 2445 | PySocket_inet_ntoa(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 2446 | { |
| 2447 | char *packed_str; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 2448 | int addr_len; |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 2449 | struct in_addr packed_addr; |
| 2450 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2451 | if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) { |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 2452 | return NULL; |
| 2453 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2454 | |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 2455 | if (addr_len != sizeof(packed_addr)) { |
| 2456 | PyErr_SetString(PySocket_Error, |
| 2457 | "packed IP wrong length for inet_ntoa"); |
| 2458 | return NULL; |
| 2459 | } |
| 2460 | |
| 2461 | memcpy(&packed_addr, packed_str, addr_len); |
| 2462 | |
| 2463 | return PyString_FromString(inet_ntoa(packed_addr)); |
| 2464 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2465 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2466 | /* Python interface to getaddrinfo(host, port). */ |
| 2467 | |
| 2468 | /*ARGSUSED*/ |
| 2469 | static PyObject * |
| 2470 | PySocket_getaddrinfo(PyObject *self, PyObject *args) |
| 2471 | { |
Martin v. Löwis | f0b11d2 | 2001-11-07 08:31:03 +0000 | [diff] [blame] | 2472 | struct addrinfo hints, *res; |
| 2473 | struct addrinfo *res0 = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2474 | PyObject *pobj = (PyObject *)NULL; |
Martin v. Löwis | f65b1a1 | 2001-08-12 09:28:40 +0000 | [diff] [blame] | 2475 | char pbuf[30]; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2476 | char *hptr, *pptr; |
| 2477 | int family, socktype, protocol, flags; |
| 2478 | int error; |
| 2479 | PyObject *all = (PyObject *)NULL; |
| 2480 | PyObject *single = (PyObject *)NULL; |
| 2481 | |
| 2482 | family = socktype = protocol = flags = 0; |
| 2483 | family = PF_UNSPEC; |
| 2484 | if (!PyArg_ParseTuple(args, "zO|iiii:getaddrinfo", |
| 2485 | &hptr, &pobj, &family, &socktype, |
| 2486 | &protocol, &flags)) { |
| 2487 | return NULL; |
| 2488 | } |
| 2489 | if (PyInt_Check(pobj)) { |
Marc-André Lemburg | 03d1b18 | 2001-07-31 18:05:33 +0000 | [diff] [blame] | 2490 | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2491 | pptr = pbuf; |
| 2492 | } else if (PyString_Check(pobj)) { |
| 2493 | pptr = PyString_AsString(pobj); |
| 2494 | } else if (pobj == Py_None) { |
| 2495 | pptr = (char *)NULL; |
| 2496 | } else { |
| 2497 | PyErr_SetString(PySocket_Error, "Int or String expected"); |
| 2498 | return NULL; |
| 2499 | } |
| 2500 | memset(&hints, 0, sizeof(hints)); |
| 2501 | hints.ai_family = family; |
| 2502 | hints.ai_socktype = socktype; |
| 2503 | hints.ai_protocol = protocol; |
| 2504 | hints.ai_flags = flags; |
| 2505 | error = getaddrinfo(hptr, pptr, &hints, &res0); |
| 2506 | if (error) { |
| 2507 | PyGAI_Err(error); |
| 2508 | return NULL; |
| 2509 | } |
| 2510 | |
| 2511 | if ((all = PyList_New(0)) == NULL) |
| 2512 | goto err; |
| 2513 | for (res = res0; res; res = res->ai_next) { |
Guido van Rossum | 716aac0 | 2001-10-12 18:59:27 +0000 | [diff] [blame] | 2514 | PyObject *addr = |
| 2515 | makesockaddr(-1, res->ai_addr, res->ai_addrlen); |
| 2516 | if (addr == NULL) |
| 2517 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2518 | single = Py_BuildValue("iiisO", res->ai_family, |
| 2519 | res->ai_socktype, res->ai_protocol, |
| 2520 | res->ai_canonname ? res->ai_canonname : "", |
Guido van Rossum | 716aac0 | 2001-10-12 18:59:27 +0000 | [diff] [blame] | 2521 | addr); |
| 2522 | Py_DECREF(addr); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2523 | if (single == NULL) |
| 2524 | goto err; |
| 2525 | |
| 2526 | if (PyList_Append(all, single)) |
| 2527 | goto err; |
| 2528 | Py_XDECREF(single); |
| 2529 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2530 | return all; |
| 2531 | err: |
| 2532 | Py_XDECREF(single); |
| 2533 | Py_XDECREF(all); |
Martin v. Löwis | f0b11d2 | 2001-11-07 08:31:03 +0000 | [diff] [blame] | 2534 | if (res0) |
| 2535 | freeaddrinfo(res0); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2536 | return (PyObject *)NULL; |
| 2537 | } |
| 2538 | |
| 2539 | static char getaddrinfo_doc[] = |
| 2540 | "socket.getaddrinfo(host, port [, family, socktype, proto, flags])\n\ |
| 2541 | --> List of (family, socktype, proto, canonname, sockaddr)\n\ |
| 2542 | \n\ |
| 2543 | Resolve host and port into addrinfo struct."; |
| 2544 | |
| 2545 | /* Python interface to getnameinfo(sa, flags). */ |
| 2546 | |
| 2547 | /*ARGSUSED*/ |
| 2548 | static PyObject * |
| 2549 | PySocket_getnameinfo(PyObject *self, PyObject *args) |
| 2550 | { |
| 2551 | PyObject *sa = (PyObject *)NULL; |
| 2552 | int flags; |
| 2553 | char *hostp; |
Martin v. Löwis | 06b1d21 | 2001-11-02 23:34:52 +0000 | [diff] [blame] | 2554 | int port, flowinfo, scope_id; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2555 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; |
| 2556 | struct addrinfo hints, *res = NULL; |
| 2557 | int error; |
| 2558 | PyObject *ret = (PyObject *)NULL; |
| 2559 | |
| 2560 | flags = flowinfo = scope_id = 0; |
Martin v. Löwis | 06b1d21 | 2001-11-02 23:34:52 +0000 | [diff] [blame] | 2561 | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2562 | return NULL; |
Martin v. Löwis | 06b1d21 | 2001-11-02 23:34:52 +0000 | [diff] [blame] | 2563 | if (!PyArg_ParseTuple(sa, "si|ii", &hostp, &port, &flowinfo, &scope_id)) |
| 2564 | return NULL; |
Marc-André Lemburg | 03d1b18 | 2001-07-31 18:05:33 +0000 | [diff] [blame] | 2565 | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2566 | memset(&hints, 0, sizeof(hints)); |
| 2567 | hints.ai_family = PF_UNSPEC; |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 2568 | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2569 | error = getaddrinfo(hostp, pbuf, &hints, &res); |
| 2570 | if (error) { |
| 2571 | PyGAI_Err(error); |
| 2572 | goto fail; |
| 2573 | } |
| 2574 | if (res->ai_next) { |
| 2575 | PyErr_SetString(PySocket_Error, |
| 2576 | "sockaddr resolved to multiple addresses"); |
| 2577 | goto fail; |
| 2578 | } |
| 2579 | switch (res->ai_family) { |
| 2580 | case AF_INET: |
| 2581 | { |
| 2582 | char *t1; |
| 2583 | int t2; |
| 2584 | if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) { |
| 2585 | PyErr_SetString(PySocket_Error, |
| 2586 | "IPv4 sockaddr must be 2 tuple"); |
| 2587 | goto fail; |
| 2588 | } |
| 2589 | break; |
| 2590 | } |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 2591 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2592 | case AF_INET6: |
| 2593 | { |
| 2594 | struct sockaddr_in6 *sin6; |
| 2595 | sin6 = (struct sockaddr_in6 *)res->ai_addr; |
| 2596 | sin6->sin6_flowinfo = flowinfo; |
| 2597 | sin6->sin6_scope_id = scope_id; |
| 2598 | break; |
| 2599 | } |
| 2600 | #endif |
| 2601 | } |
| 2602 | error = getnameinfo(res->ai_addr, res->ai_addrlen, |
| 2603 | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); |
| 2604 | if (error) { |
| 2605 | PyGAI_Err(error); |
| 2606 | goto fail; |
| 2607 | } |
| 2608 | ret = Py_BuildValue("ss", hbuf, pbuf); |
| 2609 | |
| 2610 | fail: |
| 2611 | if (res) |
| 2612 | freeaddrinfo(res); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2613 | return ret; |
| 2614 | } |
| 2615 | |
| 2616 | static char getnameinfo_doc[] = |
| 2617 | "socket.getnameinfo(sockaddr, flags) --> (host, port)\n\ |
| 2618 | \n\ |
| 2619 | Get host and port for a sockaddr."; |
| 2620 | |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 2621 | /* XXX It might be helpful to augment the error message generated |
| 2622 | below with the name of the SSL function that generated the error. |
| 2623 | I expect it's obvious most of the time. |
| 2624 | */ |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2625 | |
| 2626 | #ifdef USE_SSL |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 2627 | static PyObject * |
| 2628 | PySSL_SetError(SSL *ssl, int ret) |
| 2629 | { |
| 2630 | PyObject *v, *n, *s; |
| 2631 | char *errstr; |
| 2632 | int err; |
| 2633 | |
| 2634 | assert(ret <= 0); |
| 2635 | |
| 2636 | err = SSL_get_error(ssl, ret); |
| 2637 | n = PyInt_FromLong(err); |
| 2638 | if (n == NULL) |
| 2639 | return NULL; |
| 2640 | v = PyTuple_New(2); |
| 2641 | if (v == NULL) { |
| 2642 | Py_DECREF(n); |
| 2643 | return NULL; |
| 2644 | } |
| 2645 | |
| 2646 | switch (SSL_get_error(ssl, ret)) { |
| 2647 | case SSL_ERROR_ZERO_RETURN: |
| 2648 | errstr = "TLS/SSL connection has been closed"; |
| 2649 | break; |
| 2650 | case SSL_ERROR_WANT_READ: |
| 2651 | errstr = "The operation did not complete (read)"; |
| 2652 | break; |
| 2653 | case SSL_ERROR_WANT_WRITE: |
| 2654 | errstr = "The operation did not complete (write)"; |
| 2655 | break; |
| 2656 | case SSL_ERROR_WANT_X509_LOOKUP: |
| 2657 | errstr = "The operation did not complete (X509 lookup)"; |
| 2658 | break; |
| 2659 | case SSL_ERROR_SYSCALL: |
| 2660 | case SSL_ERROR_SSL: |
| 2661 | { |
| 2662 | unsigned long e = ERR_get_error(); |
| 2663 | if (e == 0) { |
| 2664 | /* an EOF was observed that violates the protocol */ |
| 2665 | errstr = "EOF occurred in violation of protocol"; |
| 2666 | } else if (e == -1) { |
| 2667 | /* the underlying BIO reported an I/O error */ |
| 2668 | Py_DECREF(v); |
| 2669 | Py_DECREF(n); |
Jeremy Hylton | e2adc6c | 2001-10-11 17:27:58 +0000 | [diff] [blame] | 2670 | return PySocket_Err(); |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 2671 | } else { |
| 2672 | /* XXX Protected by global interpreter lock */ |
| 2673 | errstr = ERR_error_string(e, NULL); |
| 2674 | } |
| 2675 | break; |
| 2676 | } |
| 2677 | default: |
| 2678 | errstr = "Invalid error code"; |
| 2679 | } |
| 2680 | s = PyString_FromString(errstr); |
| 2681 | if (s == NULL) { |
| 2682 | Py_DECREF(v); |
| 2683 | Py_DECREF(n); |
| 2684 | } |
| 2685 | PyTuple_SET_ITEM(v, 0, n); |
| 2686 | PyTuple_SET_ITEM(v, 1, s); |
| 2687 | PyErr_SetObject(PySSLErrorObject, v); |
| 2688 | return NULL; |
| 2689 | } |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2690 | |
| 2691 | /* This is a C function to be called for new object initialization */ |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 2692 | static PySSLObject * |
| 2693 | newPySSLObject(PySocketSockObject *Sock, char *key_file, char *cert_file) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2694 | { |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 2695 | PySSLObject *self; |
Jeremy Hylton | b0b0bd6 | 2001-10-10 22:33:32 +0000 | [diff] [blame] | 2696 | char *errstr = NULL; |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 2697 | int ret; |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2698 | |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 2699 | self = PyObject_New(PySSLObject, &PySSL_Type); /* Create new object */ |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2700 | if (self == NULL){ |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 2701 | errstr = "newPySSLObject error"; |
Jeremy Hylton | f86d63e | 2001-10-10 03:19:39 +0000 | [diff] [blame] | 2702 | goto fail; |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2703 | } |
Jeremy Hylton | ba69936 | 2001-10-11 17:23:34 +0000 | [diff] [blame] | 2704 | memset(self->server, '\0', sizeof(char) * X509_NAME_MAXLEN); |
| 2705 | memset(self->issuer, '\0', sizeof(char) * X509_NAME_MAXLEN); |
Jeremy Hylton | ec4b545 | 2001-10-10 03:37:05 +0000 | [diff] [blame] | 2706 | self->server_cert = NULL; |
| 2707 | self->ssl = NULL; |
| 2708 | self->ctx = NULL; |
| 2709 | self->Socket = NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2710 | |
Jeremy Hylton | 22738b9 | 2001-10-10 22:37:48 +0000 | [diff] [blame] | 2711 | if ((key_file && !cert_file) || (!key_file && cert_file)) { |
| 2712 | errstr = "Both the key & certificate files must be specified"; |
Jeremy Hylton | f86d63e | 2001-10-10 03:19:39 +0000 | [diff] [blame] | 2713 | goto fail; |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2714 | } |
| 2715 | |
Jeremy Hylton | 22738b9 | 2001-10-10 22:37:48 +0000 | [diff] [blame] | 2716 | self->ctx = SSL_CTX_new(SSLv23_method()); /* Set up context */ |
| 2717 | if (self->ctx == NULL) { |
| 2718 | errstr = "SSL_CTX_new error"; |
Jeremy Hylton | f86d63e | 2001-10-10 03:19:39 +0000 | [diff] [blame] | 2719 | goto fail; |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2720 | } |
| 2721 | |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 2722 | if (key_file) { |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2723 | if (SSL_CTX_use_PrivateKey_file(self->ctx, key_file, |
Jeremy Hylton | f86d63e | 2001-10-10 03:19:39 +0000 | [diff] [blame] | 2724 | SSL_FILETYPE_PEM) < 1) { |
Jeremy Hylton | b0b0bd6 | 2001-10-10 22:33:32 +0000 | [diff] [blame] | 2725 | errstr = "SSL_CTX_use_PrivateKey_file error"; |
Jeremy Hylton | f86d63e | 2001-10-10 03:19:39 +0000 | [diff] [blame] | 2726 | goto fail; |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2727 | } |
| 2728 | |
| 2729 | if (SSL_CTX_use_certificate_chain_file(self->ctx, |
Jeremy Hylton | f86d63e | 2001-10-10 03:19:39 +0000 | [diff] [blame] | 2730 | cert_file) < 1) { |
Jeremy Hylton | b0b0bd6 | 2001-10-10 22:33:32 +0000 | [diff] [blame] | 2731 | errstr = "SSL_CTX_use_certificate_chain_file error"; |
Jeremy Hylton | f86d63e | 2001-10-10 03:19:39 +0000 | [diff] [blame] | 2732 | goto fail; |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2733 | } |
| 2734 | } |
| 2735 | |
| 2736 | SSL_CTX_set_verify(self->ctx, |
| 2737 | SSL_VERIFY_NONE, NULL); /* set verify lvl */ |
| 2738 | self->ssl = SSL_new(self->ctx); /* New ssl struct */ |
| 2739 | SSL_set_fd(self->ssl, Sock->sock_fd); /* Set the socket for SSL */ |
| 2740 | SSL_set_connect_state(self->ssl); |
| 2741 | |
Jeremy Hylton | f86d63e | 2001-10-10 03:19:39 +0000 | [diff] [blame] | 2742 | /* Actually negotiate SSL connection */ |
| 2743 | /* XXX If SSL_connect() returns 0, it's also a failure. */ |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 2744 | ret = SSL_connect(self->ssl); |
| 2745 | if (ret <= 0) { |
| 2746 | PySSL_SetError(self->ssl, ret); |
Jeremy Hylton | f86d63e | 2001-10-10 03:19:39 +0000 | [diff] [blame] | 2747 | goto fail; |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2748 | } |
| 2749 | self->ssl->debug = 1; |
| 2750 | |
| 2751 | if ((self->server_cert = SSL_get_peer_certificate(self->ssl))) { |
| 2752 | X509_NAME_oneline(X509_get_subject_name(self->server_cert), |
Jeremy Hylton | ba69936 | 2001-10-11 17:23:34 +0000 | [diff] [blame] | 2753 | self->server, X509_NAME_MAXLEN); |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2754 | X509_NAME_oneline(X509_get_issuer_name(self->server_cert), |
Jeremy Hylton | ba69936 | 2001-10-11 17:23:34 +0000 | [diff] [blame] | 2755 | self->issuer, X509_NAME_MAXLEN); |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2756 | } |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2757 | self->Socket = Sock; |
| 2758 | Py_INCREF(self->Socket); |
| 2759 | return self; |
Jeremy Hylton | f86d63e | 2001-10-10 03:19:39 +0000 | [diff] [blame] | 2760 | fail: |
Jeremy Hylton | b0b0bd6 | 2001-10-10 22:33:32 +0000 | [diff] [blame] | 2761 | if (errstr) |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 2762 | PyErr_SetString(PySSLErrorObject, errstr); |
Jeremy Hylton | f86d63e | 2001-10-10 03:19:39 +0000 | [diff] [blame] | 2763 | Py_DECREF(self); |
| 2764 | return NULL; |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
| 2767 | /* This is the Python function called for new object initialization */ |
| 2768 | static PyObject * |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 2769 | PySocket_ssl(PyObject *self, PyObject *args) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2770 | { |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 2771 | PySSLObject *rv; |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2772 | PySocketSockObject *Sock; |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 2773 | char *key_file = NULL; |
| 2774 | char *cert_file = NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2775 | |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 2776 | if (!PyArg_ParseTuple(args, "O!|zz:ssl", |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2777 | &PySocketSock_Type, (PyObject*)&Sock, |
Jeremy Hylton | ab00645 | 2001-10-10 03:33:24 +0000 | [diff] [blame] | 2778 | &key_file, &cert_file)) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2779 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2780 | |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 2781 | rv = newPySSLObject(Sock, key_file, cert_file); |
Jeremy Hylton | ab00645 | 2001-10-10 03:33:24 +0000 | [diff] [blame] | 2782 | if (rv == NULL) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2783 | return NULL; |
| 2784 | return (PyObject *)rv; |
| 2785 | } |
| 2786 | |
| 2787 | static char ssl_doc[] = |
Jeremy Hylton | ba69936 | 2001-10-11 17:23:34 +0000 | [diff] [blame] | 2788 | "ssl(socket, [keyfile, certfile]) -> sslobject"; |
| 2789 | |
| 2790 | /* SSL object methods */ |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2791 | |
| 2792 | static PyObject * |
Jeremy Hylton | ba69936 | 2001-10-11 17:23:34 +0000 | [diff] [blame] | 2793 | PySSL_server(PySSLObject *self) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2794 | { |
| 2795 | return PyString_FromString(self->server); |
| 2796 | } |
| 2797 | |
| 2798 | static PyObject * |
Jeremy Hylton | ba69936 | 2001-10-11 17:23:34 +0000 | [diff] [blame] | 2799 | PySSL_issuer(PySSLObject *self) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2800 | { |
| 2801 | return PyString_FromString(self->issuer); |
| 2802 | } |
| 2803 | |
| 2804 | |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 2805 | static void PySSL_dealloc(PySSLObject *self) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2806 | { |
| 2807 | if (self->server_cert) /* Possible not to have one? */ |
| 2808 | X509_free (self->server_cert); |
Jeremy Hylton | ec4b545 | 2001-10-10 03:37:05 +0000 | [diff] [blame] | 2809 | if (self->ssl) |
| 2810 | SSL_free(self->ssl); |
| 2811 | if (self->ctx) |
| 2812 | SSL_CTX_free(self->ctx); |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2813 | Py_XDECREF(self->Socket); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 2814 | PyObject_Del(self); |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2815 | } |
| 2816 | |
Jeremy Hylton | ba69936 | 2001-10-11 17:23:34 +0000 | [diff] [blame] | 2817 | static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args) |
| 2818 | { |
| 2819 | char *data; |
| 2820 | int len; |
| 2821 | |
| 2822 | if (!PyArg_ParseTuple(args, "s#:write", &data, &len)) |
| 2823 | return NULL; |
| 2824 | |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2825 | Py_BEGIN_ALLOW_THREADS |
Jeremy Hylton | ba69936 | 2001-10-11 17:23:34 +0000 | [diff] [blame] | 2826 | len = SSL_write(self->ssl, data, len); |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2827 | Py_END_ALLOW_THREADS |
Jeremy Hylton | ba69936 | 2001-10-11 17:23:34 +0000 | [diff] [blame] | 2828 | if (len > 0) |
| 2829 | return PyInt_FromLong(len); |
| 2830 | else |
| 2831 | return PySSL_SetError(self->ssl, len); |
| 2832 | } |
| 2833 | |
| 2834 | static char PySSL_SSLwrite_doc[] = |
| 2835 | "write(s) -> len\n\ |
| 2836 | \n\ |
| 2837 | Writes the string s into the SSL object. Returns the number\n\ |
| 2838 | of bytes written."; |
| 2839 | |
| 2840 | static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args) |
| 2841 | { |
| 2842 | PyObject *buf; |
| 2843 | int count = 0; |
| 2844 | int len = 1024; |
| 2845 | |
| 2846 | if (!PyArg_ParseTuple(args, "|i:read", &len)) |
| 2847 | return NULL; |
| 2848 | |
| 2849 | if (!(buf = PyString_FromStringAndSize((char *) 0, len))) |
| 2850 | return NULL; |
| 2851 | |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2852 | Py_BEGIN_ALLOW_THREADS |
Jeremy Hylton | ba69936 | 2001-10-11 17:23:34 +0000 | [diff] [blame] | 2853 | count = SSL_read(self->ssl, PyString_AsString(buf), len); |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2854 | Py_END_ALLOW_THREADS |
Jeremy Hylton | ba69936 | 2001-10-11 17:23:34 +0000 | [diff] [blame] | 2855 | if (count <= 0) { |
| 2856 | Py_DECREF(buf); |
| 2857 | return PySSL_SetError(self->ssl, count); |
| 2858 | } |
| 2859 | if (count != len && _PyString_Resize(&buf, count) < 0) |
| 2860 | return NULL; |
| 2861 | return buf; |
| 2862 | } |
| 2863 | |
| 2864 | static char PySSL_SSLread_doc[] = |
| 2865 | "read([len]) -> string\n\ |
| 2866 | \n\ |
| 2867 | Read up to len bytes from the SSL socket."; |
| 2868 | |
| 2869 | static PyMethodDef PySSLMethods[] = { |
| 2870 | {"write", (PyCFunction)PySSL_SSLwrite, 1, |
| 2871 | PySSL_SSLwrite_doc}, |
| 2872 | {"read", (PyCFunction)PySSL_SSLread, 1, |
| 2873 | PySSL_SSLread_doc}, |
Guido van Rossum | 17209fc | 2001-10-15 21:12:54 +0000 | [diff] [blame] | 2874 | {"server", (PyCFunction)PySSL_server, METH_NOARGS}, |
| 2875 | {"issuer", (PyCFunction)PySSL_issuer, METH_NOARGS}, |
Jeremy Hylton | ba69936 | 2001-10-11 17:23:34 +0000 | [diff] [blame] | 2876 | {NULL, NULL} |
| 2877 | }; |
| 2878 | |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 2879 | static PyObject *PySSL_getattr(PySSLObject *self, char *name) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2880 | { |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 2881 | return Py_FindMethod(PySSLMethods, (PyObject *)self, name); |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2882 | } |
| 2883 | |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 2884 | staticforward PyTypeObject PySSL_Type = { |
Guido van Rossum | a120ffc | 2001-01-22 15:29:14 +0000 | [diff] [blame] | 2885 | PyObject_HEAD_INIT(NULL) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2886 | 0, /*ob_size*/ |
Guido van Rossum | ca6dfa5 | 2001-10-28 12:31:33 +0000 | [diff] [blame] | 2887 | "_socket.SSL", /*tp_name*/ |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 2888 | sizeof(PySSLObject), /*tp_basicsize*/ |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2889 | 0, /*tp_itemsize*/ |
| 2890 | /* methods */ |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 2891 | (destructor)PySSL_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2892 | 0, /*tp_print*/ |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 2893 | (getattrfunc)PySSL_getattr, /*tp_getattr*/ |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2894 | 0, /*tp_setattr*/ |
| 2895 | 0, /*tp_compare*/ |
| 2896 | 0, /*tp_repr*/ |
| 2897 | 0, /*tp_as_number*/ |
| 2898 | 0, /*tp_as_sequence*/ |
| 2899 | 0, /*tp_as_mapping*/ |
| 2900 | 0, /*tp_hash*/ |
| 2901 | }; |
| 2902 | |
Jeremy Hylton | de80f2e | 2001-10-18 00:28:50 +0000 | [diff] [blame] | 2903 | /* helper routines for seeding the SSL PRNG */ |
| 2904 | static PyObject * |
| 2905 | PySSL_RAND_add(PyObject *self, PyObject *args) |
| 2906 | { |
| 2907 | char *buf; |
| 2908 | int len; |
| 2909 | double entropy; |
| 2910 | |
| 2911 | if (!PyArg_ParseTuple(args, "s#d:RAND_add", &buf, &len, &entropy)) |
| 2912 | return NULL; |
| 2913 | RAND_add(buf, len, entropy); |
| 2914 | Py_INCREF(Py_None); |
| 2915 | return Py_None; |
| 2916 | } |
| 2917 | |
| 2918 | static char PySSL_RAND_add_doc[] = |
| 2919 | "RAND_add(string, entropy)\n\ |
| 2920 | \n\ |
| 2921 | Mix string into the OpenSSL PRNG state. entropy (a float) is a lower\n\ |
| 2922 | bound on the entropy contained in string."; |
| 2923 | |
| 2924 | static PyObject * |
| 2925 | PySSL_RAND_status(PyObject *self) |
| 2926 | { |
| 2927 | return PyInt_FromLong(RAND_status()); |
| 2928 | } |
| 2929 | |
| 2930 | static char PySSL_RAND_status_doc[] = |
| 2931 | "RAND_status() -> 0 or 1\n\ |
| 2932 | \n\ |
| 2933 | Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not.\n\ |
| 2934 | It is necessary to seed the PRNG with RAND_add() on some platforms before\n\ |
| 2935 | using the ssl() function."; |
| 2936 | |
| 2937 | static PyObject * |
| 2938 | PySSL_RAND_egd(PyObject *self, PyObject *arg) |
| 2939 | { |
| 2940 | int bytes; |
| 2941 | |
| 2942 | if (!PyString_Check(arg)) |
| 2943 | return PyErr_Format(PyExc_TypeError, |
| 2944 | "RAND_egd() expected string, found %s", |
| 2945 | arg->ob_type->tp_name); |
| 2946 | bytes = RAND_egd(PyString_AS_STRING(arg)); |
| 2947 | if (bytes == -1) { |
| 2948 | PyErr_SetString(PySSLErrorObject, |
| 2949 | "EGD connection failed or EGD did not return " |
| 2950 | "enough data to seed the PRNG"); |
| 2951 | return NULL; |
| 2952 | } |
| 2953 | return PyInt_FromLong(bytes); |
| 2954 | } |
| 2955 | |
| 2956 | static char PySSL_RAND_egd_doc[] = |
Guido van Rossum | cad8fa1 | 2001-10-19 12:40:40 +0000 | [diff] [blame] | 2957 | "RAND_egd(path) -> bytes\n\ |
Jeremy Hylton | de80f2e | 2001-10-18 00:28:50 +0000 | [diff] [blame] | 2958 | \n\ |
| 2959 | Queries the entropy gather daemon (EGD) on socket path. Returns number\n\ |
| 2960 | of bytes read. Raises socket.sslerror if connection to EGD fails or\n\ |
| 2961 | if it does provide enough data to seed PRNG."; |
| 2962 | |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 2963 | #endif /* USE_SSL */ |
| 2964 | |
| 2965 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2966 | /* List of functions exported by this module. */ |
| 2967 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2968 | static PyMethodDef PySocket_methods[] = { |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2969 | {"gethostbyname", PySocket_gethostbyname, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 2970 | METH_VARARGS, gethostbyname_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2971 | {"gethostbyname_ex", PySocket_gethostbyname_ex, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 2972 | METH_VARARGS, ghbn_ex_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2973 | {"gethostbyaddr", PySocket_gethostbyaddr, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 2974 | METH_VARARGS, gethostbyaddr_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2975 | {"gethostname", PySocket_gethostname, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 2976 | METH_VARARGS, gethostname_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2977 | {"getservbyname", PySocket_getservbyname, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 2978 | METH_VARARGS, getservbyname_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2979 | {"getprotobyname", PySocket_getprotobyname, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 2980 | METH_VARARGS,getprotobyname_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2981 | #ifndef NO_DUP |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2982 | {"fromfd", PySocket_fromfd, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 2983 | METH_VARARGS, fromfd_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2984 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2985 | {"ntohs", PySocket_ntohs, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 2986 | METH_VARARGS, ntohs_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2987 | {"ntohl", PySocket_ntohl, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 2988 | METH_VARARGS, ntohl_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2989 | {"htons", PySocket_htons, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 2990 | METH_VARARGS, htons_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2991 | {"htonl", PySocket_htonl, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 2992 | METH_VARARGS, htonl_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2993 | {"inet_aton", PySocket_inet_aton, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 2994 | METH_VARARGS, inet_aton_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2995 | {"inet_ntoa", PySocket_inet_ntoa, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 2996 | METH_VARARGS, inet_ntoa_doc}, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2997 | {"getaddrinfo", PySocket_getaddrinfo, |
| 2998 | METH_VARARGS, getaddrinfo_doc}, |
| 2999 | {"getnameinfo", PySocket_getnameinfo, |
| 3000 | METH_VARARGS, getnameinfo_doc}, |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 3001 | #ifdef USE_SSL |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3002 | {"ssl", PySocket_ssl, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3003 | METH_VARARGS, ssl_doc}, |
Jeremy Hylton | de80f2e | 2001-10-18 00:28:50 +0000 | [diff] [blame] | 3004 | {"RAND_add", PySSL_RAND_add, METH_VARARGS, |
| 3005 | PySSL_RAND_add_doc}, |
| 3006 | {"RAND_egd", PySSL_RAND_egd, METH_O, |
| 3007 | PySSL_RAND_egd_doc}, |
| 3008 | {"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS, |
| 3009 | PySSL_RAND_status_doc}, |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 3010 | #endif /* USE_SSL */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3011 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3012 | }; |
| 3013 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3014 | |
| 3015 | /* Convenience routine to export an integer value. |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 3016 | * |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 3017 | * Errors are silently ignored, for better or for worse... |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 3018 | */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3019 | static void |
Fredrik Lundh | 3c5d43d | 2000-07-09 15:09:56 +0000 | [diff] [blame] | 3020 | insint(PyObject *d, char *name, int value) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3021 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3022 | PyObject *v = PyInt_FromLong((long) value); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 3023 | if (!v || PyDict_SetItemString(d, name, v)) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 3024 | PyErr_Clear(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 3025 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 3026 | Py_XDECREF(v); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3027 | } |
| 3028 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3029 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3030 | #ifdef MS_WINDOWS |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3031 | |
| 3032 | /* Additional initialization and cleanup for NT/Windows */ |
| 3033 | |
| 3034 | static void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 3035 | NTcleanup(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3036 | { |
| 3037 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3038 | } |
| 3039 | |
| 3040 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 3041 | NTinit(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3042 | { |
| 3043 | WSADATA WSAData; |
| 3044 | int ret; |
| 3045 | char buf[100]; |
| 3046 | ret = WSAStartup(0x0101, &WSAData); |
| 3047 | switch (ret) { |
| 3048 | case 0: /* no error */ |
| 3049 | atexit(NTcleanup); |
| 3050 | return 1; |
| 3051 | case WSASYSNOTREADY: |
| 3052 | PyErr_SetString(PyExc_ImportError, |
| 3053 | "WSAStartup failed: network not ready"); |
| 3054 | break; |
| 3055 | case WSAVERNOTSUPPORTED: |
| 3056 | case WSAEINVAL: |
| 3057 | PyErr_SetString(PyExc_ImportError, |
| 3058 | "WSAStartup failed: requested version not supported"); |
| 3059 | break; |
| 3060 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 3061 | PyOS_snprintf(buf, sizeof(buf), |
| 3062 | "WSAStartup failed: error code %d", ret); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3063 | PyErr_SetString(PyExc_ImportError, buf); |
| 3064 | break; |
| 3065 | } |
| 3066 | return 0; |
| 3067 | } |
| 3068 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3069 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3070 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3071 | #if defined(PYOS_OS2) |
| 3072 | |
| 3073 | /* Additional initialization and cleanup for OS/2 */ |
| 3074 | |
| 3075 | static void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 3076 | OS2cleanup(void) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3077 | { |
| 3078 | /* No cleanup is necessary for OS/2 Sockets */ |
| 3079 | } |
| 3080 | |
| 3081 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 3082 | OS2init(void) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3083 | { |
| 3084 | char reason[64]; |
| 3085 | int rc = sock_init(); |
| 3086 | |
| 3087 | if (rc == 0) { |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 3088 | atexit(OS2cleanup); |
| 3089 | return 1; /* Indicate Success */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3090 | } |
| 3091 | |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 3092 | PyOS_snprintf(reason, sizeof(reason), |
| 3093 | "OS/2 TCP/IP Error# %d", sock_errno()); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3094 | PyErr_SetString(PyExc_ImportError, reason); |
| 3095 | |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 3096 | return 0; /* Indicate Failure */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
| 3099 | #endif /* PYOS_OS2 */ |
| 3100 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3101 | /* Initialize this module. |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3102 | * This is called when the first 'import socket' is done, |
| 3103 | * via a table in config.c, if config.c is compiled with USE_SOCKET |
| 3104 | * defined. |
| 3105 | * |
| 3106 | * For MS_WINDOWS (which means any Windows variant), this module |
| 3107 | * is actually called "_socket", and there's a wrapper "socket.py" |
| 3108 | * which implements some missing functionality (such as makefile(), |
| 3109 | * dup() and fromfd()). The import of "_socket" may fail with an |
| 3110 | * ImportError exception if initialization of WINSOCK fails. When |
| 3111 | * WINSOCK is initialized succesfully, a call to WSACleanup() is |
| 3112 | * scheduled to be made at exit time. |
| 3113 | * |
| 3114 | * For OS/2, this module is also called "_socket" and uses a wrapper |
| 3115 | * "socket.py" which implements that functionality that is missing |
| 3116 | * when PC operating systems don't put socket descriptors in the |
| 3117 | * operating system's filesystem layer. |
| 3118 | */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3119 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3120 | static char module_doc[] = |
Fred Drake | a136d49 | 2000-08-16 14:18:30 +0000 | [diff] [blame] | 3121 | "Implementation module for socket operations. See the socket module\n\ |
| 3122 | for documentation."; |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3123 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 3124 | DL_EXPORT(void) |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3125 | init_socket(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3126 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3127 | PyObject *m, *d; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3128 | #ifdef RISCOS |
| 3129 | _kernel_swi_regs r; |
| 3130 | r.r[0]=0; |
| 3131 | _kernel_swi(0x43380, &r, &r); |
| 3132 | taskwindow = r.r[0]; |
| 3133 | #else |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3134 | #ifdef MS_WINDOWS |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3135 | if (!NTinit()) |
| 3136 | return; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3137 | #else |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 3138 | #if defined(__TOS_OS2__) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3139 | if (!OS2init()) |
| 3140 | return; |
Fred Drake | a136d49 | 2000-08-16 14:18:30 +0000 | [diff] [blame] | 3141 | #endif /* __TOS_OS2__ */ |
| 3142 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3143 | #endif /* RISCOS */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3144 | PySocketSock_Type.ob_type = &PyType_Type; |
Michael W. Hudson | 03f96bd | 2001-11-09 10:06:23 +0000 | [diff] [blame] | 3145 | PySocketSock_Type.tp_getattro = PyObject_GenericGetAttr; |
| 3146 | PySocketSock_Type.tp_alloc = PyType_GenericAlloc; |
| 3147 | PySocketSock_Type.tp_free = _PyObject_Del; |
Guido van Rossum | a120ffc | 2001-01-22 15:29:14 +0000 | [diff] [blame] | 3148 | #ifdef USE_SSL |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 3149 | PySSL_Type.ob_type = &PyType_Type; |
Guido van Rossum | a120ffc | 2001-01-22 15:29:14 +0000 | [diff] [blame] | 3150 | #endif |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3151 | m = Py_InitModule3("_socket", PySocket_methods, module_doc); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3152 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 3153 | PySocket_Error = PyErr_NewException("socket.error", NULL, NULL); |
| 3154 | if (PySocket_Error == NULL) |
| 3155 | return; |
Martin v. Löwis | 864e9ff | 2001-08-04 22:32:03 +0000 | [diff] [blame] | 3156 | PyDict_SetItemString(d, "error", PySocket_Error); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3157 | PyH_Error = PyErr_NewException("socket.herror", PySocket_Error, NULL); |
| 3158 | if (PyH_Error == NULL) |
| 3159 | return; |
Martin v. Löwis | 864e9ff | 2001-08-04 22:32:03 +0000 | [diff] [blame] | 3160 | PyDict_SetItemString(d, "herror", PyH_Error); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3161 | PyGAI_Error = PyErr_NewException("socket.gaierror", PySocket_Error, |
| 3162 | NULL); |
| 3163 | if (PyGAI_Error == NULL) |
| 3164 | return; |
Martin v. Löwis | 864e9ff | 2001-08-04 22:32:03 +0000 | [diff] [blame] | 3165 | PyDict_SetItemString(d, "gaierror", PyGAI_Error); |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 3166 | #ifdef USE_SSL |
| 3167 | SSL_load_error_strings(); |
| 3168 | SSLeay_add_ssl_algorithms(); |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 3169 | PySSLErrorObject = PyErr_NewException("socket.sslerror", NULL, NULL); |
| 3170 | if (PySSLErrorObject == NULL) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 3171 | return; |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 3172 | PyDict_SetItemString(d, "sslerror", PySSLErrorObject); |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 3173 | if (PyDict_SetItemString(d, "SSLType", |
Jeremy Hylton | 5b6ce5a | 2001-10-10 23:55:43 +0000 | [diff] [blame] | 3174 | (PyObject *)&PySSL_Type) != 0) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 3175 | return; |
Jeremy Hylton | 57ca873 | 2001-10-11 00:00:17 +0000 | [diff] [blame] | 3176 | PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN", |
| 3177 | SSL_ERROR_ZERO_RETURN); |
| 3178 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ", |
| 3179 | SSL_ERROR_WANT_READ); |
| 3180 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE", |
| 3181 | SSL_ERROR_WANT_WRITE); |
| 3182 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP", |
| 3183 | SSL_ERROR_WANT_X509_LOOKUP); |
| 3184 | PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL", |
| 3185 | SSL_ERROR_SYSCALL); |
| 3186 | PyModule_AddIntConstant(m, "SSL_ERROR_SSL", |
| 3187 | SSL_ERROR_SSL); |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 3188 | #endif /* USE_SSL */ |
Guido van Rossum | 91ba64d | 1997-06-02 22:18:09 +0000 | [diff] [blame] | 3189 | if (PyDict_SetItemString(d, "SocketType", |
Guido van Rossum | 3b4b6fc | 1997-05-21 14:37:37 +0000 | [diff] [blame] | 3190 | (PyObject *)&PySocketSock_Type) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 3191 | return; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3192 | if (PyDict_SetItemString(d, "socket", |
| 3193 | (PyObject *)&PySocketSock_Type) != 0) |
| 3194 | return; |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3195 | |
| 3196 | /* Address families (we only support AF_INET and AF_UNIX) */ |
| 3197 | #ifdef AF_UNSPEC |
| 3198 | insint(d, "AF_UNSPEC", AF_UNSPEC); |
| 3199 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3200 | insint(d, "AF_INET", AF_INET); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3201 | #ifdef AF_INET6 |
| 3202 | insint(d, "AF_INET6", AF_INET6); |
| 3203 | #endif /* AF_INET6 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3204 | #ifdef AF_UNIX |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3205 | insint(d, "AF_UNIX", AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3206 | #endif /* AF_UNIX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3207 | #ifdef AF_AX25 |
| 3208 | insint(d, "AF_AX25", AF_AX25); /* Amateur Radio AX.25 */ |
| 3209 | #endif |
| 3210 | #ifdef AF_IPX |
| 3211 | insint(d, "AF_IPX", AF_IPX); /* Novell IPX */ |
| 3212 | #endif |
| 3213 | #ifdef AF_APPLETALK |
| 3214 | insint(d, "AF_APPLETALK", AF_APPLETALK); /* Appletalk DDP */ |
| 3215 | #endif |
| 3216 | #ifdef AF_NETROM |
| 3217 | insint(d, "AF_NETROM", AF_NETROM); /* Amateur radio NetROM */ |
| 3218 | #endif |
| 3219 | #ifdef AF_BRIDGE |
| 3220 | insint(d, "AF_BRIDGE", AF_BRIDGE); /* Multiprotocol bridge */ |
| 3221 | #endif |
| 3222 | #ifdef AF_AAL5 |
| 3223 | insint(d, "AF_AAL5", AF_AAL5); /* Reserved for Werner's ATM */ |
| 3224 | #endif |
| 3225 | #ifdef AF_X25 |
| 3226 | insint(d, "AF_X25", AF_X25); /* Reserved for X.25 project */ |
| 3227 | #endif |
| 3228 | #ifdef AF_INET6 |
| 3229 | insint(d, "AF_INET6", AF_INET6); /* IP version 6 */ |
| 3230 | #endif |
| 3231 | #ifdef AF_ROSE |
| 3232 | insint(d, "AF_ROSE", AF_ROSE); /* Amateur Radio X.25 PLP */ |
| 3233 | #endif |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 3234 | #ifdef HAVE_NETPACKET_PACKET_H |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 3235 | insint(d, "AF_PACKET", AF_PACKET); |
| 3236 | insint(d, "PF_PACKET", PF_PACKET); |
| 3237 | insint(d, "PACKET_HOST", PACKET_HOST); |
| 3238 | insint(d, "PACKET_BROADCAST", PACKET_BROADCAST); |
| 3239 | insint(d, "PACKET_MULTICAST", PACKET_MULTICAST); |
| 3240 | insint(d, "PACKET_OTHERHOST", PACKET_OTHERHOST); |
| 3241 | insint(d, "PACKET_OUTGOING", PACKET_OUTGOING); |
| 3242 | insint(d, "PACKET_LOOPBACK", PACKET_LOOPBACK); |
| 3243 | insint(d, "PACKET_FASTROUTE", PACKET_FASTROUTE); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3244 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3245 | |
| 3246 | /* Socket types */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3247 | insint(d, "SOCK_STREAM", SOCK_STREAM); |
| 3248 | insint(d, "SOCK_DGRAM", SOCK_DGRAM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3249 | #ifndef __BEOS__ |
| 3250 | /* We have incomplete socket support. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3251 | insint(d, "SOCK_RAW", SOCK_RAW); |
| 3252 | insint(d, "SOCK_SEQPACKET", SOCK_SEQPACKET); |
| 3253 | insint(d, "SOCK_RDM", SOCK_RDM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3254 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3255 | |
| 3256 | #ifdef SO_DEBUG |
| 3257 | insint(d, "SO_DEBUG", SO_DEBUG); |
| 3258 | #endif |
| 3259 | #ifdef SO_ACCEPTCONN |
| 3260 | insint(d, "SO_ACCEPTCONN", SO_ACCEPTCONN); |
| 3261 | #endif |
| 3262 | #ifdef SO_REUSEADDR |
| 3263 | insint(d, "SO_REUSEADDR", SO_REUSEADDR); |
| 3264 | #endif |
| 3265 | #ifdef SO_KEEPALIVE |
| 3266 | insint(d, "SO_KEEPALIVE", SO_KEEPALIVE); |
| 3267 | #endif |
| 3268 | #ifdef SO_DONTROUTE |
| 3269 | insint(d, "SO_DONTROUTE", SO_DONTROUTE); |
| 3270 | #endif |
| 3271 | #ifdef SO_BROADCAST |
| 3272 | insint(d, "SO_BROADCAST", SO_BROADCAST); |
| 3273 | #endif |
| 3274 | #ifdef SO_USELOOPBACK |
| 3275 | insint(d, "SO_USELOOPBACK", SO_USELOOPBACK); |
| 3276 | #endif |
| 3277 | #ifdef SO_LINGER |
| 3278 | insint(d, "SO_LINGER", SO_LINGER); |
| 3279 | #endif |
| 3280 | #ifdef SO_OOBINLINE |
| 3281 | insint(d, "SO_OOBINLINE", SO_OOBINLINE); |
| 3282 | #endif |
| 3283 | #ifdef SO_REUSEPORT |
| 3284 | insint(d, "SO_REUSEPORT", SO_REUSEPORT); |
| 3285 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3286 | #ifdef SO_SNDBUF |
| 3287 | insint(d, "SO_SNDBUF", SO_SNDBUF); |
| 3288 | #endif |
| 3289 | #ifdef SO_RCVBUF |
| 3290 | insint(d, "SO_RCVBUF", SO_RCVBUF); |
| 3291 | #endif |
| 3292 | #ifdef SO_SNDLOWAT |
| 3293 | insint(d, "SO_SNDLOWAT", SO_SNDLOWAT); |
| 3294 | #endif |
| 3295 | #ifdef SO_RCVLOWAT |
| 3296 | insint(d, "SO_RCVLOWAT", SO_RCVLOWAT); |
| 3297 | #endif |
| 3298 | #ifdef SO_SNDTIMEO |
| 3299 | insint(d, "SO_SNDTIMEO", SO_SNDTIMEO); |
| 3300 | #endif |
| 3301 | #ifdef SO_RCVTIMEO |
| 3302 | insint(d, "SO_RCVTIMEO", SO_RCVTIMEO); |
| 3303 | #endif |
| 3304 | #ifdef SO_ERROR |
| 3305 | insint(d, "SO_ERROR", SO_ERROR); |
| 3306 | #endif |
| 3307 | #ifdef SO_TYPE |
| 3308 | insint(d, "SO_TYPE", SO_TYPE); |
| 3309 | #endif |
| 3310 | |
| 3311 | /* Maximum number of connections for "listen" */ |
| 3312 | #ifdef SOMAXCONN |
| 3313 | insint(d, "SOMAXCONN", SOMAXCONN); |
| 3314 | #else |
| 3315 | insint(d, "SOMAXCONN", 5); /* Common value */ |
| 3316 | #endif |
| 3317 | |
| 3318 | /* Flags for send, recv */ |
| 3319 | #ifdef MSG_OOB |
| 3320 | insint(d, "MSG_OOB", MSG_OOB); |
| 3321 | #endif |
| 3322 | #ifdef MSG_PEEK |
| 3323 | insint(d, "MSG_PEEK", MSG_PEEK); |
| 3324 | #endif |
| 3325 | #ifdef MSG_DONTROUTE |
| 3326 | insint(d, "MSG_DONTROUTE", MSG_DONTROUTE); |
| 3327 | #endif |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 3328 | #ifdef MSG_DONTWAIT |
Guido van Rossum | 14f3f59 | 2000-04-25 21:53:58 +0000 | [diff] [blame] | 3329 | insint(d, "MSG_DONTWAIT", MSG_DONTWAIT); |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 3330 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3331 | #ifdef MSG_EOR |
| 3332 | insint(d, "MSG_EOR", MSG_EOR); |
| 3333 | #endif |
| 3334 | #ifdef MSG_TRUNC |
| 3335 | insint(d, "MSG_TRUNC", MSG_TRUNC); |
| 3336 | #endif |
| 3337 | #ifdef MSG_CTRUNC |
| 3338 | insint(d, "MSG_CTRUNC", MSG_CTRUNC); |
| 3339 | #endif |
| 3340 | #ifdef MSG_WAITALL |
| 3341 | insint(d, "MSG_WAITALL", MSG_WAITALL); |
| 3342 | #endif |
| 3343 | #ifdef MSG_BTAG |
| 3344 | insint(d, "MSG_BTAG", MSG_BTAG); |
| 3345 | #endif |
| 3346 | #ifdef MSG_ETAG |
| 3347 | insint(d, "MSG_ETAG", MSG_ETAG); |
| 3348 | #endif |
| 3349 | |
| 3350 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 3351 | #ifdef SOL_SOCKET |
| 3352 | insint(d, "SOL_SOCKET", SOL_SOCKET); |
| 3353 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3354 | #ifdef SOL_IP |
| 3355 | insint(d, "SOL_IP", SOL_IP); |
| 3356 | #else |
| 3357 | insint(d, "SOL_IP", 0); |
| 3358 | #endif |
| 3359 | #ifdef SOL_IPX |
| 3360 | insint(d, "SOL_IPX", SOL_IPX); |
| 3361 | #endif |
| 3362 | #ifdef SOL_AX25 |
| 3363 | insint(d, "SOL_AX25", SOL_AX25); |
| 3364 | #endif |
| 3365 | #ifdef SOL_ATALK |
| 3366 | insint(d, "SOL_ATALK", SOL_ATALK); |
| 3367 | #endif |
| 3368 | #ifdef SOL_NETROM |
| 3369 | insint(d, "SOL_NETROM", SOL_NETROM); |
| 3370 | #endif |
| 3371 | #ifdef SOL_ROSE |
| 3372 | insint(d, "SOL_ROSE", SOL_ROSE); |
| 3373 | #endif |
| 3374 | #ifdef SOL_TCP |
| 3375 | insint(d, "SOL_TCP", SOL_TCP); |
| 3376 | #else |
| 3377 | insint(d, "SOL_TCP", 6); |
| 3378 | #endif |
| 3379 | #ifdef SOL_UDP |
| 3380 | insint(d, "SOL_UDP", SOL_UDP); |
| 3381 | #else |
| 3382 | insint(d, "SOL_UDP", 17); |
| 3383 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3384 | #ifdef IPPROTO_IP |
| 3385 | insint(d, "IPPROTO_IP", IPPROTO_IP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 3386 | #else |
| 3387 | insint(d, "IPPROTO_IP", 0); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3388 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3389 | #ifdef IPPROTO_HOPOPTS |
| 3390 | insint(d, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS); |
| 3391 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3392 | #ifdef IPPROTO_ICMP |
| 3393 | insint(d, "IPPROTO_ICMP", IPPROTO_ICMP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 3394 | #else |
| 3395 | insint(d, "IPPROTO_ICMP", 1); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3396 | #endif |
| 3397 | #ifdef IPPROTO_IGMP |
| 3398 | insint(d, "IPPROTO_IGMP", IPPROTO_IGMP); |
| 3399 | #endif |
| 3400 | #ifdef IPPROTO_GGP |
| 3401 | insint(d, "IPPROTO_GGP", IPPROTO_GGP); |
| 3402 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3403 | #ifdef IPPROTO_IPV4 |
| 3404 | insint(d, "IPPROTO_IPV4", IPPROTO_IPV4); |
| 3405 | #endif |
| 3406 | #ifdef IPPROTO_IPIP |
| 3407 | insint(d, "IPPROTO_IPIP", IPPROTO_IPIP); |
| 3408 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3409 | #ifdef IPPROTO_TCP |
| 3410 | insint(d, "IPPROTO_TCP", IPPROTO_TCP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 3411 | #else |
| 3412 | insint(d, "IPPROTO_TCP", 6); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3413 | #endif |
| 3414 | #ifdef IPPROTO_EGP |
| 3415 | insint(d, "IPPROTO_EGP", IPPROTO_EGP); |
| 3416 | #endif |
| 3417 | #ifdef IPPROTO_PUP |
| 3418 | insint(d, "IPPROTO_PUP", IPPROTO_PUP); |
| 3419 | #endif |
| 3420 | #ifdef IPPROTO_UDP |
| 3421 | insint(d, "IPPROTO_UDP", IPPROTO_UDP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 3422 | #else |
| 3423 | insint(d, "IPPROTO_UDP", 17); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3424 | #endif |
| 3425 | #ifdef IPPROTO_IDP |
| 3426 | insint(d, "IPPROTO_IDP", IPPROTO_IDP); |
| 3427 | #endif |
| 3428 | #ifdef IPPROTO_HELLO |
| 3429 | insint(d, "IPPROTO_HELLO", IPPROTO_HELLO); |
| 3430 | #endif |
| 3431 | #ifdef IPPROTO_ND |
| 3432 | insint(d, "IPPROTO_ND", IPPROTO_ND); |
| 3433 | #endif |
| 3434 | #ifdef IPPROTO_TP |
| 3435 | insint(d, "IPPROTO_TP", IPPROTO_TP); |
| 3436 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3437 | #ifdef IPPROTO_IPV6 |
| 3438 | insint(d, "IPPROTO_IPV6", IPPROTO_IPV6); |
| 3439 | #endif |
| 3440 | #ifdef IPPROTO_ROUTING |
| 3441 | insint(d, "IPPROTO_ROUTING", IPPROTO_ROUTING); |
| 3442 | #endif |
| 3443 | #ifdef IPPROTO_FRAGMENT |
| 3444 | insint(d, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT); |
| 3445 | #endif |
| 3446 | #ifdef IPPROTO_RSVP |
| 3447 | insint(d, "IPPROTO_RSVP", IPPROTO_RSVP); |
| 3448 | #endif |
| 3449 | #ifdef IPPROTO_GRE |
| 3450 | insint(d, "IPPROTO_GRE", IPPROTO_GRE); |
| 3451 | #endif |
| 3452 | #ifdef IPPROTO_ESP |
| 3453 | insint(d, "IPPROTO_ESP", IPPROTO_ESP); |
| 3454 | #endif |
| 3455 | #ifdef IPPROTO_AH |
| 3456 | insint(d, "IPPROTO_AH", IPPROTO_AH); |
| 3457 | #endif |
| 3458 | #ifdef IPPROTO_MOBILE |
| 3459 | insint(d, "IPPROTO_MOBILE", IPPROTO_MOBILE); |
| 3460 | #endif |
| 3461 | #ifdef IPPROTO_ICMPV6 |
| 3462 | insint(d, "IPPROTO_ICMPV6", IPPROTO_ICMPV6); |
| 3463 | #endif |
| 3464 | #ifdef IPPROTO_NONE |
| 3465 | insint(d, "IPPROTO_NONE", IPPROTO_NONE); |
| 3466 | #endif |
| 3467 | #ifdef IPPROTO_DSTOPTS |
| 3468 | insint(d, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS); |
| 3469 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3470 | #ifdef IPPROTO_XTP |
| 3471 | insint(d, "IPPROTO_XTP", IPPROTO_XTP); |
| 3472 | #endif |
| 3473 | #ifdef IPPROTO_EON |
| 3474 | insint(d, "IPPROTO_EON", IPPROTO_EON); |
| 3475 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3476 | #ifdef IPPROTO_PIM |
| 3477 | insint(d, "IPPROTO_PIM", IPPROTO_PIM); |
| 3478 | #endif |
| 3479 | #ifdef IPPROTO_IPCOMP |
| 3480 | insint(d, "IPPROTO_IPCOMP", IPPROTO_IPCOMP); |
| 3481 | #endif |
| 3482 | #ifdef IPPROTO_VRRP |
| 3483 | insint(d, "IPPROTO_VRRP", IPPROTO_VRRP); |
| 3484 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3485 | #ifdef IPPROTO_BIP |
| 3486 | insint(d, "IPPROTO_BIP", IPPROTO_BIP); |
| 3487 | #endif |
| 3488 | /**/ |
| 3489 | #ifdef IPPROTO_RAW |
| 3490 | insint(d, "IPPROTO_RAW", IPPROTO_RAW); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 3491 | #else |
| 3492 | insint(d, "IPPROTO_RAW", 255); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3493 | #endif |
| 3494 | #ifdef IPPROTO_MAX |
| 3495 | insint(d, "IPPROTO_MAX", IPPROTO_MAX); |
| 3496 | #endif |
| 3497 | |
| 3498 | /* Some port configuration */ |
| 3499 | #ifdef IPPORT_RESERVED |
| 3500 | insint(d, "IPPORT_RESERVED", IPPORT_RESERVED); |
| 3501 | #else |
| 3502 | insint(d, "IPPORT_RESERVED", 1024); |
| 3503 | #endif |
| 3504 | #ifdef IPPORT_USERRESERVED |
| 3505 | insint(d, "IPPORT_USERRESERVED", IPPORT_USERRESERVED); |
| 3506 | #else |
| 3507 | insint(d, "IPPORT_USERRESERVED", 5000); |
| 3508 | #endif |
| 3509 | |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 3510 | /* Some reserved IP v.4 addresses */ |
| 3511 | #ifdef INADDR_ANY |
| 3512 | insint(d, "INADDR_ANY", INADDR_ANY); |
| 3513 | #else |
| 3514 | insint(d, "INADDR_ANY", 0x00000000); |
| 3515 | #endif |
| 3516 | #ifdef INADDR_BROADCAST |
| 3517 | insint(d, "INADDR_BROADCAST", INADDR_BROADCAST); |
| 3518 | #else |
| 3519 | insint(d, "INADDR_BROADCAST", 0xffffffff); |
| 3520 | #endif |
| 3521 | #ifdef INADDR_LOOPBACK |
| 3522 | insint(d, "INADDR_LOOPBACK", INADDR_LOOPBACK); |
| 3523 | #else |
| 3524 | insint(d, "INADDR_LOOPBACK", 0x7F000001); |
| 3525 | #endif |
| 3526 | #ifdef INADDR_UNSPEC_GROUP |
| 3527 | insint(d, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP); |
| 3528 | #else |
| 3529 | insint(d, "INADDR_UNSPEC_GROUP", 0xe0000000); |
| 3530 | #endif |
| 3531 | #ifdef INADDR_ALLHOSTS_GROUP |
| 3532 | insint(d, "INADDR_ALLHOSTS_GROUP", INADDR_ALLHOSTS_GROUP); |
| 3533 | #else |
| 3534 | insint(d, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
| 3535 | #endif |
| 3536 | #ifdef INADDR_MAX_LOCAL_GROUP |
| 3537 | insint(d, "INADDR_MAX_LOCAL_GROUP", INADDR_MAX_LOCAL_GROUP); |
| 3538 | #else |
| 3539 | insint(d, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
| 3540 | #endif |
| 3541 | #ifdef INADDR_NONE |
| 3542 | insint(d, "INADDR_NONE", INADDR_NONE); |
| 3543 | #else |
| 3544 | insint(d, "INADDR_NONE", 0xffffffff); |
| 3545 | #endif |
| 3546 | |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3547 | /* IPv4 [gs]etsockopt options */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3548 | #ifdef IP_OPTIONS |
| 3549 | insint(d, "IP_OPTIONS", IP_OPTIONS); |
| 3550 | #endif |
| 3551 | #ifdef IP_HDRINCL |
| 3552 | insint(d, "IP_HDRINCL", IP_HDRINCL); |
| 3553 | #endif |
| 3554 | #ifdef IP_TOS |
| 3555 | insint(d, "IP_TOS", IP_TOS); |
| 3556 | #endif |
| 3557 | #ifdef IP_TTL |
| 3558 | insint(d, "IP_TTL", IP_TTL); |
| 3559 | #endif |
| 3560 | #ifdef IP_RECVOPTS |
| 3561 | insint(d, "IP_RECVOPTS", IP_RECVOPTS); |
| 3562 | #endif |
| 3563 | #ifdef IP_RECVRETOPTS |
| 3564 | insint(d, "IP_RECVRETOPTS", IP_RECVRETOPTS); |
| 3565 | #endif |
| 3566 | #ifdef IP_RECVDSTADDR |
| 3567 | insint(d, "IP_RECVDSTADDR", IP_RECVDSTADDR); |
| 3568 | #endif |
| 3569 | #ifdef IP_RETOPTS |
| 3570 | insint(d, "IP_RETOPTS", IP_RETOPTS); |
| 3571 | #endif |
| 3572 | #ifdef IP_MULTICAST_IF |
| 3573 | insint(d, "IP_MULTICAST_IF", IP_MULTICAST_IF); |
| 3574 | #endif |
| 3575 | #ifdef IP_MULTICAST_TTL |
| 3576 | insint(d, "IP_MULTICAST_TTL", IP_MULTICAST_TTL); |
| 3577 | #endif |
| 3578 | #ifdef IP_MULTICAST_LOOP |
| 3579 | insint(d, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP); |
| 3580 | #endif |
| 3581 | #ifdef IP_ADD_MEMBERSHIP |
| 3582 | insint(d, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP); |
| 3583 | #endif |
| 3584 | #ifdef IP_DROP_MEMBERSHIP |
| 3585 | insint(d, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP); |
| 3586 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3587 | #ifdef IP_DEFAULT_MULTICAST_TTL |
| 3588 | insint(d, "IP_DEFAULT_MULTICAST_TTL", IP_DEFAULT_MULTICAST_TTL); |
| 3589 | #endif |
| 3590 | #ifdef IP_DEFAULT_MULTICAST_LOOP |
| 3591 | insint(d, "IP_DEFAULT_MULTICAST_LOOP", IP_DEFAULT_MULTICAST_LOOP); |
| 3592 | #endif |
| 3593 | #ifdef IP_MAX_MEMBERSHIPS |
| 3594 | insint(d, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS); |
| 3595 | #endif |
| 3596 | |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3597 | /* IPv6 [gs]etsockopt options, defined in RFC2553 */ |
| 3598 | #ifdef IPV6_JOIN_GROUP |
| 3599 | insint(d, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP); |
| 3600 | #endif |
| 3601 | #ifdef IPV6_LEAVE_GROUP |
| 3602 | insint(d, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP); |
| 3603 | #endif |
| 3604 | #ifdef IPV6_MULTICAST_HOPS |
| 3605 | insint(d, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS); |
| 3606 | #endif |
| 3607 | #ifdef IPV6_MULTICAST_IF |
| 3608 | insint(d, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF); |
| 3609 | #endif |
| 3610 | #ifdef IPV6_MULTICAST_LOOP |
| 3611 | insint(d, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP); |
| 3612 | #endif |
| 3613 | #ifdef IPV6_UNICAST_HOPS |
| 3614 | insint(d, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS); |
| 3615 | #endif |
| 3616 | |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3617 | /* TCP options */ |
| 3618 | #ifdef TCP_NODELAY |
| 3619 | insint(d, "TCP_NODELAY", TCP_NODELAY); |
| 3620 | #endif |
| 3621 | #ifdef TCP_MAXSEG |
| 3622 | insint(d, "TCP_MAXSEG", TCP_MAXSEG); |
| 3623 | #endif |
| 3624 | |
| 3625 | /* IPX options */ |
| 3626 | #ifdef IPX_TYPE |
| 3627 | insint(d, "IPX_TYPE", IPX_TYPE); |
| 3628 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3629 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3630 | /* get{addr,name}info parameters */ |
| 3631 | #ifdef EAI_ADDRFAMILY |
| 3632 | insint(d, "EAI_ADDRFAMILY", EAI_ADDRFAMILY); |
| 3633 | #endif |
| 3634 | #ifdef EAI_AGAIN |
| 3635 | insint(d, "EAI_AGAIN", EAI_AGAIN); |
| 3636 | #endif |
| 3637 | #ifdef EAI_BADFLAGS |
| 3638 | insint(d, "EAI_BADFLAGS", EAI_BADFLAGS); |
| 3639 | #endif |
| 3640 | #ifdef EAI_FAIL |
| 3641 | insint(d, "EAI_FAIL", EAI_FAIL); |
| 3642 | #endif |
| 3643 | #ifdef EAI_FAMILY |
| 3644 | insint(d, "EAI_FAMILY", EAI_FAMILY); |
| 3645 | #endif |
| 3646 | #ifdef EAI_MEMORY |
| 3647 | insint(d, "EAI_MEMORY", EAI_MEMORY); |
| 3648 | #endif |
| 3649 | #ifdef EAI_NODATA |
| 3650 | insint(d, "EAI_NODATA", EAI_NODATA); |
| 3651 | #endif |
| 3652 | #ifdef EAI_NONAME |
| 3653 | insint(d, "EAI_NONAME", EAI_NONAME); |
| 3654 | #endif |
| 3655 | #ifdef EAI_SERVICE |
| 3656 | insint(d, "EAI_SERVICE", EAI_SERVICE); |
| 3657 | #endif |
| 3658 | #ifdef EAI_SOCKTYPE |
| 3659 | insint(d, "EAI_SOCKTYPE", EAI_SOCKTYPE); |
| 3660 | #endif |
| 3661 | #ifdef EAI_SYSTEM |
| 3662 | insint(d, "EAI_SYSTEM", EAI_SYSTEM); |
| 3663 | #endif |
| 3664 | #ifdef EAI_BADHINTS |
| 3665 | insint(d, "EAI_BADHINTS", EAI_BADHINTS); |
| 3666 | #endif |
| 3667 | #ifdef EAI_PROTOCOL |
| 3668 | insint(d, "EAI_PROTOCOL", EAI_PROTOCOL); |
| 3669 | #endif |
| 3670 | #ifdef EAI_MAX |
| 3671 | insint(d, "EAI_MAX", EAI_MAX); |
| 3672 | #endif |
| 3673 | #ifdef AI_PASSIVE |
| 3674 | insint(d, "AI_PASSIVE", AI_PASSIVE); |
| 3675 | #endif |
| 3676 | #ifdef AI_CANONNAME |
| 3677 | insint(d, "AI_CANONNAME", AI_CANONNAME); |
| 3678 | #endif |
| 3679 | #ifdef AI_NUMERICHOST |
| 3680 | insint(d, "AI_NUMERICHOST", AI_NUMERICHOST); |
| 3681 | #endif |
| 3682 | #ifdef AI_MASK |
| 3683 | insint(d, "AI_MASK", AI_MASK); |
| 3684 | #endif |
| 3685 | #ifdef AI_ALL |
| 3686 | insint(d, "AI_ALL", AI_ALL); |
| 3687 | #endif |
| 3688 | #ifdef AI_V4MAPPED_CFG |
| 3689 | insint(d, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG); |
| 3690 | #endif |
| 3691 | #ifdef AI_ADDRCONFIG |
| 3692 | insint(d, "AI_ADDRCONFIG", AI_ADDRCONFIG); |
| 3693 | #endif |
| 3694 | #ifdef AI_V4MAPPED |
| 3695 | insint(d, "AI_V4MAPPED", AI_V4MAPPED); |
| 3696 | #endif |
| 3697 | #ifdef AI_DEFAULT |
| 3698 | insint(d, "AI_DEFAULT", AI_DEFAULT); |
| 3699 | #endif |
| 3700 | #ifdef NI_MAXHOST |
| 3701 | insint(d, "NI_MAXHOST", NI_MAXHOST); |
| 3702 | #endif |
| 3703 | #ifdef NI_MAXSERV |
| 3704 | insint(d, "NI_MAXSERV", NI_MAXSERV); |
| 3705 | #endif |
| 3706 | #ifdef NI_NOFQDN |
| 3707 | insint(d, "NI_NOFQDN", NI_NOFQDN); |
| 3708 | #endif |
| 3709 | #ifdef NI_NUMERICHOST |
| 3710 | insint(d, "NI_NUMERICHOST", NI_NUMERICHOST); |
| 3711 | #endif |
| 3712 | #ifdef NI_NAMEREQD |
| 3713 | insint(d, "NI_NAMEREQD", NI_NAMEREQD); |
| 3714 | #endif |
| 3715 | #ifdef NI_NUMERICSERV |
| 3716 | insint(d, "NI_NUMERICSERV", NI_NUMERICSERV); |
| 3717 | #endif |
| 3718 | #ifdef NI_DGRAM |
| 3719 | insint(d, "NI_DGRAM", NI_DGRAM); |
| 3720 | #endif |
| 3721 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3722 | /* Initialize gethostbyname lock */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3723 | #ifdef USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 3724 | gethostbyname_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3725 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3726 | } |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 3727 | |
| 3728 | /* Simplistic emulation code for inet_pton that only works for IPv4 */ |
| 3729 | #ifndef HAVE_INET_PTON |
Martin v. Löwis | a2ca1ae | 2001-06-24 21:35:43 +0000 | [diff] [blame] | 3730 | int |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 3731 | inet_pton (int af, const char *src, void *dst) |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 3732 | { |
| 3733 | if(af == AF_INET){ |
| 3734 | long packed_addr; |
| 3735 | #ifdef USE_GUSI1 |
| 3736 | packed_addr = (long)inet_addr(src).s_addr; |
| 3737 | #else |
| 3738 | packed_addr = inet_addr(src); |
| 3739 | #endif |
| 3740 | if (packed_addr == INADDR_NONE) |
| 3741 | return 0; |
| 3742 | memcpy(dst, &packed_addr, 4); |
| 3743 | return 1; |
| 3744 | } |
| 3745 | /* Should set errno to EAFNOSUPPORT */ |
| 3746 | return -1; |
| 3747 | } |
| 3748 | |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 3749 | const char * |
| 3750 | inet_ntop(int af, const void *src, char *dst, socklen_t size) |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 3751 | { |
| 3752 | if (af == AF_INET) { |
| 3753 | struct in_addr packed_addr; |
| 3754 | if (size < 16) |
| 3755 | /* Should set errno to ENOSPC. */ |
| 3756 | return NULL; |
| 3757 | memcpy(&packed_addr, src, sizeof(packed_addr)); |
| 3758 | return strncpy(dst, inet_ntoa(packed_addr), size); |
| 3759 | } |
| 3760 | /* Should set errno to EAFNOSUPPORT */ |
| 3761 | return NULL; |
| 3762 | } |
| 3763 | #endif |