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