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