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