Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1 | /* Socket module */ |
| 2 | |
| 3 | /* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5 | This module provides an interface to Berkeley socket IPC. |
| 6 | |
| 7 | Limitations: |
| 8 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 9 | - Only AF_INET, AF_INET6 and AF_UNIX address families are supported in a |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 10 | portable manner, though AF_PACKET, AF_NETLINK and AF_TIPC are supported |
| 11 | under Linux. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 12 | - No read/write operations (use sendall/recv or makefile instead). |
| 13 | - Additional restrictions apply on some non-Unix platforms (compensated |
| 14 | for by socket.py). |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 16 | Module interface: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 18 | - socket.error: exception raised for socket specific errors |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 19 | - socket.gaierror: exception raised for getaddrinfo/getnameinfo errors, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 20 | a subclass of socket.error |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 21 | - socket.herror: exception raised for gethostby* errors, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 22 | a subclass of socket.error |
Guido van Rossum | 83a072d | 2002-09-03 19:10:18 +0000 | [diff] [blame] | 23 | - socket.fromfd(fd, family, type[, proto]) --> new socket object (created |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 24 | from an existing file descriptor) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 25 | - socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd') |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 26 | - socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...]) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 27 | - socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com') |
Guido van Rossum | 25405c7 | 1996-12-19 16:42:52 +0000 | [diff] [blame] | 28 | - socket.getprotobyname(protocolname) --> protocol number |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 29 | - socket.getservbyname(servicename[, protocolname]) --> port number |
| 30 | - socket.getservbyport(portnumber[, protocolname]) --> service name |
Guido van Rossum | 83a072d | 2002-09-03 19:10:18 +0000 | [diff] [blame] | 31 | - socket.socket([family[, type [, proto]]]) --> new socket object |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 32 | - socket.socketpair([family[, type [, proto]]]) --> (socket, socket) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 33 | - socket.ntohs(16 bit value) --> new int object |
| 34 | - socket.ntohl(32 bit value) --> new int object |
| 35 | - socket.htons(16 bit value) --> new int object |
| 36 | - socket.htonl(32 bit value) --> new int object |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 37 | - socket.getaddrinfo(host, port [, family, socktype, proto, flags]) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 38 | --> List of (family, socktype, proto, canonname, sockaddr) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 39 | - socket.getnameinfo(sockaddr, flags) --> (host, port) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 40 | - socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h> |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 41 | - socket.has_ipv6: boolean value indicating if IPv6 is supported |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 42 | - socket.inet_aton(IP address) -> 32-bit packed IP representation |
| 43 | - socket.inet_ntoa(packed IP) -> IP address string |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 44 | - socket.getdefaulttimeout() -> None | float |
| 45 | - socket.setdefaulttimeout(None | float) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 46 | - an Internet socket address is a pair (hostname, port) |
| 47 | where hostname can be anything recognized by gethostbyname() |
| 48 | (including the dd.dd.dd.dd notation) and port is in host byte order |
| 49 | - 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] | 50 | - a UNIX domain socket address is a string specifying the pathname |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 51 | - an AF_PACKET socket address is a tuple containing a string |
| 52 | specifying the ethernet interface and an integer specifying |
| 53 | the Ethernet protocol number to be received. For example: |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 54 | ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple |
Andrew M. Kuchling | b8e1717 | 2004-07-10 23:39:35 +0000 | [diff] [blame] | 55 | specify packet-type and ha-type/addr. |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 56 | - an AF_TIPC socket address is expressed as |
| 57 | (addr_type, v1, v2, v3 [, scope]); where addr_type can be one of: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 58 | TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, and TIPC_ADDR_ID; |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 59 | and scope can be one of: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 60 | TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and TIPC_NODE_SCOPE. |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 61 | The meaning of v1, v2 and v3 depends on the value of addr_type: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 62 | if addr_type is TIPC_ADDR_NAME: |
| 63 | v1 is the server type |
| 64 | v2 is the port identifier |
| 65 | v3 is ignored |
| 66 | if addr_type is TIPC_ADDR_NAMESEQ: |
| 67 | v1 is the server type |
| 68 | v2 is the lower port number |
| 69 | v3 is the upper port number |
| 70 | if addr_type is TIPC_ADDR_ID: |
| 71 | v1 is the node |
| 72 | v2 is the ref |
| 73 | v3 is ignored |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 76 | Local naming conventions: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 78 | - names starting with sock_ are socket object methods |
| 79 | - names starting with socket_ are module-level functions |
| 80 | - names starting with PySocket are exported through socketmodule.h |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 82 | */ |
| 83 | |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 84 | #ifdef __APPLE__ |
| 85 | /* |
| 86 | * inet_aton is not available on OSX 10.3, yet we want to use a binary |
| 87 | * that was build on 10.4 or later to work on that release, weak linking |
| 88 | * comes to the rescue. |
| 89 | */ |
| 90 | # pragma weak inet_aton |
| 91 | #endif |
| 92 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 93 | #include "Python.h" |
Georg Brandl | bc45a3f | 2006-03-17 19:17:34 +0000 | [diff] [blame] | 94 | #include "structmember.h" |
Kristján Valur Jónsson | 868f0aa | 2013-03-19 13:53:56 -0700 | [diff] [blame] | 95 | #include "timefuncs.h" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 96 | |
Victor Stinner | 465db3c | 2014-07-26 14:47:56 +0200 | [diff] [blame] | 97 | #ifndef INVALID_SOCKET /* MS defines this */ |
| 98 | #define INVALID_SOCKET (-1) |
| 99 | #endif |
| 100 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 101 | #undef MAX |
| 102 | #define MAX(x, y) ((x) < (y) ? (y) : (x)) |
| 103 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 104 | /* Socket object documentation */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 105 | PyDoc_STRVAR(sock_doc, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 106 | "socket([family[, type[, proto]]]) -> socket object\n\ |
| 107 | \n\ |
| 108 | Open a socket of the given type. The family argument specifies the\n\ |
| 109 | address family; it defaults to AF_INET. The type argument specifies\n\ |
| 110 | whether this is a stream (SOCK_STREAM, this is the default)\n\ |
| 111 | or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\ |
| 112 | specifying the default protocol. Keyword arguments are accepted.\n\ |
| 113 | \n\ |
| 114 | A socket object represents one endpoint of a network connection.\n\ |
| 115 | \n\ |
| 116 | Methods of socket objects (keyword arguments not allowed):\n\ |
| 117 | \n\ |
| 118 | accept() -- accept a connection, returning new socket and client address\n\ |
| 119 | bind(addr) -- bind the socket to a local address\n\ |
| 120 | close() -- close the socket\n\ |
| 121 | connect(addr) -- connect the socket to a remote address\n\ |
| 122 | connect_ex(addr) -- connect, return an error code instead of an exception\n\ |
| 123 | dup() -- return a new socket object identical to the current one [*]\n\ |
| 124 | fileno() -- return underlying file descriptor\n\ |
| 125 | getpeername() -- return remote address [*]\n\ |
| 126 | getsockname() -- return local address\n\ |
| 127 | getsockopt(level, optname[, buflen]) -- get socket options\n\ |
| 128 | gettimeout() -- return timeout or None\n\ |
| 129 | listen(n) -- start listening for incoming connections\n\ |
| 130 | makefile([mode, [bufsize]]) -- return a file object for the socket [*]\n\ |
| 131 | recv(buflen[, flags]) -- receive data\n\ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 132 | recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 133 | recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 134 | recvfrom_into(buffer[, nbytes, [, flags])\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 135 | -- receive data and sender\'s address (into a buffer)\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 136 | sendall(data[, flags]) -- send all data\n\ |
| 137 | send(data[, flags]) -- send data, may not send all of it\n\ |
| 138 | sendto(data[, flags], addr) -- send data to a given address\n\ |
| 139 | setblocking(0 | 1) -- set or clear the blocking I/O flag\n\ |
| 140 | setsockopt(level, optname, value) -- set socket options\n\ |
| 141 | settimeout(None | float) -- set or clear the timeout\n\ |
| 142 | shutdown(how) -- shut down traffic in one or both directions\n\ |
| 143 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 144 | [*] not available on all platforms!"); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 145 | |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 146 | /* XXX This is a terrible mess of platform-dependent preprocessor hacks. |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 147 | I hope some day someone can clean this up please... */ |
| 148 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 149 | /* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure |
| 150 | script doesn't get this right, so we hardcode some platform checks below. |
| 151 | On the other hand, not all Linux versions agree, so there the settings |
| 152 | computed by the configure script are needed! */ |
| 153 | |
| 154 | #ifndef linux |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 155 | # undef HAVE_GETHOSTBYNAME_R_3_ARG |
| 156 | # undef HAVE_GETHOSTBYNAME_R_5_ARG |
| 157 | # undef HAVE_GETHOSTBYNAME_R_6_ARG |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 158 | #endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 159 | |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 160 | #ifndef WITH_THREAD |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 161 | # undef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 162 | #endif |
| 163 | |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 164 | #ifdef HAVE_GETHOSTBYNAME_R |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 165 | # if defined(_AIX) || defined(__osf__) |
| 166 | # define HAVE_GETHOSTBYNAME_R_3_ARG |
| 167 | # elif defined(__sun) || defined(__sgi) |
| 168 | # define HAVE_GETHOSTBYNAME_R_5_ARG |
| 169 | # elif defined(linux) |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 170 | /* Rely on the configure script */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 171 | # else |
| 172 | # undef HAVE_GETHOSTBYNAME_R |
| 173 | # endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 174 | #endif |
| 175 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 176 | #if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \ |
| 177 | !defined(MS_WINDOWS) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 178 | # define USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 179 | #endif |
| 180 | |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 181 | /* To use __FreeBSD_version */ |
| 182 | #ifdef HAVE_SYS_PARAM_H |
| 183 | #include <sys/param.h> |
| 184 | #endif |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 185 | /* On systems on which getaddrinfo() is believed to not be thread-safe, |
Just van Rossum | 09aecd7 | 2003-05-09 08:03:44 +0000 | [diff] [blame] | 186 | (this includes the getaddrinfo emulation) protect access with a lock. */ |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 187 | #if defined(WITH_THREAD) && (defined(__APPLE__) || \ |
| 188 | (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \ |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 189 | defined(__OpenBSD__) || defined(__NetBSD__) || \ |
| 190 | defined(__VMS) || !defined(HAVE_GETADDRINFO)) |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 191 | #define USE_GETADDRINFO_LOCK |
| 192 | #endif |
| 193 | |
| 194 | #ifdef USE_GETADDRINFO_LOCK |
| 195 | #define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1); |
| 196 | #define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock); |
| 197 | #else |
| 198 | #define ACQUIRE_GETADDRINFO_LOCK |
| 199 | #define RELEASE_GETADDRINFO_LOCK |
| 200 | #endif |
| 201 | |
| 202 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 203 | # include "pythread.h" |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 204 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 205 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 206 | #if defined(PYCC_VACPP) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 207 | # include <types.h> |
| 208 | # include <io.h> |
| 209 | # include <sys/ioctl.h> |
| 210 | # include <utils.h> |
| 211 | # include <ctype.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 212 | #endif |
| 213 | |
Martin v. Löwis | 9e43730 | 2002-12-06 12:57:26 +0000 | [diff] [blame] | 214 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 215 | # include <ioctl.h> |
| 216 | #endif |
| 217 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 218 | #if defined(PYOS_OS2) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 219 | # define INCL_DOS |
| 220 | # define INCL_DOSERRORS |
| 221 | # define INCL_NOPMAPI |
| 222 | # include <os2.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 223 | #endif |
| 224 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 225 | #if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 226 | /* make sure that the reentrant (gethostbyaddr_r etc) |
| 227 | functions are declared correctly if compiling with |
| 228 | MIPSPro 7.x in ANSI C mode (default) */ |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 229 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 230 | /* XXX Using _SGIAPI is the wrong thing, |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 231 | but I don't know what the right thing is. */ |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 232 | #undef _SGIAPI /* to avoid warning */ |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 233 | #define _SGIAPI 1 |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 234 | |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 235 | #undef _XOPEN_SOURCE |
| 236 | #include <sys/socket.h> |
| 237 | #include <sys/types.h> |
| 238 | #include <netinet/in.h> |
| 239 | #ifdef _SS_ALIGNSIZE |
| 240 | #define HAVE_GETADDRINFO 1 |
| 241 | #define HAVE_GETNAMEINFO 1 |
| 242 | #endif |
| 243 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 244 | #define HAVE_INET_PTON |
| 245 | #include <netdb.h> |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 246 | #endif |
| 247 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 248 | /* Irix 6.5 fails to define this variable at all. This is needed |
| 249 | for both GCC and SGI's compiler. I'd say that the SGI headers |
Georg Brandl | dbd8339 | 2006-02-20 09:42:33 +0000 | [diff] [blame] | 250 | are just busted. Same thing for Solaris. */ |
| 251 | #if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN) |
Anthony Baxter | bab23cf | 2003-10-04 08:00:49 +0000 | [diff] [blame] | 252 | #define INET_ADDRSTRLEN 16 |
| 253 | #endif |
| 254 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 255 | /* Generic includes */ |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 256 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 257 | #include <sys/types.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 258 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 259 | |
Marc-André Lemburg | 976ade6 | 2002-02-16 18:47:07 +0000 | [diff] [blame] | 260 | /* Generic socket object definitions and includes */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 261 | #define PySocket_BUILDING_SOCKET |
Marc-André Lemburg | bb8b78b | 2002-02-16 18:44:52 +0000 | [diff] [blame] | 262 | #include "socketmodule.h" |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 263 | |
| 264 | /* Addressing includes */ |
| 265 | |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 266 | #ifndef MS_WINDOWS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 267 | |
| 268 | /* Non-MS WINDOWS includes */ |
| 269 | # include <netdb.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 270 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 271 | /* Headers needed for inet_ntoa() and inet_addr() */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 272 | # ifdef __BEOS__ |
| 273 | # include <net/netdb.h> |
| 274 | # elif defined(PYOS_OS2) && defined(PYCC_VACPP) |
| 275 | # include <netdb.h> |
Tim Peters | 603c683 | 2001-11-05 02:45:59 +0000 | [diff] [blame] | 276 | typedef size_t socklen_t; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 277 | # else |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 278 | # include <arpa/inet.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 279 | # endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 280 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 281 | # ifndef RISCOS |
| 282 | # include <fcntl.h> |
| 283 | # else |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 284 | # include <sys/ioctl.h> |
| 285 | # include <socklib.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 286 | # define NO_DUP |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 287 | int h_errno; /* not used */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 288 | # define INET_ADDRSTRLEN 16 |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 289 | # endif |
| 290 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 291 | #else |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 292 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 293 | /* MS_WINDOWS includes */ |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 294 | # ifdef HAVE_FCNTL_H |
| 295 | # include <fcntl.h> |
| 296 | # endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 297 | |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 298 | #endif |
| 299 | |
Skip Montanaro | 7befb99 | 2004-02-10 16:50:21 +0000 | [diff] [blame] | 300 | #include <stddef.h> |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 301 | |
| 302 | #ifndef offsetof |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 303 | # define offsetof(type, member) ((size_t)(&((type *)0)->member)) |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 304 | #endif |
| 305 | |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 306 | #ifndef O_NONBLOCK |
| 307 | # define O_NONBLOCK O_NDELAY |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 308 | #endif |
| 309 | |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 310 | /* include Python's addrinfo.h unless it causes trouble */ |
| 311 | #if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE) |
| 312 | /* Do not include addinfo.h on some newer IRIX versions. |
| 313 | * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21, |
| 314 | * for example, but not by 6.5.10. |
| 315 | */ |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 316 | #elif defined(_MSC_VER) && _MSC_VER>1201 |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 317 | /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and |
| 318 | * EAI_* constants are defined in (the already included) ws2tcpip.h. |
| 319 | */ |
| 320 | #else |
| 321 | # include "addrinfo.h" |
| 322 | #endif |
Jason Tishler | c246cb7 | 2004-08-09 13:25:59 +0000 | [diff] [blame] | 323 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 324 | #ifndef HAVE_INET_PTON |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 325 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 326 | int inet_pton(int af, const char *src, void *dst); |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 327 | 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] | 328 | #endif |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 329 | #endif |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 330 | |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 331 | #ifdef __APPLE__ |
| 332 | /* On OS X, getaddrinfo returns no error indication of lookup |
| 333 | failure, so we must use the emulation instead of the libinfo |
| 334 | implementation. Unfortunately, performing an autoconf test |
| 335 | for this bug would require DNS access for the machine performing |
| 336 | the configuration, which is not acceptable. Therefore, we |
| 337 | determine the bug just by checking for __APPLE__. If this bug |
| 338 | gets ever fixed, perhaps checking for sys/version.h would be |
| 339 | appropriate, which is 10/0 on the system with the bug. */ |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 340 | #ifndef HAVE_GETNAMEINFO |
| 341 | /* This bug seems to be fixed in Jaguar. Ths easiest way I could |
| 342 | Find to check for Jaguar is that it has getnameinfo(), which |
| 343 | older releases don't have */ |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 344 | #undef HAVE_GETADDRINFO |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 345 | #endif |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 346 | |
| 347 | #ifdef HAVE_INET_ATON |
| 348 | #define USE_INET_ATON_WEAKLINK |
| 349 | #endif |
| 350 | |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 351 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 352 | |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 353 | /* 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] | 354 | #if !defined(HAVE_GETADDRINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 355 | /* avoid clashes with the C library definition of the symbol. */ |
| 356 | #define getaddrinfo fake_getaddrinfo |
| 357 | #define gai_strerror fake_gai_strerror |
| 358 | #define freeaddrinfo fake_freeaddrinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 359 | #include "getaddrinfo.c" |
| 360 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 361 | #if !defined(HAVE_GETNAMEINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 362 | #define getnameinfo fake_getnameinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 363 | #include "getnameinfo.c" |
| 364 | #endif |
| 365 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 366 | #if defined(MS_WINDOWS) || defined(__BEOS__) |
| 367 | /* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 368 | /* seem to be a few differences in the API */ |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 369 | #define SOCKETCLOSE closesocket |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 370 | #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] | 371 | #endif |
| 372 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 373 | #ifdef MS_WIN32 |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 374 | #define EAFNOSUPPORT WSAEAFNOSUPPORT |
| 375 | #define snprintf _snprintf |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 376 | #endif |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 377 | |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 378 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 379 | #define SOCKETCLOSE soclose |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 380 | #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] | 381 | #endif |
| 382 | |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 383 | #ifndef SOCKETCLOSE |
| 384 | #define SOCKETCLOSE close |
| 385 | #endif |
| 386 | |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 387 | #if (defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H)) && !defined(__NetBSD__) && !defined(__DragonFly__) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 388 | #define USE_BLUETOOTH 1 |
| 389 | #if defined(__FreeBSD__) |
| 390 | #define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP |
| 391 | #define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 392 | #define BTPROTO_HCI BLUETOOTH_PROTO_HCI |
Hye-Shik Chang | 82958f0 | 2007-06-05 18:16:52 +0000 | [diff] [blame] | 393 | #define SOL_HCI SOL_HCI_RAW |
| 394 | #define HCI_FILTER SO_HCI_RAW_FILTER |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 395 | #define sockaddr_l2 sockaddr_l2cap |
| 396 | #define sockaddr_rc sockaddr_rfcomm |
Hye-Shik Chang | 82958f0 | 2007-06-05 18:16:52 +0000 | [diff] [blame] | 397 | #define hci_dev hci_node |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 398 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb) |
| 399 | #define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb) |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 400 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 401 | #elif defined(__NetBSD__) || defined(__DragonFly__) |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 402 | #define sockaddr_l2 sockaddr_bt |
| 403 | #define sockaddr_rc sockaddr_bt |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 404 | #define sockaddr_hci sockaddr_bt |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 405 | #define sockaddr_sco sockaddr_bt |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 406 | #define SOL_HCI BTPROTO_HCI |
| 407 | #define HCI_DATA_DIR SO_HCI_DIRECTION |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 408 | #define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb) |
| 409 | #define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb) |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 410 | #define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb) |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 411 | #define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 412 | #else |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 413 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb) |
| 414 | #define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb) |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 415 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 416 | #define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb) |
| 417 | #endif |
| 418 | #endif |
| 419 | |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 420 | #ifdef __VMS |
| 421 | /* TCP/IP Services for VMS uses a maximum send/recv buffer length */ |
| 422 | #define SEGMENT_SIZE (32 * 1024 -1) |
| 423 | #endif |
| 424 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 425 | #define SAS2SA(x) ((struct sockaddr *)(x)) |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 426 | |
Martin v. Löwis | e941617 | 2003-05-03 10:12:45 +0000 | [diff] [blame] | 427 | /* |
| 428 | * Constants for getnameinfo() |
| 429 | */ |
| 430 | #if !defined(NI_MAXHOST) |
| 431 | #define NI_MAXHOST 1025 |
| 432 | #endif |
| 433 | #if !defined(NI_MAXSERV) |
| 434 | #define NI_MAXSERV 32 |
| 435 | #endif |
| 436 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 437 | /* XXX There's a problem here: *static* functions are not supposed to have |
| 438 | a Py prefix (or use CapitalizedWords). Later... */ |
| 439 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 440 | /* Global variable holding the exception type for errors detected |
| 441 | by this module (but not argument type or memory errors, etc.). */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 442 | static PyObject *socket_error; |
| 443 | static PyObject *socket_herror; |
| 444 | static PyObject *socket_gaierror; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 445 | static PyObject *socket_timeout; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 446 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 447 | #ifdef RISCOS |
| 448 | /* Global variable which is !=0 if Python is running in a RISC OS taskwindow */ |
| 449 | static int taskwindow; |
| 450 | #endif |
| 451 | |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 452 | /* A forward reference to the socket type object. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 453 | The sock_type variable contains pointers to various functions, |
| 454 | some of which call new_sockobject(), which uses sock_type, so |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 455 | there has to be a circular reference. */ |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 456 | static PyTypeObject sock_type; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 457 | |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 458 | #if defined(HAVE_POLL_H) |
| 459 | #include <poll.h> |
| 460 | #elif defined(HAVE_SYS_POLL_H) |
| 461 | #include <sys/poll.h> |
| 462 | #endif |
| 463 | |
Charles-François Natali | fda7b37 | 2011-08-28 16:22:33 +0200 | [diff] [blame] | 464 | #ifdef HAVE_POLL |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 465 | /* Instead of select(), we'll use poll() since poll() works on any fd. */ |
| 466 | #define IS_SELECTABLE(s) 1 |
| 467 | /* Can we call select() with this socket without a buffer overrun? */ |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 468 | #else |
Charles-François Natali | fda7b37 | 2011-08-28 16:22:33 +0200 | [diff] [blame] | 469 | /* If there's no timeout left, we don't have to call select, so it's a safe, |
| 470 | * little white lie. */ |
| 471 | #define IS_SELECTABLE(s) (_PyIsSelectable_fd((s)->sock_fd) || (s)->sock_timeout <= 0.0) |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 472 | #endif |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 473 | |
| 474 | static PyObject* |
| 475 | select_error(void) |
| 476 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 477 | PyErr_SetString(socket_error, "unable to select on socket"); |
| 478 | return NULL; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 481 | #ifdef MS_WINDOWS |
| 482 | #ifndef WSAEAGAIN |
| 483 | #define WSAEAGAIN WSAEWOULDBLOCK |
| 484 | #endif |
| 485 | #define CHECK_ERRNO(expected) \ |
| 486 | (WSAGetLastError() == WSA ## expected) |
| 487 | #else |
| 488 | #define CHECK_ERRNO(expected) \ |
| 489 | (errno == expected) |
| 490 | #endif |
| 491 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 492 | /* Convenience function to raise an error according to errno |
| 493 | and return a NULL pointer from a function. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 494 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 495 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 496 | set_error(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 497 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 498 | #ifdef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 499 | int err_no = WSAGetLastError(); |
| 500 | /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which |
| 501 | recognizes the error codes used by both GetLastError() and |
| 502 | WSAGetLastError */ |
| 503 | if (err_no) |
| 504 | return PyErr_SetExcFromWindowsErr(socket_error, err_no); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 505 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 506 | |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 507 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 508 | if (sock_errno() != NO_ERROR) { |
| 509 | APIRET rc; |
| 510 | ULONG msglen; |
| 511 | char outbuf[100]; |
| 512 | int myerrorcode = sock_errno(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 513 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 514 | /* Retrieve socket-related error message from MPTN.MSG file */ |
| 515 | rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf), |
| 516 | myerrorcode - SOCBASEERR + 26, |
| 517 | "mptn.msg", |
| 518 | &msglen); |
| 519 | if (rc == NO_ERROR) { |
| 520 | PyObject *v; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 521 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 522 | /* OS/2 doesn't guarantee a terminator */ |
| 523 | outbuf[msglen] = '\0'; |
| 524 | if (strlen(outbuf) > 0) { |
| 525 | /* If non-empty msg, trim CRLF */ |
| 526 | char *lastc = &outbuf[ strlen(outbuf)-1 ]; |
| 527 | while (lastc > outbuf && |
| 528 | isspace(Py_CHARMASK(*lastc))) { |
| 529 | /* Trim trailing whitespace (CRLF) */ |
| 530 | *lastc-- = '\0'; |
| 531 | } |
| 532 | } |
| 533 | v = Py_BuildValue("(is)", myerrorcode, outbuf); |
| 534 | if (v != NULL) { |
| 535 | PyErr_SetObject(socket_error, v); |
| 536 | Py_DECREF(v); |
| 537 | } |
| 538 | return NULL; |
| 539 | } |
| 540 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 541 | #endif |
| 542 | |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 543 | #if defined(RISCOS) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 544 | if (_inet_error.errnum != NULL) { |
| 545 | PyObject *v; |
| 546 | v = Py_BuildValue("(is)", errno, _inet_err()); |
| 547 | if (v != NULL) { |
| 548 | PyErr_SetObject(socket_error, v); |
| 549 | Py_DECREF(v); |
| 550 | } |
| 551 | return NULL; |
| 552 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 553 | #endif |
| 554 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 555 | return PyErr_SetFromErrno(socket_error); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 558 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 559 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 560 | set_herror(int h_error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 561 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 562 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 563 | |
| 564 | #ifdef HAVE_HSTRERROR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 565 | v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 566 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 567 | v = Py_BuildValue("(is)", h_error, "host not found"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 568 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 569 | if (v != NULL) { |
| 570 | PyErr_SetObject(socket_herror, v); |
| 571 | Py_DECREF(v); |
| 572 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 573 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 574 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | |
| 578 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 579 | set_gaierror(int error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 580 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 581 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 582 | |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 583 | #ifdef EAI_SYSTEM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 584 | /* EAI_SYSTEM is not available on Windows XP. */ |
| 585 | if (error == EAI_SYSTEM) |
| 586 | return set_error(); |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 587 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 588 | |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 589 | #ifdef HAVE_GAI_STRERROR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 590 | v = Py_BuildValue("(is)", error, gai_strerror(error)); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 591 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 592 | v = Py_BuildValue("(is)", error, "getaddrinfo failed"); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 593 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 594 | if (v != NULL) { |
| 595 | PyErr_SetObject(socket_gaierror, v); |
| 596 | Py_DECREF(v); |
| 597 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 598 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 599 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 602 | #ifdef __VMS |
| 603 | /* Function to send in segments */ |
| 604 | static int |
| 605 | sendsegmented(int sock_fd, char *buf, int len, int flags) |
| 606 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 607 | int n = 0; |
| 608 | int remaining = len; |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 609 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 610 | while (remaining > 0) { |
| 611 | unsigned int segment; |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 612 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 613 | segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining); |
| 614 | n = send(sock_fd, buf, segment, flags); |
| 615 | if (n < 0) { |
| 616 | return n; |
| 617 | } |
| 618 | remaining -= segment; |
| 619 | buf += segment; |
| 620 | } /* end while */ |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 621 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 622 | return len; |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 623 | } |
| 624 | #endif |
| 625 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 626 | /* Function to perform the setting of socket blocking mode |
| 627 | internally. block = (1 | 0). */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 628 | static int |
| 629 | internal_setblocking(PySocketSockObject *s, int block) |
| 630 | { |
| 631 | #ifndef RISCOS |
| 632 | #ifndef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 633 | int delay_flag; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 634 | #endif |
| 635 | #endif |
| 636 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 637 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 638 | #ifdef __BEOS__ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 639 | block = !block; |
| 640 | setsockopt(s->sock_fd, SOL_SOCKET, SO_NONBLOCK, |
| 641 | (void *)(&block), sizeof(int)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 642 | #else |
| 643 | #ifndef RISCOS |
| 644 | #ifndef MS_WINDOWS |
| 645 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 646 | block = !block; |
| 647 | ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block)); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 648 | #elif defined(__VMS) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 649 | block = !block; |
| 650 | ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 651 | #else /* !PYOS_OS2 && !__VMS */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 652 | delay_flag = fcntl(s->sock_fd, F_GETFL, 0); |
| 653 | if (block) |
| 654 | delay_flag &= (~O_NONBLOCK); |
| 655 | else |
| 656 | delay_flag |= O_NONBLOCK; |
| 657 | fcntl(s->sock_fd, F_SETFL, delay_flag); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 658 | #endif /* !PYOS_OS2 */ |
| 659 | #else /* MS_WINDOWS */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 660 | block = !block; |
| 661 | ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 662 | #endif /* MS_WINDOWS */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 663 | #else /* RISCOS */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 664 | block = !block; |
| 665 | socketioctl(s->sock_fd, FIONBIO, (u_long*)&block); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 666 | #endif /* RISCOS */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 667 | #endif /* __BEOS__ */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 668 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 669 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 670 | /* Since these don't return anything */ |
| 671 | return 1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 674 | /* Do a select()/poll() on the socket, if necessary (sock_timeout > 0). |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 675 | The argument writing indicates the direction. |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 676 | This does not raise an exception; we'll let our caller do that |
| 677 | after they've reacquired the interpreter lock. |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 678 | Returns 1 on timeout, -1 on error, 0 otherwise. */ |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 679 | static int |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 680 | internal_select_ex(PySocketSockObject *s, int writing, double interval) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 681 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 682 | int n; |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 683 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 684 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
| 685 | if (s->sock_timeout <= 0.0) |
| 686 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 687 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 688 | /* Guard against closed socket */ |
| 689 | if (s->sock_fd < 0) |
| 690 | return 0; |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 691 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 692 | /* Handling this condition here simplifies the select loops */ |
| 693 | if (interval < 0.0) |
| 694 | return 1; |
| 695 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 696 | /* Prefer poll, if available, since you can poll() any fd |
| 697 | * which can't be done with select(). */ |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 698 | #ifdef HAVE_POLL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 699 | { |
| 700 | struct pollfd pollfd; |
| 701 | int timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 702 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 703 | pollfd.fd = s->sock_fd; |
| 704 | pollfd.events = writing ? POLLOUT : POLLIN; |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 705 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 706 | /* s->sock_timeout is in seconds, timeout in ms */ |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 707 | timeout = (int)(interval * 1000 + 0.5); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 708 | n = poll(&pollfd, 1, timeout); |
| 709 | } |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 710 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 711 | { |
| 712 | /* Construct the arguments to select */ |
| 713 | fd_set fds; |
| 714 | struct timeval tv; |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 715 | tv.tv_sec = (int)interval; |
| 716 | tv.tv_usec = (int)((interval - tv.tv_sec) * 1e6); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 717 | FD_ZERO(&fds); |
| 718 | FD_SET(s->sock_fd, &fds); |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 719 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 720 | /* See if the socket is ready */ |
| 721 | if (writing) |
| 722 | n = select(s->sock_fd+1, NULL, &fds, NULL, &tv); |
| 723 | else |
| 724 | n = select(s->sock_fd+1, &fds, NULL, NULL, &tv); |
| 725 | } |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 726 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 727 | |
| 728 | if (n < 0) |
| 729 | return -1; |
| 730 | if (n == 0) |
| 731 | return 1; |
| 732 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 735 | static int |
| 736 | internal_select(PySocketSockObject *s, int writing) |
| 737 | { |
| 738 | return internal_select_ex(s, writing, s->sock_timeout); |
| 739 | } |
| 740 | |
| 741 | /* |
| 742 | Two macros for automatic retry of select() in case of false positives |
| 743 | (for example, select() could indicate a socket is ready for reading |
| 744 | but the data then discarded by the OS because of a wrong checksum). |
| 745 | Here is an example of use: |
| 746 | |
| 747 | BEGIN_SELECT_LOOP(s) |
| 748 | Py_BEGIN_ALLOW_THREADS |
| 749 | timeout = internal_select_ex(s, 0, interval); |
| 750 | if (!timeout) |
| 751 | outlen = recv(s->sock_fd, cbuf, len, flags); |
| 752 | Py_END_ALLOW_THREADS |
| 753 | if (timeout == 1) { |
| 754 | PyErr_SetString(socket_timeout, "timed out"); |
| 755 | return -1; |
| 756 | } |
| 757 | END_SELECT_LOOP(s) |
| 758 | */ |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 759 | #define BEGIN_SELECT_LOOP(s) \ |
| 760 | { \ |
| 761 | double deadline, interval = s->sock_timeout; \ |
| 762 | int has_timeout = s->sock_timeout > 0.0; \ |
| 763 | if (has_timeout) { \ |
Kristján Valur Jónsson | 868f0aa | 2013-03-19 13:53:56 -0700 | [diff] [blame] | 764 | deadline = _PyTime_FloatTime() + s->sock_timeout; \ |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 765 | } \ |
| 766 | while (1) { \ |
Kristján Valur Jónsson | 868f0aa | 2013-03-19 13:53:56 -0700 | [diff] [blame] | 767 | errno = 0; |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 768 | |
| 769 | #define END_SELECT_LOOP(s) \ |
| 770 | if (!has_timeout || \ |
| 771 | (!CHECK_ERRNO(EWOULDBLOCK) && !CHECK_ERRNO(EAGAIN))) \ |
| 772 | break; \ |
Kristján Valur Jónsson | 868f0aa | 2013-03-19 13:53:56 -0700 | [diff] [blame] | 773 | interval = deadline - _PyTime_FloatTime(); \ |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 774 | } \ |
Kristján Valur Jónsson | 620e364 | 2013-03-19 13:01:05 -0700 | [diff] [blame] | 775 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 776 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 777 | /* Initialize a new socket object. */ |
| 778 | |
Tim Peters | a12b4cf | 2002-07-18 22:38:44 +0000 | [diff] [blame] | 779 | static double defaulttimeout = -1.0; /* Default timeout for new sockets */ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 780 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 781 | PyMODINIT_FUNC |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 782 | init_sockobject(PySocketSockObject *s, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 783 | SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 784 | { |
| 785 | #ifdef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 786 | int block = 1; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 787 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 788 | s->sock_fd = fd; |
| 789 | s->sock_family = family; |
| 790 | s->sock_type = type; |
| 791 | s->sock_proto = proto; |
| 792 | s->sock_timeout = defaulttimeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 793 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 794 | s->errorhandler = &set_error; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 795 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 796 | if (defaulttimeout >= 0.0) |
| 797 | internal_setblocking(s, 0); |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 798 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 799 | #ifdef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 800 | if (taskwindow) |
| 801 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block); |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 802 | #endif |
| 803 | } |
| 804 | |
| 805 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 806 | /* Create a new socket object. |
| 807 | This just creates the object and initializes it. |
| 808 | If the creation fails, return NULL and set an exception (implicit |
| 809 | in NEWOBJ()). */ |
| 810 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 811 | static PySocketSockObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 812 | new_sockobject(SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 813 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 814 | PySocketSockObject *s; |
| 815 | s = (PySocketSockObject *) |
| 816 | PyType_GenericNew(&sock_type, NULL, NULL); |
| 817 | if (s != NULL) |
| 818 | init_sockobject(s, fd, family, type, proto); |
| 819 | return s; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 822 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 823 | /* Lock to allow python interpreter to continue, but only allow one |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 824 | thread to be in gethostbyname or getaddrinfo */ |
| 825 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Matthias Klose | c551776 | 2012-08-14 17:24:47 +0200 | [diff] [blame] | 826 | static PyThread_type_lock netdb_lock; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 827 | #endif |
| 828 | |
| 829 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 830 | /* Convert a string specifying a host name or one of a few symbolic |
| 831 | names to a numeric IP address. This usually calls gethostbyname() |
| 832 | to do the work; the names "" and "<broadcast>" are special. |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 833 | Return the length (IPv4 should be 4 bytes), or negative if |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 834 | an error occurred; then an exception is raised. */ |
| 835 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 836 | static int |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 837 | setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 838 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 839 | struct addrinfo hints, *res; |
| 840 | int error; |
| 841 | int d1, d2, d3, d4; |
| 842 | char ch; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 843 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 844 | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
| 845 | if (name[0] == '\0') { |
| 846 | int siz; |
| 847 | memset(&hints, 0, sizeof(hints)); |
| 848 | hints.ai_family = af; |
| 849 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
| 850 | hints.ai_flags = AI_PASSIVE; |
| 851 | Py_BEGIN_ALLOW_THREADS |
| 852 | ACQUIRE_GETADDRINFO_LOCK |
| 853 | error = getaddrinfo(NULL, "0", &hints, &res); |
| 854 | Py_END_ALLOW_THREADS |
| 855 | /* We assume that those thread-unsafe getaddrinfo() versions |
| 856 | *are* safe regarding their return value, ie. that a |
| 857 | subsequent call to getaddrinfo() does not destroy the |
| 858 | outcome of the first call. */ |
| 859 | RELEASE_GETADDRINFO_LOCK |
| 860 | if (error) { |
| 861 | set_gaierror(error); |
| 862 | return -1; |
| 863 | } |
| 864 | switch (res->ai_family) { |
| 865 | case AF_INET: |
| 866 | siz = 4; |
| 867 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 868 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 869 | case AF_INET6: |
| 870 | siz = 16; |
| 871 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 872 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 873 | default: |
| 874 | freeaddrinfo(res); |
| 875 | PyErr_SetString(socket_error, |
| 876 | "unsupported address family"); |
| 877 | return -1; |
| 878 | } |
| 879 | if (res->ai_next) { |
| 880 | freeaddrinfo(res); |
| 881 | PyErr_SetString(socket_error, |
| 882 | "wildcard resolved to multiple address"); |
| 883 | return -1; |
| 884 | } |
| 885 | if (res->ai_addrlen < addr_ret_size) |
| 886 | addr_ret_size = res->ai_addrlen; |
| 887 | memcpy(addr_ret, res->ai_addr, addr_ret_size); |
| 888 | freeaddrinfo(res); |
| 889 | return siz; |
| 890 | } |
| 891 | if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) { |
| 892 | struct sockaddr_in *sin; |
| 893 | if (af != AF_INET && af != AF_UNSPEC) { |
| 894 | PyErr_SetString(socket_error, |
| 895 | "address family mismatched"); |
| 896 | return -1; |
| 897 | } |
| 898 | sin = (struct sockaddr_in *)addr_ret; |
| 899 | memset((void *) sin, '\0', sizeof(*sin)); |
| 900 | sin->sin_family = AF_INET; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 901 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 902 | sin->sin_len = sizeof(*sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 903 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 904 | sin->sin_addr.s_addr = INADDR_BROADCAST; |
| 905 | return sizeof(sin->sin_addr); |
| 906 | } |
| 907 | if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 && |
| 908 | 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 && |
| 909 | 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) { |
| 910 | struct sockaddr_in *sin; |
| 911 | sin = (struct sockaddr_in *)addr_ret; |
| 912 | sin->sin_addr.s_addr = htonl( |
| 913 | ((long) d1 << 24) | ((long) d2 << 16) | |
| 914 | ((long) d3 << 8) | ((long) d4 << 0)); |
| 915 | sin->sin_family = AF_INET; |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 916 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 917 | sin->sin_len = sizeof(*sin); |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 918 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 919 | return 4; |
| 920 | } |
| 921 | memset(&hints, 0, sizeof(hints)); |
| 922 | hints.ai_family = af; |
| 923 | Py_BEGIN_ALLOW_THREADS |
| 924 | ACQUIRE_GETADDRINFO_LOCK |
| 925 | error = getaddrinfo(name, NULL, &hints, &res); |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 926 | #if defined(__digital__) && defined(__unix__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 927 | if (error == EAI_NONAME && af == AF_UNSPEC) { |
| 928 | /* On Tru64 V5.1, numeric-to-addr conversion fails |
| 929 | if no address family is given. Assume IPv4 for now.*/ |
| 930 | hints.ai_family = AF_INET; |
| 931 | error = getaddrinfo(name, NULL, &hints, &res); |
| 932 | } |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 933 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 934 | Py_END_ALLOW_THREADS |
| 935 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 936 | if (error) { |
| 937 | set_gaierror(error); |
| 938 | return -1; |
| 939 | } |
| 940 | if (res->ai_addrlen < addr_ret_size) |
| 941 | addr_ret_size = res->ai_addrlen; |
| 942 | memcpy((char *) addr_ret, res->ai_addr, addr_ret_size); |
| 943 | freeaddrinfo(res); |
| 944 | switch (addr_ret->sa_family) { |
| 945 | case AF_INET: |
| 946 | return 4; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 947 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 948 | case AF_INET6: |
| 949 | return 16; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 950 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 951 | default: |
| 952 | PyErr_SetString(socket_error, "unknown address family"); |
| 953 | return -1; |
| 954 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 957 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 958 | /* Create a string object representing an IP address. |
| 959 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 960 | size numbers). */ |
| 961 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 962 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 963 | makeipaddr(struct sockaddr *addr, int addrlen) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 964 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 965 | char buf[NI_MAXHOST]; |
| 966 | int error; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 967 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 968 | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, |
| 969 | NI_NUMERICHOST); |
| 970 | if (error) { |
| 971 | set_gaierror(error); |
| 972 | return NULL; |
| 973 | } |
| 974 | return PyString_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 978 | #ifdef USE_BLUETOOTH |
| 979 | /* Convert a string representation of a Bluetooth address into a numeric |
| 980 | address. Returns the length (6), or raises an exception and returns -1 if |
| 981 | an error occurred. */ |
| 982 | |
| 983 | static int |
| 984 | setbdaddr(char *name, bdaddr_t *bdaddr) |
| 985 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 986 | unsigned int b0, b1, b2, b3, b4, b5; |
| 987 | char ch; |
| 988 | int n; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 989 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 990 | n = sscanf(name, "%X:%X:%X:%X:%X:%X%c", |
| 991 | &b5, &b4, &b3, &b2, &b1, &b0, &ch); |
| 992 | if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) { |
| 993 | bdaddr->b[0] = b0; |
| 994 | bdaddr->b[1] = b1; |
| 995 | bdaddr->b[2] = b2; |
| 996 | bdaddr->b[3] = b3; |
| 997 | bdaddr->b[4] = b4; |
| 998 | bdaddr->b[5] = b5; |
| 999 | return 6; |
| 1000 | } else { |
| 1001 | PyErr_SetString(socket_error, "bad bluetooth address"); |
| 1002 | return -1; |
| 1003 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | /* Create a string representation of the Bluetooth address. This is always a |
| 1007 | string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal |
| 1008 | value (zero padded if necessary). */ |
| 1009 | |
| 1010 | static PyObject * |
| 1011 | makebdaddr(bdaddr_t *bdaddr) |
| 1012 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1013 | char buf[(6 * 2) + 5 + 1]; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1014 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1015 | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 1016 | bdaddr->b[5], bdaddr->b[4], bdaddr->b[3], |
| 1017 | bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]); |
| 1018 | return PyString_FromString(buf); |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1019 | } |
| 1020 | #endif |
| 1021 | |
| 1022 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1023 | /* Create an object representing the given socket address, |
| 1024 | suitable for passing it back to bind(), connect() etc. |
| 1025 | The family field of the sockaddr structure is inspected |
| 1026 | to determine what kind of address it really is. */ |
| 1027 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1028 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1029 | static PyObject * |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1030 | makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1031 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1032 | if (addrlen == 0) { |
| 1033 | /* No address -- may be recvfrom() from known socket */ |
| 1034 | Py_INCREF(Py_None); |
| 1035 | return Py_None; |
| 1036 | } |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1037 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1038 | #ifdef __BEOS__ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1039 | /* XXX: BeOS version of accept() doesn't set family correctly */ |
| 1040 | addr->sa_family = AF_INET; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1041 | #endif |
| 1042 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1043 | switch (addr->sa_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1044 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1045 | case AF_INET: |
| 1046 | { |
| 1047 | struct sockaddr_in *a; |
| 1048 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1049 | PyObject *ret = NULL; |
| 1050 | if (addrobj) { |
| 1051 | a = (struct sockaddr_in *)addr; |
| 1052 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 1053 | Py_DECREF(addrobj); |
| 1054 | } |
| 1055 | return ret; |
| 1056 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1057 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1058 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1059 | case AF_UNIX: |
| 1060 | { |
| 1061 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1062 | #ifdef linux |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1063 | if (a->sun_path[0] == 0) { /* Linux abstract namespace */ |
| 1064 | addrlen -= offsetof(struct sockaddr_un, sun_path); |
| 1065 | return PyString_FromStringAndSize(a->sun_path, |
| 1066 | addrlen); |
| 1067 | } |
| 1068 | else |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1069 | #endif /* linux */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1070 | { |
| 1071 | /* regular NULL-terminated string */ |
| 1072 | return PyString_FromString(a->sun_path); |
| 1073 | } |
| 1074 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1075 | #endif /* AF_UNIX */ |
| 1076 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1077 | #if defined(AF_NETLINK) |
| 1078 | case AF_NETLINK: |
| 1079 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1080 | struct sockaddr_nl *a = (struct sockaddr_nl *) addr; |
| 1081 | return Py_BuildValue("II", a->nl_pid, a->nl_groups); |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1082 | } |
| 1083 | #endif /* AF_NETLINK */ |
| 1084 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1085 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1086 | case AF_INET6: |
| 1087 | { |
| 1088 | struct sockaddr_in6 *a; |
| 1089 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1090 | PyObject *ret = NULL; |
| 1091 | if (addrobj) { |
| 1092 | a = (struct sockaddr_in6 *)addr; |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1093 | ret = Py_BuildValue("OiII", |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1094 | addrobj, |
| 1095 | ntohs(a->sin6_port), |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1096 | ntohl(a->sin6_flowinfo), |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1097 | a->sin6_scope_id); |
| 1098 | Py_DECREF(addrobj); |
| 1099 | } |
| 1100 | return ret; |
| 1101 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1102 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1103 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1104 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1105 | case AF_BLUETOOTH: |
| 1106 | switch (proto) { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1107 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1108 | case BTPROTO_L2CAP: |
| 1109 | { |
| 1110 | struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr; |
| 1111 | PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr)); |
| 1112 | PyObject *ret = NULL; |
| 1113 | if (addrobj) { |
| 1114 | ret = Py_BuildValue("Oi", |
| 1115 | addrobj, |
| 1116 | _BT_L2_MEMB(a, psm)); |
| 1117 | Py_DECREF(addrobj); |
| 1118 | } |
| 1119 | return ret; |
| 1120 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1121 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1122 | case BTPROTO_RFCOMM: |
| 1123 | { |
| 1124 | struct sockaddr_rc *a = (struct sockaddr_rc *) addr; |
| 1125 | PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr)); |
| 1126 | PyObject *ret = NULL; |
| 1127 | if (addrobj) { |
| 1128 | ret = Py_BuildValue("Oi", |
| 1129 | addrobj, |
| 1130 | _BT_RC_MEMB(a, channel)); |
| 1131 | Py_DECREF(addrobj); |
| 1132 | } |
| 1133 | return ret; |
| 1134 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1135 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1136 | case BTPROTO_HCI: |
| 1137 | { |
| 1138 | struct sockaddr_hci *a = (struct sockaddr_hci *) addr; |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 1139 | #if defined(__NetBSD__) || defined(__DragonFly__) |
| 1140 | return makebdaddr(&_BT_HCI_MEMB(a, bdaddr)); |
| 1141 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1142 | PyObject *ret = NULL; |
| 1143 | ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev)); |
| 1144 | return ret; |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 1145 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1146 | } |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 1147 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1148 | #if !defined(__FreeBSD__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1149 | case BTPROTO_SCO: |
| 1150 | { |
| 1151 | struct sockaddr_sco *a = (struct sockaddr_sco *) addr; |
| 1152 | return makebdaddr(&_BT_SCO_MEMB(a, bdaddr)); |
| 1153 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1154 | #endif |
| 1155 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1156 | default: |
| 1157 | PyErr_SetString(PyExc_ValueError, |
| 1158 | "Unknown Bluetooth protocol"); |
| 1159 | return NULL; |
| 1160 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1161 | #endif |
| 1162 | |
Antoine Pitrou | e0c5f3c | 2010-10-27 20:35:26 +0000 | [diff] [blame] | 1163 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1164 | case AF_PACKET: |
| 1165 | { |
| 1166 | struct sockaddr_ll *a = (struct sockaddr_ll *)addr; |
| 1167 | char *ifname = ""; |
| 1168 | struct ifreq ifr; |
| 1169 | /* need to look up interface name give index */ |
| 1170 | if (a->sll_ifindex) { |
| 1171 | ifr.ifr_ifindex = a->sll_ifindex; |
| 1172 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
| 1173 | ifname = ifr.ifr_name; |
| 1174 | } |
| 1175 | return Py_BuildValue("shbhs#", |
| 1176 | ifname, |
| 1177 | ntohs(a->sll_protocol), |
| 1178 | a->sll_pkttype, |
| 1179 | a->sll_hatype, |
| 1180 | a->sll_addr, |
| 1181 | a->sll_halen); |
| 1182 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1183 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1184 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1185 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1186 | case AF_TIPC: |
| 1187 | { |
| 1188 | struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr; |
| 1189 | if (a->addrtype == TIPC_ADDR_NAMESEQ) { |
| 1190 | return Py_BuildValue("IIIII", |
| 1191 | a->addrtype, |
| 1192 | a->addr.nameseq.type, |
| 1193 | a->addr.nameseq.lower, |
| 1194 | a->addr.nameseq.upper, |
| 1195 | a->scope); |
| 1196 | } else if (a->addrtype == TIPC_ADDR_NAME) { |
| 1197 | return Py_BuildValue("IIIII", |
| 1198 | a->addrtype, |
| 1199 | a->addr.name.name.type, |
| 1200 | a->addr.name.name.instance, |
| 1201 | a->addr.name.name.instance, |
| 1202 | a->scope); |
| 1203 | } else if (a->addrtype == TIPC_ADDR_ID) { |
| 1204 | return Py_BuildValue("IIIII", |
| 1205 | a->addrtype, |
| 1206 | a->addr.id.node, |
| 1207 | a->addr.id.ref, |
| 1208 | 0, |
| 1209 | a->scope); |
| 1210 | } else { |
| 1211 | PyErr_SetString(PyExc_ValueError, |
| 1212 | "Invalid address type"); |
| 1213 | return NULL; |
| 1214 | } |
| 1215 | } |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1216 | #endif |
| 1217 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1218 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1219 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1220 | default: |
| 1221 | /* If we don't know the address family, don't raise an |
| 1222 | exception -- return it as a tuple. */ |
| 1223 | return Py_BuildValue("is#", |
| 1224 | addr->sa_family, |
| 1225 | addr->sa_data, |
| 1226 | sizeof(addr->sa_data)); |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1227 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1228 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1231 | |
| 1232 | /* Parse a socket address argument according to the socket object's |
| 1233 | address family. Return 1 if the address was in the proper format, |
| 1234 | 0 of not. The address is returned through addr_ret, its length |
| 1235 | through len_ret. */ |
| 1236 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1237 | static int |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1238 | getsockaddrarg(PySocketSockObject *s, PyObject *args, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1239 | struct sockaddr *addr_ret, int *len_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1240 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1241 | switch (s->sock_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1242 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1243 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1244 | case AF_UNIX: |
| 1245 | { |
| 1246 | struct sockaddr_un* addr; |
| 1247 | char *path; |
| 1248 | int len; |
| 1249 | if (!PyArg_Parse(args, "t#", &path, &len)) |
| 1250 | return 0; |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1251 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1252 | addr = (struct sockaddr_un*)addr_ret; |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1253 | #ifdef linux |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1254 | if (len > 0 && path[0] == 0) { |
| 1255 | /* Linux abstract namespace extension */ |
| 1256 | if (len > sizeof addr->sun_path) { |
| 1257 | PyErr_SetString(socket_error, |
| 1258 | "AF_UNIX path too long"); |
| 1259 | return 0; |
| 1260 | } |
| 1261 | } |
| 1262 | else |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1263 | #endif /* linux */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1264 | { |
| 1265 | /* regular NULL-terminated string */ |
| 1266 | if (len >= sizeof addr->sun_path) { |
| 1267 | PyErr_SetString(socket_error, |
| 1268 | "AF_UNIX path too long"); |
| 1269 | return 0; |
| 1270 | } |
| 1271 | addr->sun_path[len] = 0; |
| 1272 | } |
| 1273 | addr->sun_family = s->sock_family; |
| 1274 | memcpy(addr->sun_path, path, len); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1275 | #if defined(PYOS_OS2) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1276 | *len_ret = sizeof(*addr); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1277 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1278 | *len_ret = len + offsetof(struct sockaddr_un, sun_path); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1279 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1280 | return 1; |
| 1281 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1282 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1283 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1284 | #if defined(AF_NETLINK) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1285 | case AF_NETLINK: |
| 1286 | { |
| 1287 | struct sockaddr_nl* addr; |
| 1288 | int pid, groups; |
| 1289 | addr = (struct sockaddr_nl *)addr_ret; |
| 1290 | if (!PyTuple_Check(args)) { |
| 1291 | PyErr_Format( |
| 1292 | PyExc_TypeError, |
| 1293 | "getsockaddrarg: " |
| 1294 | "AF_NETLINK address must be tuple, not %.500s", |
| 1295 | Py_TYPE(args)->tp_name); |
| 1296 | return 0; |
| 1297 | } |
| 1298 | if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups)) |
| 1299 | return 0; |
| 1300 | addr->nl_family = AF_NETLINK; |
| 1301 | addr->nl_pid = pid; |
| 1302 | addr->nl_groups = groups; |
| 1303 | *len_ret = sizeof(*addr); |
| 1304 | return 1; |
| 1305 | } |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1306 | #endif |
| 1307 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1308 | case AF_INET: |
| 1309 | { |
| 1310 | struct sockaddr_in* addr; |
| 1311 | char *host; |
| 1312 | int port, result; |
| 1313 | if (!PyTuple_Check(args)) { |
| 1314 | PyErr_Format( |
| 1315 | PyExc_TypeError, |
| 1316 | "getsockaddrarg: " |
| 1317 | "AF_INET address must be tuple, not %.500s", |
| 1318 | Py_TYPE(args)->tp_name); |
| 1319 | return 0; |
| 1320 | } |
| 1321 | if (!PyArg_ParseTuple(args, "eti:getsockaddrarg", |
| 1322 | "idna", &host, &port)) |
| 1323 | return 0; |
| 1324 | addr=(struct sockaddr_in*)addr_ret; |
| 1325 | result = setipaddr(host, (struct sockaddr *)addr, |
| 1326 | sizeof(*addr), AF_INET); |
| 1327 | PyMem_Free(host); |
| 1328 | if (result < 0) |
| 1329 | return 0; |
| 1330 | if (port < 0 || port > 0xffff) { |
| 1331 | PyErr_SetString( |
| 1332 | PyExc_OverflowError, |
| 1333 | "getsockaddrarg: port must be 0-65535."); |
| 1334 | return 0; |
| 1335 | } |
| 1336 | addr->sin_family = AF_INET; |
| 1337 | addr->sin_port = htons((short)port); |
| 1338 | *len_ret = sizeof *addr; |
| 1339 | return 1; |
| 1340 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1341 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1342 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1343 | case AF_INET6: |
| 1344 | { |
| 1345 | struct sockaddr_in6* addr; |
| 1346 | char *host; |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1347 | int port, result; |
| 1348 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1349 | flowinfo = scope_id = 0; |
| 1350 | if (!PyTuple_Check(args)) { |
| 1351 | PyErr_Format( |
| 1352 | PyExc_TypeError, |
| 1353 | "getsockaddrarg: " |
| 1354 | "AF_INET6 address must be tuple, not %.500s", |
| 1355 | Py_TYPE(args)->tp_name); |
| 1356 | return 0; |
| 1357 | } |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1358 | if (!PyArg_ParseTuple(args, "eti|II", |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1359 | "idna", &host, &port, &flowinfo, |
| 1360 | &scope_id)) { |
| 1361 | return 0; |
| 1362 | } |
| 1363 | addr = (struct sockaddr_in6*)addr_ret; |
| 1364 | result = setipaddr(host, (struct sockaddr *)addr, |
| 1365 | sizeof(*addr), AF_INET6); |
| 1366 | PyMem_Free(host); |
| 1367 | if (result < 0) |
| 1368 | return 0; |
| 1369 | if (port < 0 || port > 0xffff) { |
| 1370 | PyErr_SetString( |
| 1371 | PyExc_OverflowError, |
| 1372 | "getsockaddrarg: port must be 0-65535."); |
| 1373 | return 0; |
| 1374 | } |
Charles-François Natali | 65dd745 | 2012-06-23 10:06:56 +0200 | [diff] [blame] | 1375 | if (flowinfo > 0xfffff) { |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1376 | PyErr_SetString( |
| 1377 | PyExc_OverflowError, |
| 1378 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 1379 | return 0; |
| 1380 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1381 | addr->sin6_family = s->sock_family; |
| 1382 | addr->sin6_port = htons((short)port); |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1383 | addr->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1384 | addr->sin6_scope_id = scope_id; |
| 1385 | *len_ret = sizeof *addr; |
| 1386 | return 1; |
| 1387 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1388 | #endif |
| 1389 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1390 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1391 | case AF_BLUETOOTH: |
| 1392 | { |
| 1393 | switch (s->sock_proto) { |
| 1394 | case BTPROTO_L2CAP: |
| 1395 | { |
| 1396 | struct sockaddr_l2 *addr; |
| 1397 | char *straddr; |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1398 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1399 | addr = (struct sockaddr_l2 *)addr_ret; |
| 1400 | memset(addr, 0, sizeof(struct sockaddr_l2)); |
| 1401 | _BT_L2_MEMB(addr, family) = AF_BLUETOOTH; |
| 1402 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1403 | &_BT_L2_MEMB(addr, psm))) { |
| 1404 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1405 | "wrong format"); |
| 1406 | return 0; |
| 1407 | } |
| 1408 | if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0) |
| 1409 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1410 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1411 | *len_ret = sizeof *addr; |
| 1412 | return 1; |
| 1413 | } |
| 1414 | case BTPROTO_RFCOMM: |
| 1415 | { |
| 1416 | struct sockaddr_rc *addr; |
| 1417 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1418 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1419 | addr = (struct sockaddr_rc *)addr_ret; |
| 1420 | _BT_RC_MEMB(addr, family) = AF_BLUETOOTH; |
| 1421 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1422 | &_BT_RC_MEMB(addr, channel))) { |
| 1423 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1424 | "wrong format"); |
| 1425 | return 0; |
| 1426 | } |
| 1427 | if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0) |
| 1428 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1429 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1430 | *len_ret = sizeof *addr; |
| 1431 | return 1; |
| 1432 | } |
| 1433 | case BTPROTO_HCI: |
| 1434 | { |
| 1435 | struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret; |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 1436 | #if defined(__NetBSD__) || defined(__DragonFly__) |
| 1437 | char *straddr = PyBytes_AS_STRING(args); |
| 1438 | |
| 1439 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
| 1440 | if (straddr == NULL) { |
| 1441 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1442 | "wrong format"); |
| 1443 | return 0; |
| 1444 | } |
| 1445 | if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0) |
| 1446 | return 0; |
| 1447 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1448 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
| 1449 | if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) { |
| 1450 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1451 | "wrong format"); |
| 1452 | return 0; |
| 1453 | } |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 1454 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1455 | *len_ret = sizeof *addr; |
| 1456 | return 1; |
| 1457 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1458 | #if !defined(__FreeBSD__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1459 | case BTPROTO_SCO: |
| 1460 | { |
| 1461 | struct sockaddr_sco *addr; |
| 1462 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1463 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1464 | addr = (struct sockaddr_sco *)addr_ret; |
| 1465 | _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; |
| 1466 | straddr = PyString_AsString(args); |
| 1467 | if (straddr == NULL) { |
| 1468 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1469 | "wrong format"); |
| 1470 | return 0; |
| 1471 | } |
| 1472 | if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0) |
| 1473 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1474 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1475 | *len_ret = sizeof *addr; |
| 1476 | return 1; |
| 1477 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1478 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1479 | default: |
| 1480 | PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol"); |
| 1481 | return 0; |
| 1482 | } |
| 1483 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1484 | #endif |
| 1485 | |
Antoine Pitrou | e0c5f3c | 2010-10-27 20:35:26 +0000 | [diff] [blame] | 1486 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1487 | case AF_PACKET: |
| 1488 | { |
| 1489 | struct sockaddr_ll* addr; |
| 1490 | struct ifreq ifr; |
| 1491 | char *interfaceName; |
| 1492 | int protoNumber; |
| 1493 | int hatype = 0; |
| 1494 | int pkttype = 0; |
| 1495 | char *haddr = NULL; |
| 1496 | unsigned int halen = 0; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1497 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1498 | if (!PyTuple_Check(args)) { |
| 1499 | PyErr_Format( |
| 1500 | PyExc_TypeError, |
| 1501 | "getsockaddrarg: " |
| 1502 | "AF_PACKET address must be tuple, not %.500s", |
| 1503 | Py_TYPE(args)->tp_name); |
| 1504 | return 0; |
| 1505 | } |
| 1506 | if (!PyArg_ParseTuple(args, "si|iis#", &interfaceName, |
| 1507 | &protoNumber, &pkttype, &hatype, |
| 1508 | &haddr, &halen)) |
| 1509 | return 0; |
| 1510 | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name)); |
| 1511 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
| 1512 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
| 1513 | s->errorhandler(); |
| 1514 | return 0; |
| 1515 | } |
| 1516 | if (halen > 8) { |
| 1517 | PyErr_SetString(PyExc_ValueError, |
| 1518 | "Hardware address must be 8 bytes or less"); |
| 1519 | return 0; |
| 1520 | } |
| 1521 | if (protoNumber < 0 || protoNumber > 0xffff) { |
| 1522 | PyErr_SetString( |
| 1523 | PyExc_OverflowError, |
| 1524 | "getsockaddrarg: protoNumber must be 0-65535."); |
| 1525 | return 0; |
| 1526 | } |
| 1527 | addr = (struct sockaddr_ll*)addr_ret; |
| 1528 | addr->sll_family = AF_PACKET; |
| 1529 | addr->sll_protocol = htons((short)protoNumber); |
| 1530 | addr->sll_ifindex = ifr.ifr_ifindex; |
| 1531 | addr->sll_pkttype = pkttype; |
| 1532 | addr->sll_hatype = hatype; |
| 1533 | if (halen != 0) { |
| 1534 | memcpy(&addr->sll_addr, haddr, halen); |
| 1535 | } |
| 1536 | addr->sll_halen = halen; |
| 1537 | *len_ret = sizeof *addr; |
| 1538 | return 1; |
| 1539 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1540 | #endif |
| 1541 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1542 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1543 | case AF_TIPC: |
| 1544 | { |
| 1545 | unsigned int atype, v1, v2, v3; |
| 1546 | unsigned int scope = TIPC_CLUSTER_SCOPE; |
| 1547 | struct sockaddr_tipc *addr; |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1548 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1549 | if (!PyTuple_Check(args)) { |
| 1550 | PyErr_Format( |
| 1551 | PyExc_TypeError, |
| 1552 | "getsockaddrarg: " |
| 1553 | "AF_TIPC address must be tuple, not %.500s", |
| 1554 | Py_TYPE(args)->tp_name); |
| 1555 | return 0; |
| 1556 | } |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1557 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1558 | if (!PyArg_ParseTuple(args, |
| 1559 | "IIII|I;Invalid TIPC address format", |
| 1560 | &atype, &v1, &v2, &v3, &scope)) |
| 1561 | return 0; |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1562 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1563 | addr = (struct sockaddr_tipc *) addr_ret; |
| 1564 | memset(addr, 0, sizeof(struct sockaddr_tipc)); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1565 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1566 | addr->family = AF_TIPC; |
| 1567 | addr->scope = scope; |
| 1568 | addr->addrtype = atype; |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1569 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1570 | if (atype == TIPC_ADDR_NAMESEQ) { |
| 1571 | addr->addr.nameseq.type = v1; |
| 1572 | addr->addr.nameseq.lower = v2; |
| 1573 | addr->addr.nameseq.upper = v3; |
| 1574 | } else if (atype == TIPC_ADDR_NAME) { |
| 1575 | addr->addr.name.name.type = v1; |
| 1576 | addr->addr.name.name.instance = v2; |
| 1577 | } else if (atype == TIPC_ADDR_ID) { |
| 1578 | addr->addr.id.node = v1; |
| 1579 | addr->addr.id.ref = v2; |
| 1580 | } else { |
| 1581 | /* Shouldn't happen */ |
| 1582 | PyErr_SetString(PyExc_TypeError, "Invalid address type"); |
| 1583 | return 0; |
| 1584 | } |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1585 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1586 | *len_ret = sizeof(*addr); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1587 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1588 | return 1; |
| 1589 | } |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1590 | #endif |
| 1591 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1592 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1593 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1594 | default: |
| 1595 | PyErr_SetString(socket_error, "getsockaddrarg: bad family"); |
| 1596 | return 0; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1597 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1598 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1601 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1602 | /* 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] | 1603 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 1604 | through len_ret. */ |
| 1605 | |
| 1606 | static int |
Peter Schneider-Kamp | 618e25d | 2000-07-11 23:00:12 +0000 | [diff] [blame] | 1607 | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1608 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1609 | switch (s->sock_family) { |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1610 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1611 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1612 | case AF_UNIX: |
| 1613 | { |
| 1614 | *len_ret = sizeof (struct sockaddr_un); |
| 1615 | return 1; |
| 1616 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1617 | #endif /* AF_UNIX */ |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1618 | #if defined(AF_NETLINK) |
| 1619 | case AF_NETLINK: |
| 1620 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1621 | *len_ret = sizeof (struct sockaddr_nl); |
| 1622 | return 1; |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1623 | } |
| 1624 | #endif |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1625 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1626 | case AF_INET: |
| 1627 | { |
| 1628 | *len_ret = sizeof (struct sockaddr_in); |
| 1629 | return 1; |
| 1630 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1631 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1632 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1633 | case AF_INET6: |
| 1634 | { |
| 1635 | *len_ret = sizeof (struct sockaddr_in6); |
| 1636 | return 1; |
| 1637 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1638 | #endif |
| 1639 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1640 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1641 | case AF_BLUETOOTH: |
| 1642 | { |
| 1643 | switch(s->sock_proto) |
| 1644 | { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1645 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1646 | case BTPROTO_L2CAP: |
| 1647 | *len_ret = sizeof (struct sockaddr_l2); |
| 1648 | return 1; |
| 1649 | case BTPROTO_RFCOMM: |
| 1650 | *len_ret = sizeof (struct sockaddr_rc); |
| 1651 | return 1; |
| 1652 | case BTPROTO_HCI: |
| 1653 | *len_ret = sizeof (struct sockaddr_hci); |
| 1654 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1655 | #if !defined(__FreeBSD__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1656 | case BTPROTO_SCO: |
| 1657 | *len_ret = sizeof (struct sockaddr_sco); |
| 1658 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1659 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1660 | default: |
| 1661 | PyErr_SetString(socket_error, "getsockaddrlen: " |
| 1662 | "unknown BT protocol"); |
| 1663 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1664 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1665 | } |
| 1666 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1667 | #endif |
| 1668 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1669 | #ifdef HAVE_NETPACKET_PACKET_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1670 | case AF_PACKET: |
| 1671 | { |
| 1672 | *len_ret = sizeof (struct sockaddr_ll); |
| 1673 | return 1; |
| 1674 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1675 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1676 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1677 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1678 | case AF_TIPC: |
| 1679 | { |
| 1680 | *len_ret = sizeof (struct sockaddr_tipc); |
| 1681 | return 1; |
| 1682 | } |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1683 | #endif |
| 1684 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1685 | /* More cases here... */ |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1686 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1687 | default: |
| 1688 | PyErr_SetString(socket_error, "getsockaddrlen: bad family"); |
| 1689 | return 0; |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1690 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1691 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1695 | /* s.accept() method */ |
| 1696 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1697 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1698 | sock_accept(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1699 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1700 | sock_addr_t addrbuf; |
| 1701 | SOCKET_T newfd; |
| 1702 | socklen_t addrlen; |
| 1703 | PyObject *sock = NULL; |
| 1704 | PyObject *addr = NULL; |
| 1705 | PyObject *res = NULL; |
| 1706 | int timeout; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1707 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1708 | if (!getsockaddrlen(s, &addrlen)) |
| 1709 | return NULL; |
| 1710 | memset(&addrbuf, 0, addrlen); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1711 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1712 | newfd = INVALID_SOCKET; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1713 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1714 | if (!IS_SELECTABLE(s)) |
| 1715 | return select_error(); |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 1716 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 1717 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1718 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 1719 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1720 | if (!timeout) |
| 1721 | newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 1722 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1723 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1724 | if (timeout == 1) { |
| 1725 | PyErr_SetString(socket_timeout, "timed out"); |
| 1726 | return NULL; |
| 1727 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 1728 | END_SELECT_LOOP(s) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1729 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1730 | if (newfd == INVALID_SOCKET) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1731 | return s->errorhandler(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1732 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1733 | /* Create the new object with unspecified family, |
| 1734 | to avoid calls to bind() etc. on it. */ |
| 1735 | sock = (PyObject *) new_sockobject(newfd, |
| 1736 | s->sock_family, |
| 1737 | s->sock_type, |
| 1738 | s->sock_proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1739 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1740 | if (sock == NULL) { |
| 1741 | SOCKETCLOSE(newfd); |
| 1742 | goto finally; |
| 1743 | } |
| 1744 | addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 1745 | addrlen, s->sock_proto); |
| 1746 | if (addr == NULL) |
| 1747 | goto finally; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1748 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1749 | res = PyTuple_Pack(2, sock, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1750 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1751 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1752 | Py_XDECREF(sock); |
| 1753 | Py_XDECREF(addr); |
| 1754 | return res; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1757 | PyDoc_STRVAR(accept_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1758 | "accept() -> (socket object, address info)\n\ |
| 1759 | \n\ |
| 1760 | Wait for an incoming connection. Return a new socket representing the\n\ |
| 1761 | connection, and the address of the client. For IP sockets, the address\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1762 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1763 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1764 | /* s.setblocking(flag) method. Argument: |
| 1765 | False -- non-blocking mode; same as settimeout(0) |
| 1766 | True -- blocking mode; same as settimeout(None) |
| 1767 | */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1768 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1769 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1770 | sock_setblocking(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1771 | { |
Serhiy Storchaka | 74f49ab | 2013-01-19 12:55:39 +0200 | [diff] [blame] | 1772 | long block; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1773 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1774 | block = PyInt_AsLong(arg); |
| 1775 | if (block == -1 && PyErr_Occurred()) |
| 1776 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1777 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1778 | s->sock_timeout = block ? -1.0 : 0.0; |
| 1779 | internal_setblocking(s, block); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1780 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1781 | Py_INCREF(Py_None); |
| 1782 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1783 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1784 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1785 | PyDoc_STRVAR(setblocking_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1786 | "setblocking(flag)\n\ |
| 1787 | \n\ |
| 1788 | Set the socket to blocking (flag is true) or non-blocking (false).\n\ |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1789 | setblocking(True) is equivalent to settimeout(None);\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1790 | setblocking(False) is equivalent to settimeout(0.0)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1791 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1792 | /* s.settimeout(timeout) method. Argument: |
| 1793 | None -- no timeout, blocking mode; same as setblocking(True) |
| 1794 | 0.0 -- non-blocking mode; same as setblocking(False) |
| 1795 | > 0 -- timeout mode; operations time out after timeout seconds |
| 1796 | < 0 -- illegal; raises an exception |
| 1797 | */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1798 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1799 | sock_settimeout(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1800 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1801 | double timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1802 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1803 | if (arg == Py_None) |
| 1804 | timeout = -1.0; |
| 1805 | else { |
| 1806 | timeout = PyFloat_AsDouble(arg); |
| 1807 | if (timeout < 0.0) { |
| 1808 | if (!PyErr_Occurred()) |
| 1809 | PyErr_SetString(PyExc_ValueError, |
| 1810 | "Timeout value out of range"); |
| 1811 | return NULL; |
| 1812 | } |
| 1813 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1814 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1815 | s->sock_timeout = timeout; |
| 1816 | internal_setblocking(s, timeout < 0.0); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1817 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1818 | Py_INCREF(Py_None); |
| 1819 | return Py_None; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1822 | PyDoc_STRVAR(settimeout_doc, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1823 | "settimeout(timeout)\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1824 | \n\ |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1825 | Set a timeout on socket operations. 'timeout' can be a float,\n\ |
| 1826 | giving in seconds, or None. Setting a timeout of None disables\n\ |
| 1827 | the timeout feature and is equivalent to setblocking(1).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1828 | Setting a timeout of zero is the same as setblocking(0)."); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1829 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1830 | /* s.gettimeout() method. |
| 1831 | Returns the timeout associated with a socket. */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1832 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1833 | sock_gettimeout(PySocketSockObject *s) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1834 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1835 | if (s->sock_timeout < 0.0) { |
| 1836 | Py_INCREF(Py_None); |
| 1837 | return Py_None; |
| 1838 | } |
| 1839 | else |
| 1840 | return PyFloat_FromDouble(s->sock_timeout); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1843 | PyDoc_STRVAR(gettimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 1844 | "gettimeout() -> timeout\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1845 | \n\ |
Ezio Melotti | ca5e908 | 2011-08-14 08:27:36 +0300 | [diff] [blame] | 1846 | Returns the timeout in seconds (float) associated with socket \n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1847 | operations. A timeout of None indicates that timeouts on socket \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1848 | operations are disabled."); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1849 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1850 | #ifdef RISCOS |
| 1851 | /* s.sleeptaskw(1 | 0) method */ |
| 1852 | |
| 1853 | static PyObject * |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 1854 | sock_sleeptaskw(PySocketSockObject *s,PyObject *arg) |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1855 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1856 | int block; |
| 1857 | block = PyInt_AsLong(arg); |
| 1858 | if (block == -1 && PyErr_Occurred()) |
| 1859 | return NULL; |
| 1860 | Py_BEGIN_ALLOW_THREADS |
| 1861 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block); |
| 1862 | Py_END_ALLOW_THREADS |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1863 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1864 | Py_INCREF(Py_None); |
| 1865 | return Py_None; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1866 | } |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1867 | PyDoc_STRVAR(sleeptaskw_doc, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1868 | "sleeptaskw(flag)\n\ |
| 1869 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1870 | Allow sleeps in taskwindows."); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1871 | #endif |
| 1872 | |
| 1873 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1874 | /* s.setsockopt() method. |
| 1875 | With an integer third argument, sets an integer option. |
| 1876 | With a string third argument, sets an option from a buffer; |
| 1877 | use optional built-in module 'struct' to encode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1878 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1879 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1880 | sock_setsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1881 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1882 | int level; |
| 1883 | int optname; |
| 1884 | int res; |
| 1885 | char *buf; |
| 1886 | int buflen; |
| 1887 | int flag; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1888 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1889 | if (PyArg_ParseTuple(args, "iii:setsockopt", |
| 1890 | &level, &optname, &flag)) { |
| 1891 | buf = (char *) &flag; |
| 1892 | buflen = sizeof flag; |
| 1893 | } |
| 1894 | else { |
| 1895 | PyErr_Clear(); |
| 1896 | if (!PyArg_ParseTuple(args, "iis#:setsockopt", |
| 1897 | &level, &optname, &buf, &buflen)) |
| 1898 | return NULL; |
| 1899 | } |
| 1900 | res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen); |
| 1901 | if (res < 0) |
| 1902 | return s->errorhandler(); |
| 1903 | Py_INCREF(Py_None); |
| 1904 | return Py_None; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1905 | } |
| 1906 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1907 | PyDoc_STRVAR(setsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1908 | "setsockopt(level, option, value)\n\ |
| 1909 | \n\ |
| 1910 | Set a socket option. See the Unix manual for level and option.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1911 | The value argument can either be an integer or a string."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1912 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1913 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1914 | /* s.getsockopt() method. |
| 1915 | With two arguments, retrieves an integer option. |
| 1916 | With a third integer argument, retrieves a string buffer of that size; |
| 1917 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1918 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1919 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1920 | sock_getsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1921 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1922 | int level; |
| 1923 | int optname; |
| 1924 | int res; |
| 1925 | PyObject *buf; |
| 1926 | socklen_t buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1927 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1928 | #ifdef __BEOS__ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1929 | /* We have incomplete socket support. */ |
| 1930 | PyErr_SetString(socket_error, "getsockopt not supported"); |
| 1931 | return NULL; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1932 | #else |
| 1933 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1934 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt", |
| 1935 | &level, &optname, &buflen)) |
| 1936 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1937 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1938 | if (buflen == 0) { |
| 1939 | int flag = 0; |
| 1940 | socklen_t flagsize = sizeof flag; |
| 1941 | res = getsockopt(s->sock_fd, level, optname, |
| 1942 | (void *)&flag, &flagsize); |
| 1943 | if (res < 0) |
| 1944 | return s->errorhandler(); |
| 1945 | return PyInt_FromLong(flag); |
| 1946 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1947 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1948 | /* socklen_t is unsigned so no negative test is needed, |
| 1949 | test buflen == 0 is previously done */ |
| 1950 | if (buflen > 1024) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1951 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1952 | if (buflen <= 0 || buflen > 1024) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1953 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1954 | PyErr_SetString(socket_error, |
| 1955 | "getsockopt buflen out of range"); |
| 1956 | return NULL; |
| 1957 | } |
| 1958 | buf = PyString_FromStringAndSize((char *)NULL, buflen); |
| 1959 | if (buf == NULL) |
| 1960 | return NULL; |
| 1961 | res = getsockopt(s->sock_fd, level, optname, |
| 1962 | (void *)PyString_AS_STRING(buf), &buflen); |
| 1963 | if (res < 0) { |
| 1964 | Py_DECREF(buf); |
| 1965 | return s->errorhandler(); |
| 1966 | } |
| 1967 | _PyString_Resize(&buf, buflen); |
| 1968 | return buf; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1969 | #endif /* __BEOS__ */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1972 | PyDoc_STRVAR(getsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1973 | "getsockopt(level, option[, buffersize]) -> value\n\ |
| 1974 | \n\ |
| 1975 | Get a socket option. See the Unix manual for level and option.\n\ |
| 1976 | If a nonzero buffersize argument is given, the return value is a\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1977 | string of that length; otherwise it is an integer."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1978 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1979 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1980 | /* s.bind(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1981 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1982 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1983 | sock_bind(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1984 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1985 | sock_addr_t addrbuf; |
| 1986 | int addrlen; |
| 1987 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1988 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1989 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 1990 | return NULL; |
| 1991 | Py_BEGIN_ALLOW_THREADS |
| 1992 | res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen); |
| 1993 | Py_END_ALLOW_THREADS |
| 1994 | if (res < 0) |
| 1995 | return s->errorhandler(); |
| 1996 | Py_INCREF(Py_None); |
| 1997 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1998 | } |
| 1999 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2000 | PyDoc_STRVAR(bind_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2001 | "bind(address)\n\ |
| 2002 | \n\ |
| 2003 | 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] | 2004 | pair (host, port); the host must refer to the local host. For raw packet\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2005 | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2006 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2007 | |
| 2008 | /* s.close() method. |
| 2009 | Set the file descriptor to -1 so operations tried subsequently |
| 2010 | will surely fail. */ |
| 2011 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2012 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2013 | sock_close(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2014 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2015 | SOCKET_T fd; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2016 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2017 | if ((fd = s->sock_fd) != -1) { |
| 2018 | s->sock_fd = -1; |
| 2019 | Py_BEGIN_ALLOW_THREADS |
| 2020 | (void) SOCKETCLOSE(fd); |
| 2021 | Py_END_ALLOW_THREADS |
| 2022 | } |
| 2023 | Py_INCREF(Py_None); |
| 2024 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2027 | PyDoc_STRVAR(close_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2028 | "close()\n\ |
| 2029 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2030 | Close the socket. It cannot be used after this call."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2031 | |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2032 | static int |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2033 | internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2034 | int *timeoutp) |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2035 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2036 | int res, timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2037 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2038 | timeout = 0; |
| 2039 | res = connect(s->sock_fd, addr, addrlen); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2040 | |
| 2041 | #ifdef MS_WINDOWS |
| 2042 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2043 | if (s->sock_timeout > 0.0) { |
| 2044 | if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK && |
| 2045 | IS_SELECTABLE(s)) { |
| 2046 | /* This is a mess. Best solution: trust select */ |
| 2047 | fd_set fds; |
| 2048 | fd_set fds_exc; |
| 2049 | struct timeval tv; |
| 2050 | tv.tv_sec = (int)s->sock_timeout; |
| 2051 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 2052 | FD_ZERO(&fds); |
| 2053 | FD_SET(s->sock_fd, &fds); |
| 2054 | FD_ZERO(&fds_exc); |
| 2055 | FD_SET(s->sock_fd, &fds_exc); |
| 2056 | res = select(s->sock_fd+1, NULL, &fds, &fds_exc, &tv); |
| 2057 | if (res == 0) { |
| 2058 | res = WSAEWOULDBLOCK; |
| 2059 | timeout = 1; |
| 2060 | } else if (res > 0) { |
| 2061 | if (FD_ISSET(s->sock_fd, &fds)) |
| 2062 | /* The socket is in the writeable set - this |
| 2063 | means connected */ |
| 2064 | res = 0; |
| 2065 | else { |
| 2066 | /* As per MS docs, we need to call getsockopt() |
| 2067 | to get the underlying error */ |
| 2068 | int res_size = sizeof res; |
| 2069 | /* It must be in the exception set */ |
| 2070 | assert(FD_ISSET(s->sock_fd, &fds_exc)); |
| 2071 | if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR, |
| 2072 | (char *)&res, &res_size)) |
| 2073 | /* getsockopt also clears WSAGetLastError, |
| 2074 | so reset it back. */ |
| 2075 | WSASetLastError(res); |
| 2076 | else |
| 2077 | res = WSAGetLastError(); |
| 2078 | } |
| 2079 | } |
| 2080 | /* else if (res < 0) an error occurred */ |
| 2081 | } |
| 2082 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2083 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2084 | if (res < 0) |
| 2085 | res = WSAGetLastError(); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2086 | |
| 2087 | #else |
| 2088 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2089 | if (s->sock_timeout > 0.0) { |
| 2090 | if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) { |
| 2091 | timeout = internal_select(s, 1); |
| 2092 | if (timeout == 0) { |
| 2093 | /* Bug #1019808: in case of an EINPROGRESS, |
| 2094 | use getsockopt(SO_ERROR) to get the real |
| 2095 | error. */ |
| 2096 | socklen_t res_size = sizeof res; |
| 2097 | (void)getsockopt(s->sock_fd, SOL_SOCKET, |
| 2098 | SO_ERROR, &res, &res_size); |
| 2099 | if (res == EISCONN) |
| 2100 | res = 0; |
| 2101 | errno = res; |
| 2102 | } |
| 2103 | else if (timeout == -1) { |
| 2104 | res = errno; /* had error */ |
| 2105 | } |
| 2106 | else |
| 2107 | res = EWOULDBLOCK; /* timed out */ |
| 2108 | } |
| 2109 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2110 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2111 | if (res < 0) |
| 2112 | res = errno; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2113 | |
| 2114 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2115 | *timeoutp = timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2116 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2117 | return res; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2118 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2119 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2120 | /* s.connect(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2121 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2122 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2123 | sock_connect(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2124 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2125 | sock_addr_t addrbuf; |
| 2126 | int addrlen; |
| 2127 | int res; |
| 2128 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2129 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2130 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2131 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2132 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2133 | Py_BEGIN_ALLOW_THREADS |
| 2134 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 2135 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2136 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2137 | if (timeout == 1) { |
| 2138 | PyErr_SetString(socket_timeout, "timed out"); |
| 2139 | return NULL; |
| 2140 | } |
| 2141 | if (res != 0) |
| 2142 | return s->errorhandler(); |
| 2143 | Py_INCREF(Py_None); |
| 2144 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2147 | PyDoc_STRVAR(connect_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2148 | "connect(address)\n\ |
| 2149 | \n\ |
| 2150 | Connect the socket to a remote address. For IP sockets, the address\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2151 | is a pair (host, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2152 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2153 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2154 | /* s.connect_ex(sockaddr) method */ |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2155 | |
| 2156 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2157 | sock_connect_ex(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2158 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2159 | sock_addr_t addrbuf; |
| 2160 | int addrlen; |
| 2161 | int res; |
| 2162 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2163 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2164 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2165 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2166 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2167 | Py_BEGIN_ALLOW_THREADS |
| 2168 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 2169 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2170 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2171 | /* Signals are not errors (though they may raise exceptions). Adapted |
| 2172 | from PyErr_SetFromErrnoWithFilenameObject(). */ |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2173 | #ifdef EINTR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2174 | if (res == EINTR && PyErr_CheckSignals()) |
| 2175 | return NULL; |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2176 | #endif |
| 2177 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2178 | return PyInt_FromLong((long) res); |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2181 | PyDoc_STRVAR(connect_ex_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2182 | "connect_ex(address) -> errno\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2183 | \n\ |
| 2184 | This is like connect(address), but returns an error code (the errno value)\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2185 | instead of raising an exception when an error occurs."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2186 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2187 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2188 | /* s.fileno() method */ |
| 2189 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2190 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2191 | sock_fileno(PySocketSockObject *s) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2192 | { |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2193 | #if SIZEOF_SOCKET_T <= SIZEOF_LONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2194 | return PyInt_FromLong((long) s->sock_fd); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2195 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2196 | return PyLong_FromLongLong((PY_LONG_LONG)s->sock_fd); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2197 | #endif |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2198 | } |
| 2199 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2200 | PyDoc_STRVAR(fileno_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2201 | "fileno() -> integer\n\ |
| 2202 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2203 | Return the integer file descriptor of the socket."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2204 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2205 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2206 | #ifndef NO_DUP |
| 2207 | /* s.dup() method */ |
| 2208 | |
| 2209 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2210 | sock_dup(PySocketSockObject *s) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2211 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2212 | SOCKET_T newfd; |
| 2213 | PyObject *sock; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2214 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2215 | newfd = dup(s->sock_fd); |
| 2216 | if (newfd < 0) |
| 2217 | return s->errorhandler(); |
| 2218 | sock = (PyObject *) new_sockobject(newfd, |
| 2219 | s->sock_family, |
| 2220 | s->sock_type, |
| 2221 | s->sock_proto); |
| 2222 | if (sock == NULL) |
| 2223 | SOCKETCLOSE(newfd); |
| 2224 | return sock; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2225 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2226 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2227 | PyDoc_STRVAR(dup_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2228 | "dup() -> socket object\n\ |
| 2229 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2230 | Return a new socket object connected to the same system resource."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2231 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2232 | #endif |
| 2233 | |
| 2234 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2235 | /* s.getsockname() method */ |
| 2236 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2237 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2238 | sock_getsockname(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2239 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2240 | sock_addr_t addrbuf; |
| 2241 | int res; |
| 2242 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2243 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2244 | if (!getsockaddrlen(s, &addrlen)) |
| 2245 | return NULL; |
| 2246 | memset(&addrbuf, 0, addrlen); |
| 2247 | Py_BEGIN_ALLOW_THREADS |
| 2248 | res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2249 | Py_END_ALLOW_THREADS |
| 2250 | if (res < 0) |
| 2251 | return s->errorhandler(); |
| 2252 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2253 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2256 | PyDoc_STRVAR(getsockname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2257 | "getsockname() -> address info\n\ |
| 2258 | \n\ |
| 2259 | Return the address of the local endpoint. For IP sockets, the address\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2260 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2261 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2262 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2263 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2264 | /* s.getpeername() method */ |
| 2265 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2266 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2267 | sock_getpeername(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2268 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2269 | sock_addr_t addrbuf; |
| 2270 | int res; |
| 2271 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2272 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2273 | if (!getsockaddrlen(s, &addrlen)) |
| 2274 | return NULL; |
| 2275 | memset(&addrbuf, 0, addrlen); |
| 2276 | Py_BEGIN_ALLOW_THREADS |
| 2277 | res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2278 | Py_END_ALLOW_THREADS |
| 2279 | if (res < 0) |
| 2280 | return s->errorhandler(); |
| 2281 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2282 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2283 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2284 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2285 | PyDoc_STRVAR(getpeername_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2286 | "getpeername() -> address info\n\ |
| 2287 | \n\ |
| 2288 | Return the address of the remote endpoint. For IP sockets, the address\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2289 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2290 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2291 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2292 | |
| 2293 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2294 | /* s.listen(n) method */ |
| 2295 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2296 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2297 | sock_listen(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2298 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2299 | int backlog; |
| 2300 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2301 | |
Serhiy Storchaka | 74f49ab | 2013-01-19 12:55:39 +0200 | [diff] [blame] | 2302 | backlog = _PyInt_AsInt(arg); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2303 | if (backlog == -1 && PyErr_Occurred()) |
| 2304 | return NULL; |
| 2305 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 47d1d0d | 2011-05-10 19:16:03 +0200 | [diff] [blame] | 2306 | /* To avoid problems on systems that don't allow a negative backlog |
| 2307 | * (which doesn't make sense anyway) we force a minimum value of 0. */ |
| 2308 | if (backlog < 0) |
| 2309 | backlog = 0; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2310 | res = listen(s->sock_fd, backlog); |
| 2311 | Py_END_ALLOW_THREADS |
| 2312 | if (res < 0) |
| 2313 | return s->errorhandler(); |
| 2314 | Py_INCREF(Py_None); |
| 2315 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2318 | PyDoc_STRVAR(listen_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2319 | "listen(backlog)\n\ |
| 2320 | \n\ |
| 2321 | Enable a server to accept connections. The backlog argument must be at\n\ |
Antoine Pitrou | 47d1d0d | 2011-05-10 19:16:03 +0200 | [diff] [blame] | 2322 | least 0 (if it is lower, it is set to 0); it specifies the number of\n\ |
| 2323 | unaccepted connections that the system will allow before refusing new\n\ |
| 2324 | connections."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2325 | |
| 2326 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2327 | #ifndef NO_DUP |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2328 | /* s.makefile(mode) method. |
| 2329 | Create a new open file object referring to a dupped version of |
| 2330 | the socket's file descriptor. (The dup() call is necessary so |
| 2331 | that the open file and socket objects may be closed independent |
| 2332 | of each other.) |
| 2333 | The mode argument specifies 'r' or 'w' passed to fdopen(). */ |
| 2334 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2335 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2336 | sock_makefile(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2337 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2338 | extern int fclose(FILE *); |
| 2339 | char *mode = "r"; |
| 2340 | int bufsize = -1; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2341 | #ifdef MS_WIN32 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2342 | Py_intptr_t fd; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2343 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2344 | int fd; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2345 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2346 | FILE *fp; |
| 2347 | PyObject *f; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2348 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2349 | char *mode_r = "r"; |
| 2350 | char *mode_w = "w"; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2351 | #endif |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 2352 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2353 | if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize)) |
| 2354 | return NULL; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2355 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2356 | if (strcmp(mode,"rb") == 0) { |
| 2357 | mode = mode_r; |
| 2358 | } |
| 2359 | else { |
| 2360 | if (strcmp(mode,"wb") == 0) { |
| 2361 | mode = mode_w; |
| 2362 | } |
| 2363 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2364 | #endif |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2365 | #ifdef MS_WIN32 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2366 | if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) || |
| 2367 | ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL)) |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 2368 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2369 | 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] | 2370 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2371 | { |
| 2372 | if (fd >= 0) |
| 2373 | SOCKETCLOSE(fd); |
| 2374 | return s->errorhandler(); |
| 2375 | } |
| 2376 | f = PyFile_FromFile(fp, "<socket>", mode, fclose); |
| 2377 | if (f != NULL) |
| 2378 | PyFile_SetBufSize(f, bufsize); |
| 2379 | return f; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2380 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2381 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2382 | PyDoc_STRVAR(makefile_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2383 | "makefile([mode[, buffersize]]) -> file object\n\ |
| 2384 | \n\ |
| 2385 | Return a regular file object corresponding to the socket.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2386 | The mode and buffersize arguments are as for the built-in open() function."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2387 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2388 | #endif /* NO_DUP */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2389 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2390 | /* |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2391 | * This is the guts of the recv() and recv_into() methods, which reads into a |
Andrew M. Kuchling | 8d0baae | 2006-12-19 14:29:04 +0000 | [diff] [blame] | 2392 | * char buffer. If you have any inc/dec ref to do to the objects that contain |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2393 | * the buffer, do it in the caller. This function returns the number of bytes |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2394 | * successfully read. If there was an error, it returns -1. Note that it is |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2395 | * also possible that we return a number of bytes smaller than the request |
| 2396 | * bytes. |
| 2397 | */ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2398 | static ssize_t |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2399 | sock_recv_guts(PySocketSockObject *s, char* cbuf, int len, int flags) |
| 2400 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2401 | ssize_t outlen = -1; |
| 2402 | int timeout; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2403 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2404 | int remaining; |
| 2405 | char *read_buf; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2406 | #endif |
| 2407 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2408 | if (!IS_SELECTABLE(s)) { |
| 2409 | select_error(); |
| 2410 | return -1; |
| 2411 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2412 | |
| 2413 | #ifndef __VMS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2414 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2415 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2416 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2417 | if (!timeout) |
| 2418 | outlen = recv(s->sock_fd, cbuf, len, flags); |
| 2419 | Py_END_ALLOW_THREADS |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2420 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2421 | if (timeout == 1) { |
| 2422 | PyErr_SetString(socket_timeout, "timed out"); |
| 2423 | return -1; |
| 2424 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2425 | END_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2426 | if (outlen < 0) { |
| 2427 | /* Note: the call to errorhandler() ALWAYS indirectly returned |
| 2428 | NULL, so ignore its return value */ |
| 2429 | s->errorhandler(); |
| 2430 | return -1; |
| 2431 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2432 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2433 | read_buf = cbuf; |
| 2434 | remaining = len; |
| 2435 | while (remaining != 0) { |
| 2436 | unsigned int segment; |
| 2437 | int nread = -1; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2438 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2439 | segment = remaining /SEGMENT_SIZE; |
| 2440 | if (segment != 0) { |
| 2441 | segment = SEGMENT_SIZE; |
| 2442 | } |
| 2443 | else { |
| 2444 | segment = remaining; |
| 2445 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2446 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2447 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2448 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2449 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2450 | if (!timeout) |
| 2451 | nread = recv(s->sock_fd, read_buf, segment, flags); |
| 2452 | Py_END_ALLOW_THREADS |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2453 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2454 | if (timeout == 1) { |
| 2455 | PyErr_SetString(socket_timeout, "timed out"); |
| 2456 | return -1; |
| 2457 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2458 | END_SELECT_LOOP(s) |
| 2459 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2460 | if (nread < 0) { |
| 2461 | s->errorhandler(); |
| 2462 | return -1; |
| 2463 | } |
| 2464 | if (nread != remaining) { |
| 2465 | read_buf += nread; |
| 2466 | break; |
| 2467 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2468 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2469 | remaining -= segment; |
| 2470 | read_buf += segment; |
| 2471 | } |
| 2472 | outlen = read_buf - cbuf; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2473 | #endif /* !__VMS */ |
| 2474 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2475 | return outlen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2478 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2479 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2480 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2481 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2482 | sock_recv(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2483 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2484 | int recvlen, flags = 0; |
| 2485 | ssize_t outlen; |
| 2486 | PyObject *buf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2487 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2488 | if (!PyArg_ParseTuple(args, "i|i:recv", &recvlen, &flags)) |
| 2489 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2490 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2491 | if (recvlen < 0) { |
| 2492 | PyErr_SetString(PyExc_ValueError, |
| 2493 | "negative buffersize in recv"); |
| 2494 | return NULL; |
| 2495 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2496 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2497 | /* Allocate a new string. */ |
| 2498 | buf = PyString_FromStringAndSize((char *) 0, recvlen); |
| 2499 | if (buf == NULL) |
| 2500 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2501 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2502 | /* Call the guts */ |
| 2503 | outlen = sock_recv_guts(s, PyString_AS_STRING(buf), recvlen, flags); |
| 2504 | if (outlen < 0) { |
| 2505 | /* An error occurred, release the string and return an |
| 2506 | error. */ |
| 2507 | Py_DECREF(buf); |
| 2508 | return NULL; |
| 2509 | } |
| 2510 | if (outlen != recvlen) { |
| 2511 | /* We did not read as many bytes as we anticipated, resize the |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2512 | string if possible and be successful. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2513 | if (_PyString_Resize(&buf, outlen) < 0) |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2514 | /* Oopsy, not so successful after all. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2515 | return NULL; |
| 2516 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2517 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2518 | return buf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2519 | } |
| 2520 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2521 | PyDoc_STRVAR(recv_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2522 | "recv(buffersize[, flags]) -> data\n\ |
| 2523 | \n\ |
| 2524 | Receive up to buffersize bytes from the socket. For the optional flags\n\ |
| 2525 | argument, see the Unix manual. When no data is available, block until\n\ |
| 2526 | at least one byte is available or until the remote end is closed. When\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2527 | the remote end is closed and all data is read, return the empty string."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2528 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2529 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2530 | /* s.recv_into(buffer, [nbytes [,flags]]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2531 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2532 | static PyObject* |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2533 | sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2534 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2535 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2536 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2537 | int recvlen = 0, flags = 0; |
| 2538 | ssize_t readlen; |
| 2539 | Py_buffer buf; |
| 2540 | Py_ssize_t buflen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2541 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2542 | /* Get the buffer's memory */ |
| 2543 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recv_into", kwlist, |
| 2544 | &buf, &recvlen, &flags)) |
| 2545 | return NULL; |
| 2546 | buflen = buf.len; |
| 2547 | assert(buf.buf != 0 && buflen > 0); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2548 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2549 | if (recvlen < 0) { |
| 2550 | PyErr_SetString(PyExc_ValueError, |
| 2551 | "negative buffersize in recv_into"); |
| 2552 | goto error; |
| 2553 | } |
| 2554 | if (recvlen == 0) { |
| 2555 | /* If nbytes was not specified, use the buffer's length */ |
| 2556 | recvlen = buflen; |
| 2557 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2558 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2559 | /* Check if the buffer is large enough */ |
| 2560 | if (buflen < recvlen) { |
| 2561 | PyErr_SetString(PyExc_ValueError, |
| 2562 | "buffer too small for requested bytes"); |
| 2563 | goto error; |
| 2564 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2565 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2566 | /* Call the guts */ |
| 2567 | readlen = sock_recv_guts(s, buf.buf, recvlen, flags); |
| 2568 | if (readlen < 0) { |
| 2569 | /* Return an error. */ |
| 2570 | goto error; |
| 2571 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2572 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2573 | PyBuffer_Release(&buf); |
| 2574 | /* Return the number of bytes read. Note that we do not do anything |
| 2575 | special here in the case that readlen < recvlen. */ |
| 2576 | return PyInt_FromSsize_t(readlen); |
Antoine Pitrou | d7b731d | 2010-03-17 22:45:39 +0000 | [diff] [blame] | 2577 | |
| 2578 | error: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2579 | PyBuffer_Release(&buf); |
| 2580 | return NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2581 | } |
| 2582 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2583 | PyDoc_STRVAR(recv_into_doc, |
| 2584 | "recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2585 | \n\ |
| 2586 | A version of recv() that stores its data into a buffer rather than creating \n\ |
| 2587 | a new string. Receive up to buffersize bytes from the socket. If buffersize \n\ |
| 2588 | is not specified (or 0), receive up to the size available in the given buffer.\n\ |
| 2589 | \n\ |
| 2590 | See recv() for documentation about the flags."); |
| 2591 | |
| 2592 | |
| 2593 | /* |
Guido van Rossum | 3c887b2 | 2007-12-18 20:10:42 +0000 | [diff] [blame] | 2594 | * This is the guts of the recvfrom() and recvfrom_into() methods, which reads |
| 2595 | * into a char buffer. If you have any inc/def ref to do to the objects that |
| 2596 | * contain the buffer, do it in the caller. This function returns the number |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2597 | * of bytes successfully read. If there was an error, it returns -1. Note |
Guido van Rossum | 3c887b2 | 2007-12-18 20:10:42 +0000 | [diff] [blame] | 2598 | * that it is also possible that we return a number of bytes smaller than the |
| 2599 | * request bytes. |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2600 | * |
| 2601 | * 'addr' is a return value for the address object. Note that you must decref |
| 2602 | * it yourself. |
| 2603 | */ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2604 | static ssize_t |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2605 | sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, int len, int flags, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2606 | PyObject** addr) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2607 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2608 | sock_addr_t addrbuf; |
| 2609 | int timeout; |
| 2610 | ssize_t n = -1; |
| 2611 | socklen_t addrlen; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2612 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2613 | *addr = NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2614 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2615 | if (!getsockaddrlen(s, &addrlen)) |
| 2616 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2617 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2618 | if (!IS_SELECTABLE(s)) { |
| 2619 | select_error(); |
| 2620 | return -1; |
| 2621 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2622 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2623 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2624 | Py_BEGIN_ALLOW_THREADS |
| 2625 | memset(&addrbuf, 0, addrlen); |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2626 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2627 | if (!timeout) { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2628 | #ifndef MS_WINDOWS |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 2629 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2630 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2631 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2632 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2633 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2634 | (void *) &addrbuf, &addrlen); |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2635 | #endif |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2636 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2637 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2638 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2639 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2640 | } |
| 2641 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2642 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2643 | if (timeout == 1) { |
| 2644 | PyErr_SetString(socket_timeout, "timed out"); |
| 2645 | return -1; |
| 2646 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2647 | END_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2648 | if (n < 0) { |
| 2649 | s->errorhandler(); |
| 2650 | return -1; |
| 2651 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2652 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2653 | if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 2654 | addrlen, s->sock_proto))) |
| 2655 | return -1; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2656 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2657 | return n; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2658 | } |
| 2659 | |
| 2660 | /* s.recvfrom(nbytes [,flags]) method */ |
| 2661 | |
| 2662 | static PyObject * |
| 2663 | sock_recvfrom(PySocketSockObject *s, PyObject *args) |
| 2664 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2665 | PyObject *buf = NULL; |
| 2666 | PyObject *addr = NULL; |
| 2667 | PyObject *ret = NULL; |
| 2668 | int recvlen, flags = 0; |
| 2669 | ssize_t outlen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2670 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2671 | if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags)) |
| 2672 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2673 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2674 | if (recvlen < 0) { |
| 2675 | PyErr_SetString(PyExc_ValueError, |
| 2676 | "negative buffersize in recvfrom"); |
| 2677 | return NULL; |
| 2678 | } |
Facundo Batista | 1fe9f96 | 2007-03-28 03:45:20 +0000 | [diff] [blame] | 2679 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2680 | buf = PyString_FromStringAndSize((char *) 0, recvlen); |
| 2681 | if (buf == NULL) |
| 2682 | return NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2683 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2684 | outlen = sock_recvfrom_guts(s, PyString_AS_STRING(buf), |
| 2685 | recvlen, flags, &addr); |
| 2686 | if (outlen < 0) { |
| 2687 | goto finally; |
| 2688 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2689 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2690 | if (outlen != recvlen) { |
| 2691 | /* We did not read as many bytes as we anticipated, resize the |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2692 | string if possible and be successful. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2693 | if (_PyString_Resize(&buf, outlen) < 0) |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2694 | /* Oopsy, not so successful after all. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2695 | goto finally; |
| 2696 | } |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2697 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2698 | ret = PyTuple_Pack(2, buf, addr); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2699 | |
| 2700 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2701 | Py_XDECREF(buf); |
| 2702 | Py_XDECREF(addr); |
| 2703 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2706 | PyDoc_STRVAR(recvfrom_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2707 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
| 2708 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2709 | Like recv(buffersize, flags) but also return the sender's address info."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2710 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2711 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2712 | /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2713 | |
| 2714 | static PyObject * |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2715 | sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2716 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2717 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2718 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2719 | int recvlen = 0, flags = 0; |
| 2720 | ssize_t readlen; |
| 2721 | Py_buffer buf; |
| 2722 | int buflen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2723 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2724 | PyObject *addr = NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2725 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2726 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recvfrom_into", |
| 2727 | kwlist, &buf, |
| 2728 | &recvlen, &flags)) |
| 2729 | return NULL; |
| 2730 | buflen = buf.len; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2731 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2732 | if (recvlen < 0) { |
| 2733 | PyErr_SetString(PyExc_ValueError, |
| 2734 | "negative buffersize in recvfrom_into"); |
| 2735 | goto error; |
| 2736 | } |
| 2737 | if (recvlen == 0) { |
| 2738 | /* If nbytes was not specified, use the buffer's length */ |
| 2739 | recvlen = buflen; |
Benjamin Peterson | 28cf368 | 2014-01-13 22:59:38 -0500 | [diff] [blame] | 2740 | } else if (recvlen > buflen) { |
| 2741 | PyErr_SetString(PyExc_ValueError, |
| 2742 | "nbytes is greater than the length of the buffer"); |
| 2743 | goto error; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2744 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2745 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2746 | readlen = sock_recvfrom_guts(s, buf.buf, recvlen, flags, &addr); |
| 2747 | if (readlen < 0) { |
| 2748 | /* Return an error */ |
| 2749 | goto error; |
| 2750 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2751 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2752 | PyBuffer_Release(&buf); |
| 2753 | /* Return the number of bytes read and the address. Note that we do |
| 2754 | not do anything special here in the case that readlen < recvlen. */ |
| 2755 | return Py_BuildValue("lN", readlen, addr); |
Antoine Pitrou | d7b731d | 2010-03-17 22:45:39 +0000 | [diff] [blame] | 2756 | |
| 2757 | error: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2758 | Py_XDECREF(addr); |
| 2759 | PyBuffer_Release(&buf); |
| 2760 | return NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2761 | } |
| 2762 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2763 | PyDoc_STRVAR(recvfrom_into_doc, |
| 2764 | "recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2765 | \n\ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2766 | Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info."); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2767 | |
| 2768 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2769 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2770 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2771 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2772 | sock_send(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2773 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2774 | char *buf; |
| 2775 | int len, n = -1, flags = 0, timeout; |
| 2776 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2777 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2778 | if (!PyArg_ParseTuple(args, "s*|i:send", &pbuf, &flags)) |
| 2779 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2780 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2781 | if (!IS_SELECTABLE(s)) { |
| 2782 | PyBuffer_Release(&pbuf); |
| 2783 | return select_error(); |
| 2784 | } |
| 2785 | buf = pbuf.buf; |
| 2786 | len = pbuf.len; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2787 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2788 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2789 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2790 | timeout = internal_select_ex(s, 1, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2791 | if (!timeout) |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2792 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2793 | n = sendsegmented(s->sock_fd, buf, len, flags); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2794 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2795 | n = send(s->sock_fd, buf, len, flags); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2796 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2797 | Py_END_ALLOW_THREADS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2798 | if (timeout == 1) { |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2799 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2800 | PyErr_SetString(socket_timeout, "timed out"); |
| 2801 | return NULL; |
| 2802 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2803 | END_SELECT_LOOP(s) |
| 2804 | |
| 2805 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2806 | if (n < 0) |
| 2807 | return s->errorhandler(); |
| 2808 | return PyInt_FromLong((long)n); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2811 | PyDoc_STRVAR(send_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2812 | "send(data[, flags]) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2813 | \n\ |
| 2814 | Send a data string to the socket. For the optional flags\n\ |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2815 | argument, see the Unix manual. Return the number of bytes\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2816 | sent; this may be less than len(data) if the network is busy."); |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2817 | |
| 2818 | |
| 2819 | /* s.sendall(data [,flags]) method */ |
| 2820 | |
| 2821 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2822 | sock_sendall(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2823 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2824 | char *buf; |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2825 | int len, n = -1, flags = 0, timeout, saved_errno; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2826 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2827 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2828 | if (!PyArg_ParseTuple(args, "s*|i:sendall", &pbuf, &flags)) |
| 2829 | return NULL; |
| 2830 | buf = pbuf.buf; |
| 2831 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2832 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2833 | if (!IS_SELECTABLE(s)) { |
| 2834 | PyBuffer_Release(&pbuf); |
| 2835 | return select_error(); |
| 2836 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2837 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2838 | do { |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2839 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2840 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2841 | timeout = internal_select_ex(s, 1, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2842 | n = -1; |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2843 | if (!timeout) { |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2844 | #ifdef __VMS |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2845 | n = sendsegmented(s->sock_fd, buf, len, flags); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2846 | #else |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2847 | n = send(s->sock_fd, buf, len, flags); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2848 | #endif |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2849 | } |
| 2850 | Py_END_ALLOW_THREADS |
| 2851 | if (timeout == 1) { |
| 2852 | PyBuffer_Release(&pbuf); |
| 2853 | PyErr_SetString(socket_timeout, "timed out"); |
| 2854 | return NULL; |
| 2855 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2856 | END_SELECT_LOOP(s) |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2857 | /* PyErr_CheckSignals() might change errno */ |
| 2858 | saved_errno = errno; |
| 2859 | /* We must run our signal handlers before looping again. |
| 2860 | send() can return a successful partial write when it is |
| 2861 | interrupted, so we can't restrict ourselves to EINTR. */ |
| 2862 | if (PyErr_CheckSignals()) { |
| 2863 | PyBuffer_Release(&pbuf); |
| 2864 | return NULL; |
| 2865 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2866 | if (n < 0) { |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2867 | /* If interrupted, try again */ |
| 2868 | if (saved_errno == EINTR) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2869 | continue; |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2870 | else |
| 2871 | break; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2872 | } |
| 2873 | buf += n; |
| 2874 | len -= n; |
| 2875 | } while (len > 0); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2876 | PyBuffer_Release(&pbuf); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2877 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2878 | if (n < 0) |
| 2879 | return s->errorhandler(); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2880 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2881 | Py_INCREF(Py_None); |
| 2882 | return Py_None; |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2885 | PyDoc_STRVAR(sendall_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2886 | "sendall(data[, flags])\n\ |
| 2887 | \n\ |
| 2888 | Send a data string to the socket. For the optional flags\n\ |
| 2889 | argument, see the Unix manual. This calls send() repeatedly\n\ |
| 2890 | until all data is sent. If an error occurs, it's impossible\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2891 | to tell how much data has been sent."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2892 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2893 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2894 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2895 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2896 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2897 | sock_sendto(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2898 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2899 | Py_buffer pbuf; |
| 2900 | PyObject *addro; |
| 2901 | char *buf; |
| 2902 | Py_ssize_t len; |
| 2903 | sock_addr_t addrbuf; |
Victor Stinner | 31c7e4f | 2015-04-02 17:19:17 +0200 | [diff] [blame] | 2904 | int addrlen, flags, timeout; |
| 2905 | long n = -1; |
Ezio Melotti | 0639be6 | 2011-05-07 19:21:22 +0300 | [diff] [blame] | 2906 | int arglen; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2907 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2908 | flags = 0; |
Ezio Melotti | 0639be6 | 2011-05-07 19:21:22 +0300 | [diff] [blame] | 2909 | arglen = PyTuple_Size(args); |
| 2910 | switch(arglen) { |
| 2911 | case 2: |
| 2912 | PyArg_ParseTuple(args, "s*O:sendto", &pbuf, &addro); |
| 2913 | break; |
| 2914 | case 3: |
| 2915 | PyArg_ParseTuple(args, "s*iO:sendto", &pbuf, &flags, &addro); |
| 2916 | break; |
| 2917 | default: |
| 2918 | PyErr_Format(PyExc_TypeError, "sendto() takes 2 or 3" |
| 2919 | " arguments (%d given)", arglen); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2920 | } |
Ezio Melotti | 0639be6 | 2011-05-07 19:21:22 +0300 | [diff] [blame] | 2921 | if (PyErr_Occurred()) |
| 2922 | return NULL; |
| 2923 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2924 | buf = pbuf.buf; |
| 2925 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2926 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2927 | if (!IS_SELECTABLE(s)) { |
| 2928 | PyBuffer_Release(&pbuf); |
| 2929 | return select_error(); |
| 2930 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2931 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2932 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) { |
| 2933 | PyBuffer_Release(&pbuf); |
| 2934 | return NULL; |
| 2935 | } |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 2936 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2937 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2938 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2939 | timeout = internal_select_ex(s, 1, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2940 | if (!timeout) |
| 2941 | n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen); |
| 2942 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2943 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2944 | if (timeout == 1) { |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2945 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2946 | PyErr_SetString(socket_timeout, "timed out"); |
| 2947 | return NULL; |
| 2948 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2949 | END_SELECT_LOOP(s) |
| 2950 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2951 | if (n < 0) |
| 2952 | return s->errorhandler(); |
| 2953 | return PyInt_FromLong((long)n); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2954 | } |
| 2955 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2956 | PyDoc_STRVAR(sendto_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2957 | "sendto(data[, flags], address) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2958 | \n\ |
| 2959 | Like send(data, flags) but allows specifying the destination address.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2960 | For IP sockets, the address is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2961 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2962 | |
| 2963 | /* s.shutdown(how) method */ |
| 2964 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2965 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2966 | sock_shutdown(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2967 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2968 | int how; |
| 2969 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2970 | |
Serhiy Storchaka | 74f49ab | 2013-01-19 12:55:39 +0200 | [diff] [blame] | 2971 | how = _PyInt_AsInt(arg); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2972 | if (how == -1 && PyErr_Occurred()) |
| 2973 | return NULL; |
| 2974 | Py_BEGIN_ALLOW_THREADS |
| 2975 | res = shutdown(s->sock_fd, how); |
| 2976 | Py_END_ALLOW_THREADS |
| 2977 | if (res < 0) |
| 2978 | return s->errorhandler(); |
| 2979 | Py_INCREF(Py_None); |
| 2980 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2983 | PyDoc_STRVAR(shutdown_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2984 | "shutdown(flag)\n\ |
| 2985 | \n\ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 2986 | Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\ |
| 2987 | of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2988 | |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 2989 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 2990 | static PyObject* |
| 2991 | sock_ioctl(PySocketSockObject *s, PyObject *arg) |
| 2992 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2993 | unsigned long cmd = SIO_RCVALL; |
| 2994 | PyObject *argO; |
| 2995 | DWORD recv; |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 2996 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2997 | if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO)) |
| 2998 | return NULL; |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 2999 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3000 | switch (cmd) { |
| 3001 | case SIO_RCVALL: { |
| 3002 | unsigned int option = RCVALL_ON; |
| 3003 | if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option)) |
| 3004 | return NULL; |
| 3005 | if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option), |
| 3006 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 3007 | return set_error(); |
| 3008 | } |
| 3009 | return PyLong_FromUnsignedLong(recv); } |
| 3010 | case SIO_KEEPALIVE_VALS: { |
| 3011 | struct tcp_keepalive ka; |
| 3012 | if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd, |
| 3013 | &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval)) |
| 3014 | return NULL; |
| 3015 | if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka), |
| 3016 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 3017 | return set_error(); |
| 3018 | } |
| 3019 | return PyLong_FromUnsignedLong(recv); } |
| 3020 | default: |
| 3021 | PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd); |
| 3022 | return NULL; |
| 3023 | } |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 3024 | } |
| 3025 | PyDoc_STRVAR(sock_ioctl_doc, |
| 3026 | "ioctl(cmd, option) -> long\n\ |
| 3027 | \n\ |
Kristján Valur Jónsson | 2fcd03b | 2009-09-25 15:19:51 +0000 | [diff] [blame] | 3028 | Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\ |
| 3029 | SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\ |
| 3030 | SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval)."); |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 3031 | |
| 3032 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3033 | |
| 3034 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3035 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3036 | static PyMethodDef sock_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3037 | {"accept", (PyCFunction)sock_accept, METH_NOARGS, |
| 3038 | accept_doc}, |
| 3039 | {"bind", (PyCFunction)sock_bind, METH_O, |
| 3040 | bind_doc}, |
| 3041 | {"close", (PyCFunction)sock_close, METH_NOARGS, |
| 3042 | close_doc}, |
| 3043 | {"connect", (PyCFunction)sock_connect, METH_O, |
| 3044 | connect_doc}, |
| 3045 | {"connect_ex", (PyCFunction)sock_connect_ex, METH_O, |
| 3046 | connect_ex_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3047 | #ifndef NO_DUP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3048 | {"dup", (PyCFunction)sock_dup, METH_NOARGS, |
| 3049 | dup_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3050 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3051 | {"fileno", (PyCFunction)sock_fileno, METH_NOARGS, |
| 3052 | fileno_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3053 | #ifdef HAVE_GETPEERNAME |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3054 | {"getpeername", (PyCFunction)sock_getpeername, |
| 3055 | METH_NOARGS, getpeername_doc}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 3056 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3057 | {"getsockname", (PyCFunction)sock_getsockname, |
| 3058 | METH_NOARGS, getsockname_doc}, |
| 3059 | {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS, |
| 3060 | getsockopt_doc}, |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 3061 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3062 | {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS, |
| 3063 | sock_ioctl_doc}, |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 3064 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3065 | {"listen", (PyCFunction)sock_listen, METH_O, |
| 3066 | listen_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3067 | #ifndef NO_DUP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3068 | {"makefile", (PyCFunction)sock_makefile, METH_VARARGS, |
| 3069 | makefile_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3070 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3071 | {"recv", (PyCFunction)sock_recv, METH_VARARGS, |
| 3072 | recv_doc}, |
| 3073 | {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS, |
| 3074 | recv_into_doc}, |
| 3075 | {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS, |
| 3076 | recvfrom_doc}, |
| 3077 | {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS, |
| 3078 | recvfrom_into_doc}, |
| 3079 | {"send", (PyCFunction)sock_send, METH_VARARGS, |
| 3080 | send_doc}, |
| 3081 | {"sendall", (PyCFunction)sock_sendall, METH_VARARGS, |
| 3082 | sendall_doc}, |
| 3083 | {"sendto", (PyCFunction)sock_sendto, METH_VARARGS, |
| 3084 | sendto_doc}, |
| 3085 | {"setblocking", (PyCFunction)sock_setblocking, METH_O, |
| 3086 | setblocking_doc}, |
| 3087 | {"settimeout", (PyCFunction)sock_settimeout, METH_O, |
| 3088 | settimeout_doc}, |
| 3089 | {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS, |
| 3090 | gettimeout_doc}, |
| 3091 | {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS, |
| 3092 | setsockopt_doc}, |
| 3093 | {"shutdown", (PyCFunction)sock_shutdown, METH_O, |
| 3094 | shutdown_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3095 | #ifdef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3096 | {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_O, |
| 3097 | sleeptaskw_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3098 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3099 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3100 | }; |
| 3101 | |
Georg Brandl | bc45a3f | 2006-03-17 19:17:34 +0000 | [diff] [blame] | 3102 | /* SockObject members */ |
| 3103 | static PyMemberDef sock_memberlist[] = { |
| 3104 | {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"}, |
| 3105 | {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"}, |
| 3106 | {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"}, |
| 3107 | {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"}, |
| 3108 | {0}, |
| 3109 | }; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3110 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3111 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3112 | First close the file description. */ |
| 3113 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3114 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3115 | sock_dealloc(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3116 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3117 | if (s->sock_fd != -1) |
| 3118 | (void) SOCKETCLOSE(s->sock_fd); |
Benjamin Peterson | 3d1f2d3 | 2014-10-06 14:38:20 -0400 | [diff] [blame] | 3119 | if (s->weakreflist != NULL) |
| 3120 | PyObject_ClearWeakRefs((PyObject *)s); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3121 | Py_TYPE(s)->tp_free((PyObject *)s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3122 | } |
| 3123 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3124 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3125 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3126 | sock_repr(PySocketSockObject *s) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3127 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3128 | char buf[512]; |
Victor Stinner | 0a649c7 | 2014-07-26 14:52:55 +0200 | [diff] [blame] | 3129 | long sock_fd; |
| 3130 | /* On Windows, this test is needed because SOCKET_T is unsigned */ |
| 3131 | if (s->sock_fd == INVALID_SOCKET) { |
| 3132 | sock_fd = -1; |
| 3133 | } |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 3134 | #if SIZEOF_SOCKET_T > SIZEOF_LONG |
Victor Stinner | 0a649c7 | 2014-07-26 14:52:55 +0200 | [diff] [blame] | 3135 | else if (s->sock_fd > LONG_MAX) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3136 | /* this can occur on Win64, and actually there is a special |
| 3137 | ugly printf formatter for decimal pointer length integer |
| 3138 | printing, only bother if necessary*/ |
| 3139 | PyErr_SetString(PyExc_OverflowError, |
| 3140 | "no printf formatter to display " |
| 3141 | "the socket descriptor in decimal"); |
| 3142 | return NULL; |
| 3143 | } |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 3144 | #endif |
Victor Stinner | 0a649c7 | 2014-07-26 14:52:55 +0200 | [diff] [blame] | 3145 | else |
| 3146 | sock_fd = (long)s->sock_fd; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3147 | PyOS_snprintf( |
| 3148 | buf, sizeof(buf), |
| 3149 | "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>", |
Victor Stinner | 0a649c7 | 2014-07-26 14:52:55 +0200 | [diff] [blame] | 3150 | sock_fd, s->sock_family, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3151 | s->sock_type, |
| 3152 | s->sock_proto); |
| 3153 | return PyString_FromString(buf); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
| 3156 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3157 | /* Create a new, uninitialized socket object. */ |
| 3158 | |
| 3159 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3160 | sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3161 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3162 | PyObject *new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3163 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3164 | new = type->tp_alloc(type, 0); |
| 3165 | if (new != NULL) { |
| 3166 | ((PySocketSockObject *)new)->sock_fd = -1; |
| 3167 | ((PySocketSockObject *)new)->sock_timeout = -1.0; |
| 3168 | ((PySocketSockObject *)new)->errorhandler = &set_error; |
Benjamin Peterson | 3d1f2d3 | 2014-10-06 14:38:20 -0400 | [diff] [blame] | 3169 | ((PySocketSockObject *)new)->weakreflist = NULL; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3170 | } |
| 3171 | return new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3172 | } |
| 3173 | |
| 3174 | |
| 3175 | /* Initialize a new socket object. */ |
| 3176 | |
| 3177 | /*ARGSUSED*/ |
| 3178 | static int |
Andrew MacIntyre | 7aec4a2 | 2002-06-13 11:53:52 +0000 | [diff] [blame] | 3179 | sock_initobj(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3180 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3181 | PySocketSockObject *s = (PySocketSockObject *)self; |
| 3182 | SOCKET_T fd; |
| 3183 | int family = AF_INET, type = SOCK_STREAM, proto = 0; |
| 3184 | static char *keywords[] = {"family", "type", "proto", 0}; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3185 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3186 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 3187 | "|iii:socket", keywords, |
| 3188 | &family, &type, &proto)) |
| 3189 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3190 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3191 | Py_BEGIN_ALLOW_THREADS |
| 3192 | fd = socket(family, type, proto); |
| 3193 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3194 | |
Victor Stinner | 465db3c | 2014-07-26 14:47:56 +0200 | [diff] [blame] | 3195 | if (fd == INVALID_SOCKET) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3196 | set_error(); |
| 3197 | return -1; |
| 3198 | } |
| 3199 | init_sockobject(s, fd, family, type, proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3200 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3201 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3202 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3206 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3207 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3208 | static PyTypeObject sock_type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3209 | PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */ |
| 3210 | "_socket.socket", /* tp_name */ |
| 3211 | sizeof(PySocketSockObject), /* tp_basicsize */ |
| 3212 | 0, /* tp_itemsize */ |
| 3213 | (destructor)sock_dealloc, /* tp_dealloc */ |
| 3214 | 0, /* tp_print */ |
| 3215 | 0, /* tp_getattr */ |
| 3216 | 0, /* tp_setattr */ |
| 3217 | 0, /* tp_compare */ |
| 3218 | (reprfunc)sock_repr, /* tp_repr */ |
| 3219 | 0, /* tp_as_number */ |
| 3220 | 0, /* tp_as_sequence */ |
| 3221 | 0, /* tp_as_mapping */ |
| 3222 | 0, /* tp_hash */ |
| 3223 | 0, /* tp_call */ |
| 3224 | 0, /* tp_str */ |
| 3225 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3226 | 0, /* tp_setattro */ |
| 3227 | 0, /* tp_as_buffer */ |
| 3228 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3229 | sock_doc, /* tp_doc */ |
| 3230 | 0, /* tp_traverse */ |
| 3231 | 0, /* tp_clear */ |
| 3232 | 0, /* tp_richcompare */ |
Benjamin Peterson | 3d1f2d3 | 2014-10-06 14:38:20 -0400 | [diff] [blame] | 3233 | offsetof(PySocketSockObject, weakreflist), /* tp_weaklistoffset */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3234 | 0, /* tp_iter */ |
| 3235 | 0, /* tp_iternext */ |
| 3236 | sock_methods, /* tp_methods */ |
| 3237 | sock_memberlist, /* tp_members */ |
| 3238 | 0, /* tp_getset */ |
| 3239 | 0, /* tp_base */ |
| 3240 | 0, /* tp_dict */ |
| 3241 | 0, /* tp_descr_get */ |
| 3242 | 0, /* tp_descr_set */ |
| 3243 | 0, /* tp_dictoffset */ |
| 3244 | sock_initobj, /* tp_init */ |
| 3245 | PyType_GenericAlloc, /* tp_alloc */ |
| 3246 | sock_new, /* tp_new */ |
| 3247 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3248 | }; |
| 3249 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3250 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 3251 | /* Python interface to gethostname(). */ |
| 3252 | |
| 3253 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3254 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 3255 | socket_gethostname(PyObject *self, PyObject *unused) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 3256 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3257 | char buf[1024]; |
| 3258 | int res; |
| 3259 | Py_BEGIN_ALLOW_THREADS |
| 3260 | res = gethostname(buf, (int) sizeof buf - 1); |
| 3261 | Py_END_ALLOW_THREADS |
| 3262 | if (res < 0) |
| 3263 | return set_error(); |
| 3264 | buf[sizeof buf - 1] = '\0'; |
| 3265 | return PyString_FromString(buf); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 3266 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 3267 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3268 | PyDoc_STRVAR(gethostname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3269 | "gethostname() -> string\n\ |
| 3270 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3271 | Return the current host name."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3272 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 3273 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3274 | /* Python interface to gethostbyname(name). */ |
| 3275 | |
| 3276 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3277 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3278 | socket_gethostbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3279 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3280 | char *name; |
| 3281 | sock_addr_t addrbuf; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3282 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3283 | if (!PyArg_ParseTuple(args, "s:gethostbyname", &name)) |
| 3284 | return NULL; |
| 3285 | if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0) |
| 3286 | return NULL; |
| 3287 | return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3288 | } |
| 3289 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3290 | PyDoc_STRVAR(gethostbyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3291 | "gethostbyname(host) -> address\n\ |
| 3292 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3293 | Return the IP address (a string of the form '255.255.255.255') for a host."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3294 | |
| 3295 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3296 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
| 3297 | |
| 3298 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3299 | 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] | 3300 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3301 | char **pch; |
| 3302 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 3303 | PyObject *name_list = (PyObject *)NULL; |
| 3304 | PyObject *addr_list = (PyObject *)NULL; |
| 3305 | PyObject *tmp; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3306 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3307 | if (h == NULL) { |
| 3308 | /* Let's get real error message to return */ |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 3309 | #ifndef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3310 | set_herror(h_errno); |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 3311 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3312 | PyErr_SetString(socket_error, "host not found"); |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 3313 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3314 | return NULL; |
| 3315 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3316 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3317 | if (h->h_addrtype != af) { |
| 3318 | /* Let's get real error message to return */ |
| 3319 | PyErr_SetString(socket_error, |
| 3320 | (char *)strerror(EAFNOSUPPORT)); |
Brett Cannon | 10ed0f5 | 2008-03-18 15:35:58 +0000 | [diff] [blame] | 3321 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3322 | return NULL; |
| 3323 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3324 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3325 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3326 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3327 | case AF_INET: |
| 3328 | if (alen < sizeof(struct sockaddr_in)) |
| 3329 | return NULL; |
| 3330 | break; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3331 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3332 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3333 | case AF_INET6: |
| 3334 | if (alen < sizeof(struct sockaddr_in6)) |
| 3335 | return NULL; |
| 3336 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3337 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3338 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3339 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3340 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3341 | if ((name_list = PyList_New(0)) == NULL) |
| 3342 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3343 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3344 | if ((addr_list = PyList_New(0)) == NULL) |
| 3345 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3346 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3347 | /* SF #1511317: h_aliases can be NULL */ |
| 3348 | if (h->h_aliases) { |
| 3349 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 3350 | int status; |
| 3351 | tmp = PyString_FromString(*pch); |
| 3352 | if (tmp == NULL) |
| 3353 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3354 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3355 | status = PyList_Append(name_list, tmp); |
| 3356 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3357 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3358 | if (status) |
| 3359 | goto err; |
| 3360 | } |
| 3361 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3362 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3363 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 3364 | int status; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3365 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3366 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3367 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3368 | case AF_INET: |
| 3369 | { |
| 3370 | struct sockaddr_in sin; |
| 3371 | memset(&sin, 0, sizeof(sin)); |
| 3372 | sin.sin_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3373 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3374 | sin.sin_len = sizeof(sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3375 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3376 | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); |
| 3377 | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3378 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3379 | if (pch == h->h_addr_list && alen >= sizeof(sin)) |
| 3380 | memcpy((char *) addr, &sin, sizeof(sin)); |
| 3381 | break; |
| 3382 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3383 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3384 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3385 | case AF_INET6: |
| 3386 | { |
| 3387 | struct sockaddr_in6 sin6; |
| 3388 | memset(&sin6, 0, sizeof(sin6)); |
| 3389 | sin6.sin6_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3390 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3391 | sin6.sin6_len = sizeof(sin6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3392 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3393 | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); |
| 3394 | tmp = makeipaddr((struct sockaddr *)&sin6, |
| 3395 | sizeof(sin6)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3396 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3397 | if (pch == h->h_addr_list && alen >= sizeof(sin6)) |
| 3398 | memcpy((char *) addr, &sin6, sizeof(sin6)); |
| 3399 | break; |
| 3400 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3401 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3402 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3403 | default: /* can't happen */ |
| 3404 | PyErr_SetString(socket_error, |
| 3405 | "unsupported address family"); |
| 3406 | return NULL; |
| 3407 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3408 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3409 | if (tmp == NULL) |
| 3410 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3411 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3412 | status = PyList_Append(addr_list, tmp); |
| 3413 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3414 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3415 | if (status) |
| 3416 | goto err; |
| 3417 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3418 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3419 | rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3420 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3421 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3422 | Py_XDECREF(name_list); |
| 3423 | Py_XDECREF(addr_list); |
| 3424 | return rtn_tuple; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3425 | } |
| 3426 | |
| 3427 | |
| 3428 | /* Python interface to gethostbyname_ex(name). */ |
| 3429 | |
| 3430 | /*ARGSUSED*/ |
| 3431 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3432 | socket_gethostbyname_ex(PyObject *self, PyObject *args) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3433 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3434 | char *name; |
| 3435 | struct hostent *h; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3436 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3437 | struct sockaddr_storage addr; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3438 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3439 | struct sockaddr_in addr; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3440 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3441 | struct sockaddr *sa; |
| 3442 | PyObject *ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3443 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3444 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3445 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3446 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3447 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3448 | char buf[16384]; |
| 3449 | int buf_len = (sizeof buf) - 1; |
| 3450 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3451 | #endif |
| 3452 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3453 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3454 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3455 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3456 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3457 | if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name)) |
| 3458 | return NULL; |
| 3459 | if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0) |
| 3460 | return NULL; |
| 3461 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3462 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3463 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3464 | result = gethostbyname_r(name, &hp_allocated, buf, buf_len, |
| 3465 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3466 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3467 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3468 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3469 | memset((void *) &data, '\0', sizeof(data)); |
| 3470 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 3471 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3472 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3473 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3474 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3475 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3476 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3477 | h = gethostbyname(name); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3478 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3479 | Py_END_ALLOW_THREADS |
| 3480 | /* Some C libraries would require addr.__ss_family instead of |
| 3481 | addr.ss_family. |
| 3482 | Therefore, we cast the sockaddr_storage into sockaddr to |
| 3483 | access sa_family. */ |
| 3484 | sa = (struct sockaddr*)&addr; |
| 3485 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), |
| 3486 | sa->sa_family); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3487 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3488 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 3489 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3490 | return ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3491 | } |
| 3492 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3493 | PyDoc_STRVAR(ghbn_ex_doc, |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3494 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
| 3495 | \n\ |
| 3496 | Return the true host name, a list of aliases, and a list of IP addresses,\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3497 | for a host. The host argument is a string giving a host name or IP number."); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3498 | |
| 3499 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3500 | /* Python interface to gethostbyaddr(IP). */ |
| 3501 | |
| 3502 | /*ARGSUSED*/ |
| 3503 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3504 | socket_gethostbyaddr(PyObject *self, PyObject *args) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3505 | { |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3506 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3507 | struct sockaddr_storage addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3508 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3509 | struct sockaddr_in addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3510 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3511 | struct sockaddr *sa = (struct sockaddr *)&addr; |
| 3512 | char *ip_num; |
| 3513 | struct hostent *h; |
| 3514 | PyObject *ret; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3515 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3516 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3517 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3518 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3519 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3520 | /* glibcs up to 2.10 assume that the buf argument to |
| 3521 | gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc |
| 3522 | does not ensure. The attribute below instructs the compiler |
| 3523 | to maintain this alignment. */ |
| 3524 | char buf[16384] Py_ALIGNED(8); |
| 3525 | int buf_len = (sizeof buf) - 1; |
| 3526 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3527 | #endif |
| 3528 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3529 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3530 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3531 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3532 | char *ap; |
| 3533 | int al; |
| 3534 | int af; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3535 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3536 | if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num)) |
| 3537 | return NULL; |
| 3538 | af = AF_UNSPEC; |
| 3539 | if (setipaddr(ip_num, sa, sizeof(addr), af) < 0) |
| 3540 | return NULL; |
| 3541 | af = sa->sa_family; |
| 3542 | ap = NULL; |
| 3543 | switch (af) { |
| 3544 | case AF_INET: |
| 3545 | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr; |
| 3546 | al = sizeof(((struct sockaddr_in *)sa)->sin_addr); |
| 3547 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3548 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3549 | case AF_INET6: |
| 3550 | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr; |
| 3551 | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); |
| 3552 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3553 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3554 | default: |
| 3555 | PyErr_SetString(socket_error, "unsupported address family"); |
| 3556 | return NULL; |
| 3557 | } |
| 3558 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3559 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3560 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3561 | result = gethostbyaddr_r(ap, al, af, |
| 3562 | &hp_allocated, buf, buf_len, |
| 3563 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3564 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3565 | h = gethostbyaddr_r(ap, al, af, |
| 3566 | &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3567 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3568 | memset((void *) &data, '\0', sizeof(data)); |
| 3569 | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); |
| 3570 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3571 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3572 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3573 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3574 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3575 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3576 | h = gethostbyaddr(ap, al, af); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3577 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3578 | Py_END_ALLOW_THREADS |
| 3579 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3580 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3581 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3582 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3583 | return ret; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3586 | PyDoc_STRVAR(gethostbyaddr_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3587 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
| 3588 | \n\ |
| 3589 | Return the true host name, a list of aliases, and a list of IP addresses,\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3590 | for a host. The host argument is a string giving a host name or IP number."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3591 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3592 | |
| 3593 | /* Python interface to getservbyname(name). |
| 3594 | This only returns the port number, since the other info is already |
| 3595 | known or not useful (like the list of aliases). */ |
| 3596 | |
| 3597 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3598 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3599 | socket_getservbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3600 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3601 | char *name, *proto=NULL; |
| 3602 | struct servent *sp; |
| 3603 | if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto)) |
| 3604 | return NULL; |
| 3605 | Py_BEGIN_ALLOW_THREADS |
| 3606 | sp = getservbyname(name, proto); |
| 3607 | Py_END_ALLOW_THREADS |
| 3608 | if (sp == NULL) { |
| 3609 | PyErr_SetString(socket_error, "service/proto not found"); |
| 3610 | return NULL; |
| 3611 | } |
| 3612 | return PyInt_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3613 | } |
| 3614 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3615 | PyDoc_STRVAR(getservbyname_doc, |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3616 | "getservbyname(servicename[, protocolname]) -> integer\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3617 | \n\ |
| 3618 | Return a port number from a service name and protocol name.\n\ |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3619 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 3620 | otherwise any protocol will match."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3621 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3622 | |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3623 | /* Python interface to getservbyport(port). |
| 3624 | This only returns the service name, since the other info is already |
| 3625 | known or not useful (like the list of aliases). */ |
| 3626 | |
| 3627 | /*ARGSUSED*/ |
| 3628 | static PyObject * |
| 3629 | socket_getservbyport(PyObject *self, PyObject *args) |
| 3630 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3631 | int port; |
| 3632 | char *proto=NULL; |
| 3633 | struct servent *sp; |
| 3634 | if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto)) |
| 3635 | return NULL; |
| 3636 | if (port < 0 || port > 0xffff) { |
| 3637 | PyErr_SetString( |
| 3638 | PyExc_OverflowError, |
| 3639 | "getservbyport: port must be 0-65535."); |
| 3640 | return NULL; |
| 3641 | } |
| 3642 | Py_BEGIN_ALLOW_THREADS |
| 3643 | sp = getservbyport(htons((short)port), proto); |
| 3644 | Py_END_ALLOW_THREADS |
| 3645 | if (sp == NULL) { |
| 3646 | PyErr_SetString(socket_error, "port/proto not found"); |
| 3647 | return NULL; |
| 3648 | } |
| 3649 | return PyString_FromString(sp->s_name); |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3650 | } |
| 3651 | |
| 3652 | PyDoc_STRVAR(getservbyport_doc, |
| 3653 | "getservbyport(port[, protocolname]) -> string\n\ |
| 3654 | \n\ |
| 3655 | Return the service name from a port number and protocol name.\n\ |
| 3656 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 3657 | otherwise any protocol will match."); |
| 3658 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3659 | /* Python interface to getprotobyname(name). |
| 3660 | This only returns the protocol number, since the other info is |
| 3661 | already known or not useful (like the list of aliases). */ |
| 3662 | |
| 3663 | /*ARGSUSED*/ |
| 3664 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3665 | socket_getprotobyname(PyObject *self, PyObject *args) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3666 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3667 | char *name; |
| 3668 | struct protoent *sp; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3669 | #ifdef __BEOS__ |
| 3670 | /* Not available in BeOS yet. - [cjh] */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3671 | PyErr_SetString(socket_error, "getprotobyname not supported"); |
| 3672 | return NULL; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3673 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3674 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) |
| 3675 | return NULL; |
| 3676 | Py_BEGIN_ALLOW_THREADS |
| 3677 | sp = getprotobyname(name); |
| 3678 | Py_END_ALLOW_THREADS |
| 3679 | if (sp == NULL) { |
| 3680 | PyErr_SetString(socket_error, "protocol not found"); |
| 3681 | return NULL; |
| 3682 | } |
| 3683 | return PyInt_FromLong((long) sp->p_proto); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3684 | #endif |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3685 | } |
| 3686 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3687 | PyDoc_STRVAR(getprotobyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3688 | "getprotobyname(name) -> integer\n\ |
| 3689 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3690 | Return the protocol number for the named protocol. (Rarely used.)"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3691 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3692 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3693 | #ifdef HAVE_SOCKETPAIR |
| 3694 | /* Create a pair of sockets using the socketpair() function. |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 3695 | Arguments as for socket() except the default family is AF_UNIX if |
Dave Cole | e8bbfe4 | 2004-08-26 00:51:16 +0000 | [diff] [blame] | 3696 | defined on the platform; otherwise, the default is AF_INET. */ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3697 | |
| 3698 | /*ARGSUSED*/ |
| 3699 | static PyObject * |
| 3700 | socket_socketpair(PyObject *self, PyObject *args) |
| 3701 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3702 | PySocketSockObject *s0 = NULL, *s1 = NULL; |
| 3703 | SOCKET_T sv[2]; |
| 3704 | int family, type = SOCK_STREAM, proto = 0; |
| 3705 | PyObject *res = NULL; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3706 | |
| 3707 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3708 | family = AF_UNIX; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3709 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3710 | family = AF_INET; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3711 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3712 | if (!PyArg_ParseTuple(args, "|iii:socketpair", |
| 3713 | &family, &type, &proto)) |
| 3714 | return NULL; |
| 3715 | /* Create a pair of socket fds */ |
| 3716 | if (socketpair(family, type, proto, sv) < 0) |
| 3717 | return set_error(); |
| 3718 | s0 = new_sockobject(sv[0], family, type, proto); |
| 3719 | if (s0 == NULL) |
| 3720 | goto finally; |
| 3721 | s1 = new_sockobject(sv[1], family, type, proto); |
| 3722 | if (s1 == NULL) |
| 3723 | goto finally; |
| 3724 | res = PyTuple_Pack(2, s0, s1); |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3725 | |
| 3726 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3727 | if (res == NULL) { |
| 3728 | if (s0 == NULL) |
| 3729 | SOCKETCLOSE(sv[0]); |
| 3730 | if (s1 == NULL) |
| 3731 | SOCKETCLOSE(sv[1]); |
| 3732 | } |
| 3733 | Py_XDECREF(s0); |
| 3734 | Py_XDECREF(s1); |
| 3735 | return res; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3736 | } |
| 3737 | |
| 3738 | PyDoc_STRVAR(socketpair_doc, |
| 3739 | "socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\ |
| 3740 | \n\ |
| 3741 | Create a pair of socket objects from the sockets returned by the platform\n\ |
| 3742 | socketpair() function.\n\ |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 3743 | The arguments are the same as for socket() except the default family is\n\ |
Dave Cole | e8bbfe4 | 2004-08-26 00:51:16 +0000 | [diff] [blame] | 3744 | AF_UNIX if defined on the platform; otherwise, the default is AF_INET."); |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3745 | |
| 3746 | #endif /* HAVE_SOCKETPAIR */ |
| 3747 | |
| 3748 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3749 | #ifndef NO_DUP |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3750 | /* Create a socket object from a numeric file description. |
| 3751 | Useful e.g. if stdin is a socket. |
| 3752 | Additional arguments as for socket(). */ |
| 3753 | |
| 3754 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3755 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3756 | socket_fromfd(PyObject *self, PyObject *args) |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3757 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3758 | PySocketSockObject *s; |
| 3759 | SOCKET_T fd; |
| 3760 | int family, type, proto = 0; |
| 3761 | if (!PyArg_ParseTuple(args, "iii|i:fromfd", |
| 3762 | &fd, &family, &type, &proto)) |
| 3763 | return NULL; |
| 3764 | /* Dup the fd so it and the socket can be closed independently */ |
| 3765 | fd = dup(fd); |
| 3766 | if (fd < 0) |
| 3767 | return set_error(); |
| 3768 | s = new_sockobject(fd, family, type, proto); |
| 3769 | return (PyObject *) s; |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3770 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3771 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3772 | PyDoc_STRVAR(fromfd_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3773 | "fromfd(fd, family, type[, proto]) -> socket object\n\ |
| 3774 | \n\ |
Georg Brandl | dcfdae7 | 2006-04-01 07:33:08 +0000 | [diff] [blame] | 3775 | Create a socket object from a duplicate of the given\n\ |
| 3776 | file descriptor.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3777 | The remaining arguments are the same as for socket()."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3778 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3779 | #endif /* NO_DUP */ |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3780 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3781 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3782 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3783 | socket_ntohs(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3784 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3785 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3786 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3787 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { |
| 3788 | return NULL; |
| 3789 | } |
| 3790 | if (x1 < 0) { |
| 3791 | PyErr_SetString(PyExc_OverflowError, |
| 3792 | "can't convert negative number to unsigned long"); |
| 3793 | return NULL; |
| 3794 | } |
| 3795 | x2 = (unsigned int)ntohs((unsigned short)x1); |
| 3796 | return PyInt_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3797 | } |
| 3798 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3799 | PyDoc_STRVAR(ntohs_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3800 | "ntohs(integer) -> integer\n\ |
| 3801 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3802 | Convert a 16-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3803 | |
| 3804 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3805 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3806 | socket_ntohl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3807 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3808 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3809 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3810 | if (PyInt_Check(arg)) { |
| 3811 | x = PyInt_AS_LONG(arg); |
| 3812 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3813 | return NULL; |
| 3814 | if ((long)x < 0) { |
| 3815 | PyErr_SetString(PyExc_OverflowError, |
| 3816 | "can't convert negative number to unsigned long"); |
| 3817 | return NULL; |
| 3818 | } |
| 3819 | } |
| 3820 | else if (PyLong_Check(arg)) { |
| 3821 | x = PyLong_AsUnsignedLong(arg); |
| 3822 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3823 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3824 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3825 | { |
| 3826 | unsigned long y; |
| 3827 | /* only want the trailing 32 bits */ |
| 3828 | y = x & 0xFFFFFFFFUL; |
| 3829 | if (y ^ x) |
| 3830 | return PyErr_Format(PyExc_OverflowError, |
| 3831 | "long int larger than 32 bits"); |
| 3832 | x = y; |
| 3833 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3834 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3835 | } |
| 3836 | else |
| 3837 | return PyErr_Format(PyExc_TypeError, |
| 3838 | "expected int/long, %s found", |
| 3839 | Py_TYPE(arg)->tp_name); |
| 3840 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3841 | return NULL; |
| 3842 | return PyLong_FromUnsignedLong(ntohl(x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3843 | } |
| 3844 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3845 | PyDoc_STRVAR(ntohl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3846 | "ntohl(integer) -> integer\n\ |
| 3847 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3848 | Convert a 32-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3849 | |
| 3850 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3851 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3852 | socket_htons(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3853 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3854 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3855 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3856 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) { |
| 3857 | return NULL; |
| 3858 | } |
| 3859 | if (x1 < 0) { |
| 3860 | PyErr_SetString(PyExc_OverflowError, |
| 3861 | "can't convert negative number to unsigned long"); |
| 3862 | return NULL; |
| 3863 | } |
| 3864 | x2 = (unsigned int)htons((unsigned short)x1); |
| 3865 | return PyInt_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3866 | } |
| 3867 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3868 | PyDoc_STRVAR(htons_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3869 | "htons(integer) -> integer\n\ |
| 3870 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3871 | Convert a 16-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3872 | |
| 3873 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3874 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3875 | socket_htonl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3876 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3877 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3878 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3879 | if (PyInt_Check(arg)) { |
| 3880 | x = PyInt_AS_LONG(arg); |
| 3881 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3882 | return NULL; |
| 3883 | if ((long)x < 0) { |
| 3884 | PyErr_SetString(PyExc_OverflowError, |
| 3885 | "can't convert negative number to unsigned long"); |
| 3886 | return NULL; |
| 3887 | } |
| 3888 | } |
| 3889 | else if (PyLong_Check(arg)) { |
| 3890 | x = PyLong_AsUnsignedLong(arg); |
| 3891 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3892 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3893 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3894 | { |
| 3895 | unsigned long y; |
| 3896 | /* only want the trailing 32 bits */ |
| 3897 | y = x & 0xFFFFFFFFUL; |
| 3898 | if (y ^ x) |
| 3899 | return PyErr_Format(PyExc_OverflowError, |
| 3900 | "long int larger than 32 bits"); |
| 3901 | x = y; |
| 3902 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3903 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3904 | } |
| 3905 | else |
| 3906 | return PyErr_Format(PyExc_TypeError, |
| 3907 | "expected int/long, %s found", |
| 3908 | Py_TYPE(arg)->tp_name); |
| 3909 | return PyLong_FromUnsignedLong(htonl((unsigned long)x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3910 | } |
| 3911 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3912 | PyDoc_STRVAR(htonl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3913 | "htonl(integer) -> integer\n\ |
| 3914 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3915 | Convert a 32-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3916 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3917 | /* socket.inet_aton() and socket.inet_ntoa() functions. */ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3918 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3919 | PyDoc_STRVAR(inet_aton_doc, |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3920 | "inet_aton(string) -> packed 32-bit IP representation\n\ |
| 3921 | \n\ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3922 | Convert an IP address in string format (123.45.67.89) to the 32-bit packed\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3923 | binary format used in low-level network functions."); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3924 | |
| 3925 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3926 | socket_inet_aton(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3927 | { |
Guido van Rossum | a2e4855 | 1999-09-09 15:42:59 +0000 | [diff] [blame] | 3928 | #ifndef INADDR_NONE |
| 3929 | #define INADDR_NONE (-1) |
| 3930 | #endif |
Neal Norwitz | 88f115b | 2003-02-13 02:15:42 +0000 | [diff] [blame] | 3931 | #ifdef HAVE_INET_ATON |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3932 | struct in_addr buf; |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 3933 | #endif |
| 3934 | |
| 3935 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
Gregory P. Smith | 3605b5c | 2009-02-11 23:45:25 +0000 | [diff] [blame] | 3936 | #if (SIZEOF_INT != 4) |
| 3937 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 3938 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3939 | /* Have to use inet_addr() instead */ |
| 3940 | unsigned int packed_addr; |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3941 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3942 | char *ip_addr; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3943 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3944 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) |
| 3945 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3946 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3947 | |
| 3948 | #ifdef HAVE_INET_ATON |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 3949 | |
| 3950 | #ifdef USE_INET_ATON_WEAKLINK |
| 3951 | if (inet_aton != NULL) { |
| 3952 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3953 | if (inet_aton(ip_addr, &buf)) |
| 3954 | return PyString_FromStringAndSize((char *)(&buf), |
| 3955 | sizeof(buf)); |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3956 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3957 | PyErr_SetString(socket_error, |
| 3958 | "illegal IP address string passed to inet_aton"); |
| 3959 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3960 | |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 3961 | #ifdef USE_INET_ATON_WEAKLINK |
| 3962 | } else { |
| 3963 | #endif |
| 3964 | |
| 3965 | #endif |
| 3966 | |
| 3967 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
| 3968 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3969 | /* special-case this address as inet_addr might return INADDR_NONE |
| 3970 | * for this */ |
| 3971 | if (strcmp(ip_addr, "255.255.255.255") == 0) { |
| 3972 | packed_addr = 0xFFFFFFFF; |
| 3973 | } else { |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 3974 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3975 | packed_addr = inet_addr(ip_addr); |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3976 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3977 | if (packed_addr == INADDR_NONE) { /* invalid address */ |
| 3978 | PyErr_SetString(socket_error, |
| 3979 | "illegal IP address string passed to inet_aton"); |
| 3980 | return NULL; |
| 3981 | } |
| 3982 | } |
| 3983 | return PyString_FromStringAndSize((char *) &packed_addr, |
| 3984 | sizeof(packed_addr)); |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 3985 | |
| 3986 | #ifdef USE_INET_ATON_WEAKLINK |
| 3987 | } |
| 3988 | #endif |
| 3989 | |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3990 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3991 | } |
| 3992 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3993 | PyDoc_STRVAR(inet_ntoa_doc, |
Fred Drake | e066134 | 2000-03-07 14:05:16 +0000 | [diff] [blame] | 3994 | "inet_ntoa(packed_ip) -> ip_address_string\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3995 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3996 | Convert an IP address from 32-bit packed binary format to string format"); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3997 | |
| 3998 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3999 | socket_inet_ntoa(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4000 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4001 | char *packed_str; |
| 4002 | int addr_len; |
| 4003 | struct in_addr packed_addr; |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4004 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4005 | if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) { |
| 4006 | return NULL; |
| 4007 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 4008 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4009 | if (addr_len != sizeof(packed_addr)) { |
| 4010 | PyErr_SetString(socket_error, |
| 4011 | "packed IP wrong length for inet_ntoa"); |
| 4012 | return NULL; |
| 4013 | } |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4014 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4015 | memcpy(&packed_addr, packed_str, addr_len); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4016 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4017 | return PyString_FromString(inet_ntoa(packed_addr)); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4018 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4019 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4020 | #ifdef HAVE_INET_PTON |
| 4021 | |
| 4022 | PyDoc_STRVAR(inet_pton_doc, |
| 4023 | "inet_pton(af, ip) -> packed IP address string\n\ |
| 4024 | \n\ |
| 4025 | Convert an IP address from string format to a packed string suitable\n\ |
| 4026 | for use with low-level network functions."); |
| 4027 | |
| 4028 | static PyObject * |
| 4029 | socket_inet_pton(PyObject *self, PyObject *args) |
| 4030 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4031 | int af; |
| 4032 | char* ip; |
| 4033 | int retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4034 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4035 | char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4036 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4037 | char packed[sizeof(struct in_addr)]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4038 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4039 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 4040 | return NULL; |
| 4041 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4042 | |
Martin v. Löwis | 04697e8 | 2004-06-02 12:35:29 +0000 | [diff] [blame] | 4043 | #if !defined(ENABLE_IPV6) && defined(AF_INET6) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4044 | if(af == AF_INET6) { |
| 4045 | PyErr_SetString(socket_error, |
| 4046 | "can't use AF_INET6, IPv6 is disabled"); |
| 4047 | return NULL; |
| 4048 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 4049 | #endif |
Martin v. Löwis | 1064909 | 2003-08-05 06:25:06 +0000 | [diff] [blame] | 4050 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4051 | retval = inet_pton(af, ip, packed); |
| 4052 | if (retval < 0) { |
| 4053 | PyErr_SetFromErrno(socket_error); |
| 4054 | return NULL; |
| 4055 | } else if (retval == 0) { |
| 4056 | PyErr_SetString(socket_error, |
| 4057 | "illegal IP address string passed to inet_pton"); |
| 4058 | return NULL; |
| 4059 | } else if (af == AF_INET) { |
| 4060 | return PyString_FromStringAndSize(packed, |
| 4061 | sizeof(struct in_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4062 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4063 | } else if (af == AF_INET6) { |
| 4064 | return PyString_FromStringAndSize(packed, |
| 4065 | sizeof(struct in6_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4066 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4067 | } else { |
| 4068 | PyErr_SetString(socket_error, "unknown address family"); |
| 4069 | return NULL; |
| 4070 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4071 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 4072 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4073 | PyDoc_STRVAR(inet_ntop_doc, |
| 4074 | "inet_ntop(af, packed_ip) -> string formatted IP address\n\ |
| 4075 | \n\ |
| 4076 | Convert a packed IP address of the given family to string format."); |
| 4077 | |
| 4078 | static PyObject * |
| 4079 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 4080 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4081 | int af; |
| 4082 | char* packed; |
| 4083 | int len; |
| 4084 | const char* retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4085 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4086 | char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4087 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4088 | char ip[INET_ADDRSTRLEN + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4089 | #endif |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 4090 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4091 | /* Guarantee NUL-termination for PyString_FromString() below */ |
| 4092 | memset((void *) &ip[0], '\0', sizeof(ip)); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4093 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4094 | if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) { |
| 4095 | return NULL; |
| 4096 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4097 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4098 | if (af == AF_INET) { |
| 4099 | if (len != sizeof(struct in_addr)) { |
| 4100 | PyErr_SetString(PyExc_ValueError, |
| 4101 | "invalid length of packed IP address string"); |
| 4102 | return NULL; |
| 4103 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4104 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4105 | } else if (af == AF_INET6) { |
| 4106 | if (len != sizeof(struct in6_addr)) { |
| 4107 | PyErr_SetString(PyExc_ValueError, |
| 4108 | "invalid length of packed IP address string"); |
| 4109 | return NULL; |
| 4110 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4111 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4112 | } else { |
| 4113 | PyErr_Format(PyExc_ValueError, |
| 4114 | "unknown address family %d", af); |
| 4115 | return NULL; |
| 4116 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4117 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4118 | retval = inet_ntop(af, packed, ip, sizeof(ip)); |
| 4119 | if (!retval) { |
| 4120 | PyErr_SetFromErrno(socket_error); |
| 4121 | return NULL; |
| 4122 | } else { |
| 4123 | return PyString_FromString(retval); |
| 4124 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4125 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4126 | /* NOTREACHED */ |
| 4127 | PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop"); |
| 4128 | return NULL; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4129 | } |
| 4130 | |
| 4131 | #endif /* HAVE_INET_PTON */ |
| 4132 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4133 | /* Python interface to getaddrinfo(host, port). */ |
| 4134 | |
| 4135 | /*ARGSUSED*/ |
| 4136 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4137 | socket_getaddrinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4138 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4139 | struct addrinfo hints, *res; |
| 4140 | struct addrinfo *res0 = NULL; |
| 4141 | PyObject *hobj = NULL; |
| 4142 | PyObject *pobj = (PyObject *)NULL; |
| 4143 | char pbuf[30]; |
| 4144 | char *hptr, *pptr; |
| 4145 | int family, socktype, protocol, flags; |
| 4146 | int error; |
| 4147 | PyObject *all = (PyObject *)NULL; |
| 4148 | PyObject *single = (PyObject *)NULL; |
| 4149 | PyObject *idna = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4150 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4151 | family = socktype = protocol = flags = 0; |
| 4152 | family = AF_UNSPEC; |
| 4153 | if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo", |
| 4154 | &hobj, &pobj, &family, &socktype, |
| 4155 | &protocol, &flags)) { |
| 4156 | return NULL; |
| 4157 | } |
| 4158 | if (hobj == Py_None) { |
| 4159 | hptr = NULL; |
| 4160 | } else if (PyUnicode_Check(hobj)) { |
| 4161 | idna = PyObject_CallMethod(hobj, "encode", "s", "idna"); |
| 4162 | if (!idna) |
| 4163 | return NULL; |
| 4164 | hptr = PyString_AsString(idna); |
| 4165 | } else if (PyString_Check(hobj)) { |
| 4166 | hptr = PyString_AsString(hobj); |
| 4167 | } else { |
| 4168 | PyErr_SetString(PyExc_TypeError, |
| 4169 | "getaddrinfo() argument 1 must be string or None"); |
| 4170 | return NULL; |
| 4171 | } |
Petri Lehtinen | ab7dd18 | 2012-12-20 21:06:14 +0200 | [diff] [blame] | 4172 | if (PyInt_Check(pobj) || PyLong_Check(pobj)) { |
| 4173 | long value = PyLong_AsLong(pobj); |
| 4174 | if (value == -1 && PyErr_Occurred()) |
| 4175 | return NULL; |
| 4176 | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4177 | pptr = pbuf; |
| 4178 | } else if (PyString_Check(pobj)) { |
| 4179 | pptr = PyString_AsString(pobj); |
| 4180 | } else if (pobj == Py_None) { |
| 4181 | pptr = (char *)NULL; |
| 4182 | } else { |
Petri Lehtinen | ab7dd18 | 2012-12-20 21:06:14 +0200 | [diff] [blame] | 4183 | PyErr_SetString(socket_error, |
| 4184 | "getaddrinfo() argument 2 must be integer or string"); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4185 | goto err; |
| 4186 | } |
Ronald Oussoren | 0e6283e | 2013-06-10 10:35:36 +0200 | [diff] [blame] | 4187 | #if defined(__APPLE__) && defined(AI_NUMERICSERV) |
Ronald Oussoren | 16c52a3 | 2013-05-24 13:45:27 +0200 | [diff] [blame] | 4188 | if ((flags & AI_NUMERICSERV) && (pptr == NULL || (pptr[0] == '0' && pptr[1] == 0))) { |
| 4189 | /* On OSX upto at least OSX 10.8 getaddrinfo crashes |
| 4190 | * if AI_NUMERICSERV is set and the servname is NULL or "0". |
| 4191 | * This workaround avoids a segfault in libsystem. |
| 4192 | */ |
| 4193 | pptr = "00"; |
| 4194 | } |
| 4195 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4196 | memset(&hints, 0, sizeof(hints)); |
| 4197 | hints.ai_family = family; |
| 4198 | hints.ai_socktype = socktype; |
| 4199 | hints.ai_protocol = protocol; |
| 4200 | hints.ai_flags = flags; |
| 4201 | Py_BEGIN_ALLOW_THREADS |
| 4202 | ACQUIRE_GETADDRINFO_LOCK |
| 4203 | error = getaddrinfo(hptr, pptr, &hints, &res0); |
| 4204 | Py_END_ALLOW_THREADS |
| 4205 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 4206 | if (error) { |
| 4207 | set_gaierror(error); |
| 4208 | goto err; |
| 4209 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4210 | |
Benjamin Peterson | a5a44ab | 2015-04-01 11:16:40 -0400 | [diff] [blame] | 4211 | all = PyList_New(0); |
| 4212 | if (all == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4213 | goto err; |
| 4214 | for (res = res0; res; res = res->ai_next) { |
| 4215 | PyObject *addr = |
| 4216 | makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol); |
| 4217 | if (addr == NULL) |
| 4218 | goto err; |
| 4219 | single = Py_BuildValue("iiisO", res->ai_family, |
| 4220 | res->ai_socktype, res->ai_protocol, |
| 4221 | res->ai_canonname ? res->ai_canonname : "", |
| 4222 | addr); |
| 4223 | Py_DECREF(addr); |
| 4224 | if (single == NULL) |
| 4225 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4226 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4227 | if (PyList_Append(all, single)) |
| 4228 | goto err; |
| 4229 | Py_XDECREF(single); |
| 4230 | } |
| 4231 | Py_XDECREF(idna); |
| 4232 | if (res0) |
| 4233 | freeaddrinfo(res0); |
| 4234 | return all; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4235 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4236 | Py_XDECREF(single); |
| 4237 | Py_XDECREF(all); |
| 4238 | Py_XDECREF(idna); |
| 4239 | if (res0) |
| 4240 | freeaddrinfo(res0); |
| 4241 | return (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4242 | } |
| 4243 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4244 | PyDoc_STRVAR(getaddrinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4245 | "getaddrinfo(host, port [, family, socktype, proto, flags])\n\ |
| 4246 | -> list of (family, socktype, proto, canonname, sockaddr)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4247 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4248 | Resolve host and port into addrinfo struct."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4249 | |
| 4250 | /* Python interface to getnameinfo(sa, flags). */ |
| 4251 | |
| 4252 | /*ARGSUSED*/ |
| 4253 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4254 | socket_getnameinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4255 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4256 | PyObject *sa = (PyObject *)NULL; |
| 4257 | int flags; |
| 4258 | char *hostp; |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 4259 | int port; |
| 4260 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4261 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; |
| 4262 | struct addrinfo hints, *res = NULL; |
| 4263 | int error; |
| 4264 | PyObject *ret = (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4265 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4266 | flags = flowinfo = scope_id = 0; |
| 4267 | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) |
| 4268 | return NULL; |
| 4269 | if (!PyTuple_Check(sa)) { |
| 4270 | PyErr_SetString(PyExc_TypeError, |
| 4271 | "getnameinfo() argument 1 must be a tuple"); |
| 4272 | return NULL; |
| 4273 | } |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 4274 | if (!PyArg_ParseTuple(sa, "si|II", |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4275 | &hostp, &port, &flowinfo, &scope_id)) |
| 4276 | return NULL; |
Charles-François Natali | 65dd745 | 2012-06-23 10:06:56 +0200 | [diff] [blame] | 4277 | if (flowinfo > 0xfffff) { |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 4278 | PyErr_SetString(PyExc_OverflowError, |
| 4279 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 4280 | return NULL; |
| 4281 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4282 | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); |
| 4283 | memset(&hints, 0, sizeof(hints)); |
| 4284 | hints.ai_family = AF_UNSPEC; |
| 4285 | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ |
| 4286 | Py_BEGIN_ALLOW_THREADS |
| 4287 | ACQUIRE_GETADDRINFO_LOCK |
| 4288 | error = getaddrinfo(hostp, pbuf, &hints, &res); |
| 4289 | Py_END_ALLOW_THREADS |
| 4290 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 4291 | if (error) { |
| 4292 | set_gaierror(error); |
| 4293 | goto fail; |
| 4294 | } |
| 4295 | if (res->ai_next) { |
| 4296 | PyErr_SetString(socket_error, |
| 4297 | "sockaddr resolved to multiple addresses"); |
| 4298 | goto fail; |
| 4299 | } |
| 4300 | switch (res->ai_family) { |
| 4301 | case AF_INET: |
| 4302 | { |
| 4303 | if (PyTuple_GET_SIZE(sa) != 2) { |
| 4304 | PyErr_SetString(socket_error, |
| 4305 | "IPv4 sockaddr must be 2 tuple"); |
| 4306 | goto fail; |
| 4307 | } |
| 4308 | break; |
| 4309 | } |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4310 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4311 | case AF_INET6: |
| 4312 | { |
| 4313 | struct sockaddr_in6 *sin6; |
| 4314 | sin6 = (struct sockaddr_in6 *)res->ai_addr; |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 4315 | sin6->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4316 | sin6->sin6_scope_id = scope_id; |
| 4317 | break; |
| 4318 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4319 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4320 | } |
| 4321 | error = getnameinfo(res->ai_addr, res->ai_addrlen, |
| 4322 | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); |
| 4323 | if (error) { |
| 4324 | set_gaierror(error); |
| 4325 | goto fail; |
| 4326 | } |
| 4327 | ret = Py_BuildValue("ss", hbuf, pbuf); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4328 | |
| 4329 | fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4330 | if (res) |
| 4331 | freeaddrinfo(res); |
| 4332 | return ret; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4333 | } |
| 4334 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4335 | PyDoc_STRVAR(getnameinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4336 | "getnameinfo(sockaddr, flags) --> (host, port)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4337 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4338 | Get host and port for a sockaddr."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4339 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4340 | |
| 4341 | /* Python API to getting and setting the default timeout value. */ |
| 4342 | |
| 4343 | static PyObject * |
| 4344 | socket_getdefaulttimeout(PyObject *self) |
| 4345 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4346 | if (defaulttimeout < 0.0) { |
| 4347 | Py_INCREF(Py_None); |
| 4348 | return Py_None; |
| 4349 | } |
| 4350 | else |
| 4351 | return PyFloat_FromDouble(defaulttimeout); |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4352 | } |
| 4353 | |
| 4354 | PyDoc_STRVAR(getdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4355 | "getdefaulttimeout() -> timeout\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4356 | \n\ |
Ezio Melotti | ca5e908 | 2011-08-14 08:27:36 +0300 | [diff] [blame] | 4357 | Returns the default timeout in seconds (float) for new socket objects.\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4358 | A value of None indicates that new socket objects have no timeout.\n\ |
| 4359 | When the socket module is first imported, the default is None."); |
| 4360 | |
| 4361 | static PyObject * |
| 4362 | socket_setdefaulttimeout(PyObject *self, PyObject *arg) |
| 4363 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4364 | double timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4365 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4366 | if (arg == Py_None) |
| 4367 | timeout = -1.0; |
| 4368 | else { |
| 4369 | timeout = PyFloat_AsDouble(arg); |
| 4370 | if (timeout < 0.0) { |
| 4371 | if (!PyErr_Occurred()) |
| 4372 | PyErr_SetString(PyExc_ValueError, |
| 4373 | "Timeout value out of range"); |
| 4374 | return NULL; |
| 4375 | } |
| 4376 | } |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4377 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4378 | defaulttimeout = timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4379 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4380 | Py_INCREF(Py_None); |
| 4381 | return Py_None; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4382 | } |
| 4383 | |
| 4384 | PyDoc_STRVAR(setdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4385 | "setdefaulttimeout(timeout)\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4386 | \n\ |
Ezio Melotti | ca5e908 | 2011-08-14 08:27:36 +0300 | [diff] [blame] | 4387 | Set the default timeout in seconds (float) for new socket objects.\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4388 | A value of None indicates that new socket objects have no timeout.\n\ |
| 4389 | When the socket module is first imported, the default is None."); |
| 4390 | |
| 4391 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4392 | /* List of functions exported by this module. */ |
| 4393 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4394 | static PyMethodDef socket_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4395 | {"gethostbyname", socket_gethostbyname, |
| 4396 | METH_VARARGS, gethostbyname_doc}, |
| 4397 | {"gethostbyname_ex", socket_gethostbyname_ex, |
| 4398 | METH_VARARGS, ghbn_ex_doc}, |
| 4399 | {"gethostbyaddr", socket_gethostbyaddr, |
| 4400 | METH_VARARGS, gethostbyaddr_doc}, |
| 4401 | {"gethostname", socket_gethostname, |
| 4402 | METH_NOARGS, gethostname_doc}, |
| 4403 | {"getservbyname", socket_getservbyname, |
| 4404 | METH_VARARGS, getservbyname_doc}, |
| 4405 | {"getservbyport", socket_getservbyport, |
| 4406 | METH_VARARGS, getservbyport_doc}, |
| 4407 | {"getprotobyname", socket_getprotobyname, |
| 4408 | METH_VARARGS, getprotobyname_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4409 | #ifndef NO_DUP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4410 | {"fromfd", socket_fromfd, |
| 4411 | METH_VARARGS, fromfd_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4412 | #endif |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4413 | #ifdef HAVE_SOCKETPAIR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4414 | {"socketpair", socket_socketpair, |
| 4415 | METH_VARARGS, socketpair_doc}, |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4416 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4417 | {"ntohs", socket_ntohs, |
| 4418 | METH_VARARGS, ntohs_doc}, |
| 4419 | {"ntohl", socket_ntohl, |
| 4420 | METH_O, ntohl_doc}, |
| 4421 | {"htons", socket_htons, |
| 4422 | METH_VARARGS, htons_doc}, |
| 4423 | {"htonl", socket_htonl, |
| 4424 | METH_O, htonl_doc}, |
| 4425 | {"inet_aton", socket_inet_aton, |
| 4426 | METH_VARARGS, inet_aton_doc}, |
| 4427 | {"inet_ntoa", socket_inet_ntoa, |
| 4428 | METH_VARARGS, inet_ntoa_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4429 | #ifdef HAVE_INET_PTON |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4430 | {"inet_pton", socket_inet_pton, |
| 4431 | METH_VARARGS, inet_pton_doc}, |
| 4432 | {"inet_ntop", socket_inet_ntop, |
| 4433 | METH_VARARGS, inet_ntop_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4434 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4435 | {"getaddrinfo", socket_getaddrinfo, |
| 4436 | METH_VARARGS, getaddrinfo_doc}, |
| 4437 | {"getnameinfo", socket_getnameinfo, |
| 4438 | METH_VARARGS, getnameinfo_doc}, |
| 4439 | {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout, |
| 4440 | METH_NOARGS, getdefaulttimeout_doc}, |
| 4441 | {"setdefaulttimeout", socket_setdefaulttimeout, |
| 4442 | METH_O, setdefaulttimeout_doc}, |
| 4443 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4444 | }; |
| 4445 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4446 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4447 | #ifdef RISCOS |
| 4448 | #define OS_INIT_DEFINED |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4449 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4450 | static int |
| 4451 | os_init(void) |
| 4452 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4453 | _kernel_swi_regs r; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4454 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4455 | r.r[0] = 0; |
| 4456 | _kernel_swi(0x43380, &r, &r); |
| 4457 | taskwindow = r.r[0]; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4458 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4459 | return 1; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4460 | } |
| 4461 | |
| 4462 | #endif /* RISCOS */ |
| 4463 | |
| 4464 | |
| 4465 | #ifdef MS_WINDOWS |
| 4466 | #define OS_INIT_DEFINED |
| 4467 | |
| 4468 | /* Additional initialization and cleanup for Windows */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4469 | |
| 4470 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4471 | os_cleanup(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4472 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4473 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4474 | } |
| 4475 | |
| 4476 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4477 | os_init(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4478 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4479 | WSADATA WSAData; |
| 4480 | int ret; |
| 4481 | char buf[100]; |
| 4482 | ret = WSAStartup(0x0101, &WSAData); |
| 4483 | switch (ret) { |
| 4484 | case 0: /* No error */ |
| 4485 | Py_AtExit(os_cleanup); |
| 4486 | return 1; /* Success */ |
| 4487 | case WSASYSNOTREADY: |
| 4488 | PyErr_SetString(PyExc_ImportError, |
| 4489 | "WSAStartup failed: network not ready"); |
| 4490 | break; |
| 4491 | case WSAVERNOTSUPPORTED: |
| 4492 | case WSAEINVAL: |
| 4493 | PyErr_SetString( |
| 4494 | PyExc_ImportError, |
| 4495 | "WSAStartup failed: requested version not supported"); |
| 4496 | break; |
| 4497 | default: |
| 4498 | PyOS_snprintf(buf, sizeof(buf), |
| 4499 | "WSAStartup failed: error code %d", ret); |
| 4500 | PyErr_SetString(PyExc_ImportError, buf); |
| 4501 | break; |
| 4502 | } |
| 4503 | return 0; /* Failure */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4504 | } |
| 4505 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 4506 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4507 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4508 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4509 | #ifdef PYOS_OS2 |
| 4510 | #define OS_INIT_DEFINED |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4511 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4512 | /* Additional initialization for OS/2 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4513 | |
| 4514 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4515 | os_init(void) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4516 | { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4517 | #ifndef PYCC_GCC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4518 | char reason[64]; |
| 4519 | int rc = sock_init(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4520 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4521 | if (rc == 0) { |
| 4522 | return 1; /* Success */ |
| 4523 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4524 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4525 | PyOS_snprintf(reason, sizeof(reason), |
| 4526 | "OS/2 TCP/IP Error# %d", sock_errno()); |
| 4527 | PyErr_SetString(PyExc_ImportError, reason); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4528 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4529 | return 0; /* Failure */ |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 4530 | #else |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 4531 | /* No need to initialize sockets with GCC/EMX */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4532 | return 1; /* Success */ |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 4533 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4534 | } |
| 4535 | |
| 4536 | #endif /* PYOS_OS2 */ |
| 4537 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4538 | |
| 4539 | #ifndef OS_INIT_DEFINED |
| 4540 | static int |
| 4541 | os_init(void) |
| 4542 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4543 | return 1; /* Success */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4544 | } |
| 4545 | #endif |
| 4546 | |
| 4547 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4548 | /* C API table - always add new things to the end for binary |
| 4549 | compatibility. */ |
| 4550 | static |
| 4551 | PySocketModule_APIObject PySocketModuleAPI = |
| 4552 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4553 | &sock_type, |
| 4554 | NULL |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4555 | }; |
| 4556 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4557 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4558 | /* Initialize the _socket module. |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4559 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4560 | This module is actually called "_socket", and there's a wrapper |
| 4561 | "socket.py" which implements some additional functionality. On some |
| 4562 | platforms (e.g. Windows and OS/2), socket.py also implements a |
| 4563 | wrapper for the socket type that provides missing functionality such |
| 4564 | as makefile(), dup() and fromfd(). The import of "_socket" may fail |
| 4565 | with an ImportError exception if os-specific initialization fails. |
| 4566 | On Windows, this does WINSOCK initialization. When WINSOCK is |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 4567 | initialized successfully, a call to WSACleanup() is scheduled to be |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4568 | made at exit time. |
| 4569 | */ |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4570 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4571 | PyDoc_STRVAR(socket_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4572 | "Implementation module for socket operations.\n\ |
| 4573 | \n\ |
| 4574 | See the socket module for documentation."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4575 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4576 | PyMODINIT_FUNC |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 4577 | init_socket(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4578 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4579 | PyObject *m, *has_ipv6; |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4580 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4581 | if (!os_init()) |
| 4582 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4583 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4584 | Py_TYPE(&sock_type) = &PyType_Type; |
| 4585 | m = Py_InitModule3(PySocket_MODULE_NAME, |
| 4586 | socket_methods, |
| 4587 | socket_doc); |
| 4588 | if (m == NULL) |
| 4589 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4590 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4591 | socket_error = PyErr_NewException("socket.error", |
| 4592 | PyExc_IOError, NULL); |
| 4593 | if (socket_error == NULL) |
| 4594 | return; |
| 4595 | PySocketModuleAPI.error = socket_error; |
| 4596 | Py_INCREF(socket_error); |
| 4597 | PyModule_AddObject(m, "error", socket_error); |
| 4598 | socket_herror = PyErr_NewException("socket.herror", |
| 4599 | socket_error, NULL); |
| 4600 | if (socket_herror == NULL) |
| 4601 | return; |
| 4602 | Py_INCREF(socket_herror); |
| 4603 | PyModule_AddObject(m, "herror", socket_herror); |
| 4604 | socket_gaierror = PyErr_NewException("socket.gaierror", socket_error, |
| 4605 | NULL); |
| 4606 | if (socket_gaierror == NULL) |
| 4607 | return; |
| 4608 | Py_INCREF(socket_gaierror); |
| 4609 | PyModule_AddObject(m, "gaierror", socket_gaierror); |
| 4610 | socket_timeout = PyErr_NewException("socket.timeout", |
| 4611 | socket_error, NULL); |
| 4612 | if (socket_timeout == NULL) |
| 4613 | return; |
| 4614 | Py_INCREF(socket_timeout); |
| 4615 | PyModule_AddObject(m, "timeout", socket_timeout); |
| 4616 | Py_INCREF((PyObject *)&sock_type); |
| 4617 | if (PyModule_AddObject(m, "SocketType", |
| 4618 | (PyObject *)&sock_type) != 0) |
| 4619 | return; |
| 4620 | Py_INCREF((PyObject *)&sock_type); |
| 4621 | if (PyModule_AddObject(m, "socket", |
| 4622 | (PyObject *)&sock_type) != 0) |
| 4623 | return; |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4624 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4625 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4626 | has_ipv6 = Py_True; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4627 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4628 | has_ipv6 = Py_False; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4629 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4630 | Py_INCREF(has_ipv6); |
| 4631 | PyModule_AddObject(m, "has_ipv6", has_ipv6); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4632 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4633 | /* Export C API */ |
| 4634 | if (PyModule_AddObject(m, PySocket_CAPI_NAME, |
| 4635 | PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL) |
| 4636 | ) != 0) |
| 4637 | return; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4638 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4639 | /* Address families (we only support AF_INET and AF_UNIX) */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4640 | #ifdef AF_UNSPEC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4641 | PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4642 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4643 | PyModule_AddIntConstant(m, "AF_INET", AF_INET); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4644 | #ifdef AF_INET6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4645 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4646 | #endif /* AF_INET6 */ |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 4647 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4648 | PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4649 | #endif /* AF_UNIX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4650 | #ifdef AF_AX25 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4651 | /* Amateur Radio AX.25 */ |
| 4652 | PyModule_AddIntConstant(m, "AF_AX25", AF_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4653 | #endif |
| 4654 | #ifdef AF_IPX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4655 | PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4656 | #endif |
| 4657 | #ifdef AF_APPLETALK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4658 | /* Appletalk DDP */ |
| 4659 | PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4660 | #endif |
| 4661 | #ifdef AF_NETROM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4662 | /* Amateur radio NetROM */ |
| 4663 | PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4664 | #endif |
| 4665 | #ifdef AF_BRIDGE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4666 | /* Multiprotocol bridge */ |
| 4667 | PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4668 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4669 | #ifdef AF_ATMPVC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4670 | /* ATM PVCs */ |
| 4671 | PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4672 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4673 | #ifdef AF_AAL5 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4674 | /* Reserved for Werner's ATM */ |
| 4675 | PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4676 | #endif |
| 4677 | #ifdef AF_X25 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4678 | /* Reserved for X.25 project */ |
| 4679 | PyModule_AddIntConstant(m, "AF_X25", AF_X25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4680 | #endif |
| 4681 | #ifdef AF_INET6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4682 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4683 | #endif |
| 4684 | #ifdef AF_ROSE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4685 | /* Amateur Radio X.25 PLP */ |
| 4686 | PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4687 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4688 | #ifdef AF_DECnet |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4689 | /* Reserved for DECnet project */ |
| 4690 | PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4691 | #endif |
| 4692 | #ifdef AF_NETBEUI |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4693 | /* Reserved for 802.2LLC project */ |
| 4694 | PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4695 | #endif |
| 4696 | #ifdef AF_SECURITY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4697 | /* Security callback pseudo AF */ |
| 4698 | PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4699 | #endif |
| 4700 | #ifdef AF_KEY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4701 | /* PF_KEY key management API */ |
| 4702 | PyModule_AddIntConstant(m, "AF_KEY", AF_KEY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4703 | #endif |
| 4704 | #ifdef AF_NETLINK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4705 | /* */ |
| 4706 | PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK); |
| 4707 | PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4708 | #ifdef NETLINK_SKIP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4709 | PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4710 | #endif |
| 4711 | #ifdef NETLINK_W1 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4712 | PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4713 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4714 | PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK); |
| 4715 | PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4716 | #ifdef NETLINK_TCPDIAG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4717 | PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4718 | #endif |
| 4719 | #ifdef NETLINK_NFLOG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4720 | PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4721 | #endif |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 4722 | #ifdef NETLINK_XFRM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4723 | PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM); |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 4724 | #endif |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4725 | #ifdef NETLINK_ARPD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4726 | PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4727 | #endif |
| 4728 | #ifdef NETLINK_ROUTE6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4729 | PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4730 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4731 | PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW); |
Martin v. Löwis | b1cc1d4 | 2007-02-13 12:14:19 +0000 | [diff] [blame] | 4732 | #ifdef NETLINK_DNRTMSG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4733 | PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG); |
| 4734 | #endif |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4735 | #ifdef NETLINK_TAPBASE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4736 | PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4737 | #endif |
Georg Brandl | dcfdae7 | 2006-04-01 07:33:08 +0000 | [diff] [blame] | 4738 | #endif /* AF_NETLINK */ |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4739 | #ifdef AF_ROUTE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4740 | /* Alias to emulate 4.4BSD */ |
| 4741 | PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4742 | #endif |
| 4743 | #ifdef AF_ASH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4744 | /* Ash */ |
| 4745 | PyModule_AddIntConstant(m, "AF_ASH", AF_ASH); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4746 | #endif |
| 4747 | #ifdef AF_ECONET |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4748 | /* Acorn Econet */ |
| 4749 | PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4750 | #endif |
| 4751 | #ifdef AF_ATMSVC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4752 | /* ATM SVCs */ |
| 4753 | PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4754 | #endif |
| 4755 | #ifdef AF_SNA |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4756 | /* Linux SNA Project (nutters!) */ |
| 4757 | PyModule_AddIntConstant(m, "AF_SNA", AF_SNA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4758 | #endif |
| 4759 | #ifdef AF_IRDA |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4760 | /* IRDA sockets */ |
| 4761 | PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4762 | #endif |
| 4763 | #ifdef AF_PPPOX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4764 | /* PPPoX sockets */ |
| 4765 | PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4766 | #endif |
| 4767 | #ifdef AF_WANPIPE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4768 | /* Wanpipe API Sockets */ |
| 4769 | PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4770 | #endif |
| 4771 | #ifdef AF_LLC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4772 | /* Linux LLC */ |
| 4773 | PyModule_AddIntConstant(m, "AF_LLC", AF_LLC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4774 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 4775 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4776 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4777 | PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH); |
| 4778 | PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP); |
| 4779 | PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI); |
| 4780 | PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI); |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 4781 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4782 | PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER); |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 4783 | #endif |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4784 | #if !defined(__FreeBSD__) |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 4785 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4786 | PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP); |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 4787 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4788 | PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR); |
| 4789 | PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4790 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4791 | PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM); |
| 4792 | PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00"); |
| 4793 | PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF"); |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 4794 | #endif |
| 4795 | |
Antoine Pitrou | e0c5f3c | 2010-10-27 20:35:26 +0000 | [diff] [blame] | 4796 | #ifdef AF_PACKET |
| 4797 | PyModule_AddIntMacro(m, AF_PACKET); |
| 4798 | #endif |
| 4799 | #ifdef PF_PACKET |
| 4800 | PyModule_AddIntMacro(m, PF_PACKET); |
| 4801 | #endif |
| 4802 | #ifdef PACKET_HOST |
| 4803 | PyModule_AddIntMacro(m, PACKET_HOST); |
| 4804 | #endif |
| 4805 | #ifdef PACKET_BROADCAST |
| 4806 | PyModule_AddIntMacro(m, PACKET_BROADCAST); |
| 4807 | #endif |
| 4808 | #ifdef PACKET_MULTICAST |
| 4809 | PyModule_AddIntMacro(m, PACKET_MULTICAST); |
| 4810 | #endif |
| 4811 | #ifdef PACKET_OTHERHOST |
| 4812 | PyModule_AddIntMacro(m, PACKET_OTHERHOST); |
| 4813 | #endif |
| 4814 | #ifdef PACKET_OUTGOING |
| 4815 | PyModule_AddIntMacro(m, PACKET_OUTGOING); |
| 4816 | #endif |
| 4817 | #ifdef PACKET_LOOPBACK |
| 4818 | PyModule_AddIntMacro(m, PACKET_LOOPBACK); |
| 4819 | #endif |
| 4820 | #ifdef PACKET_FASTROUTE |
| 4821 | PyModule_AddIntMacro(m, PACKET_FASTROUTE); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 4822 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4823 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4824 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4825 | PyModule_AddIntConstant(m, "AF_TIPC", AF_TIPC); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4826 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4827 | /* for addresses */ |
| 4828 | PyModule_AddIntConstant(m, "TIPC_ADDR_NAMESEQ", TIPC_ADDR_NAMESEQ); |
| 4829 | PyModule_AddIntConstant(m, "TIPC_ADDR_NAME", TIPC_ADDR_NAME); |
| 4830 | PyModule_AddIntConstant(m, "TIPC_ADDR_ID", TIPC_ADDR_ID); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4831 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4832 | PyModule_AddIntConstant(m, "TIPC_ZONE_SCOPE", TIPC_ZONE_SCOPE); |
| 4833 | PyModule_AddIntConstant(m, "TIPC_CLUSTER_SCOPE", TIPC_CLUSTER_SCOPE); |
| 4834 | PyModule_AddIntConstant(m, "TIPC_NODE_SCOPE", TIPC_NODE_SCOPE); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4835 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4836 | /* for setsockopt() */ |
| 4837 | PyModule_AddIntConstant(m, "SOL_TIPC", SOL_TIPC); |
| 4838 | PyModule_AddIntConstant(m, "TIPC_IMPORTANCE", TIPC_IMPORTANCE); |
| 4839 | PyModule_AddIntConstant(m, "TIPC_SRC_DROPPABLE", TIPC_SRC_DROPPABLE); |
| 4840 | PyModule_AddIntConstant(m, "TIPC_DEST_DROPPABLE", |
| 4841 | TIPC_DEST_DROPPABLE); |
| 4842 | PyModule_AddIntConstant(m, "TIPC_CONN_TIMEOUT", TIPC_CONN_TIMEOUT); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4843 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4844 | PyModule_AddIntConstant(m, "TIPC_LOW_IMPORTANCE", |
| 4845 | TIPC_LOW_IMPORTANCE); |
| 4846 | PyModule_AddIntConstant(m, "TIPC_MEDIUM_IMPORTANCE", |
| 4847 | TIPC_MEDIUM_IMPORTANCE); |
| 4848 | PyModule_AddIntConstant(m, "TIPC_HIGH_IMPORTANCE", |
| 4849 | TIPC_HIGH_IMPORTANCE); |
| 4850 | PyModule_AddIntConstant(m, "TIPC_CRITICAL_IMPORTANCE", |
| 4851 | TIPC_CRITICAL_IMPORTANCE); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4852 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4853 | /* for subscriptions */ |
| 4854 | PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS); |
| 4855 | PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE); |
Georg Brandl | ff15c86 | 2008-01-11 09:19:11 +0000 | [diff] [blame] | 4856 | #ifdef TIPC_SUB_CANCEL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4857 | /* doesn't seem to be available everywhere */ |
| 4858 | PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL); |
Georg Brandl | ff15c86 | 2008-01-11 09:19:11 +0000 | [diff] [blame] | 4859 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4860 | PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER); |
| 4861 | PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED); |
| 4862 | PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN); |
| 4863 | PyModule_AddIntConstant(m, "TIPC_SUBSCR_TIMEOUT", TIPC_SUBSCR_TIMEOUT); |
| 4864 | PyModule_AddIntConstant(m, "TIPC_CFG_SRV", TIPC_CFG_SRV); |
| 4865 | PyModule_AddIntConstant(m, "TIPC_TOP_SRV", TIPC_TOP_SRV); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4866 | #endif |
| 4867 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4868 | /* Socket types */ |
| 4869 | PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM); |
| 4870 | PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 4871 | #ifndef __BEOS__ |
| 4872 | /* We have incomplete socket support. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4873 | PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW); |
| 4874 | PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET); |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 4875 | #if defined(SOCK_RDM) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4876 | PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 4877 | #endif |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 4878 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4879 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4880 | #ifdef SO_DEBUG |
| 4881 | PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4882 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4883 | #ifdef SO_ACCEPTCONN |
| 4884 | PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4885 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4886 | #ifdef SO_REUSEADDR |
| 4887 | PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4888 | #endif |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 4889 | #ifdef SO_EXCLUSIVEADDRUSE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4890 | PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE); |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 4891 | #endif |
| 4892 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4893 | #ifdef SO_KEEPALIVE |
| 4894 | PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4895 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4896 | #ifdef SO_DONTROUTE |
| 4897 | PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4898 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4899 | #ifdef SO_BROADCAST |
| 4900 | PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4901 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4902 | #ifdef SO_USELOOPBACK |
| 4903 | PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4904 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4905 | #ifdef SO_LINGER |
| 4906 | PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4907 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4908 | #ifdef SO_OOBINLINE |
| 4909 | PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4910 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4911 | #ifdef SO_REUSEPORT |
| 4912 | PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4913 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4914 | #ifdef SO_SNDBUF |
| 4915 | PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4916 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4917 | #ifdef SO_RCVBUF |
| 4918 | PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4919 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4920 | #ifdef SO_SNDLOWAT |
| 4921 | PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4922 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4923 | #ifdef SO_RCVLOWAT |
| 4924 | PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4925 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4926 | #ifdef SO_SNDTIMEO |
| 4927 | PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4928 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4929 | #ifdef SO_RCVTIMEO |
| 4930 | PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4931 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4932 | #ifdef SO_ERROR |
| 4933 | PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4934 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4935 | #ifdef SO_TYPE |
| 4936 | PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4937 | #endif |
Larry Hastings | 3b958e3 | 2010-04-02 11:18:17 +0000 | [diff] [blame] | 4938 | #ifdef SO_SETFIB |
| 4939 | PyModule_AddIntConstant(m, "SO_SETFIB", SO_SETFIB); |
| 4940 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4941 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4942 | /* Maximum number of connections for "listen" */ |
| 4943 | #ifdef SOMAXCONN |
| 4944 | PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4945 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4946 | PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4947 | #endif |
| 4948 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4949 | /* Flags for send, recv */ |
| 4950 | #ifdef MSG_OOB |
| 4951 | PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4952 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4953 | #ifdef MSG_PEEK |
| 4954 | PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4955 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4956 | #ifdef MSG_DONTROUTE |
| 4957 | PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4958 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4959 | #ifdef MSG_DONTWAIT |
| 4960 | PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT); |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 4961 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4962 | #ifdef MSG_EOR |
| 4963 | PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4964 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4965 | #ifdef MSG_TRUNC |
| 4966 | PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4967 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4968 | #ifdef MSG_CTRUNC |
| 4969 | PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4970 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4971 | #ifdef MSG_WAITALL |
| 4972 | PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4973 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4974 | #ifdef MSG_BTAG |
| 4975 | PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4976 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4977 | #ifdef MSG_ETAG |
| 4978 | PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4979 | #endif |
| 4980 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4981 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 4982 | #ifdef SOL_SOCKET |
| 4983 | PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4984 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4985 | #ifdef SOL_IP |
| 4986 | PyModule_AddIntConstant(m, "SOL_IP", SOL_IP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4987 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4988 | PyModule_AddIntConstant(m, "SOL_IP", 0); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4989 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4990 | #ifdef SOL_IPX |
| 4991 | PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4992 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4993 | #ifdef SOL_AX25 |
| 4994 | PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4995 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4996 | #ifdef SOL_ATALK |
| 4997 | PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4998 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4999 | #ifdef SOL_NETROM |
| 5000 | PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5001 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5002 | #ifdef SOL_ROSE |
| 5003 | PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5004 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5005 | #ifdef SOL_TCP |
| 5006 | PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5007 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5008 | PyModule_AddIntConstant(m, "SOL_TCP", 6); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5009 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5010 | #ifdef SOL_UDP |
| 5011 | PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5012 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5013 | PyModule_AddIntConstant(m, "SOL_UDP", 17); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5014 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5015 | #ifdef IPPROTO_IP |
| 5016 | PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 5017 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5018 | PyModule_AddIntConstant(m, "IPPROTO_IP", 0); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5019 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5020 | #ifdef IPPROTO_HOPOPTS |
| 5021 | PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5022 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5023 | #ifdef IPPROTO_ICMP |
| 5024 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 5025 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5026 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5027 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5028 | #ifdef IPPROTO_IGMP |
| 5029 | PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5030 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5031 | #ifdef IPPROTO_GGP |
| 5032 | PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5033 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5034 | #ifdef IPPROTO_IPV4 |
| 5035 | PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5036 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5037 | #ifdef IPPROTO_IPV6 |
| 5038 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
Martin v. Löwis | a0f1734 | 2003-10-03 13:56:20 +0000 | [diff] [blame] | 5039 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5040 | #ifdef IPPROTO_IPIP |
| 5041 | PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5042 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5043 | #ifdef IPPROTO_TCP |
| 5044 | PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 5045 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5046 | PyModule_AddIntConstant(m, "IPPROTO_TCP", 6); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5047 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5048 | #ifdef IPPROTO_EGP |
| 5049 | PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5050 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5051 | #ifdef IPPROTO_PUP |
| 5052 | PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5053 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5054 | #ifdef IPPROTO_UDP |
| 5055 | PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 5056 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5057 | PyModule_AddIntConstant(m, "IPPROTO_UDP", 17); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5058 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5059 | #ifdef IPPROTO_IDP |
| 5060 | PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5061 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5062 | #ifdef IPPROTO_HELLO |
| 5063 | PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5064 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5065 | #ifdef IPPROTO_ND |
| 5066 | PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5067 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5068 | #ifdef IPPROTO_TP |
| 5069 | PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5070 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5071 | #ifdef IPPROTO_IPV6 |
| 5072 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5073 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5074 | #ifdef IPPROTO_ROUTING |
| 5075 | PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5076 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5077 | #ifdef IPPROTO_FRAGMENT |
| 5078 | PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5079 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5080 | #ifdef IPPROTO_RSVP |
| 5081 | PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5082 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5083 | #ifdef IPPROTO_GRE |
| 5084 | PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5085 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5086 | #ifdef IPPROTO_ESP |
| 5087 | PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5088 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5089 | #ifdef IPPROTO_AH |
| 5090 | PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5091 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5092 | #ifdef IPPROTO_MOBILE |
| 5093 | PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5094 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5095 | #ifdef IPPROTO_ICMPV6 |
| 5096 | PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5097 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5098 | #ifdef IPPROTO_NONE |
| 5099 | PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5100 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5101 | #ifdef IPPROTO_DSTOPTS |
| 5102 | PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5103 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5104 | #ifdef IPPROTO_XTP |
| 5105 | PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5106 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5107 | #ifdef IPPROTO_EON |
| 5108 | PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5109 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5110 | #ifdef IPPROTO_PIM |
| 5111 | PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5112 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5113 | #ifdef IPPROTO_IPCOMP |
| 5114 | PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5115 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5116 | #ifdef IPPROTO_VRRP |
| 5117 | PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5118 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5119 | #ifdef IPPROTO_BIP |
| 5120 | PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5121 | #endif |
| 5122 | /**/ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5123 | #ifdef IPPROTO_RAW |
| 5124 | PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 5125 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5126 | PyModule_AddIntConstant(m, "IPPROTO_RAW", 255); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5127 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5128 | #ifdef IPPROTO_MAX |
| 5129 | PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5130 | #endif |
| 5131 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5132 | /* Some port configuration */ |
| 5133 | #ifdef IPPORT_RESERVED |
| 5134 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5135 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5136 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5137 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5138 | #ifdef IPPORT_USERRESERVED |
| 5139 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5140 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5141 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5142 | #endif |
| 5143 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5144 | /* Some reserved IP v.4 addresses */ |
| 5145 | #ifdef INADDR_ANY |
| 5146 | PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5147 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5148 | PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5149 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5150 | #ifdef INADDR_BROADCAST |
| 5151 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5152 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5153 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5154 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5155 | #ifdef INADDR_LOOPBACK |
| 5156 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5157 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5158 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5159 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5160 | #ifdef INADDR_UNSPEC_GROUP |
| 5161 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5162 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5163 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5164 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5165 | #ifdef INADDR_ALLHOSTS_GROUP |
| 5166 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", |
| 5167 | INADDR_ALLHOSTS_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5168 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5169 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5170 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5171 | #ifdef INADDR_MAX_LOCAL_GROUP |
| 5172 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", |
| 5173 | INADDR_MAX_LOCAL_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5174 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5175 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5176 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5177 | #ifdef INADDR_NONE |
| 5178 | PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5179 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5180 | PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5181 | #endif |
| 5182 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5183 | /* IPv4 [gs]etsockopt options */ |
| 5184 | #ifdef IP_OPTIONS |
| 5185 | PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5186 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5187 | #ifdef IP_HDRINCL |
| 5188 | PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5189 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5190 | #ifdef IP_TOS |
| 5191 | PyModule_AddIntConstant(m, "IP_TOS", IP_TOS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5192 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5193 | #ifdef IP_TTL |
| 5194 | PyModule_AddIntConstant(m, "IP_TTL", IP_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5195 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5196 | #ifdef IP_RECVOPTS |
| 5197 | PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5198 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5199 | #ifdef IP_RECVRETOPTS |
| 5200 | PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5201 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5202 | #ifdef IP_RECVDSTADDR |
| 5203 | PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5204 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5205 | #ifdef IP_RETOPTS |
| 5206 | PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5207 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5208 | #ifdef IP_MULTICAST_IF |
| 5209 | PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5210 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5211 | #ifdef IP_MULTICAST_TTL |
| 5212 | PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5213 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5214 | #ifdef IP_MULTICAST_LOOP |
| 5215 | PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5216 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5217 | #ifdef IP_ADD_MEMBERSHIP |
| 5218 | PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5219 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5220 | #ifdef IP_DROP_MEMBERSHIP |
| 5221 | PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5222 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5223 | #ifdef IP_DEFAULT_MULTICAST_TTL |
| 5224 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL", |
| 5225 | IP_DEFAULT_MULTICAST_TTL); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5226 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5227 | #ifdef IP_DEFAULT_MULTICAST_LOOP |
| 5228 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP", |
| 5229 | IP_DEFAULT_MULTICAST_LOOP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5230 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5231 | #ifdef IP_MAX_MEMBERSHIPS |
| 5232 | PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5233 | #endif |
| 5234 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5235 | /* IPv6 [gs]etsockopt options, defined in RFC2553 */ |
| 5236 | #ifdef IPV6_JOIN_GROUP |
| 5237 | PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5238 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5239 | #ifdef IPV6_LEAVE_GROUP |
| 5240 | PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5241 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5242 | #ifdef IPV6_MULTICAST_HOPS |
| 5243 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5244 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5245 | #ifdef IPV6_MULTICAST_IF |
| 5246 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5247 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5248 | #ifdef IPV6_MULTICAST_LOOP |
| 5249 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5250 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5251 | #ifdef IPV6_UNICAST_HOPS |
| 5252 | PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5253 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5254 | /* Additional IPV6 socket options, defined in RFC 3493 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5255 | #ifdef IPV6_V6ONLY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5256 | PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5257 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5258 | /* Advanced IPV6 socket options, from RFC 3542 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5259 | #ifdef IPV6_CHECKSUM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5260 | PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5261 | #endif |
| 5262 | #ifdef IPV6_DONTFRAG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5263 | PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5264 | #endif |
| 5265 | #ifdef IPV6_DSTOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5266 | PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5267 | #endif |
| 5268 | #ifdef IPV6_HOPLIMIT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5269 | PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5270 | #endif |
| 5271 | #ifdef IPV6_HOPOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5272 | PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5273 | #endif |
| 5274 | #ifdef IPV6_NEXTHOP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5275 | PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5276 | #endif |
| 5277 | #ifdef IPV6_PATHMTU |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5278 | PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5279 | #endif |
| 5280 | #ifdef IPV6_PKTINFO |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5281 | PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5282 | #endif |
| 5283 | #ifdef IPV6_RECVDSTOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5284 | PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5285 | #endif |
| 5286 | #ifdef IPV6_RECVHOPLIMIT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5287 | PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5288 | #endif |
| 5289 | #ifdef IPV6_RECVHOPOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5290 | PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5291 | #endif |
| 5292 | #ifdef IPV6_RECVPKTINFO |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5293 | PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5294 | #endif |
| 5295 | #ifdef IPV6_RECVRTHDR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5296 | PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5297 | #endif |
| 5298 | #ifdef IPV6_RECVTCLASS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5299 | PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5300 | #endif |
| 5301 | #ifdef IPV6_RTHDR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5302 | PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5303 | #endif |
| 5304 | #ifdef IPV6_RTHDRDSTOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5305 | PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5306 | #endif |
| 5307 | #ifdef IPV6_RTHDR_TYPE_0 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5308 | PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5309 | #endif |
| 5310 | #ifdef IPV6_RECVPATHMTU |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5311 | PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5312 | #endif |
| 5313 | #ifdef IPV6_TCLASS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5314 | PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5315 | #endif |
| 5316 | #ifdef IPV6_USE_MIN_MTU |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5317 | PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5318 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5319 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5320 | /* TCP options */ |
| 5321 | #ifdef TCP_NODELAY |
| 5322 | PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5323 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5324 | #ifdef TCP_MAXSEG |
| 5325 | PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5326 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5327 | #ifdef TCP_CORK |
| 5328 | PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5329 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5330 | #ifdef TCP_KEEPIDLE |
| 5331 | PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5332 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5333 | #ifdef TCP_KEEPINTVL |
| 5334 | PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5335 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5336 | #ifdef TCP_KEEPCNT |
| 5337 | PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5338 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5339 | #ifdef TCP_SYNCNT |
| 5340 | PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5341 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5342 | #ifdef TCP_LINGER2 |
| 5343 | PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5344 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5345 | #ifdef TCP_DEFER_ACCEPT |
| 5346 | PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5347 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5348 | #ifdef TCP_WINDOW_CLAMP |
| 5349 | PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5350 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5351 | #ifdef TCP_INFO |
| 5352 | PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5353 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5354 | #ifdef TCP_QUICKACK |
| 5355 | PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5356 | #endif |
| 5357 | |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5358 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5359 | /* IPX options */ |
| 5360 | #ifdef IPX_TYPE |
| 5361 | PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5362 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 5363 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5364 | /* get{addr,name}info parameters */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5365 | #ifdef EAI_ADDRFAMILY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5366 | PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5367 | #endif |
| 5368 | #ifdef EAI_AGAIN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5369 | PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5370 | #endif |
| 5371 | #ifdef EAI_BADFLAGS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5372 | PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5373 | #endif |
| 5374 | #ifdef EAI_FAIL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5375 | PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5376 | #endif |
| 5377 | #ifdef EAI_FAMILY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5378 | PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5379 | #endif |
| 5380 | #ifdef EAI_MEMORY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5381 | PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5382 | #endif |
| 5383 | #ifdef EAI_NODATA |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5384 | PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5385 | #endif |
| 5386 | #ifdef EAI_NONAME |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5387 | PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5388 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5389 | #ifdef EAI_OVERFLOW |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5390 | PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5391 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5392 | #ifdef EAI_SERVICE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5393 | PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5394 | #endif |
| 5395 | #ifdef EAI_SOCKTYPE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5396 | PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5397 | #endif |
| 5398 | #ifdef EAI_SYSTEM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5399 | PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5400 | #endif |
| 5401 | #ifdef EAI_BADHINTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5402 | PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5403 | #endif |
| 5404 | #ifdef EAI_PROTOCOL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5405 | PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5406 | #endif |
| 5407 | #ifdef EAI_MAX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5408 | PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5409 | #endif |
| 5410 | #ifdef AI_PASSIVE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5411 | PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5412 | #endif |
| 5413 | #ifdef AI_CANONNAME |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5414 | PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5415 | #endif |
| 5416 | #ifdef AI_NUMERICHOST |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5417 | PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5418 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5419 | #ifdef AI_NUMERICSERV |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5420 | PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5421 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5422 | #ifdef AI_MASK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5423 | PyModule_AddIntConstant(m, "AI_MASK", AI_MASK); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5424 | #endif |
| 5425 | #ifdef AI_ALL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5426 | PyModule_AddIntConstant(m, "AI_ALL", AI_ALL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5427 | #endif |
| 5428 | #ifdef AI_V4MAPPED_CFG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5429 | PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5430 | #endif |
| 5431 | #ifdef AI_ADDRCONFIG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5432 | PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5433 | #endif |
| 5434 | #ifdef AI_V4MAPPED |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5435 | PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5436 | #endif |
| 5437 | #ifdef AI_DEFAULT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5438 | PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5439 | #endif |
| 5440 | #ifdef NI_MAXHOST |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5441 | PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5442 | #endif |
| 5443 | #ifdef NI_MAXSERV |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5444 | PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5445 | #endif |
| 5446 | #ifdef NI_NOFQDN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5447 | PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5448 | #endif |
| 5449 | #ifdef NI_NUMERICHOST |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5450 | PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5451 | #endif |
| 5452 | #ifdef NI_NAMEREQD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5453 | PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5454 | #endif |
| 5455 | #ifdef NI_NUMERICSERV |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5456 | PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5457 | #endif |
| 5458 | #ifdef NI_DGRAM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5459 | PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5460 | #endif |
| 5461 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5462 | /* shutdown() parameters */ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5463 | #ifdef SHUT_RD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5464 | PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5465 | #elif defined(SD_RECEIVE) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5466 | PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5467 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5468 | PyModule_AddIntConstant(m, "SHUT_RD", 0); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5469 | #endif |
| 5470 | #ifdef SHUT_WR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5471 | PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5472 | #elif defined(SD_SEND) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5473 | PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5474 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5475 | PyModule_AddIntConstant(m, "SHUT_WR", 1); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5476 | #endif |
| 5477 | #ifdef SHUT_RDWR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5478 | PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5479 | #elif defined(SD_BOTH) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5480 | PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5481 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5482 | PyModule_AddIntConstant(m, "SHUT_RDWR", 2); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5483 | #endif |
| 5484 | |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 5485 | #ifdef SIO_RCVALL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5486 | { |
| 5487 | DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS}; |
| 5488 | const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"}; |
| 5489 | int i; |
| 5490 | for(i = 0; i<sizeof(codes)/sizeof(*codes); ++i) { |
| 5491 | PyObject *tmp; |
| 5492 | tmp = PyLong_FromUnsignedLong(codes[i]); |
| 5493 | if (tmp == NULL) |
| 5494 | return; |
| 5495 | PyModule_AddObject(m, names[i], tmp); |
| 5496 | } |
| 5497 | } |
| 5498 | PyModule_AddIntConstant(m, "RCVALL_OFF", RCVALL_OFF); |
| 5499 | PyModule_AddIntConstant(m, "RCVALL_ON", RCVALL_ON); |
| 5500 | PyModule_AddIntConstant(m, "RCVALL_SOCKETLEVELONLY", RCVALL_SOCKETLEVELONLY); |
Amaury Forgeot d'Arc | 94eba71 | 2008-03-28 21:55:29 +0000 | [diff] [blame] | 5501 | #ifdef RCVALL_IPLEVEL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5502 | PyModule_AddIntConstant(m, "RCVALL_IPLEVEL", RCVALL_IPLEVEL); |
Amaury Forgeot d'Arc | 94eba71 | 2008-03-28 21:55:29 +0000 | [diff] [blame] | 5503 | #endif |
| 5504 | #ifdef RCVALL_MAX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5505 | PyModule_AddIntConstant(m, "RCVALL_MAX", RCVALL_MAX); |
Amaury Forgeot d'Arc | 94eba71 | 2008-03-28 21:55:29 +0000 | [diff] [blame] | 5506 | #endif |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 5507 | #endif /* _MSTCPIP_ */ |
| 5508 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5509 | /* Initialize gethostbyname lock */ |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 5510 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5511 | netdb_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 5512 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5513 | } |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5514 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5515 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5516 | #ifndef HAVE_INET_PTON |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 5517 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5518 | |
| 5519 | /* Simplistic emulation code for inet_pton that only works for IPv4 */ |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5520 | /* These are not exposed because they do not set errno properly */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5521 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5522 | int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5523 | inet_pton(int af, const char *src, void *dst) |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5524 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5525 | if (af == AF_INET) { |
Gregory P. Smith | 3605b5c | 2009-02-11 23:45:25 +0000 | [diff] [blame] | 5526 | #if (SIZEOF_INT != 4) |
| 5527 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 5528 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5529 | unsigned int packed_addr; |
| 5530 | packed_addr = inet_addr(src); |
| 5531 | if (packed_addr == INADDR_NONE) |
| 5532 | return 0; |
| 5533 | memcpy(dst, &packed_addr, 4); |
| 5534 | return 1; |
| 5535 | } |
| 5536 | /* Should set errno to EAFNOSUPPORT */ |
| 5537 | return -1; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5538 | } |
| 5539 | |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 5540 | const char * |
| 5541 | 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] | 5542 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5543 | if (af == AF_INET) { |
| 5544 | struct in_addr packed_addr; |
| 5545 | if (size < 16) |
| 5546 | /* Should set errno to ENOSPC. */ |
| 5547 | return NULL; |
| 5548 | memcpy(&packed_addr, src, sizeof(packed_addr)); |
| 5549 | return strncpy(dst, inet_ntoa(packed_addr), size); |
| 5550 | } |
| 5551 | /* Should set errno to EAFNOSUPPORT */ |
| 5552 | return NULL; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5553 | } |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5554 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5555 | #endif |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 5556 | #endif |