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 | 043d6f6 | 2008-01-07 17:19:16 +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 | |
Andrew Svetlov | 0832af6 | 2012-12-18 23:10:48 +0200 | [diff] [blame] | 18 | - socket.error: exception raised for socket specific errors, alias for OSError |
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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | a subclass of socket.error |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 23 | - socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd') |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 24 | - socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...]) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 25 | - socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com') |
Guido van Rossum | 25405c7 | 1996-12-19 16:42:52 +0000 | [diff] [blame] | 26 | - socket.getprotobyname(protocolname) --> protocol number |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 27 | - socket.getservbyname(servicename[, protocolname]) --> port number |
| 28 | - socket.getservbyport(portnumber[, protocolname]) --> service name |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 29 | - socket.socket([family[, type [, proto, fileno]]]) --> new socket object |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 30 | (fileno specifies a pre-existing socket file descriptor) |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 31 | - socket.socketpair([family[, type [, proto]]]) --> (socket, socket) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 32 | - socket.ntohs(16 bit value) --> new int object |
| 33 | - socket.ntohl(32 bit value) --> new int object |
| 34 | - socket.htons(16 bit value) --> new int object |
| 35 | - socket.htonl(32 bit value) --> new int object |
Berker Peksag | a6ec5ee | 2014-06-30 11:28:40 +0300 | [diff] [blame] | 36 | - socket.getaddrinfo(host, port [, family, type, proto, flags]) |
| 37 | --> List of (family, type, proto, canonname, sockaddr) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 38 | - socket.getnameinfo(sockaddr, flags) --> (host, port) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 39 | - socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h> |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 40 | - socket.has_ipv6: boolean value indicating if IPv6 is supported |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 41 | - socket.inet_aton(IP address) -> 32-bit packed IP representation |
| 42 | - socket.inet_ntoa(packed IP) -> IP address string |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 43 | - socket.getdefaulttimeout() -> None | float |
| 44 | - socket.setdefaulttimeout(None | float) |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 45 | - socket.if_nameindex() -> list of tuples (if_index, if_name) |
| 46 | - socket.if_nametoindex(name) -> corresponding interface index |
| 47 | - socket.if_indextoname(index) -> corresponding interface name |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 48 | - an Internet socket address is a pair (hostname, port) |
| 49 | where hostname can be anything recognized by gethostbyname() |
| 50 | (including the dd.dd.dd.dd notation) and port is in host byte order |
| 51 | - 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] | 52 | - a UNIX domain socket address is a string specifying the pathname |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 53 | - an AF_PACKET socket address is a tuple containing a string |
| 54 | specifying the ethernet interface and an integer specifying |
| 55 | the Ethernet protocol number to be received. For example: |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 56 | ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple |
Andrew M. Kuchling | b8e1717 | 2004-07-10 23:39:35 +0000 | [diff] [blame] | 57 | specify packet-type and ha-type/addr. |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 58 | - an AF_TIPC socket address is expressed as |
| 59 | (addr_type, v1, v2, v3 [, scope]); where addr_type can be one of: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, and TIPC_ADDR_ID; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 61 | and scope can be one of: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and TIPC_NODE_SCOPE. |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 63 | The meaning of v1, v2 and v3 depends on the value of addr_type: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | if addr_type is TIPC_ADDR_NAME: |
| 65 | v1 is the server type |
| 66 | v2 is the port identifier |
| 67 | v3 is ignored |
| 68 | if addr_type is TIPC_ADDR_NAMESEQ: |
| 69 | v1 is the server type |
| 70 | v2 is the lower port number |
| 71 | v3 is the upper port number |
| 72 | if addr_type is TIPC_ADDR_ID: |
| 73 | v1 is the node |
| 74 | v2 is the ref |
| 75 | v3 is ignored |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 76 | |
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 | Local naming conventions: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 80 | - names starting with sock_ are socket object methods |
| 81 | - names starting with socket_ are module-level functions |
| 82 | - names starting with PySocket are exported through socketmodule.h |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 84 | */ |
| 85 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 86 | #ifdef __APPLE__ |
Ned Deily | 47299fd | 2016-02-15 16:54:08 +1100 | [diff] [blame] | 87 | #include <AvailabilityMacros.h> |
| 88 | /* for getaddrinfo thread safety test on old versions of OS X */ |
| 89 | #ifndef MAC_OS_X_VERSION_10_5 |
| 90 | #define MAC_OS_X_VERSION_10_5 1050 |
| 91 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 92 | /* |
| 93 | * inet_aton is not available on OSX 10.3, yet we want to use a binary |
| 94 | * that was build on 10.4 or later to work on that release, weak linking |
| 95 | * comes to the rescue. |
| 96 | */ |
| 97 | # pragma weak inet_aton |
| 98 | #endif |
| 99 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 100 | #include "Python.h" |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 101 | #include "structmember.h" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 102 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 103 | /* Socket object documentation */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 104 | PyDoc_STRVAR(sock_doc, |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 105 | "socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None) -> socket object\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 106 | \n\ |
| 107 | Open a socket of the given type. The family argument specifies the\n\ |
| 108 | address family; it defaults to AF_INET. The type argument specifies\n\ |
| 109 | whether this is a stream (SOCK_STREAM, this is the default)\n\ |
| 110 | or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\ |
| 111 | specifying the default protocol. Keyword arguments are accepted.\n\ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 112 | The socket is created as non-inheritable.\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 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\ |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 118 | _accept() -- accept connection, returning new socket fd and client address\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 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\ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 123 | dup() -- return a new socket fd duplicated from fileno()\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 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\ |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 129 | listen([n]) -- start listening for incoming connections\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 130 | recv(buflen[, flags]) -- receive data\n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 131 | recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 132 | recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 133 | recvfrom_into(buffer[, nbytes, [, flags])\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 134 | -- receive data and sender\'s address (into a buffer)\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 135 | sendall(data[, flags]) -- send all data\n\ |
| 136 | send(data[, flags]) -- send data, may not send all of it\n\ |
| 137 | sendto(data[, flags], addr) -- send data to a given address\n\ |
| 138 | setblocking(0 | 1) -- set or clear the blocking I/O flag\n\ |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 139 | setsockopt(level, optname, value[, optlen]) -- set socket options\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 140 | settimeout(None | float) -- set or clear the timeout\n\ |
| 141 | shutdown(how) -- shut down traffic in one or both directions\n\ |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 142 | if_nameindex() -- return all network interface indices and names\n\ |
Gregory P. Smith | b6471db | 2011-05-22 22:47:55 -0700 | [diff] [blame] | 143 | if_nametoindex(name) -- return the corresponding interface index\n\ |
| 144 | if_indextoname(index) -- return the corresponding interface name\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 145 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 146 | [*] not available on all platforms!"); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 147 | |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 148 | /* XXX This is a terrible mess of platform-dependent preprocessor hacks. |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 149 | I hope some day someone can clean this up please... */ |
| 150 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 151 | /* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure |
| 152 | script doesn't get this right, so we hardcode some platform checks below. |
| 153 | On the other hand, not all Linux versions agree, so there the settings |
| 154 | computed by the configure script are needed! */ |
| 155 | |
Benjamin Peterson | b6c3458 | 2016-09-07 14:08:34 -0700 | [diff] [blame] | 156 | #ifndef __linux__ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 157 | # undef HAVE_GETHOSTBYNAME_R_3_ARG |
| 158 | # undef HAVE_GETHOSTBYNAME_R_5_ARG |
| 159 | # undef HAVE_GETHOSTBYNAME_R_6_ARG |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 160 | #endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 161 | |
Victor Stinner | 710d27e | 2011-08-23 10:57:32 +0200 | [diff] [blame] | 162 | #if defined(__OpenBSD__) |
| 163 | # include <sys/uio.h> |
| 164 | #endif |
| 165 | |
Stefan Krah | 1f9eb87 | 2016-05-22 17:35:34 +0200 | [diff] [blame] | 166 | #if !defined(WITH_THREAD) |
| 167 | # undef HAVE_GETHOSTBYNAME_R |
| 168 | #endif |
| 169 | |
| 170 | #if defined(__ANDROID__) && __ANDROID_API__ < 23 |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 171 | # undef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 172 | #endif |
| 173 | |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 174 | #ifdef HAVE_GETHOSTBYNAME_R |
Martin Panter | c9e08d8 | 2016-11-14 04:26:36 +0000 | [diff] [blame] | 175 | # if defined(_AIX) && !defined(_LINUX_SOURCE_COMPAT) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 176 | # define HAVE_GETHOSTBYNAME_R_3_ARG |
| 177 | # elif defined(__sun) || defined(__sgi) |
| 178 | # define HAVE_GETHOSTBYNAME_R_5_ARG |
Benjamin Peterson | b6c3458 | 2016-09-07 14:08:34 -0700 | [diff] [blame] | 179 | # elif defined(__linux__) |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 180 | /* Rely on the configure script */ |
Martin Panter | c9e08d8 | 2016-11-14 04:26:36 +0000 | [diff] [blame] | 181 | # elif defined(_LINUX_SOURCE_COMPAT) /* Linux compatibility on AIX */ |
| 182 | # define HAVE_GETHOSTBYNAME_R_6_ARG |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 183 | # else |
| 184 | # undef HAVE_GETHOSTBYNAME_R |
| 185 | # endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 186 | #endif |
| 187 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 188 | #if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \ |
| 189 | !defined(MS_WINDOWS) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 190 | # define USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 191 | #endif |
| 192 | |
Ned Deily | e1d4e58 | 2016-02-23 22:05:29 +1100 | [diff] [blame] | 193 | /* To use __FreeBSD_version, __OpenBSD__, and __NetBSD_Version__ */ |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 194 | #ifdef HAVE_SYS_PARAM_H |
| 195 | #include <sys/param.h> |
| 196 | #endif |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 197 | /* On systems on which getaddrinfo() is believed to not be thread-safe, |
Ned Deily | 47299fd | 2016-02-15 16:54:08 +1100 | [diff] [blame] | 198 | (this includes the getaddrinfo emulation) protect access with a lock. |
| 199 | |
| 200 | getaddrinfo is thread-safe on Mac OS X 10.5 and later. Originally it was |
| 201 | a mix of code including an unsafe implementation from an old BSD's |
| 202 | libresolv. In 10.5 Apple reimplemented it as a safe IPC call to the |
| 203 | mDNSResponder process. 10.5 is the first be UNIX '03 certified, which |
Ned Deily | e1d4e58 | 2016-02-23 22:05:29 +1100 | [diff] [blame] | 204 | includes the requirement that getaddrinfo be thread-safe. See issue #25924. |
Ned Deily | 47299fd | 2016-02-15 16:54:08 +1100 | [diff] [blame] | 205 | |
Ned Deily | e1d4e58 | 2016-02-23 22:05:29 +1100 | [diff] [blame] | 206 | It's thread-safe in OpenBSD starting with 5.4, released Nov 2013: |
| 207 | http://www.openbsd.org/plus54.html |
| 208 | |
| 209 | It's thread-safe in NetBSD starting with 4.0, released Dec 2007: |
| 210 | |
| 211 | http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/net/getaddrinfo.c.diff?r1=1.82&r2=1.83 |
Ned Deily | 47299fd | 2016-02-15 16:54:08 +1100 | [diff] [blame] | 212 | */ |
| 213 | #if defined(WITH_THREAD) && ( \ |
| 214 | (defined(__APPLE__) && \ |
| 215 | MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5) || \ |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 216 | (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \ |
Ned Deily | e1d4e58 | 2016-02-23 22:05:29 +1100 | [diff] [blame] | 217 | (defined(__OpenBSD__) && OpenBSD+0 < 201311) || \ |
| 218 | (defined(__NetBSD__) && __NetBSD_Version__+0 < 400000000) || \ |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 219 | !defined(HAVE_GETADDRINFO)) |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 220 | #define USE_GETADDRINFO_LOCK |
| 221 | #endif |
| 222 | |
| 223 | #ifdef USE_GETADDRINFO_LOCK |
| 224 | #define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1); |
| 225 | #define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock); |
| 226 | #else |
| 227 | #define ACQUIRE_GETADDRINFO_LOCK |
| 228 | #define RELEASE_GETADDRINFO_LOCK |
| 229 | #endif |
| 230 | |
| 231 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 232 | # include "pythread.h" |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 233 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 234 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 235 | #if defined(PYCC_VACPP) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 236 | # include <types.h> |
| 237 | # include <io.h> |
| 238 | # include <sys/ioctl.h> |
| 239 | # include <utils.h> |
| 240 | # include <ctype.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 241 | #endif |
| 242 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 243 | #ifdef __APPLE__ |
| 244 | # include <sys/ioctl.h> |
| 245 | #endif |
| 246 | |
| 247 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 248 | #if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 249 | /* make sure that the reentrant (gethostbyaddr_r etc) |
| 250 | functions are declared correctly if compiling with |
| 251 | MIPSPro 7.x in ANSI C mode (default) */ |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 252 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 253 | /* XXX Using _SGIAPI is the wrong thing, |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 254 | but I don't know what the right thing is. */ |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 255 | #undef _SGIAPI /* to avoid warning */ |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 256 | #define _SGIAPI 1 |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 257 | |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 258 | #undef _XOPEN_SOURCE |
| 259 | #include <sys/socket.h> |
| 260 | #include <sys/types.h> |
| 261 | #include <netinet/in.h> |
| 262 | #ifdef _SS_ALIGNSIZE |
| 263 | #define HAVE_GETADDRINFO 1 |
| 264 | #define HAVE_GETNAMEINFO 1 |
| 265 | #endif |
| 266 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 267 | #define HAVE_INET_PTON |
| 268 | #include <netdb.h> |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 269 | #endif |
| 270 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 271 | /* Irix 6.5 fails to define this variable at all. This is needed |
| 272 | 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] | 273 | are just busted. Same thing for Solaris. */ |
| 274 | #if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN) |
Anthony Baxter | bab23cf | 2003-10-04 08:00:49 +0000 | [diff] [blame] | 275 | #define INET_ADDRSTRLEN 16 |
| 276 | #endif |
| 277 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 278 | /* Generic includes */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 279 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 280 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 281 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 282 | |
Gregory P. Smith | 3b1f2c3 | 2011-05-15 12:18:23 -0700 | [diff] [blame] | 283 | #ifdef HAVE_SYS_SOCKET_H |
| 284 | #include <sys/socket.h> |
| 285 | #endif |
| 286 | |
| 287 | #ifdef HAVE_NET_IF_H |
| 288 | #include <net/if.h> |
| 289 | #endif |
| 290 | |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 291 | #ifdef HAVE_SOCKADDR_ALG |
| 292 | #include <linux/if_alg.h> |
| 293 | #ifndef AF_ALG |
| 294 | #define AF_ALG 38 |
| 295 | #endif |
| 296 | #ifndef SOL_ALG |
| 297 | #define SOL_ALG 279 |
| 298 | #endif |
| 299 | |
| 300 | /* Linux 3.19 */ |
| 301 | #ifndef ALG_SET_AEAD_ASSOCLEN |
| 302 | #define ALG_SET_AEAD_ASSOCLEN 4 |
| 303 | #endif |
| 304 | #ifndef ALG_SET_AEAD_AUTHSIZE |
| 305 | #define ALG_SET_AEAD_AUTHSIZE 5 |
| 306 | #endif |
| 307 | /* Linux 4.8 */ |
| 308 | #ifndef ALG_SET_PUBKEY |
| 309 | #define ALG_SET_PUBKEY 6 |
| 310 | #endif |
| 311 | |
| 312 | #ifndef ALG_OP_SIGN |
| 313 | #define ALG_OP_SIGN 2 |
| 314 | #endif |
| 315 | #ifndef ALG_OP_VERIFY |
| 316 | #define ALG_OP_VERIFY 3 |
| 317 | #endif |
| 318 | |
| 319 | #endif /* HAVE_SOCKADDR_ALG */ |
| 320 | |
Marc-André Lemburg | 976ade6 | 2002-02-16 18:47:07 +0000 | [diff] [blame] | 321 | /* Generic socket object definitions and includes */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 322 | #define PySocket_BUILDING_SOCKET |
Marc-André Lemburg | bb8b78b | 2002-02-16 18:44:52 +0000 | [diff] [blame] | 323 | #include "socketmodule.h" |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 324 | |
| 325 | /* Addressing includes */ |
| 326 | |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 327 | #ifndef MS_WINDOWS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 328 | |
| 329 | /* Non-MS WINDOWS includes */ |
| 330 | # include <netdb.h> |
Nick Coghlan | 4ecf6cf | 2011-08-22 15:45:19 +1000 | [diff] [blame] | 331 | # include <unistd.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 332 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 333 | /* Headers needed for inet_ntoa() and inet_addr() */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 334 | # include <arpa/inet.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 335 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 336 | # include <fcntl.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 337 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 338 | #else |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 339 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 340 | /* MS_WINDOWS includes */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 341 | # ifdef HAVE_FCNTL_H |
| 342 | # include <fcntl.h> |
| 343 | # endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 344 | |
Steve Dower | 65e4cb1 | 2014-11-22 12:54:57 -0800 | [diff] [blame] | 345 | #if defined(_MSC_VER) && _MSC_VER >= 1800 |
| 346 | /* Provides the IsWindows7SP1OrGreater() function */ |
| 347 | #include <VersionHelpers.h> |
| 348 | #endif |
| 349 | |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 350 | #endif |
| 351 | |
Skip Montanaro | 7befb99 | 2004-02-10 16:50:21 +0000 | [diff] [blame] | 352 | #include <stddef.h> |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 353 | |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 354 | #ifndef O_NONBLOCK |
| 355 | # define O_NONBLOCK O_NDELAY |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 356 | #endif |
| 357 | |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 358 | /* include Python's addrinfo.h unless it causes trouble */ |
| 359 | #if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE) |
| 360 | /* Do not include addinfo.h on some newer IRIX versions. |
| 361 | * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21, |
| 362 | * for example, but not by 6.5.10. |
| 363 | */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 364 | #elif defined(_MSC_VER) && _MSC_VER>1201 |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 365 | /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and |
| 366 | * EAI_* constants are defined in (the already included) ws2tcpip.h. |
| 367 | */ |
| 368 | #else |
| 369 | # include "addrinfo.h" |
| 370 | #endif |
Jason Tishler | c246cb7 | 2004-08-09 13:25:59 +0000 | [diff] [blame] | 371 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 372 | #ifndef HAVE_INET_PTON |
Christian Heimes | 96e7b3d | 2007-11-20 06:51:17 +0000 | [diff] [blame] | 373 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 374 | int inet_pton(int af, const char *src, void *dst); |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 375 | 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] | 376 | #endif |
Christian Heimes | b615069 | 2007-11-15 23:37:07 +0000 | [diff] [blame] | 377 | #endif |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 378 | |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 379 | #ifdef __APPLE__ |
| 380 | /* On OS X, getaddrinfo returns no error indication of lookup |
| 381 | failure, so we must use the emulation instead of the libinfo |
| 382 | implementation. Unfortunately, performing an autoconf test |
| 383 | for this bug would require DNS access for the machine performing |
| 384 | the configuration, which is not acceptable. Therefore, we |
| 385 | determine the bug just by checking for __APPLE__. If this bug |
| 386 | gets ever fixed, perhaps checking for sys/version.h would be |
| 387 | appropriate, which is 10/0 on the system with the bug. */ |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 388 | #ifndef HAVE_GETNAMEINFO |
| 389 | /* This bug seems to be fixed in Jaguar. Ths easiest way I could |
| 390 | Find to check for Jaguar is that it has getnameinfo(), which |
| 391 | older releases don't have */ |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 392 | #undef HAVE_GETADDRINFO |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 393 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 394 | |
| 395 | #ifdef HAVE_INET_ATON |
| 396 | #define USE_INET_ATON_WEAKLINK |
| 397 | #endif |
| 398 | |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 399 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 400 | |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 401 | /* 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] | 402 | #if !defined(HAVE_GETADDRINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 403 | /* avoid clashes with the C library definition of the symbol. */ |
| 404 | #define getaddrinfo fake_getaddrinfo |
| 405 | #define gai_strerror fake_gai_strerror |
| 406 | #define freeaddrinfo fake_freeaddrinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 407 | #include "getaddrinfo.c" |
| 408 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 409 | #if !defined(HAVE_GETNAMEINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 410 | #define getnameinfo fake_getnameinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 411 | #include "getnameinfo.c" |
| 412 | #endif |
| 413 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 414 | #ifdef MS_WINDOWS |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 415 | #define SOCKETCLOSE closesocket |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 416 | #endif |
| 417 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 418 | #ifdef MS_WIN32 |
Kristján Valur Jónsson | 310052c | 2012-03-31 13:35:00 +0000 | [diff] [blame] | 419 | #undef EAFNOSUPPORT |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 420 | #define EAFNOSUPPORT WSAEAFNOSUPPORT |
| 421 | #define snprintf _snprintf |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 422 | #endif |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 423 | |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 424 | #ifndef SOCKETCLOSE |
| 425 | #define SOCKETCLOSE close |
| 426 | #endif |
| 427 | |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 428 | #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] | 429 | #define USE_BLUETOOTH 1 |
| 430 | #if defined(__FreeBSD__) |
| 431 | #define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP |
| 432 | #define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 433 | #define BTPROTO_HCI BLUETOOTH_PROTO_HCI |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 434 | #define SOL_HCI SOL_HCI_RAW |
| 435 | #define HCI_FILTER SO_HCI_RAW_FILTER |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 436 | #define sockaddr_l2 sockaddr_l2cap |
| 437 | #define sockaddr_rc sockaddr_rfcomm |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 438 | #define hci_dev hci_node |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 439 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb) |
| 440 | #define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 441 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 442 | #elif defined(__NetBSD__) || defined(__DragonFly__) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 443 | #define sockaddr_l2 sockaddr_bt |
| 444 | #define sockaddr_rc sockaddr_bt |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 445 | #define sockaddr_hci sockaddr_bt |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 446 | #define sockaddr_sco sockaddr_bt |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 447 | #define SOL_HCI BTPROTO_HCI |
| 448 | #define HCI_DATA_DIR SO_HCI_DIRECTION |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 449 | #define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb) |
| 450 | #define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 451 | #define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 452 | #define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 453 | #else |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 454 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb) |
| 455 | #define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 456 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 457 | #define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb) |
| 458 | #endif |
| 459 | #endif |
| 460 | |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 461 | /* Convert "sock_addr_t *" to "struct sockaddr *". */ |
| 462 | #define SAS2SA(x) (&((x)->sa)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 463 | |
Martin v. Löwis | e941617 | 2003-05-03 10:12:45 +0000 | [diff] [blame] | 464 | /* |
| 465 | * Constants for getnameinfo() |
| 466 | */ |
| 467 | #if !defined(NI_MAXHOST) |
| 468 | #define NI_MAXHOST 1025 |
| 469 | #endif |
| 470 | #if !defined(NI_MAXSERV) |
| 471 | #define NI_MAXSERV 32 |
| 472 | #endif |
| 473 | |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 474 | #ifndef INVALID_SOCKET /* MS defines this */ |
| 475 | #define INVALID_SOCKET (-1) |
| 476 | #endif |
| 477 | |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 478 | #ifndef INADDR_NONE |
| 479 | #define INADDR_NONE (-1) |
| 480 | #endif |
| 481 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 482 | /* XXX There's a problem here: *static* functions are not supposed to have |
| 483 | a Py prefix (or use CapitalizedWords). Later... */ |
| 484 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 485 | /* Global variable holding the exception type for errors detected |
| 486 | by this module (but not argument type or memory errors, etc.). */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 487 | static PyObject *socket_herror; |
| 488 | static PyObject *socket_gaierror; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 489 | static PyObject *socket_timeout; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 490 | |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 491 | /* A forward reference to the socket type object. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 492 | The sock_type variable contains pointers to various functions, |
| 493 | some of which call new_sockobject(), which uses sock_type, so |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 494 | there has to be a circular reference. */ |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 495 | static PyTypeObject sock_type; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 496 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 497 | #if defined(HAVE_POLL_H) |
| 498 | #include <poll.h> |
| 499 | #elif defined(HAVE_SYS_POLL_H) |
| 500 | #include <sys/poll.h> |
| 501 | #endif |
| 502 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 503 | /* Largest value to try to store in a socklen_t (used when handling |
| 504 | ancillary data). POSIX requires socklen_t to hold at least |
| 505 | (2**31)-1 and recommends against storing larger values, but |
| 506 | socklen_t was originally int in the BSD interface, so to be on the |
| 507 | safe side we use the smaller of (2**31)-1 and INT_MAX. */ |
| 508 | #if INT_MAX > 0x7fffffff |
| 509 | #define SOCKLEN_T_LIMIT 0x7fffffff |
| 510 | #else |
| 511 | #define SOCKLEN_T_LIMIT INT_MAX |
| 512 | #endif |
| 513 | |
Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 514 | #ifdef HAVE_POLL |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 515 | /* Instead of select(), we'll use poll() since poll() works on any fd. */ |
| 516 | #define IS_SELECTABLE(s) 1 |
| 517 | /* 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] | 518 | #else |
Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 519 | /* If there's no timeout left, we don't have to call select, so it's a safe, |
| 520 | * little white lie. */ |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 521 | #define IS_SELECTABLE(s) (_PyIsSelectable_fd((s)->sock_fd) || (s)->sock_timeout <= 0) |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 522 | #endif |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 523 | |
| 524 | static PyObject* |
| 525 | select_error(void) |
| 526 | { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 527 | PyErr_SetString(PyExc_OSError, "unable to select on socket"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 528 | return NULL; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 531 | #ifdef MS_WINDOWS |
Antoine Pitrou | c8f0196 | 2010-09-28 22:03:27 +0000 | [diff] [blame] | 532 | #ifndef WSAEAGAIN |
| 533 | #define WSAEAGAIN WSAEWOULDBLOCK |
| 534 | #endif |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 535 | #define CHECK_ERRNO(expected) \ |
| 536 | (WSAGetLastError() == WSA ## expected) |
| 537 | #else |
| 538 | #define CHECK_ERRNO(expected) \ |
| 539 | (errno == expected) |
| 540 | #endif |
| 541 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 542 | #ifdef MS_WINDOWS |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 543 | # define GET_SOCK_ERROR WSAGetLastError() |
| 544 | # define SET_SOCK_ERROR(err) WSASetLastError(err) |
| 545 | # define SOCK_TIMEOUT_ERR WSAEWOULDBLOCK |
| 546 | # define SOCK_INPROGRESS_ERR WSAEWOULDBLOCK |
| 547 | #else |
| 548 | # define GET_SOCK_ERROR errno |
| 549 | # define SET_SOCK_ERROR(err) do { errno = err; } while (0) |
| 550 | # define SOCK_TIMEOUT_ERR EWOULDBLOCK |
| 551 | # define SOCK_INPROGRESS_ERR EINPROGRESS |
| 552 | #endif |
| 553 | |
| 554 | |
| 555 | #ifdef MS_WINDOWS |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 556 | /* Does WSASocket() support the WSA_FLAG_NO_HANDLE_INHERIT flag? */ |
| 557 | static int support_wsa_no_inherit = -1; |
| 558 | #endif |
| 559 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 560 | /* Convenience function to raise an error according to errno |
| 561 | and return a NULL pointer from a function. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 562 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 563 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 564 | set_error(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 565 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 566 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 567 | int err_no = WSAGetLastError(); |
| 568 | /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which |
| 569 | recognizes the error codes used by both GetLastError() and |
| 570 | WSAGetLastError */ |
| 571 | if (err_no) |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 572 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, err_no); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 573 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 574 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 575 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 578 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 579 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 580 | set_herror(int h_error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 581 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 582 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 583 | |
| 584 | #ifdef HAVE_HSTRERROR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 585 | v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 586 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 587 | v = Py_BuildValue("(is)", h_error, "host not found"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 588 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | if (v != NULL) { |
| 590 | PyErr_SetObject(socket_herror, v); |
| 591 | Py_DECREF(v); |
| 592 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 593 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 594 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | |
| 598 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 599 | set_gaierror(int error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 600 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 601 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 602 | |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 603 | #ifdef EAI_SYSTEM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 604 | /* EAI_SYSTEM is not available on Windows XP. */ |
| 605 | if (error == EAI_SYSTEM) |
| 606 | return set_error(); |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 607 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 608 | |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 609 | #ifdef HAVE_GAI_STRERROR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 610 | v = Py_BuildValue("(is)", error, gai_strerror(error)); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 611 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 612 | v = Py_BuildValue("(is)", error, "getaddrinfo failed"); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 613 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 614 | if (v != NULL) { |
| 615 | PyErr_SetObject(socket_gaierror, v); |
| 616 | Py_DECREF(v); |
| 617 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 622 | /* Function to perform the setting of socket blocking mode |
| 623 | internally. block = (1 | 0). */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 624 | static int |
| 625 | internal_setblocking(PySocketSockObject *s, int block) |
| 626 | { |
Yury Selivanov | fa22b29 | 2016-10-18 16:03:52 -0400 | [diff] [blame] | 627 | int result = -1; |
Victor Stinner | b6dab6b | 2014-07-23 22:56:55 +0200 | [diff] [blame] | 628 | #ifdef MS_WINDOWS |
| 629 | u_long arg; |
| 630 | #endif |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 631 | #if !defined(MS_WINDOWS) \ |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 632 | && !((defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO))) |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 633 | int delay_flag, new_delay_flag; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 634 | #endif |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 635 | #ifdef SOCK_NONBLOCK |
| 636 | if (block) |
| 637 | s->sock_type &= (~SOCK_NONBLOCK); |
| 638 | else |
| 639 | s->sock_type |= SOCK_NONBLOCK; |
| 640 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 641 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 642 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 643 | #ifndef MS_WINDOWS |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 644 | #if (defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 645 | block = !block; |
Christian Heimes | 0b9d64e | 2016-09-09 00:28:57 +0200 | [diff] [blame] | 646 | if (ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block) == -1) |
Yury Selivanov | fa22b29 | 2016-10-18 16:03:52 -0400 | [diff] [blame] | 647 | goto done; |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 648 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 649 | delay_flag = fcntl(s->sock_fd, F_GETFL, 0); |
Christian Heimes | 0b9d64e | 2016-09-09 00:28:57 +0200 | [diff] [blame] | 650 | if (delay_flag == -1) |
Yury Selivanov | fa22b29 | 2016-10-18 16:03:52 -0400 | [diff] [blame] | 651 | goto done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 652 | if (block) |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 653 | new_delay_flag = delay_flag & (~O_NONBLOCK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 654 | else |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 655 | new_delay_flag = delay_flag | O_NONBLOCK; |
| 656 | if (new_delay_flag != delay_flag) |
Christian Heimes | 0b9d64e | 2016-09-09 00:28:57 +0200 | [diff] [blame] | 657 | if (fcntl(s->sock_fd, F_SETFL, new_delay_flag) == -1) |
Yury Selivanov | fa22b29 | 2016-10-18 16:03:52 -0400 | [diff] [blame] | 658 | goto done; |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 659 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 660 | #else /* MS_WINDOWS */ |
Victor Stinner | b6dab6b | 2014-07-23 22:56:55 +0200 | [diff] [blame] | 661 | arg = !block; |
Christian Heimes | 0b9d64e | 2016-09-09 00:28:57 +0200 | [diff] [blame] | 662 | if (ioctlsocket(s->sock_fd, FIONBIO, &arg) != 0) |
Yury Selivanov | fa22b29 | 2016-10-18 16:03:52 -0400 | [diff] [blame] | 663 | goto done; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 664 | #endif /* MS_WINDOWS */ |
Yury Selivanov | fa22b29 | 2016-10-18 16:03:52 -0400 | [diff] [blame] | 665 | |
| 666 | result = 0; |
| 667 | |
| 668 | done: |
Martin Panter | fa27d5f | 2016-10-20 00:48:23 +0000 | [diff] [blame] | 669 | ; /* necessary for --without-threads flag */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 670 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 671 | |
Yury Selivanov | fa22b29 | 2016-10-18 16:03:52 -0400 | [diff] [blame] | 672 | if (result) { |
Christian Heimes | 0b9d64e | 2016-09-09 00:28:57 +0200 | [diff] [blame] | 673 | #ifndef MS_WINDOWS |
Yury Selivanov | fa22b29 | 2016-10-18 16:03:52 -0400 | [diff] [blame] | 674 | PyErr_SetFromErrno(PyExc_OSError); |
Christian Heimes | 0b9d64e | 2016-09-09 00:28:57 +0200 | [diff] [blame] | 675 | #else |
Yury Selivanov | fa22b29 | 2016-10-18 16:03:52 -0400 | [diff] [blame] | 676 | PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError()); |
Christian Heimes | 0b9d64e | 2016-09-09 00:28:57 +0200 | [diff] [blame] | 677 | #endif |
Yury Selivanov | fa22b29 | 2016-10-18 16:03:52 -0400 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | return result; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 683 | static int |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 684 | internal_select(PySocketSockObject *s, int writing, _PyTime_t interval, |
| 685 | int connect) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 686 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 687 | int n; |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 688 | #ifdef HAVE_POLL |
| 689 | struct pollfd pollfd; |
Victor Stinner | e6951c6 | 2015-03-31 13:50:44 +0200 | [diff] [blame] | 690 | _PyTime_t ms; |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 691 | #else |
Victor Stinner | 416f2e6 | 2015-03-31 13:56:29 +0200 | [diff] [blame] | 692 | fd_set fds, efds; |
Victor Stinner | ced1174 | 2015-04-09 10:27:25 +0200 | [diff] [blame] | 693 | struct timeval tv, *tvp; |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 694 | #endif |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 695 | |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 696 | #ifdef WITH_THREAD |
| 697 | /* must be called with the GIL held */ |
| 698 | assert(PyGILState_Check()); |
| 699 | #endif |
| 700 | |
Victor Stinner | 416f2e6 | 2015-03-31 13:56:29 +0200 | [diff] [blame] | 701 | /* Error condition is for output only */ |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 702 | assert(!(connect && !writing)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 703 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | /* Guard against closed socket */ |
Victor Stinner | 0cec877 | 2016-07-22 17:26:53 +0200 | [diff] [blame] | 705 | if (s->sock_fd == INVALID_SOCKET) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 706 | return 0; |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 707 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 708 | /* Prefer poll, if available, since you can poll() any fd |
| 709 | * which can't be done with select(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 710 | #ifdef HAVE_POLL |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 711 | pollfd.fd = s->sock_fd; |
| 712 | pollfd.events = writing ? POLLOUT : POLLIN; |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 713 | if (connect) { |
| 714 | /* On Windows, the socket becomes writable on connection success, |
| 715 | but a connection failure is notified as an error. On POSIX, the |
| 716 | socket becomes writable on connection success or on connection |
| 717 | failure. */ |
Victor Stinner | 416f2e6 | 2015-03-31 13:56:29 +0200 | [diff] [blame] | 718 | pollfd.events |= POLLERR; |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 719 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 720 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 721 | /* s->sock_timeout is in seconds, timeout in ms */ |
Victor Stinner | 10550cd | 2015-04-03 13:22:27 +0200 | [diff] [blame] | 722 | ms = _PyTime_AsMilliseconds(interval, _PyTime_ROUND_CEILING); |
Victor Stinner | e6951c6 | 2015-03-31 13:50:44 +0200 | [diff] [blame] | 723 | assert(ms <= INT_MAX); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 724 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 725 | Py_BEGIN_ALLOW_THREADS; |
Victor Stinner | e6951c6 | 2015-03-31 13:50:44 +0200 | [diff] [blame] | 726 | n = poll(&pollfd, 1, (int)ms); |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 727 | Py_END_ALLOW_THREADS; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 728 | #else |
Victor Stinner | ced1174 | 2015-04-09 10:27:25 +0200 | [diff] [blame] | 729 | if (interval >= 0) { |
| 730 | _PyTime_AsTimeval_noraise(interval, &tv, _PyTime_ROUND_CEILING); |
| 731 | tvp = &tv; |
| 732 | } |
| 733 | else |
| 734 | tvp = NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 735 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 736 | FD_ZERO(&fds); |
| 737 | FD_SET(s->sock_fd, &fds); |
Victor Stinner | 416f2e6 | 2015-03-31 13:56:29 +0200 | [diff] [blame] | 738 | FD_ZERO(&efds); |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 739 | if (connect) { |
| 740 | /* On Windows, the socket becomes writable on connection success, |
| 741 | but a connection failure is notified as an error. On POSIX, the |
| 742 | socket becomes writable on connection success or on connection |
| 743 | failure. */ |
Victor Stinner | 416f2e6 | 2015-03-31 13:56:29 +0200 | [diff] [blame] | 744 | FD_SET(s->sock_fd, &efds); |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 745 | } |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 746 | |
| 747 | /* See if the socket is ready */ |
| 748 | Py_BEGIN_ALLOW_THREADS; |
| 749 | if (writing) |
| 750 | n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
Victor Stinner | ced1174 | 2015-04-09 10:27:25 +0200 | [diff] [blame] | 751 | NULL, &fds, &efds, tvp); |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 752 | else |
| 753 | n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
Victor Stinner | ced1174 | 2015-04-09 10:27:25 +0200 | [diff] [blame] | 754 | &fds, NULL, &efds, tvp); |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 755 | Py_END_ALLOW_THREADS; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 756 | #endif |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 757 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 758 | if (n < 0) |
| 759 | return -1; |
| 760 | if (n == 0) |
| 761 | return 1; |
| 762 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 765 | /* Call a socket function. |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 766 | |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 767 | On error, raise an exception and return -1 if err is set, or fill err and |
| 768 | return -1 otherwise. If a signal was received and the signal handler raised |
| 769 | an exception, return -1, and set err to -1 if err is set. |
| 770 | |
| 771 | On success, return 0, and set err to 0 if err is set. |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 772 | |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 773 | If the socket has a timeout, wait until the socket is ready before calling |
| 774 | the function: wait until the socket is writable if writing is nonzero, wait |
| 775 | until the socket received data otherwise. |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 776 | |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 777 | If the socket function is interrupted by a signal (failed with EINTR): retry |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 778 | the function, except if the signal handler raised an exception (PEP 475). |
| 779 | |
| 780 | When the function is retried, recompute the timeout using a monotonic clock. |
| 781 | |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 782 | sock_call_ex() must be called with the GIL held. The socket function is |
| 783 | called with the GIL released. */ |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 784 | static int |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 785 | sock_call_ex(PySocketSockObject *s, |
| 786 | int writing, |
| 787 | int (*sock_func) (PySocketSockObject *s, void *data), |
| 788 | void *data, |
| 789 | int connect, |
Victor Stinner | 8912d14 | 2015-04-06 23:16:34 +0200 | [diff] [blame] | 790 | int *err, |
| 791 | _PyTime_t timeout) |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 792 | { |
Victor Stinner | 8912d14 | 2015-04-06 23:16:34 +0200 | [diff] [blame] | 793 | int has_timeout = (timeout > 0); |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 794 | _PyTime_t deadline = 0; |
| 795 | int deadline_initialized = 0; |
| 796 | int res; |
| 797 | |
Victor Stinner | 92f0113 | 2015-10-11 09:54:42 +0200 | [diff] [blame] | 798 | #ifdef WITH_THREAD |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 799 | /* sock_call() must be called with the GIL held. */ |
| 800 | assert(PyGILState_Check()); |
Victor Stinner | 92f0113 | 2015-10-11 09:54:42 +0200 | [diff] [blame] | 801 | #endif |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 802 | |
| 803 | /* outer loop to retry select() when select() is interrupted by a signal |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 804 | or to retry select()+sock_func() on false positive (see above) */ |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 805 | while (1) { |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 806 | /* For connect(), poll even for blocking socket. The connection |
| 807 | runs asynchronously. */ |
| 808 | if (has_timeout || connect) { |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 809 | if (has_timeout) { |
Victor Stinner | 10550cd | 2015-04-03 13:22:27 +0200 | [diff] [blame] | 810 | _PyTime_t interval; |
| 811 | |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 812 | if (deadline_initialized) { |
| 813 | /* recompute the timeout */ |
| 814 | interval = deadline - _PyTime_GetMonotonicClock(); |
| 815 | } |
| 816 | else { |
| 817 | deadline_initialized = 1; |
Victor Stinner | 8912d14 | 2015-04-06 23:16:34 +0200 | [diff] [blame] | 818 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 819 | interval = timeout; |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 820 | } |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 821 | |
Victor Stinner | 10550cd | 2015-04-03 13:22:27 +0200 | [diff] [blame] | 822 | if (interval >= 0) |
| 823 | res = internal_select(s, writing, interval, connect); |
| 824 | else |
| 825 | res = 1; |
| 826 | } |
| 827 | else { |
Victor Stinner | 222dfc7 | 2015-04-06 22:30:41 +0200 | [diff] [blame] | 828 | res = internal_select(s, writing, timeout, connect); |
Victor Stinner | 10550cd | 2015-04-03 13:22:27 +0200 | [diff] [blame] | 829 | } |
| 830 | |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 831 | if (res == -1) { |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 832 | if (err) |
| 833 | *err = GET_SOCK_ERROR; |
| 834 | |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 835 | if (CHECK_ERRNO(EINTR)) { |
| 836 | /* select() was interrupted by a signal */ |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 837 | if (PyErr_CheckSignals()) { |
| 838 | if (err) |
| 839 | *err = -1; |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 840 | return -1; |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 841 | } |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 842 | |
| 843 | /* retry select() */ |
| 844 | continue; |
| 845 | } |
| 846 | |
| 847 | /* select() failed */ |
| 848 | s->errorhandler(); |
| 849 | return -1; |
| 850 | } |
| 851 | |
| 852 | if (res == 1) { |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 853 | if (err) |
| 854 | *err = SOCK_TIMEOUT_ERR; |
| 855 | else |
| 856 | PyErr_SetString(socket_timeout, "timed out"); |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 857 | return -1; |
| 858 | } |
| 859 | |
| 860 | /* the socket is ready */ |
| 861 | } |
| 862 | |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 863 | /* inner loop to retry sock_func() when sock_func() is interrupted |
| 864 | by a signal */ |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 865 | while (1) { |
| 866 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 867 | res = sock_func(s, data); |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 868 | Py_END_ALLOW_THREADS |
| 869 | |
| 870 | if (res) { |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 871 | /* sock_func() succeeded */ |
| 872 | if (err) |
| 873 | *err = 0; |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 874 | return 0; |
| 875 | } |
| 876 | |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 877 | if (err) |
| 878 | *err = GET_SOCK_ERROR; |
| 879 | |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 880 | if (!CHECK_ERRNO(EINTR)) |
| 881 | break; |
| 882 | |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 883 | /* sock_func() was interrupted by a signal */ |
| 884 | if (PyErr_CheckSignals()) { |
| 885 | if (err) |
| 886 | *err = -1; |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 887 | return -1; |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 888 | } |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 889 | |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 890 | /* retry sock_func() */ |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | if (s->sock_timeout > 0 |
| 894 | && (CHECK_ERRNO(EWOULDBLOCK) || CHECK_ERRNO(EAGAIN))) { |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 895 | /* False positive: sock_func() failed with EWOULDBLOCK or EAGAIN. |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 896 | |
| 897 | For example, select() could indicate a socket is ready for |
| 898 | reading, but the data then discarded by the OS because of a |
| 899 | wrong checksum. |
| 900 | |
| 901 | Loop on select() to recheck for socket readyness. */ |
| 902 | continue; |
| 903 | } |
| 904 | |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 905 | /* sock_func() failed */ |
| 906 | if (!err) |
| 907 | s->errorhandler(); |
| 908 | /* else: err was already set before */ |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 909 | return -1; |
| 910 | } |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 911 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 912 | |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 913 | static int |
| 914 | sock_call(PySocketSockObject *s, |
| 915 | int writing, |
| 916 | int (*func) (PySocketSockObject *s, void *data), |
| 917 | void *data) |
| 918 | { |
Victor Stinner | 8912d14 | 2015-04-06 23:16:34 +0200 | [diff] [blame] | 919 | return sock_call_ex(s, writing, func, data, 0, NULL, s->sock_timeout); |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 920 | } |
| 921 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 922 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 923 | /* Initialize a new socket object. */ |
| 924 | |
Victor Stinner | 88ed640 | 2015-04-09 10:23:12 +0200 | [diff] [blame] | 925 | /* Default timeout for new sockets */ |
| 926 | static _PyTime_t defaulttimeout = _PYTIME_FROMSECONDS(-1); |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 927 | |
Christian Heimes | 0b9d64e | 2016-09-09 00:28:57 +0200 | [diff] [blame] | 928 | static int |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 929 | init_sockobject(PySocketSockObject *s, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 930 | SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 931 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 932 | s->sock_fd = fd; |
| 933 | s->sock_family = family; |
| 934 | s->sock_type = type; |
| 935 | s->sock_proto = proto; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 936 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 937 | s->errorhandler = &set_error; |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 938 | #ifdef SOCK_NONBLOCK |
| 939 | if (type & SOCK_NONBLOCK) |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 940 | s->sock_timeout = 0; |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 941 | else |
| 942 | #endif |
| 943 | { |
| 944 | s->sock_timeout = defaulttimeout; |
Christian Heimes | 0b9d64e | 2016-09-09 00:28:57 +0200 | [diff] [blame] | 945 | if (defaulttimeout >= 0) { |
| 946 | if (internal_setblocking(s, 0) == -1) { |
| 947 | return -1; |
| 948 | } |
| 949 | } |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 950 | } |
Christian Heimes | 0b9d64e | 2016-09-09 00:28:57 +0200 | [diff] [blame] | 951 | return 0; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 955 | /* Create a new socket object. |
| 956 | This just creates the object and initializes it. |
| 957 | If the creation fails, return NULL and set an exception (implicit |
| 958 | in NEWOBJ()). */ |
| 959 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 960 | static PySocketSockObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 961 | new_sockobject(SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 962 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | PySocketSockObject *s; |
| 964 | s = (PySocketSockObject *) |
| 965 | PyType_GenericNew(&sock_type, NULL, NULL); |
Christian Heimes | 0b9d64e | 2016-09-09 00:28:57 +0200 | [diff] [blame] | 966 | if (s == NULL) |
| 967 | return NULL; |
| 968 | if (init_sockobject(s, fd, family, type, proto) == -1) { |
| 969 | Py_DECREF(s); |
| 970 | return NULL; |
| 971 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 972 | return s; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 975 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 976 | /* Lock to allow python interpreter to continue, but only allow one |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 977 | thread to be in gethostbyname or getaddrinfo */ |
| 978 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Matthias Klose | d182a6c | 2012-08-14 17:29:04 +0200 | [diff] [blame] | 979 | static PyThread_type_lock netdb_lock; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 980 | #endif |
| 981 | |
| 982 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 983 | /* Convert a string specifying a host name or one of a few symbolic |
| 984 | names to a numeric IP address. This usually calls gethostbyname() |
| 985 | to do the work; the names "" and "<broadcast>" are special. |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 986 | Return the length (IPv4 should be 4 bytes), or negative if |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 987 | an error occurred; then an exception is raised. */ |
| 988 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 989 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 990 | setipaddr(const 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] | 991 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 992 | struct addrinfo hints, *res; |
| 993 | int error; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 994 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 995 | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
| 996 | if (name[0] == '\0') { |
| 997 | int siz; |
| 998 | memset(&hints, 0, sizeof(hints)); |
| 999 | hints.ai_family = af; |
| 1000 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
| 1001 | hints.ai_flags = AI_PASSIVE; |
| 1002 | Py_BEGIN_ALLOW_THREADS |
| 1003 | ACQUIRE_GETADDRINFO_LOCK |
| 1004 | error = getaddrinfo(NULL, "0", &hints, &res); |
| 1005 | Py_END_ALLOW_THREADS |
| 1006 | /* We assume that those thread-unsafe getaddrinfo() versions |
| 1007 | *are* safe regarding their return value, ie. that a |
| 1008 | subsequent call to getaddrinfo() does not destroy the |
| 1009 | outcome of the first call. */ |
| 1010 | RELEASE_GETADDRINFO_LOCK |
| 1011 | if (error) { |
| 1012 | set_gaierror(error); |
| 1013 | return -1; |
| 1014 | } |
| 1015 | switch (res->ai_family) { |
| 1016 | case AF_INET: |
| 1017 | siz = 4; |
| 1018 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1019 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | case AF_INET6: |
| 1021 | siz = 16; |
| 1022 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1023 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1024 | default: |
| 1025 | freeaddrinfo(res); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1026 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | "unsupported address family"); |
| 1028 | return -1; |
| 1029 | } |
| 1030 | if (res->ai_next) { |
| 1031 | freeaddrinfo(res); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1032 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | "wildcard resolved to multiple address"); |
| 1034 | return -1; |
| 1035 | } |
| 1036 | if (res->ai_addrlen < addr_ret_size) |
| 1037 | addr_ret_size = res->ai_addrlen; |
| 1038 | memcpy(addr_ret, res->ai_addr, addr_ret_size); |
| 1039 | freeaddrinfo(res); |
| 1040 | return siz; |
| 1041 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 1042 | /* special-case broadcast - inet_addr() below can return INADDR_NONE for |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 1043 | * this */ |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 1044 | if (strcmp(name, "255.255.255.255") == 0 || |
| 1045 | strcmp(name, "<broadcast>") == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1046 | struct sockaddr_in *sin; |
| 1047 | if (af != AF_INET && af != AF_UNSPEC) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1048 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | "address family mismatched"); |
| 1050 | return -1; |
| 1051 | } |
| 1052 | sin = (struct sockaddr_in *)addr_ret; |
| 1053 | memset((void *) sin, '\0', sizeof(*sin)); |
| 1054 | sin->sin_family = AF_INET; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1055 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1056 | sin->sin_len = sizeof(*sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1057 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1058 | sin->sin_addr.s_addr = INADDR_BROADCAST; |
| 1059 | return sizeof(sin->sin_addr); |
| 1060 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 1061 | |
| 1062 | /* avoid a name resolution in case of numeric address */ |
| 1063 | #ifdef HAVE_INET_PTON |
| 1064 | /* check for an IPv4 address */ |
| 1065 | if (af == AF_UNSPEC || af == AF_INET) { |
| 1066 | struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret; |
| 1067 | memset(sin, 0, sizeof(*sin)); |
| 1068 | if (inet_pton(AF_INET, name, &sin->sin_addr) > 0) { |
| 1069 | sin->sin_family = AF_INET; |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 1070 | #ifdef HAVE_SOCKADDR_SA_LEN |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 1071 | sin->sin_len = sizeof(*sin); |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 1072 | #endif |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 1073 | return 4; |
| 1074 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 1076 | #ifdef ENABLE_IPV6 |
| 1077 | /* check for an IPv6 address - if the address contains a scope ID, we |
| 1078 | * fallback to getaddrinfo(), which can handle translation from interface |
| 1079 | * name to interface index */ |
| 1080 | if ((af == AF_UNSPEC || af == AF_INET6) && !strchr(name, '%')) { |
| 1081 | struct sockaddr_in6 *sin = (struct sockaddr_in6 *)addr_ret; |
| 1082 | memset(sin, 0, sizeof(*sin)); |
| 1083 | if (inet_pton(AF_INET6, name, &sin->sin6_addr) > 0) { |
| 1084 | sin->sin6_family = AF_INET6; |
| 1085 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 1086 | sin->sin6_len = sizeof(*sin); |
| 1087 | #endif |
| 1088 | return 16; |
| 1089 | } |
| 1090 | } |
| 1091 | #endif /* ENABLE_IPV6 */ |
| 1092 | #else /* HAVE_INET_PTON */ |
| 1093 | /* check for an IPv4 address */ |
| 1094 | if (af == AF_INET || af == AF_UNSPEC) { |
| 1095 | struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret; |
| 1096 | memset(sin, 0, sizeof(*sin)); |
| 1097 | if ((sin->sin_addr.s_addr = inet_addr(name)) != INADDR_NONE) { |
| 1098 | sin->sin_family = AF_INET; |
| 1099 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 1100 | sin->sin_len = sizeof(*sin); |
| 1101 | #endif |
| 1102 | return 4; |
| 1103 | } |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 1104 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 1105 | #endif /* HAVE_INET_PTON */ |
| 1106 | |
| 1107 | /* perform a name resolution */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1108 | memset(&hints, 0, sizeof(hints)); |
| 1109 | hints.ai_family = af; |
| 1110 | Py_BEGIN_ALLOW_THREADS |
| 1111 | ACQUIRE_GETADDRINFO_LOCK |
| 1112 | error = getaddrinfo(name, NULL, &hints, &res); |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 1113 | #if defined(__digital__) && defined(__unix__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1114 | if (error == EAI_NONAME && af == AF_UNSPEC) { |
| 1115 | /* On Tru64 V5.1, numeric-to-addr conversion fails |
| 1116 | if no address family is given. Assume IPv4 for now.*/ |
| 1117 | hints.ai_family = AF_INET; |
| 1118 | error = getaddrinfo(name, NULL, &hints, &res); |
| 1119 | } |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 1120 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1121 | Py_END_ALLOW_THREADS |
| 1122 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 1123 | if (error) { |
| 1124 | set_gaierror(error); |
| 1125 | return -1; |
| 1126 | } |
| 1127 | if (res->ai_addrlen < addr_ret_size) |
| 1128 | addr_ret_size = res->ai_addrlen; |
| 1129 | memcpy((char *) addr_ret, res->ai_addr, addr_ret_size); |
| 1130 | freeaddrinfo(res); |
| 1131 | switch (addr_ret->sa_family) { |
| 1132 | case AF_INET: |
| 1133 | return 4; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1134 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1135 | case AF_INET6: |
| 1136 | return 16; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 1137 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1138 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1139 | PyErr_SetString(PyExc_OSError, "unknown address family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1140 | return -1; |
| 1141 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1144 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1145 | /* Create a string object representing an IP address. |
| 1146 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 1147 | size numbers). */ |
| 1148 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1149 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1150 | makeipaddr(struct sockaddr *addr, int addrlen) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1151 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1152 | char buf[NI_MAXHOST]; |
| 1153 | int error; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, |
| 1156 | NI_NUMERICHOST); |
| 1157 | if (error) { |
| 1158 | set_gaierror(error); |
| 1159 | return NULL; |
| 1160 | } |
| 1161 | return PyUnicode_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1165 | #ifdef USE_BLUETOOTH |
| 1166 | /* Convert a string representation of a Bluetooth address into a numeric |
| 1167 | address. Returns the length (6), or raises an exception and returns -1 if |
| 1168 | an error occurred. */ |
| 1169 | |
| 1170 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1171 | setbdaddr(const char *name, bdaddr_t *bdaddr) |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1172 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 | unsigned int b0, b1, b2, b3, b4, b5; |
| 1174 | char ch; |
| 1175 | int n; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1177 | n = sscanf(name, "%X:%X:%X:%X:%X:%X%c", |
| 1178 | &b5, &b4, &b3, &b2, &b1, &b0, &ch); |
| 1179 | if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) { |
| 1180 | bdaddr->b[0] = b0; |
| 1181 | bdaddr->b[1] = b1; |
| 1182 | bdaddr->b[2] = b2; |
| 1183 | bdaddr->b[3] = b3; |
| 1184 | bdaddr->b[4] = b4; |
| 1185 | bdaddr->b[5] = b5; |
| 1186 | return 6; |
| 1187 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1188 | PyErr_SetString(PyExc_OSError, "bad bluetooth address"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1189 | return -1; |
| 1190 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | /* Create a string representation of the Bluetooth address. This is always a |
| 1194 | string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal |
| 1195 | value (zero padded if necessary). */ |
| 1196 | |
| 1197 | static PyObject * |
| 1198 | makebdaddr(bdaddr_t *bdaddr) |
| 1199 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1200 | char buf[(6 * 2) + 5 + 1]; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1202 | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 1203 | bdaddr->b[5], bdaddr->b[4], bdaddr->b[3], |
| 1204 | bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]); |
| 1205 | return PyUnicode_FromString(buf); |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1206 | } |
| 1207 | #endif |
| 1208 | |
| 1209 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1210 | /* Create an object representing the given socket address, |
| 1211 | suitable for passing it back to bind(), connect() etc. |
| 1212 | The family field of the sockaddr structure is inspected |
| 1213 | to determine what kind of address it really is. */ |
| 1214 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1215 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1216 | static PyObject * |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 1217 | makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1218 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1219 | if (addrlen == 0) { |
| 1220 | /* No address -- may be recvfrom() from known socket */ |
| 1221 | Py_INCREF(Py_None); |
| 1222 | return Py_None; |
| 1223 | } |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1224 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1225 | switch (addr->sa_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1227 | case AF_INET: |
| 1228 | { |
| 1229 | struct sockaddr_in *a; |
| 1230 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1231 | PyObject *ret = NULL; |
| 1232 | if (addrobj) { |
| 1233 | a = (struct sockaddr_in *)addr; |
| 1234 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 1235 | Py_DECREF(addrobj); |
| 1236 | } |
| 1237 | return ret; |
| 1238 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1239 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1240 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 | case AF_UNIX: |
| 1242 | { |
| 1243 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Benjamin Peterson | b6c3458 | 2016-09-07 14:08:34 -0700 | [diff] [blame] | 1244 | #ifdef __linux__ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1245 | if (a->sun_path[0] == 0) { /* Linux abstract namespace */ |
| 1246 | addrlen -= offsetof(struct sockaddr_un, sun_path); |
Charles-François Natali | b10c71d | 2013-05-21 10:45:46 +0200 | [diff] [blame] | 1247 | return PyBytes_FromStringAndSize(a->sun_path, addrlen); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1248 | } |
| 1249 | else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1250 | #endif /* linux */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1251 | { |
| 1252 | /* regular NULL-terminated string */ |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1253 | return PyUnicode_DecodeFSDefault(a->sun_path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1254 | } |
| 1255 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1256 | #endif /* AF_UNIX */ |
| 1257 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1258 | #if defined(AF_NETLINK) |
| 1259 | case AF_NETLINK: |
| 1260 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1261 | struct sockaddr_nl *a = (struct sockaddr_nl *) addr; |
| 1262 | return Py_BuildValue("II", a->nl_pid, a->nl_groups); |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1263 | } |
| 1264 | #endif /* AF_NETLINK */ |
| 1265 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1266 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1267 | case AF_INET6: |
| 1268 | { |
| 1269 | struct sockaddr_in6 *a; |
| 1270 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1271 | PyObject *ret = NULL; |
| 1272 | if (addrobj) { |
| 1273 | a = (struct sockaddr_in6 *)addr; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1274 | ret = Py_BuildValue("OiII", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1275 | addrobj, |
| 1276 | ntohs(a->sin6_port), |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1277 | ntohl(a->sin6_flowinfo), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1278 | a->sin6_scope_id); |
| 1279 | Py_DECREF(addrobj); |
| 1280 | } |
| 1281 | return ret; |
| 1282 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1283 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1284 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1285 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1286 | case AF_BLUETOOTH: |
| 1287 | switch (proto) { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1288 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1289 | case BTPROTO_L2CAP: |
| 1290 | { |
| 1291 | struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr; |
| 1292 | PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr)); |
| 1293 | PyObject *ret = NULL; |
| 1294 | if (addrobj) { |
| 1295 | ret = Py_BuildValue("Oi", |
| 1296 | addrobj, |
| 1297 | _BT_L2_MEMB(a, psm)); |
| 1298 | Py_DECREF(addrobj); |
| 1299 | } |
| 1300 | return ret; |
| 1301 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1303 | case BTPROTO_RFCOMM: |
| 1304 | { |
| 1305 | struct sockaddr_rc *a = (struct sockaddr_rc *) addr; |
| 1306 | PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr)); |
| 1307 | PyObject *ret = NULL; |
| 1308 | if (addrobj) { |
| 1309 | ret = Py_BuildValue("Oi", |
| 1310 | addrobj, |
| 1311 | _BT_RC_MEMB(a, channel)); |
| 1312 | Py_DECREF(addrobj); |
| 1313 | } |
| 1314 | return ret; |
| 1315 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1316 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1317 | case BTPROTO_HCI: |
| 1318 | { |
| 1319 | struct sockaddr_hci *a = (struct sockaddr_hci *) addr; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1320 | #if defined(__NetBSD__) || defined(__DragonFly__) |
| 1321 | return makebdaddr(&_BT_HCI_MEMB(a, bdaddr)); |
| 1322 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1323 | PyObject *ret = NULL; |
| 1324 | ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev)); |
| 1325 | return ret; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1326 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1327 | } |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1328 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1329 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1330 | case BTPROTO_SCO: |
| 1331 | { |
| 1332 | struct sockaddr_sco *a = (struct sockaddr_sco *) addr; |
| 1333 | return makebdaddr(&_BT_SCO_MEMB(a, bdaddr)); |
| 1334 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1335 | #endif |
| 1336 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1337 | default: |
| 1338 | PyErr_SetString(PyExc_ValueError, |
| 1339 | "Unknown Bluetooth protocol"); |
| 1340 | return NULL; |
| 1341 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1342 | #endif |
| 1343 | |
Antoine Pitrou | b156a46 | 2010-10-27 20:13:57 +0000 | [diff] [blame] | 1344 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1345 | case AF_PACKET: |
| 1346 | { |
| 1347 | struct sockaddr_ll *a = (struct sockaddr_ll *)addr; |
| 1348 | char *ifname = ""; |
| 1349 | struct ifreq ifr; |
| 1350 | /* need to look up interface name give index */ |
| 1351 | if (a->sll_ifindex) { |
| 1352 | ifr.ifr_ifindex = a->sll_ifindex; |
| 1353 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
| 1354 | ifname = ifr.ifr_name; |
| 1355 | } |
| 1356 | return Py_BuildValue("shbhy#", |
| 1357 | ifname, |
| 1358 | ntohs(a->sll_protocol), |
| 1359 | a->sll_pkttype, |
| 1360 | a->sll_hatype, |
| 1361 | a->sll_addr, |
| 1362 | a->sll_halen); |
| 1363 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1364 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1365 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1366 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1367 | case AF_TIPC: |
| 1368 | { |
| 1369 | struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr; |
| 1370 | if (a->addrtype == TIPC_ADDR_NAMESEQ) { |
| 1371 | return Py_BuildValue("IIIII", |
| 1372 | a->addrtype, |
| 1373 | a->addr.nameseq.type, |
| 1374 | a->addr.nameseq.lower, |
| 1375 | a->addr.nameseq.upper, |
| 1376 | a->scope); |
| 1377 | } else if (a->addrtype == TIPC_ADDR_NAME) { |
| 1378 | return Py_BuildValue("IIIII", |
| 1379 | a->addrtype, |
| 1380 | a->addr.name.name.type, |
| 1381 | a->addr.name.name.instance, |
| 1382 | a->addr.name.name.instance, |
| 1383 | a->scope); |
| 1384 | } else if (a->addrtype == TIPC_ADDR_ID) { |
| 1385 | return Py_BuildValue("IIIII", |
| 1386 | a->addrtype, |
| 1387 | a->addr.id.node, |
| 1388 | a->addr.id.ref, |
| 1389 | 0, |
| 1390 | a->scope); |
| 1391 | } else { |
| 1392 | PyErr_SetString(PyExc_ValueError, |
| 1393 | "Invalid address type"); |
| 1394 | return NULL; |
| 1395 | } |
| 1396 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1397 | #endif |
| 1398 | |
Vinay Sajip | ecfc98c | 2014-03-20 12:42:42 +0000 | [diff] [blame] | 1399 | #ifdef AF_CAN |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1400 | case AF_CAN: |
| 1401 | { |
| 1402 | struct sockaddr_can *a = (struct sockaddr_can *)addr; |
| 1403 | char *ifname = ""; |
| 1404 | struct ifreq ifr; |
| 1405 | /* need to look up interface name given index */ |
| 1406 | if (a->can_ifindex) { |
| 1407 | ifr.ifr_ifindex = a->can_ifindex; |
| 1408 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
| 1409 | ifname = ifr.ifr_name; |
| 1410 | } |
| 1411 | |
| 1412 | return Py_BuildValue("O&h", PyUnicode_DecodeFSDefault, |
| 1413 | ifname, |
| 1414 | a->can_family); |
| 1415 | } |
| 1416 | #endif |
| 1417 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1418 | #ifdef PF_SYSTEM |
| 1419 | case PF_SYSTEM: |
| 1420 | switch(proto) { |
| 1421 | #ifdef SYSPROTO_CONTROL |
| 1422 | case SYSPROTO_CONTROL: |
| 1423 | { |
| 1424 | struct sockaddr_ctl *a = (struct sockaddr_ctl *)addr; |
| 1425 | return Py_BuildValue("(II)", a->sc_id, a->sc_unit); |
| 1426 | } |
| 1427 | #endif |
| 1428 | default: |
| 1429 | PyErr_SetString(PyExc_ValueError, |
| 1430 | "Invalid address type"); |
| 1431 | return 0; |
| 1432 | } |
| 1433 | #endif |
| 1434 | |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 1435 | #ifdef HAVE_SOCKADDR_ALG |
| 1436 | case AF_ALG: |
| 1437 | { |
| 1438 | struct sockaddr_alg *a = (struct sockaddr_alg *)addr; |
| 1439 | return Py_BuildValue("s#s#HH", |
| 1440 | a->salg_type, |
| 1441 | strnlen((const char*)a->salg_type, |
| 1442 | sizeof(a->salg_type)), |
| 1443 | a->salg_name, |
| 1444 | strnlen((const char*)a->salg_name, |
| 1445 | sizeof(a->salg_name)), |
| 1446 | a->salg_feat, |
| 1447 | a->salg_mask); |
| 1448 | } |
| 1449 | #endif |
| 1450 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1451 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1452 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1453 | default: |
| 1454 | /* If we don't know the address family, don't raise an |
| 1455 | exception -- return it as an (int, bytes) tuple. */ |
| 1456 | return Py_BuildValue("iy#", |
| 1457 | addr->sa_family, |
| 1458 | addr->sa_data, |
| 1459 | sizeof(addr->sa_data)); |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1460 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1461 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1464 | /* Helper for getsockaddrarg: bypass IDNA for ASCII-only host names |
| 1465 | (in particular, numeric IP addresses). */ |
| 1466 | struct maybe_idna { |
| 1467 | PyObject *obj; |
| 1468 | char *buf; |
| 1469 | }; |
| 1470 | |
| 1471 | static void |
| 1472 | idna_cleanup(struct maybe_idna *data) |
| 1473 | { |
| 1474 | Py_CLEAR(data->obj); |
| 1475 | } |
| 1476 | |
| 1477 | static int |
| 1478 | idna_converter(PyObject *obj, struct maybe_idna *data) |
| 1479 | { |
| 1480 | size_t len; |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 1481 | PyObject *obj2; |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1482 | if (obj == NULL) { |
| 1483 | idna_cleanup(data); |
| 1484 | return 1; |
| 1485 | } |
| 1486 | data->obj = NULL; |
| 1487 | len = -1; |
| 1488 | if (PyBytes_Check(obj)) { |
| 1489 | data->buf = PyBytes_AsString(obj); |
| 1490 | len = PyBytes_Size(obj); |
| 1491 | } |
| 1492 | else if (PyByteArray_Check(obj)) { |
| 1493 | data->buf = PyByteArray_AsString(obj); |
| 1494 | len = PyByteArray_Size(obj); |
| 1495 | } |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 1496 | else if (PyUnicode_Check(obj)) { |
| 1497 | if (PyUnicode_READY(obj) == 0 && PyUnicode_IS_COMPACT_ASCII(obj)) { |
| 1498 | data->buf = PyUnicode_DATA(obj); |
| 1499 | len = PyUnicode_GET_LENGTH(obj); |
| 1500 | } |
| 1501 | else { |
| 1502 | obj2 = PyUnicode_AsEncodedString(obj, "idna", NULL); |
| 1503 | if (!obj2) { |
| 1504 | PyErr_SetString(PyExc_TypeError, "encoding of hostname failed"); |
| 1505 | return 0; |
| 1506 | } |
| 1507 | assert(PyBytes_Check(obj2)); |
| 1508 | data->obj = obj2; |
| 1509 | data->buf = PyBytes_AS_STRING(obj2); |
| 1510 | len = PyBytes_GET_SIZE(obj2); |
| 1511 | } |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1512 | } |
| 1513 | else { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 1514 | PyErr_Format(PyExc_TypeError, "str, bytes or bytearray expected, not %s", |
| 1515 | obj->ob_type->tp_name); |
| 1516 | return 0; |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1517 | } |
| 1518 | if (strlen(data->buf) != len) { |
| 1519 | Py_CLEAR(data->obj); |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1520 | PyErr_SetString(PyExc_TypeError, "host name must not contain null character"); |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1521 | return 0; |
| 1522 | } |
| 1523 | return Py_CLEANUP_SUPPORTED; |
Victor Stinner | 1a62a68 | 2014-08-17 19:33:28 +0200 | [diff] [blame] | 1524 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1525 | |
| 1526 | /* Parse a socket address argument according to the socket object's |
| 1527 | address family. Return 1 if the address was in the proper format, |
| 1528 | 0 of not. The address is returned through addr_ret, its length |
| 1529 | through len_ret. */ |
| 1530 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1531 | static int |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1532 | getsockaddrarg(PySocketSockObject *s, PyObject *args, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1533 | struct sockaddr *addr_ret, int *len_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1534 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1535 | switch (s->sock_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1536 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1537 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1538 | case AF_UNIX: |
| 1539 | { |
| 1540 | struct sockaddr_un* addr; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1541 | Py_buffer path; |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1542 | int retval = 0; |
| 1543 | |
| 1544 | /* PEP 383. Not using PyUnicode_FSConverter since we need to |
| 1545 | allow embedded nulls on Linux. */ |
| 1546 | if (PyUnicode_Check(args)) { |
| 1547 | if ((args = PyUnicode_EncodeFSDefault(args)) == NULL) |
| 1548 | return 0; |
| 1549 | } |
| 1550 | else |
| 1551 | Py_INCREF(args); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1552 | if (!PyArg_Parse(args, "y*", &path)) { |
| 1553 | Py_DECREF(args); |
| 1554 | return retval; |
| 1555 | } |
| 1556 | assert(path.len >= 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1557 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1558 | addr = (struct sockaddr_un*)addr_ret; |
Benjamin Peterson | b6c3458 | 2016-09-07 14:08:34 -0700 | [diff] [blame] | 1559 | #ifdef __linux__ |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1560 | if (path.len > 0 && *(const char *)path.buf == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1561 | /* Linux abstract namespace extension */ |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1562 | if ((size_t)path.len > sizeof addr->sun_path) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1563 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1564 | "AF_UNIX path too long"); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1565 | goto unix_out; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1566 | } |
| 1567 | } |
| 1568 | else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1569 | #endif /* linux */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 | { |
| 1571 | /* regular NULL-terminated string */ |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1572 | if ((size_t)path.len >= sizeof addr->sun_path) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1573 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1574 | "AF_UNIX path too long"); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1575 | goto unix_out; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1576 | } |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1577 | addr->sun_path[path.len] = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1578 | } |
| 1579 | addr->sun_family = s->sock_family; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1580 | memcpy(addr->sun_path, path.buf, path.len); |
| 1581 | *len_ret = path.len + offsetof(struct sockaddr_un, sun_path); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1582 | retval = 1; |
| 1583 | unix_out: |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1584 | PyBuffer_Release(&path); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1585 | Py_DECREF(args); |
| 1586 | return retval; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1587 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1588 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1589 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1590 | #if defined(AF_NETLINK) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1591 | case AF_NETLINK: |
| 1592 | { |
| 1593 | struct sockaddr_nl* addr; |
| 1594 | int pid, groups; |
| 1595 | addr = (struct sockaddr_nl *)addr_ret; |
| 1596 | if (!PyTuple_Check(args)) { |
| 1597 | PyErr_Format( |
| 1598 | PyExc_TypeError, |
| 1599 | "getsockaddrarg: " |
| 1600 | "AF_NETLINK address must be tuple, not %.500s", |
| 1601 | Py_TYPE(args)->tp_name); |
| 1602 | return 0; |
| 1603 | } |
| 1604 | if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups)) |
| 1605 | return 0; |
| 1606 | addr->nl_family = AF_NETLINK; |
| 1607 | addr->nl_pid = pid; |
| 1608 | addr->nl_groups = groups; |
| 1609 | *len_ret = sizeof(*addr); |
| 1610 | return 1; |
| 1611 | } |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1612 | #endif |
| 1613 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 1614 | #ifdef AF_RDS |
| 1615 | case AF_RDS: |
| 1616 | /* RDS sockets use sockaddr_in: fall-through */ |
| 1617 | #endif |
| 1618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1619 | case AF_INET: |
| 1620 | { |
| 1621 | struct sockaddr_in* addr; |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1622 | struct maybe_idna host = {NULL, NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1623 | int port, result; |
| 1624 | if (!PyTuple_Check(args)) { |
| 1625 | PyErr_Format( |
| 1626 | PyExc_TypeError, |
| 1627 | "getsockaddrarg: " |
| 1628 | "AF_INET address must be tuple, not %.500s", |
| 1629 | Py_TYPE(args)->tp_name); |
| 1630 | return 0; |
| 1631 | } |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1632 | if (!PyArg_ParseTuple(args, "O&i:getsockaddrarg", |
| 1633 | idna_converter, &host, &port)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1634 | return 0; |
| 1635 | addr=(struct sockaddr_in*)addr_ret; |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1636 | result = setipaddr(host.buf, (struct sockaddr *)addr, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1637 | sizeof(*addr), AF_INET); |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1638 | idna_cleanup(&host); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1639 | if (result < 0) |
| 1640 | return 0; |
| 1641 | if (port < 0 || port > 0xffff) { |
| 1642 | PyErr_SetString( |
| 1643 | PyExc_OverflowError, |
| 1644 | "getsockaddrarg: port must be 0-65535."); |
| 1645 | return 0; |
| 1646 | } |
| 1647 | addr->sin_family = AF_INET; |
| 1648 | addr->sin_port = htons((short)port); |
| 1649 | *len_ret = sizeof *addr; |
| 1650 | return 1; |
| 1651 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1652 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1653 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1654 | case AF_INET6: |
| 1655 | { |
| 1656 | struct sockaddr_in6* addr; |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1657 | struct maybe_idna host = {NULL, NULL}; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1658 | int port, result; |
| 1659 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1660 | flowinfo = scope_id = 0; |
| 1661 | if (!PyTuple_Check(args)) { |
| 1662 | PyErr_Format( |
| 1663 | PyExc_TypeError, |
| 1664 | "getsockaddrarg: " |
| 1665 | "AF_INET6 address must be tuple, not %.500s", |
| 1666 | Py_TYPE(args)->tp_name); |
| 1667 | return 0; |
| 1668 | } |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1669 | if (!PyArg_ParseTuple(args, "O&i|II", |
| 1670 | idna_converter, &host, &port, &flowinfo, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1671 | &scope_id)) { |
| 1672 | return 0; |
| 1673 | } |
| 1674 | addr = (struct sockaddr_in6*)addr_ret; |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1675 | result = setipaddr(host.buf, (struct sockaddr *)addr, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1676 | sizeof(*addr), AF_INET6); |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1677 | idna_cleanup(&host); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1678 | if (result < 0) |
| 1679 | return 0; |
| 1680 | if (port < 0 || port > 0xffff) { |
| 1681 | PyErr_SetString( |
| 1682 | PyExc_OverflowError, |
| 1683 | "getsockaddrarg: port must be 0-65535."); |
| 1684 | return 0; |
| 1685 | } |
Charles-François Natali | 9b0c006 | 2012-06-23 10:17:05 +0200 | [diff] [blame] | 1686 | if (flowinfo > 0xfffff) { |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1687 | PyErr_SetString( |
| 1688 | PyExc_OverflowError, |
| 1689 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 1690 | return 0; |
| 1691 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1692 | addr->sin6_family = s->sock_family; |
| 1693 | addr->sin6_port = htons((short)port); |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1694 | addr->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1695 | addr->sin6_scope_id = scope_id; |
| 1696 | *len_ret = sizeof *addr; |
| 1697 | return 1; |
| 1698 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1699 | #endif |
| 1700 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1701 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1702 | case AF_BLUETOOTH: |
| 1703 | { |
| 1704 | switch (s->sock_proto) { |
| 1705 | case BTPROTO_L2CAP: |
| 1706 | { |
| 1707 | struct sockaddr_l2 *addr; |
| 1708 | char *straddr; |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1709 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1710 | addr = (struct sockaddr_l2 *)addr_ret; |
| 1711 | memset(addr, 0, sizeof(struct sockaddr_l2)); |
| 1712 | _BT_L2_MEMB(addr, family) = AF_BLUETOOTH; |
| 1713 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1714 | &_BT_L2_MEMB(addr, psm))) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1715 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1716 | "wrong format"); |
| 1717 | return 0; |
| 1718 | } |
| 1719 | if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0) |
| 1720 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1721 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1722 | *len_ret = sizeof *addr; |
| 1723 | return 1; |
| 1724 | } |
| 1725 | case BTPROTO_RFCOMM: |
| 1726 | { |
| 1727 | struct sockaddr_rc *addr; |
| 1728 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1729 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1730 | addr = (struct sockaddr_rc *)addr_ret; |
| 1731 | _BT_RC_MEMB(addr, family) = AF_BLUETOOTH; |
| 1732 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1733 | &_BT_RC_MEMB(addr, channel))) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1734 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1735 | "wrong format"); |
| 1736 | return 0; |
| 1737 | } |
| 1738 | if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0) |
| 1739 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1740 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1741 | *len_ret = sizeof *addr; |
| 1742 | return 1; |
| 1743 | } |
| 1744 | case BTPROTO_HCI: |
| 1745 | { |
| 1746 | struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1747 | #if defined(__NetBSD__) || defined(__DragonFly__) |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1748 | char *straddr = PyBytes_AS_STRING(args); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1749 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1750 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1751 | if (straddr == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1752 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1753 | "wrong format"); |
| 1754 | return 0; |
| 1755 | } |
| 1756 | if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0) |
| 1757 | return 0; |
| 1758 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1759 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
| 1760 | if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1761 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1762 | "wrong format"); |
| 1763 | return 0; |
| 1764 | } |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1765 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1766 | *len_ret = sizeof *addr; |
| 1767 | return 1; |
| 1768 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1769 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1770 | case BTPROTO_SCO: |
| 1771 | { |
| 1772 | struct sockaddr_sco *addr; |
| 1773 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1774 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1775 | addr = (struct sockaddr_sco *)addr_ret; |
| 1776 | _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; |
| 1777 | if (!PyBytes_Check(args)) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1778 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1779 | "wrong format"); |
| 1780 | return 0; |
| 1781 | } |
| 1782 | straddr = PyBytes_AS_STRING(args); |
| 1783 | if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0) |
| 1784 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1785 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1786 | *len_ret = sizeof *addr; |
| 1787 | return 1; |
| 1788 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1789 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1790 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1791 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: unknown Bluetooth protocol"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1792 | return 0; |
| 1793 | } |
| 1794 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1795 | #endif |
| 1796 | |
Antoine Pitrou | b156a46 | 2010-10-27 20:13:57 +0000 | [diff] [blame] | 1797 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1798 | case AF_PACKET: |
| 1799 | { |
| 1800 | struct sockaddr_ll* addr; |
| 1801 | struct ifreq ifr; |
| 1802 | char *interfaceName; |
| 1803 | int protoNumber; |
| 1804 | int hatype = 0; |
| 1805 | int pkttype = 0; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1806 | Py_buffer haddr = {NULL, NULL}; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1808 | if (!PyTuple_Check(args)) { |
| 1809 | PyErr_Format( |
| 1810 | PyExc_TypeError, |
| 1811 | "getsockaddrarg: " |
| 1812 | "AF_PACKET address must be tuple, not %.500s", |
| 1813 | Py_TYPE(args)->tp_name); |
| 1814 | return 0; |
| 1815 | } |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1816 | if (!PyArg_ParseTuple(args, "si|iiy*", &interfaceName, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1817 | &protoNumber, &pkttype, &hatype, |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1818 | &haddr)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1819 | return 0; |
| 1820 | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name)); |
| 1821 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
| 1822 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
| 1823 | s->errorhandler(); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1824 | PyBuffer_Release(&haddr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1825 | return 0; |
| 1826 | } |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1827 | if (haddr.buf && haddr.len > 8) { |
| 1828 | PyErr_SetString(PyExc_ValueError, |
| 1829 | "Hardware address must be 8 bytes or less"); |
| 1830 | PyBuffer_Release(&haddr); |
| 1831 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1832 | } |
| 1833 | if (protoNumber < 0 || protoNumber > 0xffff) { |
| 1834 | PyErr_SetString( |
| 1835 | PyExc_OverflowError, |
| 1836 | "getsockaddrarg: protoNumber must be 0-65535."); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1837 | PyBuffer_Release(&haddr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1838 | return 0; |
| 1839 | } |
| 1840 | addr = (struct sockaddr_ll*)addr_ret; |
| 1841 | addr->sll_family = AF_PACKET; |
| 1842 | addr->sll_protocol = htons((short)protoNumber); |
| 1843 | addr->sll_ifindex = ifr.ifr_ifindex; |
| 1844 | addr->sll_pkttype = pkttype; |
| 1845 | addr->sll_hatype = hatype; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1846 | if (haddr.buf) { |
| 1847 | memcpy(&addr->sll_addr, haddr.buf, haddr.len); |
| 1848 | addr->sll_halen = haddr.len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1849 | } |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1850 | else |
| 1851 | addr->sll_halen = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1852 | *len_ret = sizeof *addr; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1853 | PyBuffer_Release(&haddr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1854 | return 1; |
| 1855 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1856 | #endif |
| 1857 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1858 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1859 | case AF_TIPC: |
| 1860 | { |
| 1861 | unsigned int atype, v1, v2, v3; |
| 1862 | unsigned int scope = TIPC_CLUSTER_SCOPE; |
| 1863 | struct sockaddr_tipc *addr; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1865 | if (!PyTuple_Check(args)) { |
| 1866 | PyErr_Format( |
| 1867 | PyExc_TypeError, |
| 1868 | "getsockaddrarg: " |
| 1869 | "AF_TIPC address must be tuple, not %.500s", |
| 1870 | Py_TYPE(args)->tp_name); |
| 1871 | return 0; |
| 1872 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1873 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1874 | if (!PyArg_ParseTuple(args, |
| 1875 | "IIII|I;Invalid TIPC address format", |
| 1876 | &atype, &v1, &v2, &v3, &scope)) |
| 1877 | return 0; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1879 | addr = (struct sockaddr_tipc *) addr_ret; |
| 1880 | memset(addr, 0, sizeof(struct sockaddr_tipc)); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1882 | addr->family = AF_TIPC; |
| 1883 | addr->scope = scope; |
| 1884 | addr->addrtype = atype; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1885 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1886 | if (atype == TIPC_ADDR_NAMESEQ) { |
| 1887 | addr->addr.nameseq.type = v1; |
| 1888 | addr->addr.nameseq.lower = v2; |
| 1889 | addr->addr.nameseq.upper = v3; |
| 1890 | } else if (atype == TIPC_ADDR_NAME) { |
| 1891 | addr->addr.name.name.type = v1; |
| 1892 | addr->addr.name.name.instance = v2; |
| 1893 | } else if (atype == TIPC_ADDR_ID) { |
| 1894 | addr->addr.id.node = v1; |
| 1895 | addr->addr.id.ref = v2; |
| 1896 | } else { |
| 1897 | /* Shouldn't happen */ |
| 1898 | PyErr_SetString(PyExc_TypeError, "Invalid address type"); |
| 1899 | return 0; |
| 1900 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1901 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1902 | *len_ret = sizeof(*addr); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1903 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1904 | return 1; |
| 1905 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1906 | #endif |
| 1907 | |
Vinay Sajip | ed6783f | 2014-03-21 11:44:32 +0000 | [diff] [blame] | 1908 | #if defined(AF_CAN) && defined(CAN_RAW) && defined(CAN_BCM) |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1909 | case AF_CAN: |
| 1910 | switch (s->sock_proto) { |
| 1911 | case CAN_RAW: |
Charles-François Natali | 773e42d | 2013-02-05 19:42:01 +0100 | [diff] [blame] | 1912 | /* fall-through */ |
| 1913 | case CAN_BCM: |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1914 | { |
| 1915 | struct sockaddr_can *addr; |
| 1916 | PyObject *interfaceName; |
| 1917 | struct ifreq ifr; |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1918 | Py_ssize_t len; |
| 1919 | |
Benjamin Peterson | 18b7191 | 2013-05-16 15:29:44 -0500 | [diff] [blame] | 1920 | addr = (struct sockaddr_can *)addr_ret; |
| 1921 | |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1922 | if (!PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter, |
| 1923 | &interfaceName)) |
| 1924 | return 0; |
| 1925 | |
| 1926 | len = PyBytes_GET_SIZE(interfaceName); |
| 1927 | |
| 1928 | if (len == 0) { |
| 1929 | ifr.ifr_ifindex = 0; |
Victor Stinner | 1a62a68 | 2014-08-17 19:33:28 +0200 | [diff] [blame] | 1930 | } else if ((size_t)len < sizeof(ifr.ifr_name)) { |
Christian Heimes | 15b6885 | 2012-09-10 01:25:50 +0200 | [diff] [blame] | 1931 | strncpy(ifr.ifr_name, PyBytes_AS_STRING(interfaceName), sizeof(ifr.ifr_name)); |
| 1932 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1933 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
| 1934 | s->errorhandler(); |
| 1935 | Py_DECREF(interfaceName); |
| 1936 | return 0; |
| 1937 | } |
| 1938 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1939 | PyErr_SetString(PyExc_OSError, |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1940 | "AF_CAN interface name too long"); |
| 1941 | Py_DECREF(interfaceName); |
| 1942 | return 0; |
| 1943 | } |
| 1944 | |
| 1945 | addr->can_family = AF_CAN; |
| 1946 | addr->can_ifindex = ifr.ifr_ifindex; |
| 1947 | |
| 1948 | *len_ret = sizeof(*addr); |
| 1949 | Py_DECREF(interfaceName); |
| 1950 | return 1; |
| 1951 | } |
| 1952 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1953 | PyErr_SetString(PyExc_OSError, |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1954 | "getsockaddrarg: unsupported CAN protocol"); |
| 1955 | return 0; |
| 1956 | } |
| 1957 | #endif |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1958 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1959 | #ifdef PF_SYSTEM |
| 1960 | case PF_SYSTEM: |
| 1961 | switch (s->sock_proto) { |
| 1962 | #ifdef SYSPROTO_CONTROL |
| 1963 | case SYSPROTO_CONTROL: |
| 1964 | { |
| 1965 | struct sockaddr_ctl *addr; |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1966 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1967 | addr = (struct sockaddr_ctl *)addr_ret; |
| 1968 | addr->sc_family = AF_SYSTEM; |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1969 | addr->ss_sysaddr = AF_SYS_CONTROL; |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1970 | |
| 1971 | if (PyUnicode_Check(args)) { |
| 1972 | struct ctl_info info; |
| 1973 | PyObject *ctl_name; |
| 1974 | |
| 1975 | if (!PyArg_Parse(args, "O&", |
| 1976 | PyUnicode_FSConverter, &ctl_name)) { |
| 1977 | return 0; |
| 1978 | } |
| 1979 | |
Victor Stinner | f50e187 | 2015-03-20 11:32:24 +0100 | [diff] [blame] | 1980 | if (PyBytes_GET_SIZE(ctl_name) > (Py_ssize_t)sizeof(info.ctl_name)) { |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1981 | PyErr_SetString(PyExc_ValueError, |
| 1982 | "provided string is too long"); |
| 1983 | Py_DECREF(ctl_name); |
| 1984 | return 0; |
| 1985 | } |
| 1986 | strncpy(info.ctl_name, PyBytes_AS_STRING(ctl_name), |
| 1987 | sizeof(info.ctl_name)); |
| 1988 | Py_DECREF(ctl_name); |
| 1989 | |
| 1990 | if (ioctl(s->sock_fd, CTLIOCGINFO, &info)) { |
| 1991 | PyErr_SetString(PyExc_OSError, |
| 1992 | "cannot find kernel control with provided name"); |
| 1993 | return 0; |
| 1994 | } |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1995 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1996 | addr->sc_id = info.ctl_id; |
| 1997 | addr->sc_unit = 0; |
| 1998 | } else if (!PyArg_ParseTuple(args, "II", |
| 1999 | &(addr->sc_id), &(addr->sc_unit))) { |
| 2000 | PyErr_SetString(PyExc_TypeError, "getsockaddrarg: " |
| 2001 | "expected str or tuple of two ints"); |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 2002 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 2003 | return 0; |
| 2004 | } |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 2005 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 2006 | *len_ret = sizeof(*addr); |
| 2007 | return 1; |
| 2008 | } |
| 2009 | #endif |
| 2010 | default: |
| 2011 | PyErr_SetString(PyExc_OSError, |
| 2012 | "getsockaddrarg: unsupported PF_SYSTEM protocol"); |
| 2013 | return 0; |
| 2014 | } |
| 2015 | #endif |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 2016 | #ifdef HAVE_SOCKADDR_ALG |
| 2017 | case AF_ALG: |
| 2018 | { |
| 2019 | struct sockaddr_alg *sa; |
| 2020 | char *type; |
| 2021 | char *name; |
| 2022 | sa = (struct sockaddr_alg *)addr_ret; |
| 2023 | |
| 2024 | memset(sa, 0, sizeof(*sa)); |
| 2025 | sa->salg_family = AF_ALG; |
| 2026 | |
| 2027 | if (!PyArg_ParseTuple(args, "ss|HH:getsockaddrarg", |
| 2028 | &type, &name, &sa->salg_feat, &sa->salg_mask)) |
| 2029 | return 0; |
| 2030 | /* sockaddr_alg has fixed-sized char arrays for type and name */ |
| 2031 | if (strlen(type) > sizeof(sa->salg_type)) { |
| 2032 | PyErr_SetString(PyExc_ValueError, "AF_ALG type too long."); |
| 2033 | return 0; |
| 2034 | } |
| 2035 | strncpy((char *)sa->salg_type, type, sizeof(sa->salg_type)); |
| 2036 | if (strlen(name) > sizeof(sa->salg_name)) { |
| 2037 | PyErr_SetString(PyExc_ValueError, "AF_ALG name too long."); |
| 2038 | return 0; |
| 2039 | } |
| 2040 | strncpy((char *)sa->salg_name, name, sizeof(sa->salg_name)); |
| 2041 | |
| 2042 | *len_ret = sizeof(*sa); |
| 2043 | return 1; |
| 2044 | } |
| 2045 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 2046 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2047 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2048 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2049 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2050 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: bad family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2051 | return 0; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2052 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2053 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2056 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2057 | /* 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] | 2058 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 2059 | through len_ret. */ |
| 2060 | |
| 2061 | static int |
Peter Schneider-Kamp | 618e25d | 2000-07-11 23:00:12 +0000 | [diff] [blame] | 2062 | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 2063 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2064 | switch (s->sock_family) { |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 2065 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 2066 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2067 | case AF_UNIX: |
| 2068 | { |
| 2069 | *len_ret = sizeof (struct sockaddr_un); |
| 2070 | return 1; |
| 2071 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2072 | #endif /* AF_UNIX */ |
Martin Panter | 6d57fe1 | 2016-09-17 03:26:16 +0000 | [diff] [blame] | 2073 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 2074 | #if defined(AF_NETLINK) |
Martin Panter | 6d57fe1 | 2016-09-17 03:26:16 +0000 | [diff] [blame] | 2075 | case AF_NETLINK: |
| 2076 | { |
| 2077 | *len_ret = sizeof (struct sockaddr_nl); |
| 2078 | return 1; |
| 2079 | } |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 2080 | #endif |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 2081 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 2082 | #ifdef AF_RDS |
| 2083 | case AF_RDS: |
| 2084 | /* RDS sockets use sockaddr_in: fall-through */ |
| 2085 | #endif |
| 2086 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2087 | case AF_INET: |
| 2088 | { |
| 2089 | *len_ret = sizeof (struct sockaddr_in); |
| 2090 | return 1; |
| 2091 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 2092 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 2093 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2094 | case AF_INET6: |
| 2095 | { |
| 2096 | *len_ret = sizeof (struct sockaddr_in6); |
| 2097 | return 1; |
| 2098 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2099 | #endif |
| 2100 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 2101 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2102 | case AF_BLUETOOTH: |
| 2103 | { |
| 2104 | switch(s->sock_proto) |
| 2105 | { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 2106 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2107 | case BTPROTO_L2CAP: |
| 2108 | *len_ret = sizeof (struct sockaddr_l2); |
| 2109 | return 1; |
| 2110 | case BTPROTO_RFCOMM: |
| 2111 | *len_ret = sizeof (struct sockaddr_rc); |
| 2112 | return 1; |
| 2113 | case BTPROTO_HCI: |
| 2114 | *len_ret = sizeof (struct sockaddr_hci); |
| 2115 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 2116 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2117 | case BTPROTO_SCO: |
| 2118 | *len_ret = sizeof (struct sockaddr_sco); |
| 2119 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 2120 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2121 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2122 | PyErr_SetString(PyExc_OSError, "getsockaddrlen: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2123 | "unknown BT protocol"); |
| 2124 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 2125 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2126 | } |
| 2127 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 2128 | #endif |
| 2129 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 2130 | #ifdef HAVE_NETPACKET_PACKET_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2131 | case AF_PACKET: |
| 2132 | { |
| 2133 | *len_ret = sizeof (struct sockaddr_ll); |
| 2134 | return 1; |
| 2135 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 2136 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2137 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 2138 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2139 | case AF_TIPC: |
| 2140 | { |
| 2141 | *len_ret = sizeof (struct sockaddr_tipc); |
| 2142 | return 1; |
| 2143 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 2144 | #endif |
| 2145 | |
Vinay Sajip | ecfc98c | 2014-03-20 12:42:42 +0000 | [diff] [blame] | 2146 | #ifdef AF_CAN |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 2147 | case AF_CAN: |
| 2148 | { |
| 2149 | *len_ret = sizeof (struct sockaddr_can); |
| 2150 | return 1; |
| 2151 | } |
| 2152 | #endif |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 2153 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 2154 | #ifdef PF_SYSTEM |
| 2155 | case PF_SYSTEM: |
| 2156 | switch(s->sock_proto) { |
| 2157 | #ifdef SYSPROTO_CONTROL |
| 2158 | case SYSPROTO_CONTROL: |
| 2159 | *len_ret = sizeof (struct sockaddr_ctl); |
| 2160 | return 1; |
| 2161 | #endif |
| 2162 | default: |
| 2163 | PyErr_SetString(PyExc_OSError, "getsockaddrlen: " |
| 2164 | "unknown PF_SYSTEM protocol"); |
| 2165 | return 0; |
| 2166 | } |
| 2167 | #endif |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 2168 | #ifdef HAVE_SOCKADDR_ALG |
| 2169 | case AF_ALG: |
| 2170 | { |
| 2171 | *len_ret = sizeof (struct sockaddr_alg); |
| 2172 | return 1; |
| 2173 | } |
| 2174 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 2175 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2176 | /* More cases here... */ |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 2177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2178 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2179 | PyErr_SetString(PyExc_OSError, "getsockaddrlen: bad family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2180 | return 0; |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 2181 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2182 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
| 2185 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2186 | /* Support functions for the sendmsg() and recvmsg[_into]() methods. |
| 2187 | Currently, these methods are only compiled if the RFC 2292/3542 |
| 2188 | CMSG_LEN() macro is available. Older systems seem to have used |
| 2189 | sizeof(struct cmsghdr) + (length) where CMSG_LEN() is used now, so |
| 2190 | it may be possible to define CMSG_LEN() that way if it's not |
| 2191 | provided. Some architectures might need extra padding after the |
| 2192 | cmsghdr, however, and CMSG_LEN() would have to take account of |
| 2193 | this. */ |
| 2194 | #ifdef CMSG_LEN |
| 2195 | /* If length is in range, set *result to CMSG_LEN(length) and return |
| 2196 | true; otherwise, return false. */ |
| 2197 | static int |
| 2198 | get_CMSG_LEN(size_t length, size_t *result) |
| 2199 | { |
| 2200 | size_t tmp; |
| 2201 | |
| 2202 | if (length > (SOCKLEN_T_LIMIT - CMSG_LEN(0))) |
| 2203 | return 0; |
| 2204 | tmp = CMSG_LEN(length); |
| 2205 | if (tmp > SOCKLEN_T_LIMIT || tmp < length) |
| 2206 | return 0; |
| 2207 | *result = tmp; |
| 2208 | return 1; |
| 2209 | } |
| 2210 | |
| 2211 | #ifdef CMSG_SPACE |
| 2212 | /* If length is in range, set *result to CMSG_SPACE(length) and return |
| 2213 | true; otherwise, return false. */ |
| 2214 | static int |
| 2215 | get_CMSG_SPACE(size_t length, size_t *result) |
| 2216 | { |
| 2217 | size_t tmp; |
| 2218 | |
| 2219 | /* Use CMSG_SPACE(1) here in order to take account of the padding |
| 2220 | necessary before *and* after the data. */ |
| 2221 | if (length > (SOCKLEN_T_LIMIT - CMSG_SPACE(1))) |
| 2222 | return 0; |
| 2223 | tmp = CMSG_SPACE(length); |
| 2224 | if (tmp > SOCKLEN_T_LIMIT || tmp < length) |
| 2225 | return 0; |
| 2226 | *result = tmp; |
| 2227 | return 1; |
| 2228 | } |
| 2229 | #endif |
| 2230 | |
| 2231 | /* Return true iff msg->msg_controllen is valid, cmsgh is a valid |
| 2232 | pointer in msg->msg_control with at least "space" bytes after it, |
| 2233 | and its cmsg_len member inside the buffer. */ |
| 2234 | static int |
| 2235 | cmsg_min_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t space) |
| 2236 | { |
| 2237 | size_t cmsg_offset; |
| 2238 | static const size_t cmsg_len_end = (offsetof(struct cmsghdr, cmsg_len) + |
| 2239 | sizeof(cmsgh->cmsg_len)); |
| 2240 | |
Charles-François Natali | 466517d | 2011-08-28 18:23:43 +0200 | [diff] [blame] | 2241 | /* Note that POSIX allows msg_controllen to be of signed type. */ |
Brett Cannon | b05cbe6 | 2014-01-07 17:01:01 -0500 | [diff] [blame] | 2242 | if (cmsgh == NULL || msg->msg_control == NULL) |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2243 | return 0; |
Brett Cannon | b05cbe6 | 2014-01-07 17:01:01 -0500 | [diff] [blame] | 2244 | /* Note that POSIX allows msg_controllen to be of a signed type. This is |
| 2245 | annoying under OS X as it's unsigned there and so it triggers a |
| 2246 | tautological comparison warning under Clang when compared against 0. |
| 2247 | Since the check is valid on other platforms, silence the warning under |
| 2248 | Clang. */ |
| 2249 | #ifdef __clang__ |
| 2250 | #pragma clang diagnostic push |
| 2251 | #pragma clang diagnostic ignored "-Wtautological-compare" |
| 2252 | #endif |
Serhiy Storchaka | caa01f8 | 2015-03-05 23:42:24 +0200 | [diff] [blame] | 2253 | #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))) |
Serhiy Storchaka | b48af34 | 2015-02-26 15:27:57 +0200 | [diff] [blame] | 2254 | #pragma GCC diagnostic push |
| 2255 | #pragma GCC diagnostic ignored "-Wtype-limits" |
| 2256 | #endif |
Brett Cannon | b05cbe6 | 2014-01-07 17:01:01 -0500 | [diff] [blame] | 2257 | if (msg->msg_controllen < 0) |
| 2258 | return 0; |
Serhiy Storchaka | caa01f8 | 2015-03-05 23:42:24 +0200 | [diff] [blame] | 2259 | #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))) |
Serhiy Storchaka | b48af34 | 2015-02-26 15:27:57 +0200 | [diff] [blame] | 2260 | #pragma GCC diagnostic pop |
| 2261 | #endif |
Brett Cannon | b05cbe6 | 2014-01-07 17:01:01 -0500 | [diff] [blame] | 2262 | #ifdef __clang__ |
| 2263 | #pragma clang diagnostic pop |
| 2264 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2265 | if (space < cmsg_len_end) |
| 2266 | space = cmsg_len_end; |
| 2267 | cmsg_offset = (char *)cmsgh - (char *)msg->msg_control; |
| 2268 | return (cmsg_offset <= (size_t)-1 - space && |
| 2269 | cmsg_offset + space <= msg->msg_controllen); |
| 2270 | } |
| 2271 | |
| 2272 | /* If pointer CMSG_DATA(cmsgh) is in buffer msg->msg_control, set |
| 2273 | *space to number of bytes following it in the buffer and return |
| 2274 | true; otherwise, return false. Assumes cmsgh, msg->msg_control and |
| 2275 | msg->msg_controllen are valid. */ |
| 2276 | static int |
| 2277 | get_cmsg_data_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *space) |
| 2278 | { |
| 2279 | size_t data_offset; |
| 2280 | char *data_ptr; |
| 2281 | |
| 2282 | if ((data_ptr = (char *)CMSG_DATA(cmsgh)) == NULL) |
| 2283 | return 0; |
| 2284 | data_offset = data_ptr - (char *)msg->msg_control; |
| 2285 | if (data_offset > msg->msg_controllen) |
| 2286 | return 0; |
| 2287 | *space = msg->msg_controllen - data_offset; |
| 2288 | return 1; |
| 2289 | } |
| 2290 | |
| 2291 | /* If cmsgh is invalid or not contained in the buffer pointed to by |
| 2292 | msg->msg_control, return -1. If cmsgh is valid and its associated |
| 2293 | data is entirely contained in the buffer, set *data_len to the |
| 2294 | length of the associated data and return 0. If only part of the |
| 2295 | associated data is contained in the buffer but cmsgh is otherwise |
| 2296 | valid, set *data_len to the length contained in the buffer and |
| 2297 | return 1. */ |
| 2298 | static int |
| 2299 | get_cmsg_data_len(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *data_len) |
| 2300 | { |
| 2301 | size_t space, cmsg_data_len; |
| 2302 | |
| 2303 | if (!cmsg_min_space(msg, cmsgh, CMSG_LEN(0)) || |
| 2304 | cmsgh->cmsg_len < CMSG_LEN(0)) |
| 2305 | return -1; |
| 2306 | cmsg_data_len = cmsgh->cmsg_len - CMSG_LEN(0); |
| 2307 | if (!get_cmsg_data_space(msg, cmsgh, &space)) |
| 2308 | return -1; |
| 2309 | if (space >= cmsg_data_len) { |
| 2310 | *data_len = cmsg_data_len; |
| 2311 | return 0; |
| 2312 | } |
| 2313 | *data_len = space; |
| 2314 | return 1; |
| 2315 | } |
| 2316 | #endif /* CMSG_LEN */ |
| 2317 | |
| 2318 | |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 2319 | struct sock_accept { |
| 2320 | socklen_t *addrlen; |
| 2321 | sock_addr_t *addrbuf; |
| 2322 | SOCKET_T result; |
| 2323 | }; |
| 2324 | |
| 2325 | #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) |
| 2326 | /* accept4() is available on Linux 2.6.28+ and glibc 2.10 */ |
| 2327 | static int accept4_works = -1; |
| 2328 | #endif |
| 2329 | |
| 2330 | static int |
| 2331 | sock_accept_impl(PySocketSockObject *s, void *data) |
| 2332 | { |
| 2333 | struct sock_accept *ctx = data; |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 2334 | struct sockaddr *addr = SAS2SA(ctx->addrbuf); |
| 2335 | socklen_t *paddrlen = ctx->addrlen; |
| 2336 | #ifdef HAVE_SOCKADDR_ALG |
| 2337 | /* AF_ALG does not support accept() with addr and raises |
| 2338 | * ECONNABORTED instead. */ |
| 2339 | if (s->sock_family == AF_ALG) { |
| 2340 | addr = NULL; |
| 2341 | paddrlen = NULL; |
| 2342 | *ctx->addrlen = 0; |
| 2343 | } |
| 2344 | #endif |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 2345 | |
| 2346 | #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) |
| 2347 | if (accept4_works != 0) { |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 2348 | ctx->result = accept4(s->sock_fd, addr, paddrlen, |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 2349 | SOCK_CLOEXEC); |
| 2350 | if (ctx->result == INVALID_SOCKET && accept4_works == -1) { |
| 2351 | /* On Linux older than 2.6.28, accept4() fails with ENOSYS */ |
| 2352 | accept4_works = (errno != ENOSYS); |
| 2353 | } |
| 2354 | } |
| 2355 | if (accept4_works == 0) |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 2356 | ctx->result = accept(s->sock_fd, addr, paddrlen); |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 2357 | #else |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 2358 | ctx->result = accept(s->sock_fd, addr, paddrlen); |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 2359 | #endif |
Victor Stinner | bea232a | 2015-07-27 23:37:11 +0200 | [diff] [blame] | 2360 | |
| 2361 | #ifdef MS_WINDOWS |
| 2362 | return (ctx->result != INVALID_SOCKET); |
| 2363 | #else |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 2364 | return (ctx->result >= 0); |
Victor Stinner | bea232a | 2015-07-27 23:37:11 +0200 | [diff] [blame] | 2365 | #endif |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 2366 | } |
| 2367 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2368 | /* s._accept() -> (fd, address) */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2369 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2370 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2371 | sock_accept(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2372 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2373 | sock_addr_t addrbuf; |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 2374 | SOCKET_T newfd; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2375 | socklen_t addrlen; |
| 2376 | PyObject *sock = NULL; |
| 2377 | PyObject *addr = NULL; |
| 2378 | PyObject *res = NULL; |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 2379 | struct sock_accept ctx; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2381 | if (!getsockaddrlen(s, &addrlen)) |
| 2382 | return NULL; |
| 2383 | memset(&addrbuf, 0, addrlen); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2384 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2385 | if (!IS_SELECTABLE(s)) |
| 2386 | return select_error(); |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2387 | |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 2388 | ctx.addrlen = &addrlen; |
| 2389 | ctx.addrbuf = &addrbuf; |
| 2390 | if (sock_call(s, 0, sock_accept_impl, &ctx) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2391 | return NULL; |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 2392 | newfd = ctx.result; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2393 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2394 | #ifdef MS_WINDOWS |
| 2395 | if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) { |
| 2396 | PyErr_SetFromWindowsErr(0); |
| 2397 | SOCKETCLOSE(newfd); |
| 2398 | goto finally; |
| 2399 | } |
| 2400 | #else |
| 2401 | |
| 2402 | #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) |
| 2403 | if (!accept4_works) |
| 2404 | #endif |
| 2405 | { |
| 2406 | if (_Py_set_inheritable(newfd, 0, NULL) < 0) { |
| 2407 | SOCKETCLOSE(newfd); |
| 2408 | goto finally; |
| 2409 | } |
| 2410 | } |
| 2411 | #endif |
| 2412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2413 | sock = PyLong_FromSocket_t(newfd); |
| 2414 | if (sock == NULL) { |
| 2415 | SOCKETCLOSE(newfd); |
| 2416 | goto finally; |
| 2417 | } |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2418 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2419 | addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 2420 | addrlen, s->sock_proto); |
| 2421 | if (addr == NULL) |
| 2422 | goto finally; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2424 | res = PyTuple_Pack(2, sock, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2425 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2426 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2427 | Py_XDECREF(sock); |
| 2428 | Py_XDECREF(addr); |
| 2429 | return res; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2432 | PyDoc_STRVAR(accept_doc, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2433 | "_accept() -> (integer, address info)\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2434 | \n\ |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2435 | Wait for an incoming connection. Return a new socket file descriptor\n\ |
| 2436 | representing the connection, and the address of the client.\n\ |
| 2437 | For IP sockets, the address info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2438 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2439 | /* s.setblocking(flag) method. Argument: |
| 2440 | False -- non-blocking mode; same as settimeout(0) |
| 2441 | True -- blocking mode; same as settimeout(None) |
| 2442 | */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2443 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2444 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2445 | sock_setblocking(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2446 | { |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 2447 | long block; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2448 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2449 | block = PyLong_AsLong(arg); |
| 2450 | if (block == -1 && PyErr_Occurred()) |
| 2451 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2452 | |
Victor Stinner | 9001d80 | 2015-04-06 23:06:01 +0200 | [diff] [blame] | 2453 | s->sock_timeout = _PyTime_FromSeconds(block ? -1 : 0); |
Christian Heimes | 0b9d64e | 2016-09-09 00:28:57 +0200 | [diff] [blame] | 2454 | if (internal_setblocking(s, block) == -1) { |
| 2455 | return NULL; |
| 2456 | } |
| 2457 | Py_RETURN_NONE; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2458 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2459 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2460 | PyDoc_STRVAR(setblocking_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2461 | "setblocking(flag)\n\ |
| 2462 | \n\ |
| 2463 | 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] | 2464 | setblocking(True) is equivalent to settimeout(None);\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2465 | setblocking(False) is equivalent to settimeout(0.0)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2466 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2467 | static int |
| 2468 | socket_parse_timeout(_PyTime_t *timeout, PyObject *timeout_obj) |
| 2469 | { |
| 2470 | #ifdef MS_WINDOWS |
| 2471 | struct timeval tv; |
| 2472 | #endif |
Victor Stinner | 1bb0aef | 2015-03-31 16:31:19 +0200 | [diff] [blame] | 2473 | #ifndef HAVE_POLL |
| 2474 | _PyTime_t ms; |
| 2475 | #endif |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2476 | int overflow = 0; |
| 2477 | |
| 2478 | if (timeout_obj == Py_None) { |
Victor Stinner | 10550cd | 2015-04-03 13:22:27 +0200 | [diff] [blame] | 2479 | *timeout = _PyTime_FromSeconds(-1); |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2480 | return 0; |
| 2481 | } |
| 2482 | |
Victor Stinner | 869e177 | 2015-03-30 03:49:14 +0200 | [diff] [blame] | 2483 | if (_PyTime_FromSecondsObject(timeout, |
| 2484 | timeout_obj, _PyTime_ROUND_CEILING) < 0) |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2485 | return -1; |
| 2486 | |
| 2487 | if (*timeout < 0) { |
| 2488 | PyErr_SetString(PyExc_ValueError, "Timeout value out of range"); |
| 2489 | return -1; |
| 2490 | } |
| 2491 | |
| 2492 | #ifdef MS_WINDOWS |
Victor Stinner | 1bb0aef | 2015-03-31 16:31:19 +0200 | [diff] [blame] | 2493 | overflow |= (_PyTime_AsTimeval(*timeout, &tv, _PyTime_ROUND_CEILING) < 0); |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2494 | #endif |
| 2495 | #ifndef HAVE_POLL |
Victor Stinner | 1bb0aef | 2015-03-31 16:31:19 +0200 | [diff] [blame] | 2496 | ms = _PyTime_AsMilliseconds(*timeout, _PyTime_ROUND_CEILING); |
| 2497 | overflow |= (ms > INT_MAX); |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2498 | #endif |
| 2499 | if (overflow) { |
| 2500 | PyErr_SetString(PyExc_OverflowError, |
| 2501 | "timeout doesn't fit into C timeval"); |
| 2502 | return -1; |
| 2503 | } |
| 2504 | |
| 2505 | return 0; |
| 2506 | } |
| 2507 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2508 | /* s.settimeout(timeout) method. Argument: |
| 2509 | None -- no timeout, blocking mode; same as setblocking(True) |
| 2510 | 0.0 -- non-blocking mode; same as setblocking(False) |
| 2511 | > 0 -- timeout mode; operations time out after timeout seconds |
| 2512 | < 0 -- illegal; raises an exception |
| 2513 | */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2514 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2515 | sock_settimeout(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2516 | { |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2517 | _PyTime_t timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2518 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2519 | if (socket_parse_timeout(&timeout, arg) < 0) |
| 2520 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2521 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2522 | s->sock_timeout = timeout; |
Christian Heimes | 0b9d64e | 2016-09-09 00:28:57 +0200 | [diff] [blame] | 2523 | if (internal_setblocking(s, timeout < 0) == -1) { |
| 2524 | return NULL; |
| 2525 | } |
| 2526 | Py_RETURN_NONE; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2529 | PyDoc_STRVAR(settimeout_doc, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2530 | "settimeout(timeout)\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2531 | \n\ |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2532 | Set a timeout on socket operations. 'timeout' can be a float,\n\ |
| 2533 | giving in seconds, or None. Setting a timeout of None disables\n\ |
| 2534 | the timeout feature and is equivalent to setblocking(1).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2535 | Setting a timeout of zero is the same as setblocking(0)."); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2536 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2537 | /* s.gettimeout() method. |
| 2538 | Returns the timeout associated with a socket. */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2539 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2540 | sock_gettimeout(PySocketSockObject *s) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2541 | { |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2542 | if (s->sock_timeout < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2543 | Py_INCREF(Py_None); |
| 2544 | return Py_None; |
| 2545 | } |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2546 | else { |
| 2547 | double seconds = _PyTime_AsSecondsDouble(s->sock_timeout); |
| 2548 | return PyFloat_FromDouble(seconds); |
| 2549 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2552 | PyDoc_STRVAR(gettimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2553 | "gettimeout() -> timeout\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2554 | \n\ |
Ezio Melotti | 388c945 | 2011-08-14 08:28:57 +0300 | [diff] [blame] | 2555 | Returns the timeout in seconds (float) associated with socket \n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2556 | 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] | 2557 | operations are disabled."); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2558 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 2559 | /* s.setsockopt() method. |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 2560 | With an integer third argument, sets an integer optval with optlen=4. |
| 2561 | With None as third argument and an integer fourth argument, set |
| 2562 | optval=NULL with unsigned int as optlen. |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 2563 | With a string third argument, sets an option from a buffer; |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 2564 | use optional built-in module 'struct' to encode the string. |
| 2565 | */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2566 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2567 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2568 | sock_setsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2569 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2570 | int level; |
| 2571 | int optname; |
| 2572 | int res; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 2573 | Py_buffer optval; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2574 | int flag; |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 2575 | unsigned int optlen; |
| 2576 | PyObject *none; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2577 | |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 2578 | /* setsockopt(level, opt, flag) */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2579 | if (PyArg_ParseTuple(args, "iii:setsockopt", |
| 2580 | &level, &optname, &flag)) { |
Victor Stinner | b6c15bc | 2015-03-31 16:35:35 +0200 | [diff] [blame] | 2581 | res = setsockopt(s->sock_fd, level, optname, |
| 2582 | (char*)&flag, sizeof flag); |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 2583 | goto done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2584 | } |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 2585 | |
| 2586 | PyErr_Clear(); |
Christian Heimes | 8c21ab0 | 2016-09-06 00:07:02 +0200 | [diff] [blame] | 2587 | /* setsockopt(level, opt, None, flag) */ |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 2588 | if (PyArg_ParseTuple(args, "iiO!I:setsockopt", |
| 2589 | &level, &optname, Py_TYPE(Py_None), &none, &optlen)) { |
| 2590 | assert(sizeof(socklen_t) >= sizeof(unsigned int)); |
Victor Stinner | cc73932 | 2016-03-23 21:35:29 +0100 | [diff] [blame] | 2591 | res = setsockopt(s->sock_fd, level, optname, |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 2592 | NULL, (socklen_t)optlen); |
| 2593 | goto done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2594 | } |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 2595 | |
| 2596 | PyErr_Clear(); |
| 2597 | /* setsockopt(level, opt, buffer) */ |
| 2598 | if (!PyArg_ParseTuple(args, "iiy*:setsockopt", |
| 2599 | &level, &optname, &optval)) |
| 2600 | return NULL; |
| 2601 | |
| 2602 | #ifdef MS_WINDOWS |
| 2603 | if (optval.len > INT_MAX) { |
| 2604 | PyBuffer_Release(&optval); |
| 2605 | PyErr_Format(PyExc_OverflowError, |
| 2606 | "socket option is larger than %i bytes", |
| 2607 | INT_MAX); |
| 2608 | return NULL; |
| 2609 | } |
| 2610 | res = setsockopt(s->sock_fd, level, optname, |
| 2611 | optval.buf, (int)optval.len); |
| 2612 | #else |
| 2613 | res = setsockopt(s->sock_fd, level, optname, optval.buf, optval.len); |
| 2614 | #endif |
| 2615 | PyBuffer_Release(&optval); |
| 2616 | |
| 2617 | done: |
Victor Stinner | cc73932 | 2016-03-23 21:35:29 +0100 | [diff] [blame] | 2618 | if (res < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2619 | return s->errorhandler(); |
Victor Stinner | cc73932 | 2016-03-23 21:35:29 +0100 | [diff] [blame] | 2620 | } |
| 2621 | |
| 2622 | Py_RETURN_NONE; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2623 | } |
| 2624 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2625 | PyDoc_STRVAR(setsockopt_doc, |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 2626 | "setsockopt(level, option, value: int)\n\ |
| 2627 | setsockopt(level, option, value: buffer)\n\ |
| 2628 | setsockopt(level, option, None, optlen: int)\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2629 | \n\ |
| 2630 | Set a socket option. See the Unix manual for level and option.\n\ |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 2631 | The value argument can either be an integer, a string buffer, or \n\ |
| 2632 | None, optlen."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2633 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2634 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 2635 | /* s.getsockopt() method. |
| 2636 | With two arguments, retrieves an integer option. |
| 2637 | With a third integer argument, retrieves a string buffer of that size; |
| 2638 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2639 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2640 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2641 | sock_getsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2642 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2643 | int level; |
| 2644 | int optname; |
| 2645 | int res; |
| 2646 | PyObject *buf; |
| 2647 | socklen_t buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2649 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt", |
| 2650 | &level, &optname, &buflen)) |
| 2651 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2653 | if (buflen == 0) { |
| 2654 | int flag = 0; |
| 2655 | socklen_t flagsize = sizeof flag; |
| 2656 | res = getsockopt(s->sock_fd, level, optname, |
| 2657 | (void *)&flag, &flagsize); |
| 2658 | if (res < 0) |
| 2659 | return s->errorhandler(); |
| 2660 | return PyLong_FromLong(flag); |
| 2661 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2662 | if (buflen <= 0 || buflen > 1024) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2663 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2664 | "getsockopt buflen out of range"); |
| 2665 | return NULL; |
| 2666 | } |
| 2667 | buf = PyBytes_FromStringAndSize((char *)NULL, buflen); |
| 2668 | if (buf == NULL) |
| 2669 | return NULL; |
| 2670 | res = getsockopt(s->sock_fd, level, optname, |
| 2671 | (void *)PyBytes_AS_STRING(buf), &buflen); |
| 2672 | if (res < 0) { |
| 2673 | Py_DECREF(buf); |
| 2674 | return s->errorhandler(); |
| 2675 | } |
| 2676 | _PyBytes_Resize(&buf, buflen); |
| 2677 | return buf; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2678 | } |
| 2679 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2680 | PyDoc_STRVAR(getsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2681 | "getsockopt(level, option[, buffersize]) -> value\n\ |
| 2682 | \n\ |
| 2683 | Get a socket option. See the Unix manual for level and option.\n\ |
| 2684 | 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] | 2685 | string of that length; otherwise it is an integer."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2686 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2687 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2688 | /* s.bind(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2689 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2690 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2691 | sock_bind(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2692 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2693 | sock_addr_t addrbuf; |
| 2694 | int addrlen; |
| 2695 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2697 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2698 | return NULL; |
| 2699 | Py_BEGIN_ALLOW_THREADS |
| 2700 | res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen); |
| 2701 | Py_END_ALLOW_THREADS |
| 2702 | if (res < 0) |
| 2703 | return s->errorhandler(); |
| 2704 | Py_INCREF(Py_None); |
| 2705 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2706 | } |
| 2707 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2708 | PyDoc_STRVAR(bind_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2709 | "bind(address)\n\ |
| 2710 | \n\ |
| 2711 | 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] | 2712 | 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] | 2713 | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2714 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2715 | |
| 2716 | /* s.close() method. |
| 2717 | Set the file descriptor to -1 so operations tried subsequently |
| 2718 | will surely fail. */ |
| 2719 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2720 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2721 | sock_close(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2722 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2723 | SOCKET_T fd; |
Martin Panter | 50ab1a3 | 2016-04-11 00:38:12 +0000 | [diff] [blame] | 2724 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2725 | |
Victor Stinner | 19a8e84 | 2016-03-21 16:36:48 +0100 | [diff] [blame] | 2726 | fd = s->sock_fd; |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 2727 | if (fd != INVALID_SOCKET) { |
| 2728 | s->sock_fd = INVALID_SOCKET; |
Victor Stinner | 19a8e84 | 2016-03-21 16:36:48 +0100 | [diff] [blame] | 2729 | |
| 2730 | /* We do not want to retry upon EINTR: see |
| 2731 | http://lwn.net/Articles/576478/ and |
| 2732 | http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 2733 | for more details. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2734 | Py_BEGIN_ALLOW_THREADS |
Martin Panter | 50ab1a3 | 2016-04-11 00:38:12 +0000 | [diff] [blame] | 2735 | res = SOCKETCLOSE(fd); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2736 | Py_END_ALLOW_THREADS |
Martin Panter | 50ab1a3 | 2016-04-11 00:38:12 +0000 | [diff] [blame] | 2737 | if (res < 0) { |
| 2738 | return s->errorhandler(); |
| 2739 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2740 | } |
| 2741 | Py_INCREF(Py_None); |
| 2742 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2743 | } |
| 2744 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2745 | PyDoc_STRVAR(close_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2746 | "close()\n\ |
| 2747 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2748 | Close the socket. It cannot be used after this call."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2749 | |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2750 | static PyObject * |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2751 | sock_detach(PySocketSockObject *s) |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2752 | { |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2753 | SOCKET_T fd = s->sock_fd; |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 2754 | s->sock_fd = INVALID_SOCKET; |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2755 | return PyLong_FromSocket_t(fd); |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2758 | PyDoc_STRVAR(detach_doc, |
| 2759 | "detach()\n\ |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2760 | \n\ |
Guido van Rossum | 638aebd | 2013-12-04 11:50:09 -0800 | [diff] [blame] | 2761 | Close the socket object without closing the underlying file descriptor.\n\ |
| 2762 | The object cannot be used after this call, but the file descriptor\n\ |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2763 | can be reused for other purposes. The file descriptor is returned."); |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2764 | |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2765 | static int |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 2766 | sock_connect_impl(PySocketSockObject *s, void* Py_UNUSED(data)) |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2767 | { |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 2768 | int err; |
| 2769 | socklen_t size = sizeof err; |
Victor Stinner | b6c15bc | 2015-03-31 16:35:35 +0200 | [diff] [blame] | 2770 | |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 2771 | if (getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR, (void *)&err, &size)) { |
| 2772 | /* getsockopt() failed */ |
| 2773 | return 0; |
| 2774 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2775 | |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 2776 | if (err == EISCONN) |
| 2777 | return 1; |
Victor Stinner | 38aec75 | 2015-04-02 14:37:20 +0200 | [diff] [blame] | 2778 | if (err != 0) { |
| 2779 | /* sock_call_ex() uses GET_SOCK_ERROR() to get the error code */ |
| 2780 | SET_SOCK_ERROR(err); |
Victor Stinner | 38aec75 | 2015-04-02 14:37:20 +0200 | [diff] [blame] | 2781 | return 0; |
| 2782 | } |
| 2783 | return 1; |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 2784 | } |
| 2785 | |
| 2786 | static int |
| 2787 | internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, |
| 2788 | int raise) |
| 2789 | { |
| 2790 | int res, err, wait_connect; |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2791 | |
| 2792 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2793 | res = connect(s->sock_fd, addr, addrlen); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2794 | Py_END_ALLOW_THREADS |
| 2795 | |
Victor Stinner | 70a46f6 | 2015-03-31 22:03:59 +0200 | [diff] [blame] | 2796 | if (!res) { |
| 2797 | /* connect() succeeded, the socket is connected */ |
| 2798 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2799 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2800 | |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 2801 | /* connect() failed */ |
Victor Stinner | ee699e9 | 2015-03-31 21:28:42 +0200 | [diff] [blame] | 2802 | |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 2803 | /* save error, PyErr_CheckSignals() can replace it */ |
| 2804 | err = GET_SOCK_ERROR; |
| 2805 | if (CHECK_ERRNO(EINTR)) { |
| 2806 | if (PyErr_CheckSignals()) |
Victor Stinner | 70a46f6 | 2015-03-31 22:03:59 +0200 | [diff] [blame] | 2807 | return -1; |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 2808 | |
| 2809 | /* Issue #23618: when connect() fails with EINTR, the connection is |
| 2810 | running asynchronously. |
| 2811 | |
| 2812 | If the socket is blocking or has a timeout, wait until the |
| 2813 | connection completes, fails or timed out using select(), and then |
| 2814 | get the connection status using getsockopt(SO_ERROR). |
| 2815 | |
| 2816 | If the socket is non-blocking, raise InterruptedError. The caller is |
| 2817 | responsible to wait until the connection completes, fails or timed |
| 2818 | out (it's the case in asyncio for example). */ |
| 2819 | wait_connect = (s->sock_timeout != 0 && IS_SELECTABLE(s)); |
| 2820 | } |
| 2821 | else { |
| 2822 | wait_connect = (s->sock_timeout > 0 && err == SOCK_INPROGRESS_ERR |
| 2823 | && IS_SELECTABLE(s)); |
Victor Stinner | 70a46f6 | 2015-03-31 22:03:59 +0200 | [diff] [blame] | 2824 | } |
| 2825 | |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 2826 | if (!wait_connect) { |
| 2827 | if (raise) { |
| 2828 | /* restore error, maybe replaced by PyErr_CheckSignals() */ |
| 2829 | SET_SOCK_ERROR(err); |
| 2830 | s->errorhandler(); |
| 2831 | return -1; |
| 2832 | } |
| 2833 | else |
| 2834 | return err; |
Victor Stinner | 70a46f6 | 2015-03-31 22:03:59 +0200 | [diff] [blame] | 2835 | } |
| 2836 | |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 2837 | if (raise) { |
| 2838 | /* socket.connect() raises an exception on error */ |
Victor Stinner | 8912d14 | 2015-04-06 23:16:34 +0200 | [diff] [blame] | 2839 | if (sock_call_ex(s, 1, sock_connect_impl, NULL, |
| 2840 | 1, NULL, s->sock_timeout) < 0) |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 2841 | return -1; |
Victor Stinner | 70a46f6 | 2015-03-31 22:03:59 +0200 | [diff] [blame] | 2842 | } |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 2843 | else { |
| 2844 | /* socket.connect_ex() returns the error code on error */ |
Victor Stinner | 8912d14 | 2015-04-06 23:16:34 +0200 | [diff] [blame] | 2845 | if (sock_call_ex(s, 1, sock_connect_impl, NULL, |
| 2846 | 1, &err, s->sock_timeout) < 0) |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 2847 | return err; |
| 2848 | } |
| 2849 | return 0; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2850 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2851 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2852 | /* s.connect(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2853 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2854 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2855 | sock_connect(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2856 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2857 | sock_addr_t addrbuf; |
| 2858 | int addrlen; |
| 2859 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2860 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2861 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2862 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2863 | |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 2864 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, 1); |
Victor Stinner | ee699e9 | 2015-03-31 21:28:42 +0200 | [diff] [blame] | 2865 | if (res < 0) |
| 2866 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2867 | |
Victor Stinner | ee699e9 | 2015-03-31 21:28:42 +0200 | [diff] [blame] | 2868 | Py_RETURN_NONE; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2869 | } |
| 2870 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2871 | PyDoc_STRVAR(connect_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2872 | "connect(address)\n\ |
| 2873 | \n\ |
| 2874 | 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] | 2875 | is a pair (host, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2876 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2877 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2878 | /* s.connect_ex(sockaddr) method */ |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2879 | |
| 2880 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2881 | sock_connect_ex(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2882 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2883 | sock_addr_t addrbuf; |
| 2884 | int addrlen; |
| 2885 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2886 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2887 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2888 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2889 | |
Victor Stinner | 81c41db | 2015-04-02 11:50:57 +0200 | [diff] [blame] | 2890 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, 0); |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2891 | if (res < 0) |
| 2892 | return NULL; |
| 2893 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2894 | return PyLong_FromLong((long) res); |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2897 | PyDoc_STRVAR(connect_ex_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2898 | "connect_ex(address) -> errno\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2899 | \n\ |
| 2900 | 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] | 2901 | instead of raising an exception when an error occurs."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2902 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2903 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2904 | /* s.fileno() method */ |
| 2905 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2906 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2907 | sock_fileno(PySocketSockObject *s) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2908 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2909 | return PyLong_FromSocket_t(s->sock_fd); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2910 | } |
| 2911 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2912 | PyDoc_STRVAR(fileno_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2913 | "fileno() -> integer\n\ |
| 2914 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2915 | Return the integer file descriptor of the socket."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2916 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2917 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2918 | /* s.getsockname() method */ |
| 2919 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2920 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2921 | sock_getsockname(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2922 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2923 | sock_addr_t addrbuf; |
| 2924 | int res; |
| 2925 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2926 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2927 | if (!getsockaddrlen(s, &addrlen)) |
| 2928 | return NULL; |
| 2929 | memset(&addrbuf, 0, addrlen); |
| 2930 | Py_BEGIN_ALLOW_THREADS |
| 2931 | res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2932 | Py_END_ALLOW_THREADS |
| 2933 | if (res < 0) |
| 2934 | return s->errorhandler(); |
| 2935 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2936 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2939 | PyDoc_STRVAR(getsockname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2940 | "getsockname() -> address info\n\ |
| 2941 | \n\ |
| 2942 | 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] | 2943 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2944 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2945 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2946 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2947 | /* s.getpeername() method */ |
| 2948 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2949 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2950 | sock_getpeername(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2951 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2952 | sock_addr_t addrbuf; |
| 2953 | int res; |
| 2954 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2956 | if (!getsockaddrlen(s, &addrlen)) |
| 2957 | return NULL; |
| 2958 | memset(&addrbuf, 0, addrlen); |
| 2959 | Py_BEGIN_ALLOW_THREADS |
| 2960 | res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2961 | Py_END_ALLOW_THREADS |
| 2962 | if (res < 0) |
| 2963 | return s->errorhandler(); |
| 2964 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2965 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2966 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2967 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2968 | PyDoc_STRVAR(getpeername_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2969 | "getpeername() -> address info\n\ |
| 2970 | \n\ |
| 2971 | 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] | 2972 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2973 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2974 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2975 | |
| 2976 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2977 | /* s.listen(n) method */ |
| 2978 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2979 | static PyObject * |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2980 | sock_listen(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2981 | { |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2982 | /* We try to choose a default backlog high enough to avoid connection drops |
| 2983 | * for common workloads, yet not too high to limit resource usage. */ |
| 2984 | int backlog = Py_MIN(SOMAXCONN, 128); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2985 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2986 | |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2987 | if (!PyArg_ParseTuple(args, "|i:listen", &backlog)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2988 | return NULL; |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2989 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2990 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 1be815a | 2011-05-10 19:16:29 +0200 | [diff] [blame] | 2991 | /* To avoid problems on systems that don't allow a negative backlog |
| 2992 | * (which doesn't make sense anyway) we force a minimum value of 0. */ |
| 2993 | if (backlog < 0) |
| 2994 | backlog = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2995 | res = listen(s->sock_fd, backlog); |
| 2996 | Py_END_ALLOW_THREADS |
| 2997 | if (res < 0) |
| 2998 | return s->errorhandler(); |
| 2999 | Py_INCREF(Py_None); |
| 3000 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3001 | } |
| 3002 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3003 | PyDoc_STRVAR(listen_doc, |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 3004 | "listen([backlog])\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3005 | \n\ |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 3006 | Enable a server to accept connections. If backlog is specified, it must be\n\ |
| 3007 | at least 0 (if it is lower, it is set to 0); it specifies the number of\n\ |
Antoine Pitrou | 1be815a | 2011-05-10 19:16:29 +0200 | [diff] [blame] | 3008 | unaccepted connections that the system will allow before refusing new\n\ |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 3009 | connections. If not specified, a default reasonable value is chosen."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3010 | |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3011 | struct sock_recv { |
| 3012 | char *cbuf; |
| 3013 | Py_ssize_t len; |
| 3014 | int flags; |
| 3015 | Py_ssize_t result; |
| 3016 | }; |
| 3017 | |
| 3018 | static int |
| 3019 | sock_recv_impl(PySocketSockObject *s, void *data) |
| 3020 | { |
| 3021 | struct sock_recv *ctx = data; |
| 3022 | |
| 3023 | #ifdef MS_WINDOWS |
| 3024 | if (ctx->len > INT_MAX) |
| 3025 | ctx->len = INT_MAX; |
| 3026 | ctx->result = recv(s->sock_fd, ctx->cbuf, (int)ctx->len, ctx->flags); |
| 3027 | #else |
| 3028 | ctx->result = recv(s->sock_fd, ctx->cbuf, ctx->len, ctx->flags); |
| 3029 | #endif |
| 3030 | return (ctx->result >= 0); |
| 3031 | } |
| 3032 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3033 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3034 | /* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3035 | * This is the guts of the recv() and recv_into() methods, which reads into a |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 3036 | * char buffer. If you have any inc/dec ref to do to the objects that contain |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3037 | * the buffer, do it in the caller. This function returns the number of bytes |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 3038 | * successfully read. If there was an error, it returns -1. Note that it is |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3039 | * also possible that we return a number of bytes smaller than the request |
| 3040 | * bytes. |
| 3041 | */ |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3042 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3043 | static Py_ssize_t |
| 3044 | sock_recv_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3045 | { |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3046 | struct sock_recv ctx; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3048 | if (!IS_SELECTABLE(s)) { |
| 3049 | select_error(); |
| 3050 | return -1; |
| 3051 | } |
| 3052 | if (len == 0) { |
| 3053 | /* If 0 bytes were requested, do nothing. */ |
| 3054 | return 0; |
| 3055 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3056 | |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3057 | ctx.cbuf = cbuf; |
| 3058 | ctx.len = len; |
| 3059 | ctx.flags = flags; |
| 3060 | if (sock_call(s, 0, sock_recv_impl, &ctx) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3061 | return -1; |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3062 | |
| 3063 | return ctx.result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3064 | } |
| 3065 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3066 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 3067 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3068 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3069 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3070 | sock_recv(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3071 | { |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3072 | Py_ssize_t recvlen, outlen; |
| 3073 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3074 | PyObject *buf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3075 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3076 | if (!PyArg_ParseTuple(args, "n|i:recv", &recvlen, &flags)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3077 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3078 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3079 | if (recvlen < 0) { |
| 3080 | PyErr_SetString(PyExc_ValueError, |
| 3081 | "negative buffersize in recv"); |
| 3082 | return NULL; |
| 3083 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3084 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3085 | /* Allocate a new string. */ |
| 3086 | buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
| 3087 | if (buf == NULL) |
| 3088 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3089 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3090 | /* Call the guts */ |
| 3091 | outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags); |
| 3092 | if (outlen < 0) { |
| 3093 | /* An error occurred, release the string and return an |
| 3094 | error. */ |
| 3095 | Py_DECREF(buf); |
| 3096 | return NULL; |
| 3097 | } |
| 3098 | if (outlen != recvlen) { |
| 3099 | /* We did not read as many bytes as we anticipated, resize the |
| 3100 | string if possible and be successful. */ |
| 3101 | _PyBytes_Resize(&buf, outlen); |
| 3102 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 3103 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3104 | return buf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3105 | } |
| 3106 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3107 | PyDoc_STRVAR(recv_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3108 | "recv(buffersize[, flags]) -> data\n\ |
| 3109 | \n\ |
| 3110 | Receive up to buffersize bytes from the socket. For the optional flags\n\ |
| 3111 | argument, see the Unix manual. When no data is available, block until\n\ |
| 3112 | 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] | 3113 | 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] | 3114 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3115 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3116 | /* s.recv_into(buffer, [nbytes [,flags]]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3117 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3118 | static PyObject* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3119 | sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3120 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3121 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3122 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3123 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3124 | Py_buffer pbuf; |
| 3125 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3126 | Py_ssize_t buflen, readlen, recvlen = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3128 | /* Get the buffer's memory */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3129 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recv_into", kwlist, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3130 | &pbuf, &recvlen, &flags)) |
| 3131 | return NULL; |
| 3132 | buf = pbuf.buf; |
| 3133 | buflen = pbuf.len; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3134 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3135 | if (recvlen < 0) { |
| 3136 | PyBuffer_Release(&pbuf); |
| 3137 | PyErr_SetString(PyExc_ValueError, |
| 3138 | "negative buffersize in recv_into"); |
| 3139 | return NULL; |
| 3140 | } |
| 3141 | if (recvlen == 0) { |
| 3142 | /* If nbytes was not specified, use the buffer's length */ |
| 3143 | recvlen = buflen; |
| 3144 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3145 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3146 | /* Check if the buffer is large enough */ |
| 3147 | if (buflen < recvlen) { |
| 3148 | PyBuffer_Release(&pbuf); |
| 3149 | PyErr_SetString(PyExc_ValueError, |
| 3150 | "buffer too small for requested bytes"); |
| 3151 | return NULL; |
| 3152 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3153 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3154 | /* Call the guts */ |
| 3155 | readlen = sock_recv_guts(s, buf, recvlen, flags); |
| 3156 | if (readlen < 0) { |
| 3157 | /* Return an error. */ |
| 3158 | PyBuffer_Release(&pbuf); |
| 3159 | return NULL; |
| 3160 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3162 | PyBuffer_Release(&pbuf); |
| 3163 | /* Return the number of bytes read. Note that we do not do anything |
| 3164 | special here in the case that readlen < recvlen. */ |
| 3165 | return PyLong_FromSsize_t(readlen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3166 | } |
| 3167 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3168 | PyDoc_STRVAR(recv_into_doc, |
| 3169 | "recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3170 | \n\ |
| 3171 | A version of recv() that stores its data into a buffer rather than creating \n\ |
| 3172 | a new string. Receive up to buffersize bytes from the socket. If buffersize \n\ |
| 3173 | is not specified (or 0), receive up to the size available in the given buffer.\n\ |
| 3174 | \n\ |
| 3175 | See recv() for documentation about the flags."); |
| 3176 | |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3177 | struct sock_recvfrom { |
| 3178 | char* cbuf; |
| 3179 | Py_ssize_t len; |
| 3180 | int flags; |
| 3181 | socklen_t *addrlen; |
| 3182 | sock_addr_t *addrbuf; |
| 3183 | Py_ssize_t result; |
| 3184 | }; |
| 3185 | |
| 3186 | static int |
| 3187 | sock_recvfrom_impl(PySocketSockObject *s, void *data) |
| 3188 | { |
| 3189 | struct sock_recvfrom *ctx = data; |
| 3190 | |
| 3191 | memset(ctx->addrbuf, 0, *ctx->addrlen); |
| 3192 | |
| 3193 | #ifdef MS_WINDOWS |
| 3194 | if (ctx->len > INT_MAX) |
| 3195 | ctx->len = INT_MAX; |
| 3196 | ctx->result = recvfrom(s->sock_fd, ctx->cbuf, (int)ctx->len, ctx->flags, |
| 3197 | SAS2SA(ctx->addrbuf), ctx->addrlen); |
| 3198 | #else |
| 3199 | ctx->result = recvfrom(s->sock_fd, ctx->cbuf, ctx->len, ctx->flags, |
| 3200 | SAS2SA(ctx->addrbuf), ctx->addrlen); |
| 3201 | #endif |
| 3202 | return (ctx->result >= 0); |
| 3203 | } |
| 3204 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3205 | |
| 3206 | /* |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 3207 | * This is the guts of the recvfrom() and recvfrom_into() methods, which reads |
| 3208 | * into a char buffer. If you have any inc/def ref to do to the objects that |
| 3209 | * contain the buffer, do it in the caller. This function returns the number |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 3210 | * of bytes successfully read. If there was an error, it returns -1. Note |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 3211 | * that it is also possible that we return a number of bytes smaller than the |
| 3212 | * request bytes. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3213 | * |
| 3214 | * 'addr' is a return value for the address object. Note that you must decref |
| 3215 | * it yourself. |
| 3216 | */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3217 | static Py_ssize_t |
| 3218 | sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3219 | PyObject** addr) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3220 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3221 | sock_addr_t addrbuf; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3222 | socklen_t addrlen; |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3223 | struct sock_recvfrom ctx; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3224 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3225 | *addr = NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3227 | if (!getsockaddrlen(s, &addrlen)) |
| 3228 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3230 | if (!IS_SELECTABLE(s)) { |
| 3231 | select_error(); |
| 3232 | return -1; |
| 3233 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 3234 | |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3235 | ctx.cbuf = cbuf; |
| 3236 | ctx.len = len; |
| 3237 | ctx.flags = flags; |
| 3238 | ctx.addrbuf = &addrbuf; |
| 3239 | ctx.addrlen = &addrlen; |
| 3240 | if (sock_call(s, 0, sock_recvfrom_impl, &ctx) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3241 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3242 | |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3243 | *addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 3244 | s->sock_proto); |
| 3245 | if (*addr == NULL) |
| 3246 | return -1; |
| 3247 | |
| 3248 | return ctx.result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3249 | } |
| 3250 | |
| 3251 | /* s.recvfrom(nbytes [,flags]) method */ |
| 3252 | |
| 3253 | static PyObject * |
| 3254 | sock_recvfrom(PySocketSockObject *s, PyObject *args) |
| 3255 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3256 | PyObject *buf = NULL; |
| 3257 | PyObject *addr = NULL; |
| 3258 | PyObject *ret = NULL; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3259 | int flags = 0; |
| 3260 | Py_ssize_t recvlen, outlen; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3261 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3262 | if (!PyArg_ParseTuple(args, "n|i:recvfrom", &recvlen, &flags)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3263 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3264 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3265 | if (recvlen < 0) { |
| 3266 | PyErr_SetString(PyExc_ValueError, |
| 3267 | "negative buffersize in recvfrom"); |
| 3268 | return NULL; |
| 3269 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3271 | buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
| 3272 | if (buf == NULL) |
| 3273 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3275 | outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf), |
| 3276 | recvlen, flags, &addr); |
| 3277 | if (outlen < 0) { |
| 3278 | goto finally; |
| 3279 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3281 | if (outlen != recvlen) { |
| 3282 | /* We did not read as many bytes as we anticipated, resize the |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 3283 | string if possible and be successful. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3284 | if (_PyBytes_Resize(&buf, outlen) < 0) |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 3285 | /* Oopsy, not so successful after all. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3286 | goto finally; |
| 3287 | } |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 3288 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3289 | ret = PyTuple_Pack(2, buf, addr); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3290 | |
| 3291 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3292 | Py_XDECREF(buf); |
| 3293 | Py_XDECREF(addr); |
| 3294 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3295 | } |
| 3296 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3297 | PyDoc_STRVAR(recvfrom_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3298 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
| 3299 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3300 | 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] | 3301 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3302 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3303 | /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3304 | |
| 3305 | static PyObject * |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3306 | sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3307 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3308 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3309 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3310 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3311 | Py_buffer pbuf; |
| 3312 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3313 | Py_ssize_t readlen, buflen, recvlen = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3315 | PyObject *addr = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3316 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3317 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recvfrom_into", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3318 | kwlist, &pbuf, |
| 3319 | &recvlen, &flags)) |
| 3320 | return NULL; |
| 3321 | buf = pbuf.buf; |
| 3322 | buflen = pbuf.len; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3323 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3324 | if (recvlen < 0) { |
| 3325 | PyBuffer_Release(&pbuf); |
| 3326 | PyErr_SetString(PyExc_ValueError, |
| 3327 | "negative buffersize in recvfrom_into"); |
| 3328 | return NULL; |
| 3329 | } |
| 3330 | if (recvlen == 0) { |
| 3331 | /* If nbytes was not specified, use the buffer's length */ |
| 3332 | recvlen = buflen; |
Benjamin Peterson | c6b37e2 | 2014-01-13 23:14:42 -0500 | [diff] [blame] | 3333 | } else if (recvlen > buflen) { |
| 3334 | PyBuffer_Release(&pbuf); |
| 3335 | PyErr_SetString(PyExc_ValueError, |
| 3336 | "nbytes is greater than the length of the buffer"); |
| 3337 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3338 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3340 | readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr); |
| 3341 | if (readlen < 0) { |
| 3342 | PyBuffer_Release(&pbuf); |
| 3343 | /* Return an error */ |
| 3344 | Py_XDECREF(addr); |
| 3345 | return NULL; |
| 3346 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3348 | PyBuffer_Release(&pbuf); |
| 3349 | /* Return the number of bytes read and the address. Note that we do |
| 3350 | not do anything special here in the case that readlen < recvlen. */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3351 | return Py_BuildValue("nN", readlen, addr); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3352 | } |
| 3353 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3354 | PyDoc_STRVAR(recvfrom_into_doc, |
| 3355 | "recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3356 | \n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3357 | Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3358 | |
Victor Stinner | 35bee93 | 2015-04-02 12:28:07 +0200 | [diff] [blame] | 3359 | /* The sendmsg() and recvmsg[_into]() methods require a working |
| 3360 | CMSG_LEN(). See the comment near get_CMSG_LEN(). */ |
| 3361 | #ifdef CMSG_LEN |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3362 | struct sock_recvmsg { |
| 3363 | struct msghdr *msg; |
| 3364 | int flags; |
| 3365 | ssize_t result; |
| 3366 | }; |
| 3367 | |
| 3368 | static int |
| 3369 | sock_recvmsg_impl(PySocketSockObject *s, void *data) |
| 3370 | { |
| 3371 | struct sock_recvmsg *ctx = data; |
| 3372 | |
| 3373 | ctx->result = recvmsg(s->sock_fd, ctx->msg, ctx->flags); |
| 3374 | return (ctx->result >= 0); |
| 3375 | } |
| 3376 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3377 | /* |
| 3378 | * Call recvmsg() with the supplied iovec structures, flags, and |
| 3379 | * ancillary data buffer size (controllen). Returns the tuple return |
| 3380 | * value for recvmsg() or recvmsg_into(), with the first item provided |
| 3381 | * by the supplied makeval() function. makeval() will be called with |
| 3382 | * the length read and makeval_data as arguments, and must return a |
| 3383 | * new reference (which will be decrefed if there is a subsequent |
| 3384 | * error). On error, closes any file descriptors received via |
| 3385 | * SCM_RIGHTS. |
| 3386 | */ |
| 3387 | static PyObject * |
| 3388 | sock_recvmsg_guts(PySocketSockObject *s, struct iovec *iov, int iovlen, |
| 3389 | int flags, Py_ssize_t controllen, |
| 3390 | PyObject *(*makeval)(ssize_t, void *), void *makeval_data) |
| 3391 | { |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3392 | sock_addr_t addrbuf; |
| 3393 | socklen_t addrbuflen; |
Charles-François Natali | e9e95ae | 2011-08-24 21:40:53 +0200 | [diff] [blame] | 3394 | struct msghdr msg = {0}; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3395 | PyObject *cmsg_list = NULL, *retval = NULL; |
| 3396 | void *controlbuf = NULL; |
| 3397 | struct cmsghdr *cmsgh; |
| 3398 | size_t cmsgdatalen = 0; |
| 3399 | int cmsg_status; |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3400 | struct sock_recvmsg ctx; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3401 | |
| 3402 | /* XXX: POSIX says that msg_name and msg_namelen "shall be |
| 3403 | ignored" when the socket is connected (Linux fills them in |
| 3404 | anyway for AF_UNIX sockets at least). Normally msg_namelen |
| 3405 | seems to be set to 0 if there's no address, but try to |
| 3406 | initialize msg_name to something that won't be mistaken for a |
| 3407 | real address if that doesn't happen. */ |
| 3408 | if (!getsockaddrlen(s, &addrbuflen)) |
| 3409 | return NULL; |
| 3410 | memset(&addrbuf, 0, addrbuflen); |
| 3411 | SAS2SA(&addrbuf)->sa_family = AF_UNSPEC; |
| 3412 | |
| 3413 | if (controllen < 0 || controllen > SOCKLEN_T_LIMIT) { |
| 3414 | PyErr_SetString(PyExc_ValueError, |
| 3415 | "invalid ancillary data buffer length"); |
| 3416 | return NULL; |
| 3417 | } |
| 3418 | if (controllen > 0 && (controlbuf = PyMem_Malloc(controllen)) == NULL) |
| 3419 | return PyErr_NoMemory(); |
| 3420 | |
| 3421 | /* Make the system call. */ |
| 3422 | if (!IS_SELECTABLE(s)) { |
| 3423 | select_error(); |
| 3424 | goto finally; |
| 3425 | } |
| 3426 | |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3427 | msg.msg_name = SAS2SA(&addrbuf); |
| 3428 | msg.msg_namelen = addrbuflen; |
| 3429 | msg.msg_iov = iov; |
| 3430 | msg.msg_iovlen = iovlen; |
| 3431 | msg.msg_control = controlbuf; |
| 3432 | msg.msg_controllen = controllen; |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3433 | |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3434 | ctx.msg = &msg; |
| 3435 | ctx.flags = flags; |
| 3436 | if (sock_call(s, 0, sock_recvmsg_impl, &ctx) < 0) |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3437 | goto finally; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3438 | |
| 3439 | /* Make list of (level, type, data) tuples from control messages. */ |
| 3440 | if ((cmsg_list = PyList_New(0)) == NULL) |
| 3441 | goto err_closefds; |
| 3442 | /* Check for empty ancillary data as old CMSG_FIRSTHDR() |
| 3443 | implementations didn't do so. */ |
| 3444 | for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL); |
| 3445 | cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) { |
| 3446 | PyObject *bytes, *tuple; |
| 3447 | int tmp; |
| 3448 | |
| 3449 | cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen); |
| 3450 | if (cmsg_status != 0) { |
| 3451 | if (PyErr_WarnEx(PyExc_RuntimeWarning, |
| 3452 | "received malformed or improperly-truncated " |
| 3453 | "ancillary data", 1) == -1) |
| 3454 | goto err_closefds; |
| 3455 | } |
| 3456 | if (cmsg_status < 0) |
| 3457 | break; |
| 3458 | if (cmsgdatalen > PY_SSIZE_T_MAX) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3459 | PyErr_SetString(PyExc_OSError, "control message too long"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3460 | goto err_closefds; |
| 3461 | } |
| 3462 | |
| 3463 | bytes = PyBytes_FromStringAndSize((char *)CMSG_DATA(cmsgh), |
| 3464 | cmsgdatalen); |
| 3465 | tuple = Py_BuildValue("iiN", (int)cmsgh->cmsg_level, |
| 3466 | (int)cmsgh->cmsg_type, bytes); |
| 3467 | if (tuple == NULL) |
| 3468 | goto err_closefds; |
| 3469 | tmp = PyList_Append(cmsg_list, tuple); |
| 3470 | Py_DECREF(tuple); |
| 3471 | if (tmp != 0) |
| 3472 | goto err_closefds; |
| 3473 | |
| 3474 | if (cmsg_status != 0) |
| 3475 | break; |
| 3476 | } |
| 3477 | |
| 3478 | retval = Py_BuildValue("NOiN", |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3479 | (*makeval)(ctx.result, makeval_data), |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3480 | cmsg_list, |
| 3481 | (int)msg.msg_flags, |
| 3482 | makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 3483 | ((msg.msg_namelen > addrbuflen) ? |
| 3484 | addrbuflen : msg.msg_namelen), |
| 3485 | s->sock_proto)); |
| 3486 | if (retval == NULL) |
| 3487 | goto err_closefds; |
| 3488 | |
| 3489 | finally: |
| 3490 | Py_XDECREF(cmsg_list); |
| 3491 | PyMem_Free(controlbuf); |
| 3492 | return retval; |
| 3493 | |
| 3494 | err_closefds: |
| 3495 | #ifdef SCM_RIGHTS |
| 3496 | /* Close all descriptors coming from SCM_RIGHTS, so they don't leak. */ |
| 3497 | for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL); |
| 3498 | cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) { |
| 3499 | cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen); |
| 3500 | if (cmsg_status < 0) |
| 3501 | break; |
| 3502 | if (cmsgh->cmsg_level == SOL_SOCKET && |
| 3503 | cmsgh->cmsg_type == SCM_RIGHTS) { |
| 3504 | size_t numfds; |
| 3505 | int *fdp; |
| 3506 | |
| 3507 | numfds = cmsgdatalen / sizeof(int); |
| 3508 | fdp = (int *)CMSG_DATA(cmsgh); |
| 3509 | while (numfds-- > 0) |
| 3510 | close(*fdp++); |
| 3511 | } |
| 3512 | if (cmsg_status != 0) |
| 3513 | break; |
| 3514 | } |
| 3515 | #endif /* SCM_RIGHTS */ |
| 3516 | goto finally; |
| 3517 | } |
| 3518 | |
| 3519 | |
| 3520 | static PyObject * |
| 3521 | makeval_recvmsg(ssize_t received, void *data) |
| 3522 | { |
| 3523 | PyObject **buf = data; |
| 3524 | |
| 3525 | if (received < PyBytes_GET_SIZE(*buf)) |
| 3526 | _PyBytes_Resize(buf, received); |
| 3527 | Py_XINCREF(*buf); |
| 3528 | return *buf; |
| 3529 | } |
| 3530 | |
| 3531 | /* s.recvmsg(bufsize[, ancbufsize[, flags]]) method */ |
| 3532 | |
| 3533 | static PyObject * |
| 3534 | sock_recvmsg(PySocketSockObject *s, PyObject *args) |
| 3535 | { |
| 3536 | Py_ssize_t bufsize, ancbufsize = 0; |
| 3537 | int flags = 0; |
| 3538 | struct iovec iov; |
| 3539 | PyObject *buf = NULL, *retval = NULL; |
| 3540 | |
| 3541 | if (!PyArg_ParseTuple(args, "n|ni:recvmsg", &bufsize, &ancbufsize, &flags)) |
| 3542 | return NULL; |
| 3543 | |
| 3544 | if (bufsize < 0) { |
| 3545 | PyErr_SetString(PyExc_ValueError, "negative buffer size in recvmsg()"); |
| 3546 | return NULL; |
| 3547 | } |
| 3548 | if ((buf = PyBytes_FromStringAndSize(NULL, bufsize)) == NULL) |
| 3549 | return NULL; |
| 3550 | iov.iov_base = PyBytes_AS_STRING(buf); |
| 3551 | iov.iov_len = bufsize; |
| 3552 | |
| 3553 | /* Note that we're passing a pointer to *our pointer* to the bytes |
| 3554 | object here (&buf); makeval_recvmsg() may incref the object, or |
| 3555 | deallocate it and set our pointer to NULL. */ |
| 3556 | retval = sock_recvmsg_guts(s, &iov, 1, flags, ancbufsize, |
| 3557 | &makeval_recvmsg, &buf); |
| 3558 | Py_XDECREF(buf); |
| 3559 | return retval; |
| 3560 | } |
| 3561 | |
| 3562 | PyDoc_STRVAR(recvmsg_doc, |
| 3563 | "recvmsg(bufsize[, ancbufsize[, flags]]) -> (data, ancdata, msg_flags, address)\n\ |
| 3564 | \n\ |
| 3565 | Receive normal data (up to bufsize bytes) and ancillary data from the\n\ |
| 3566 | socket. The ancbufsize argument sets the size in bytes of the\n\ |
| 3567 | internal buffer used to receive the ancillary data; it defaults to 0,\n\ |
| 3568 | meaning that no ancillary data will be received. Appropriate buffer\n\ |
| 3569 | sizes for ancillary data can be calculated using CMSG_SPACE() or\n\ |
| 3570 | CMSG_LEN(), and items which do not fit into the buffer might be\n\ |
| 3571 | truncated or discarded. The flags argument defaults to 0 and has the\n\ |
| 3572 | same meaning as for recv().\n\ |
| 3573 | \n\ |
| 3574 | The return value is a 4-tuple: (data, ancdata, msg_flags, address).\n\ |
| 3575 | The data item is a bytes object holding the non-ancillary data\n\ |
| 3576 | received. The ancdata item is a list of zero or more tuples\n\ |
| 3577 | (cmsg_level, cmsg_type, cmsg_data) representing the ancillary data\n\ |
| 3578 | (control messages) received: cmsg_level and cmsg_type are integers\n\ |
| 3579 | specifying the protocol level and protocol-specific type respectively,\n\ |
| 3580 | and cmsg_data is a bytes object holding the associated data. The\n\ |
| 3581 | msg_flags item is the bitwise OR of various flags indicating\n\ |
| 3582 | conditions on the received message; see your system documentation for\n\ |
| 3583 | details. If the receiving socket is unconnected, address is the\n\ |
| 3584 | address of the sending socket, if available; otherwise, its value is\n\ |
| 3585 | unspecified.\n\ |
| 3586 | \n\ |
| 3587 | If recvmsg() raises an exception after the system call returns, it\n\ |
| 3588 | will first attempt to close any file descriptors received via the\n\ |
| 3589 | SCM_RIGHTS mechanism."); |
| 3590 | |
| 3591 | |
| 3592 | static PyObject * |
| 3593 | makeval_recvmsg_into(ssize_t received, void *data) |
| 3594 | { |
| 3595 | return PyLong_FromSsize_t(received); |
| 3596 | } |
| 3597 | |
| 3598 | /* s.recvmsg_into(buffers[, ancbufsize[, flags]]) method */ |
| 3599 | |
| 3600 | static PyObject * |
| 3601 | sock_recvmsg_into(PySocketSockObject *s, PyObject *args) |
| 3602 | { |
| 3603 | Py_ssize_t ancbufsize = 0; |
| 3604 | int flags = 0; |
| 3605 | struct iovec *iovs = NULL; |
| 3606 | Py_ssize_t i, nitems, nbufs = 0; |
| 3607 | Py_buffer *bufs = NULL; |
| 3608 | PyObject *buffers_arg, *fast, *retval = NULL; |
| 3609 | |
| 3610 | if (!PyArg_ParseTuple(args, "O|ni:recvmsg_into", |
| 3611 | &buffers_arg, &ancbufsize, &flags)) |
| 3612 | return NULL; |
| 3613 | |
| 3614 | if ((fast = PySequence_Fast(buffers_arg, |
| 3615 | "recvmsg_into() argument 1 must be an " |
| 3616 | "iterable")) == NULL) |
| 3617 | return NULL; |
| 3618 | nitems = PySequence_Fast_GET_SIZE(fast); |
| 3619 | if (nitems > INT_MAX) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3620 | PyErr_SetString(PyExc_OSError, "recvmsg_into() argument 1 is too long"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3621 | goto finally; |
| 3622 | } |
| 3623 | |
| 3624 | /* Fill in an iovec for each item, and save the Py_buffer |
| 3625 | structs to release afterwards. */ |
| 3626 | if (nitems > 0 && ((iovs = PyMem_New(struct iovec, nitems)) == NULL || |
| 3627 | (bufs = PyMem_New(Py_buffer, nitems)) == NULL)) { |
| 3628 | PyErr_NoMemory(); |
| 3629 | goto finally; |
| 3630 | } |
| 3631 | for (; nbufs < nitems; nbufs++) { |
| 3632 | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(fast, nbufs), |
| 3633 | "w*;recvmsg_into() argument 1 must be an iterable " |
| 3634 | "of single-segment read-write buffers", |
| 3635 | &bufs[nbufs])) |
| 3636 | goto finally; |
| 3637 | iovs[nbufs].iov_base = bufs[nbufs].buf; |
| 3638 | iovs[nbufs].iov_len = bufs[nbufs].len; |
| 3639 | } |
| 3640 | |
| 3641 | retval = sock_recvmsg_guts(s, iovs, nitems, flags, ancbufsize, |
| 3642 | &makeval_recvmsg_into, NULL); |
| 3643 | finally: |
| 3644 | for (i = 0; i < nbufs; i++) |
| 3645 | PyBuffer_Release(&bufs[i]); |
| 3646 | PyMem_Free(bufs); |
| 3647 | PyMem_Free(iovs); |
| 3648 | Py_DECREF(fast); |
| 3649 | return retval; |
| 3650 | } |
| 3651 | |
| 3652 | PyDoc_STRVAR(recvmsg_into_doc, |
| 3653 | "recvmsg_into(buffers[, ancbufsize[, flags]]) -> (nbytes, ancdata, msg_flags, address)\n\ |
| 3654 | \n\ |
| 3655 | Receive normal data and ancillary data from the socket, scattering the\n\ |
| 3656 | non-ancillary data into a series of buffers. The buffers argument\n\ |
| 3657 | must be an iterable of objects that export writable buffers\n\ |
| 3658 | (e.g. bytearray objects); these will be filled with successive chunks\n\ |
| 3659 | of the non-ancillary data until it has all been written or there are\n\ |
| 3660 | no more buffers. The ancbufsize argument sets the size in bytes of\n\ |
| 3661 | the internal buffer used to receive the ancillary data; it defaults to\n\ |
| 3662 | 0, meaning that no ancillary data will be received. Appropriate\n\ |
| 3663 | buffer sizes for ancillary data can be calculated using CMSG_SPACE()\n\ |
| 3664 | or CMSG_LEN(), and items which do not fit into the buffer might be\n\ |
| 3665 | truncated or discarded. The flags argument defaults to 0 and has the\n\ |
| 3666 | same meaning as for recv().\n\ |
| 3667 | \n\ |
| 3668 | The return value is a 4-tuple: (nbytes, ancdata, msg_flags, address).\n\ |
| 3669 | The nbytes item is the total number of bytes of non-ancillary data\n\ |
| 3670 | written into the buffers. The ancdata item is a list of zero or more\n\ |
| 3671 | tuples (cmsg_level, cmsg_type, cmsg_data) representing the ancillary\n\ |
| 3672 | data (control messages) received: cmsg_level and cmsg_type are\n\ |
| 3673 | integers specifying the protocol level and protocol-specific type\n\ |
| 3674 | respectively, and cmsg_data is a bytes object holding the associated\n\ |
| 3675 | data. The msg_flags item is the bitwise OR of various flags\n\ |
| 3676 | indicating conditions on the received message; see your system\n\ |
| 3677 | documentation for details. If the receiving socket is unconnected,\n\ |
| 3678 | address is the address of the sending socket, if available; otherwise,\n\ |
| 3679 | its value is unspecified.\n\ |
| 3680 | \n\ |
| 3681 | If recvmsg_into() raises an exception after the system call returns,\n\ |
| 3682 | it will first attempt to close any file descriptors received via the\n\ |
| 3683 | SCM_RIGHTS mechanism."); |
| 3684 | #endif /* CMSG_LEN */ |
| 3685 | |
| 3686 | |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3687 | struct sock_send { |
| 3688 | char *buf; |
| 3689 | Py_ssize_t len; |
| 3690 | int flags; |
| 3691 | Py_ssize_t result; |
| 3692 | }; |
| 3693 | |
| 3694 | static int |
| 3695 | sock_send_impl(PySocketSockObject *s, void *data) |
| 3696 | { |
| 3697 | struct sock_send *ctx = data; |
| 3698 | |
| 3699 | #ifdef MS_WINDOWS |
| 3700 | if (ctx->len > INT_MAX) |
| 3701 | ctx->len = INT_MAX; |
| 3702 | ctx->result = send(s->sock_fd, ctx->buf, (int)ctx->len, ctx->flags); |
| 3703 | #else |
| 3704 | ctx->result = send(s->sock_fd, ctx->buf, ctx->len, ctx->flags); |
| 3705 | #endif |
| 3706 | return (ctx->result >= 0); |
| 3707 | } |
| 3708 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 3709 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3710 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3711 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3712 | sock_send(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3713 | { |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3714 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3715 | Py_buffer pbuf; |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3716 | struct sock_send ctx; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3718 | if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags)) |
| 3719 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3720 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3721 | if (!IS_SELECTABLE(s)) { |
| 3722 | PyBuffer_Release(&pbuf); |
| 3723 | return select_error(); |
| 3724 | } |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3725 | ctx.buf = pbuf.buf; |
| 3726 | ctx.len = pbuf.len; |
| 3727 | ctx.flags = flags; |
| 3728 | if (sock_call(s, 1, sock_send_impl, &ctx) < 0) { |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3729 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3730 | return NULL; |
| 3731 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3732 | PyBuffer_Release(&pbuf); |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3733 | |
| 3734 | return PyLong_FromSsize_t(ctx.result); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3735 | } |
| 3736 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3737 | PyDoc_STRVAR(send_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3738 | "send(data[, flags]) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3739 | \n\ |
| 3740 | 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] | 3741 | 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] | 3742 | 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] | 3743 | |
| 3744 | |
| 3745 | /* s.sendall(data [,flags]) method */ |
| 3746 | |
| 3747 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3748 | sock_sendall(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3749 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3750 | char *buf; |
Victor Stinner | 02f32ab | 2015-04-01 22:53:26 +0200 | [diff] [blame] | 3751 | Py_ssize_t len, n; |
| 3752 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3753 | Py_buffer pbuf; |
Victor Stinner | 02f32ab | 2015-04-01 22:53:26 +0200 | [diff] [blame] | 3754 | struct sock_send ctx; |
Victor Stinner | 8912d14 | 2015-04-06 23:16:34 +0200 | [diff] [blame] | 3755 | int has_timeout = (s->sock_timeout > 0); |
| 3756 | _PyTime_t interval = s->sock_timeout; |
| 3757 | _PyTime_t deadline = 0; |
| 3758 | int deadline_initialized = 0; |
| 3759 | PyObject *res = NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3760 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3761 | if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags)) |
| 3762 | return NULL; |
| 3763 | buf = pbuf.buf; |
| 3764 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3766 | if (!IS_SELECTABLE(s)) { |
| 3767 | PyBuffer_Release(&pbuf); |
| 3768 | return select_error(); |
| 3769 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 3770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3771 | do { |
Victor Stinner | 8912d14 | 2015-04-06 23:16:34 +0200 | [diff] [blame] | 3772 | if (has_timeout) { |
| 3773 | if (deadline_initialized) { |
| 3774 | /* recompute the timeout */ |
| 3775 | interval = deadline - _PyTime_GetMonotonicClock(); |
| 3776 | } |
| 3777 | else { |
| 3778 | deadline_initialized = 1; |
| 3779 | deadline = _PyTime_GetMonotonicClock() + s->sock_timeout; |
| 3780 | } |
| 3781 | |
| 3782 | if (interval <= 0) { |
| 3783 | PyErr_SetString(socket_timeout, "timed out"); |
| 3784 | goto done; |
| 3785 | } |
| 3786 | } |
| 3787 | |
Victor Stinner | 02f32ab | 2015-04-01 22:53:26 +0200 | [diff] [blame] | 3788 | ctx.buf = buf; |
| 3789 | ctx.len = len; |
| 3790 | ctx.flags = flags; |
Victor Stinner | 8912d14 | 2015-04-06 23:16:34 +0200 | [diff] [blame] | 3791 | if (sock_call_ex(s, 1, sock_send_impl, &ctx, 0, NULL, interval) < 0) |
| 3792 | goto done; |
Victor Stinner | 02f32ab | 2015-04-01 22:53:26 +0200 | [diff] [blame] | 3793 | n = ctx.result; |
| 3794 | assert(n >= 0); |
| 3795 | |
| 3796 | buf += n; |
| 3797 | len -= n; |
| 3798 | |
| 3799 | /* We must run our signal handlers before looping again. |
| 3800 | send() can return a successful partial write when it is |
| 3801 | interrupted, so we can't restrict ourselves to EINTR. */ |
Victor Stinner | 8912d14 | 2015-04-06 23:16:34 +0200 | [diff] [blame] | 3802 | if (PyErr_CheckSignals()) |
| 3803 | goto done; |
Victor Stinner | 02f32ab | 2015-04-01 22:53:26 +0200 | [diff] [blame] | 3804 | } while (len > 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3805 | PyBuffer_Release(&pbuf); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3806 | |
Victor Stinner | 8912d14 | 2015-04-06 23:16:34 +0200 | [diff] [blame] | 3807 | Py_INCREF(Py_None); |
| 3808 | res = Py_None; |
| 3809 | |
| 3810 | done: |
| 3811 | PyBuffer_Release(&pbuf); |
| 3812 | return res; |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3813 | } |
| 3814 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3815 | PyDoc_STRVAR(sendall_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3816 | "sendall(data[, flags])\n\ |
| 3817 | \n\ |
| 3818 | Send a data string to the socket. For the optional flags\n\ |
| 3819 | argument, see the Unix manual. This calls send() repeatedly\n\ |
| 3820 | 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] | 3821 | to tell how much data has been sent."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3822 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3823 | |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3824 | struct sock_sendto { |
| 3825 | char *buf; |
| 3826 | Py_ssize_t len; |
| 3827 | int flags; |
| 3828 | int addrlen; |
| 3829 | sock_addr_t *addrbuf; |
| 3830 | Py_ssize_t result; |
| 3831 | }; |
| 3832 | |
| 3833 | static int |
| 3834 | sock_sendto_impl(PySocketSockObject *s, void *data) |
| 3835 | { |
| 3836 | struct sock_sendto *ctx = data; |
| 3837 | |
| 3838 | #ifdef MS_WINDOWS |
| 3839 | if (ctx->len > INT_MAX) |
| 3840 | ctx->len = INT_MAX; |
| 3841 | ctx->result = sendto(s->sock_fd, ctx->buf, (int)ctx->len, ctx->flags, |
| 3842 | SAS2SA(ctx->addrbuf), ctx->addrlen); |
| 3843 | #else |
| 3844 | ctx->result = sendto(s->sock_fd, ctx->buf, ctx->len, ctx->flags, |
| 3845 | SAS2SA(ctx->addrbuf), ctx->addrlen); |
| 3846 | #endif |
| 3847 | return (ctx->result >= 0); |
| 3848 | } |
| 3849 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 3850 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3851 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3852 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3853 | sock_sendto(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3854 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3855 | Py_buffer pbuf; |
| 3856 | PyObject *addro; |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3857 | Py_ssize_t arglen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3858 | sock_addr_t addrbuf; |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3859 | int addrlen, flags; |
| 3860 | struct sock_sendto ctx; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3861 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3862 | flags = 0; |
Antoine Pitrou | 5e98141 | 2011-03-17 22:38:37 +0100 | [diff] [blame] | 3863 | arglen = PyTuple_Size(args); |
| 3864 | switch (arglen) { |
| 3865 | case 2: |
| 3866 | PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro); |
| 3867 | break; |
| 3868 | case 3: |
| 3869 | PyArg_ParseTuple(args, "y*iO:sendto", |
| 3870 | &pbuf, &flags, &addro); |
| 3871 | break; |
| 3872 | default: |
| 3873 | PyErr_Format(PyExc_TypeError, |
| 3874 | "sendto() takes 2 or 3 arguments (%d given)", |
| 3875 | arglen); |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 3876 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3877 | } |
Antoine Pitrou | 5e98141 | 2011-03-17 22:38:37 +0100 | [diff] [blame] | 3878 | if (PyErr_Occurred()) |
| 3879 | return NULL; |
| 3880 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3881 | if (!IS_SELECTABLE(s)) { |
| 3882 | PyBuffer_Release(&pbuf); |
| 3883 | return select_error(); |
| 3884 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 3885 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3886 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) { |
| 3887 | PyBuffer_Release(&pbuf); |
| 3888 | return NULL; |
| 3889 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3890 | |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3891 | ctx.buf = pbuf.buf; |
| 3892 | ctx.len = pbuf.len; |
| 3893 | ctx.flags = flags; |
| 3894 | ctx.addrlen = addrlen; |
| 3895 | ctx.addrbuf = &addrbuf; |
| 3896 | if (sock_call(s, 1, sock_sendto_impl, &ctx) < 0) { |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3897 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3898 | return NULL; |
| 3899 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3900 | PyBuffer_Release(&pbuf); |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3901 | |
| 3902 | return PyLong_FromSsize_t(ctx.result); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3905 | PyDoc_STRVAR(sendto_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3906 | "sendto(data[, flags], address) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3907 | \n\ |
| 3908 | 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] | 3909 | For IP sockets, the address is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3910 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3911 | |
Victor Stinner | 35bee93 | 2015-04-02 12:28:07 +0200 | [diff] [blame] | 3912 | /* The sendmsg() and recvmsg[_into]() methods require a working |
| 3913 | CMSG_LEN(). See the comment near get_CMSG_LEN(). */ |
| 3914 | #ifdef CMSG_LEN |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3915 | struct sock_sendmsg { |
| 3916 | struct msghdr *msg; |
| 3917 | int flags; |
| 3918 | ssize_t result; |
| 3919 | }; |
| 3920 | |
| 3921 | static int |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 3922 | sock_sendmsg_iovec(PySocketSockObject *s, PyObject *data_arg, |
| 3923 | struct msghdr *msg, |
| 3924 | Py_buffer **databufsout, Py_ssize_t *ndatabufsout) { |
| 3925 | Py_ssize_t ndataparts, ndatabufs = 0; |
| 3926 | int result = -1; |
| 3927 | struct iovec *iovs = NULL; |
| 3928 | PyObject *data_fast = NULL; |
| 3929 | Py_buffer *databufs = NULL; |
| 3930 | |
| 3931 | /* Fill in an iovec for each message part, and save the Py_buffer |
| 3932 | structs to release afterwards. */ |
Victor Stinner | 288b5bf | 2016-09-12 11:45:59 +0200 | [diff] [blame] | 3933 | data_fast = PySequence_Fast(data_arg, |
| 3934 | "sendmsg() argument 1 must be an " |
| 3935 | "iterable"); |
| 3936 | if (data_fast == NULL) { |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 3937 | goto finally; |
Victor Stinner | 288b5bf | 2016-09-12 11:45:59 +0200 | [diff] [blame] | 3938 | } |
| 3939 | |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 3940 | ndataparts = PySequence_Fast_GET_SIZE(data_fast); |
| 3941 | if (ndataparts > INT_MAX) { |
| 3942 | PyErr_SetString(PyExc_OSError, "sendmsg() argument 1 is too long"); |
| 3943 | goto finally; |
| 3944 | } |
Victor Stinner | 288b5bf | 2016-09-12 11:45:59 +0200 | [diff] [blame] | 3945 | |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 3946 | msg->msg_iovlen = ndataparts; |
Victor Stinner | 288b5bf | 2016-09-12 11:45:59 +0200 | [diff] [blame] | 3947 | if (ndataparts > 0) { |
| 3948 | iovs = PyMem_New(struct iovec, ndataparts); |
| 3949 | if (iovs == NULL) { |
| 3950 | PyErr_NoMemory(); |
| 3951 | goto finally; |
| 3952 | } |
| 3953 | msg->msg_iov = iovs; |
| 3954 | |
| 3955 | databufs = PyMem_New(Py_buffer, ndataparts); |
Christian Heimes | faf2cea | 2016-09-13 10:07:16 +0200 | [diff] [blame] | 3956 | if (databufs == NULL) { |
Victor Stinner | 288b5bf | 2016-09-12 11:45:59 +0200 | [diff] [blame] | 3957 | PyErr_NoMemory(); |
| 3958 | goto finally; |
| 3959 | } |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 3960 | } |
| 3961 | for (; ndatabufs < ndataparts; ndatabufs++) { |
| 3962 | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(data_fast, ndatabufs), |
| 3963 | "y*;sendmsg() argument 1 must be an iterable of " |
| 3964 | "bytes-like objects", |
| 3965 | &databufs[ndatabufs])) |
| 3966 | goto finally; |
| 3967 | iovs[ndatabufs].iov_base = databufs[ndatabufs].buf; |
| 3968 | iovs[ndatabufs].iov_len = databufs[ndatabufs].len; |
| 3969 | } |
| 3970 | result = 0; |
| 3971 | finally: |
| 3972 | *databufsout = databufs; |
| 3973 | *ndatabufsout = ndatabufs; |
| 3974 | Py_XDECREF(data_fast); |
| 3975 | return result; |
| 3976 | } |
| 3977 | |
| 3978 | static int |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 3979 | sock_sendmsg_impl(PySocketSockObject *s, void *data) |
| 3980 | { |
| 3981 | struct sock_sendmsg *ctx = data; |
| 3982 | |
| 3983 | ctx->result = sendmsg(s->sock_fd, ctx->msg, ctx->flags); |
| 3984 | return (ctx->result >= 0); |
| 3985 | } |
| 3986 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3987 | /* s.sendmsg(buffers[, ancdata[, flags[, address]]]) method */ |
| 3988 | |
| 3989 | static PyObject * |
| 3990 | sock_sendmsg(PySocketSockObject *s, PyObject *args) |
| 3991 | { |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 3992 | Py_ssize_t i, ndatabufs = 0, ncmsgs, ncmsgbufs = 0; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3993 | Py_buffer *databufs = NULL; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3994 | sock_addr_t addrbuf; |
Victor Stinner | 288b5bf | 2016-09-12 11:45:59 +0200 | [diff] [blame] | 3995 | struct msghdr msg; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3996 | struct cmsginfo { |
| 3997 | int level; |
| 3998 | int type; |
| 3999 | Py_buffer data; |
| 4000 | } *cmsgs = NULL; |
| 4001 | void *controlbuf = NULL; |
| 4002 | size_t controllen, controllen_last; |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 4003 | int addrlen, flags = 0; |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 4004 | PyObject *data_arg, *cmsg_arg = NULL, *addr_arg = NULL, |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4005 | *cmsg_fast = NULL, *retval = NULL; |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 4006 | struct sock_sendmsg ctx; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4007 | |
| 4008 | if (!PyArg_ParseTuple(args, "O|OiO:sendmsg", |
Victor Stinner | 288b5bf | 2016-09-12 11:45:59 +0200 | [diff] [blame] | 4009 | &data_arg, &cmsg_arg, &flags, &addr_arg)) { |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4010 | return NULL; |
Victor Stinner | 288b5bf | 2016-09-12 11:45:59 +0200 | [diff] [blame] | 4011 | } |
| 4012 | |
| 4013 | memset(&msg, 0, sizeof(msg)); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4014 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4015 | /* Parse destination address. */ |
| 4016 | if (addr_arg != NULL && addr_arg != Py_None) { |
| 4017 | if (!getsockaddrarg(s, addr_arg, SAS2SA(&addrbuf), &addrlen)) |
| 4018 | goto finally; |
| 4019 | msg.msg_name = &addrbuf; |
| 4020 | msg.msg_namelen = addrlen; |
| 4021 | } |
| 4022 | |
| 4023 | /* Fill in an iovec for each message part, and save the Py_buffer |
| 4024 | structs to release afterwards. */ |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 4025 | if (sock_sendmsg_iovec(s, data_arg, &msg, &databufs, &ndatabufs) == -1) { |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4026 | goto finally; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4027 | } |
| 4028 | |
| 4029 | if (cmsg_arg == NULL) |
| 4030 | ncmsgs = 0; |
| 4031 | else { |
| 4032 | if ((cmsg_fast = PySequence_Fast(cmsg_arg, |
| 4033 | "sendmsg() argument 2 must be an " |
| 4034 | "iterable")) == NULL) |
| 4035 | goto finally; |
| 4036 | ncmsgs = PySequence_Fast_GET_SIZE(cmsg_fast); |
| 4037 | } |
| 4038 | |
| 4039 | #ifndef CMSG_SPACE |
| 4040 | if (ncmsgs > 1) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4041 | PyErr_SetString(PyExc_OSError, |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4042 | "sending multiple control messages is not supported " |
| 4043 | "on this system"); |
| 4044 | goto finally; |
| 4045 | } |
| 4046 | #endif |
| 4047 | /* Save level, type and Py_buffer for each control message, |
| 4048 | and calculate total size. */ |
| 4049 | if (ncmsgs > 0 && (cmsgs = PyMem_New(struct cmsginfo, ncmsgs)) == NULL) { |
| 4050 | PyErr_NoMemory(); |
| 4051 | goto finally; |
| 4052 | } |
| 4053 | controllen = controllen_last = 0; |
| 4054 | while (ncmsgbufs < ncmsgs) { |
| 4055 | size_t bufsize, space; |
| 4056 | |
| 4057 | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(cmsg_fast, ncmsgbufs), |
| 4058 | "(iiy*):[sendmsg() ancillary data items]", |
| 4059 | &cmsgs[ncmsgbufs].level, |
| 4060 | &cmsgs[ncmsgbufs].type, |
| 4061 | &cmsgs[ncmsgbufs].data)) |
| 4062 | goto finally; |
| 4063 | bufsize = cmsgs[ncmsgbufs++].data.len; |
| 4064 | |
| 4065 | #ifdef CMSG_SPACE |
| 4066 | if (!get_CMSG_SPACE(bufsize, &space)) { |
| 4067 | #else |
| 4068 | if (!get_CMSG_LEN(bufsize, &space)) { |
| 4069 | #endif |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4070 | PyErr_SetString(PyExc_OSError, "ancillary data item too large"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4071 | goto finally; |
| 4072 | } |
| 4073 | controllen += space; |
| 4074 | if (controllen > SOCKLEN_T_LIMIT || controllen < controllen_last) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4075 | PyErr_SetString(PyExc_OSError, "too much ancillary data"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4076 | goto finally; |
| 4077 | } |
| 4078 | controllen_last = controllen; |
| 4079 | } |
| 4080 | |
| 4081 | /* Construct ancillary data block from control message info. */ |
| 4082 | if (ncmsgbufs > 0) { |
| 4083 | struct cmsghdr *cmsgh = NULL; |
| 4084 | |
Victor Stinner | 52d61e4 | 2016-09-12 11:41:58 +0200 | [diff] [blame] | 4085 | controlbuf = PyMem_Malloc(controllen); |
| 4086 | if (controlbuf == NULL) { |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4087 | PyErr_NoMemory(); |
| 4088 | goto finally; |
| 4089 | } |
Victor Stinner | 52d61e4 | 2016-09-12 11:41:58 +0200 | [diff] [blame] | 4090 | msg.msg_control = controlbuf; |
| 4091 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4092 | msg.msg_controllen = controllen; |
| 4093 | |
| 4094 | /* Need to zero out the buffer as a workaround for glibc's |
| 4095 | CMSG_NXTHDR() implementation. After getting the pointer to |
| 4096 | the next header, it checks its (uninitialized) cmsg_len |
| 4097 | member to see if the "message" fits in the buffer, and |
| 4098 | returns NULL if it doesn't. Zero-filling the buffer |
Terry Jan Reedy | 0f84764 | 2013-03-11 18:34:00 -0400 | [diff] [blame] | 4099 | ensures that this doesn't happen. */ |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4100 | memset(controlbuf, 0, controllen); |
| 4101 | |
| 4102 | for (i = 0; i < ncmsgbufs; i++) { |
| 4103 | size_t msg_len, data_len = cmsgs[i].data.len; |
| 4104 | int enough_space = 0; |
| 4105 | |
| 4106 | cmsgh = (i == 0) ? CMSG_FIRSTHDR(&msg) : CMSG_NXTHDR(&msg, cmsgh); |
| 4107 | if (cmsgh == NULL) { |
| 4108 | PyErr_Format(PyExc_RuntimeError, |
| 4109 | "unexpected NULL result from %s()", |
| 4110 | (i == 0) ? "CMSG_FIRSTHDR" : "CMSG_NXTHDR"); |
| 4111 | goto finally; |
| 4112 | } |
| 4113 | if (!get_CMSG_LEN(data_len, &msg_len)) { |
| 4114 | PyErr_SetString(PyExc_RuntimeError, |
| 4115 | "item size out of range for CMSG_LEN()"); |
| 4116 | goto finally; |
| 4117 | } |
| 4118 | if (cmsg_min_space(&msg, cmsgh, msg_len)) { |
| 4119 | size_t space; |
| 4120 | |
| 4121 | cmsgh->cmsg_len = msg_len; |
| 4122 | if (get_cmsg_data_space(&msg, cmsgh, &space)) |
| 4123 | enough_space = (space >= data_len); |
| 4124 | } |
| 4125 | if (!enough_space) { |
| 4126 | PyErr_SetString(PyExc_RuntimeError, |
| 4127 | "ancillary data does not fit in calculated " |
| 4128 | "space"); |
| 4129 | goto finally; |
| 4130 | } |
| 4131 | cmsgh->cmsg_level = cmsgs[i].level; |
| 4132 | cmsgh->cmsg_type = cmsgs[i].type; |
| 4133 | memcpy(CMSG_DATA(cmsgh), cmsgs[i].data.buf, data_len); |
| 4134 | } |
| 4135 | } |
| 4136 | |
| 4137 | /* Make the system call. */ |
| 4138 | if (!IS_SELECTABLE(s)) { |
| 4139 | select_error(); |
| 4140 | goto finally; |
| 4141 | } |
| 4142 | |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 4143 | ctx.msg = &msg; |
| 4144 | ctx.flags = flags; |
| 4145 | if (sock_call(s, 1, sock_sendmsg_impl, &ctx) < 0) |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4146 | goto finally; |
Victor Stinner | 31bf2d5 | 2015-04-01 21:57:09 +0200 | [diff] [blame] | 4147 | |
| 4148 | retval = PyLong_FromSsize_t(ctx.result); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4149 | |
| 4150 | finally: |
| 4151 | PyMem_Free(controlbuf); |
| 4152 | for (i = 0; i < ncmsgbufs; i++) |
| 4153 | PyBuffer_Release(&cmsgs[i].data); |
| 4154 | PyMem_Free(cmsgs); |
| 4155 | Py_XDECREF(cmsg_fast); |
Victor Stinner | 52d61e4 | 2016-09-12 11:41:58 +0200 | [diff] [blame] | 4156 | PyMem_Free(msg.msg_iov); |
| 4157 | for (i = 0; i < ndatabufs; i++) { |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4158 | PyBuffer_Release(&databufs[i]); |
Victor Stinner | 52d61e4 | 2016-09-12 11:41:58 +0200 | [diff] [blame] | 4159 | } |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4160 | PyMem_Free(databufs); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4161 | return retval; |
| 4162 | } |
| 4163 | |
| 4164 | PyDoc_STRVAR(sendmsg_doc, |
| 4165 | "sendmsg(buffers[, ancdata[, flags[, address]]]) -> count\n\ |
| 4166 | \n\ |
| 4167 | Send normal and ancillary data to the socket, gathering the\n\ |
| 4168 | non-ancillary data from a series of buffers and concatenating it into\n\ |
| 4169 | a single message. The buffers argument specifies the non-ancillary\n\ |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 4170 | data as an iterable of bytes-like objects (e.g. bytes objects).\n\ |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4171 | The ancdata argument specifies the ancillary data (control messages)\n\ |
| 4172 | as an iterable of zero or more tuples (cmsg_level, cmsg_type,\n\ |
| 4173 | cmsg_data), where cmsg_level and cmsg_type are integers specifying the\n\ |
| 4174 | protocol level and protocol-specific type respectively, and cmsg_data\n\ |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 4175 | is a bytes-like object holding the associated data. The flags\n\ |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4176 | argument defaults to 0 and has the same meaning as for send(). If\n\ |
| 4177 | address is supplied and not None, it sets a destination address for\n\ |
| 4178 | the message. The return value is the number of bytes of non-ancillary\n\ |
| 4179 | data sent."); |
| 4180 | #endif /* CMSG_LEN */ |
| 4181 | |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 4182 | #ifdef HAVE_SOCKADDR_ALG |
| 4183 | static PyObject* |
| 4184 | sock_sendmsg_afalg(PySocketSockObject *self, PyObject *args, PyObject *kwds) |
| 4185 | { |
| 4186 | PyObject *retval = NULL; |
| 4187 | |
| 4188 | Py_ssize_t i, ndatabufs = 0; |
| 4189 | Py_buffer *databufs = NULL; |
| 4190 | PyObject *data_arg = NULL; |
| 4191 | |
| 4192 | Py_buffer iv = {NULL, NULL}; |
| 4193 | |
| 4194 | PyObject *opobj = NULL; |
| 4195 | int op = -1; |
| 4196 | |
| 4197 | PyObject *assoclenobj = NULL; |
| 4198 | int assoclen = -1; |
| 4199 | |
| 4200 | unsigned int *uiptr; |
| 4201 | int flags = 0; |
| 4202 | |
| 4203 | struct msghdr msg; |
| 4204 | struct cmsghdr *header = NULL; |
| 4205 | struct af_alg_iv *alg_iv = NULL; |
| 4206 | struct sock_sendmsg ctx; |
| 4207 | Py_ssize_t controllen; |
| 4208 | void *controlbuf = NULL; |
| 4209 | static char *keywords[] = {"msg", "op", "iv", "assoclen", "flags", 0}; |
| 4210 | |
| 4211 | if (self->sock_family != AF_ALG) { |
| 4212 | PyErr_SetString(PyExc_OSError, |
| 4213 | "algset is only supported for AF_ALG"); |
| 4214 | return NULL; |
| 4215 | } |
| 4216 | |
| 4217 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 4218 | "|O$O!y*O!i:sendmsg_afalg", keywords, |
| 4219 | &data_arg, |
| 4220 | &PyLong_Type, &opobj, &iv, |
Victor Stinner | 288b5bf | 2016-09-12 11:45:59 +0200 | [diff] [blame] | 4221 | &PyLong_Type, &assoclenobj, &flags)) { |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 4222 | return NULL; |
Victor Stinner | 288b5bf | 2016-09-12 11:45:59 +0200 | [diff] [blame] | 4223 | } |
| 4224 | |
| 4225 | memset(&msg, 0, sizeof(msg)); |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 4226 | |
| 4227 | /* op is a required, keyword-only argument >= 0 */ |
| 4228 | if (opobj != NULL) { |
| 4229 | op = _PyLong_AsInt(opobj); |
| 4230 | } |
| 4231 | if (op < 0) { |
| 4232 | /* override exception from _PyLong_AsInt() */ |
| 4233 | PyErr_SetString(PyExc_TypeError, |
| 4234 | "Invalid or missing argument 'op'"); |
| 4235 | goto finally; |
| 4236 | } |
| 4237 | /* assoclen is optional but must be >= 0 */ |
| 4238 | if (assoclenobj != NULL) { |
| 4239 | assoclen = _PyLong_AsInt(assoclenobj); |
| 4240 | if (assoclen == -1 && PyErr_Occurred()) { |
| 4241 | goto finally; |
| 4242 | } |
| 4243 | if (assoclen < 0) { |
| 4244 | PyErr_SetString(PyExc_TypeError, |
| 4245 | "assoclen must be positive"); |
| 4246 | goto finally; |
| 4247 | } |
| 4248 | } |
| 4249 | |
| 4250 | controllen = CMSG_SPACE(4); |
| 4251 | if (iv.buf != NULL) { |
| 4252 | controllen += CMSG_SPACE(sizeof(*alg_iv) + iv.len); |
| 4253 | } |
| 4254 | if (assoclen >= 0) { |
| 4255 | controllen += CMSG_SPACE(4); |
| 4256 | } |
| 4257 | |
| 4258 | controlbuf = PyMem_Malloc(controllen); |
| 4259 | if (controlbuf == NULL) { |
Victor Stinner | 52d61e4 | 2016-09-12 11:41:58 +0200 | [diff] [blame] | 4260 | PyErr_NoMemory(); |
| 4261 | goto finally; |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 4262 | } |
| 4263 | memset(controlbuf, 0, controllen); |
| 4264 | |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 4265 | msg.msg_controllen = controllen; |
| 4266 | msg.msg_control = controlbuf; |
| 4267 | |
| 4268 | /* Fill in an iovec for each message part, and save the Py_buffer |
| 4269 | structs to release afterwards. */ |
| 4270 | if (data_arg != NULL) { |
| 4271 | if (sock_sendmsg_iovec(self, data_arg, &msg, &databufs, &ndatabufs) == -1) { |
| 4272 | goto finally; |
| 4273 | } |
| 4274 | } |
| 4275 | |
| 4276 | /* set operation to encrypt or decrypt */ |
| 4277 | header = CMSG_FIRSTHDR(&msg); |
| 4278 | if (header == NULL) { |
| 4279 | PyErr_SetString(PyExc_RuntimeError, |
| 4280 | "unexpected NULL result from CMSG_FIRSTHDR"); |
| 4281 | goto finally; |
| 4282 | } |
| 4283 | header->cmsg_level = SOL_ALG; |
| 4284 | header->cmsg_type = ALG_SET_OP; |
| 4285 | header->cmsg_len = CMSG_LEN(4); |
| 4286 | uiptr = (void*)CMSG_DATA(header); |
| 4287 | *uiptr = (unsigned int)op; |
| 4288 | |
| 4289 | /* set initialization vector */ |
| 4290 | if (iv.buf != NULL) { |
| 4291 | header = CMSG_NXTHDR(&msg, header); |
| 4292 | if (header == NULL) { |
| 4293 | PyErr_SetString(PyExc_RuntimeError, |
| 4294 | "unexpected NULL result from CMSG_NXTHDR(iv)"); |
| 4295 | goto finally; |
| 4296 | } |
| 4297 | header->cmsg_level = SOL_ALG; |
| 4298 | header->cmsg_type = ALG_SET_IV; |
| 4299 | header->cmsg_len = CMSG_SPACE(sizeof(*alg_iv) + iv.len); |
| 4300 | alg_iv = (void*)CMSG_DATA(header); |
| 4301 | alg_iv->ivlen = iv.len; |
| 4302 | memcpy(alg_iv->iv, iv.buf, iv.len); |
| 4303 | } |
| 4304 | |
| 4305 | /* set length of associated data for AEAD */ |
| 4306 | if (assoclen >= 0) { |
| 4307 | header = CMSG_NXTHDR(&msg, header); |
| 4308 | if (header == NULL) { |
| 4309 | PyErr_SetString(PyExc_RuntimeError, |
| 4310 | "unexpected NULL result from CMSG_NXTHDR(assoc)"); |
| 4311 | goto finally; |
| 4312 | } |
| 4313 | header->cmsg_level = SOL_ALG; |
| 4314 | header->cmsg_type = ALG_SET_AEAD_ASSOCLEN; |
| 4315 | header->cmsg_len = CMSG_LEN(4); |
| 4316 | uiptr = (void*)CMSG_DATA(header); |
| 4317 | *uiptr = (unsigned int)assoclen; |
| 4318 | } |
| 4319 | |
| 4320 | ctx.msg = &msg; |
| 4321 | ctx.flags = flags; |
Victor Stinner | 288b5bf | 2016-09-12 11:45:59 +0200 | [diff] [blame] | 4322 | if (sock_call(self, 1, sock_sendmsg_impl, &ctx) < 0) { |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 4323 | goto finally; |
Victor Stinner | 288b5bf | 2016-09-12 11:45:59 +0200 | [diff] [blame] | 4324 | } |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 4325 | |
| 4326 | retval = PyLong_FromSsize_t(ctx.result); |
| 4327 | |
| 4328 | finally: |
| 4329 | PyMem_Free(controlbuf); |
| 4330 | if (iv.buf != NULL) { |
| 4331 | PyBuffer_Release(&iv); |
| 4332 | } |
Victor Stinner | 52d61e4 | 2016-09-12 11:41:58 +0200 | [diff] [blame] | 4333 | PyMem_Free(msg.msg_iov); |
| 4334 | for (i = 0; i < ndatabufs; i++) { |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 4335 | PyBuffer_Release(&databufs[i]); |
Victor Stinner | 52d61e4 | 2016-09-12 11:41:58 +0200 | [diff] [blame] | 4336 | } |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 4337 | PyMem_Free(databufs); |
| 4338 | return retval; |
| 4339 | } |
| 4340 | |
| 4341 | PyDoc_STRVAR(sendmsg_afalg_doc, |
| 4342 | "sendmsg_afalg([msg], *, op[, iv[, assoclen[, flags=MSG_MORE]]])\n\ |
| 4343 | \n\ |
| 4344 | Set operation mode, IV and length of associated data for an AF_ALG\n\ |
| 4345 | operation socket."); |
| 4346 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4347 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4348 | /* s.shutdown(how) method */ |
| 4349 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4350 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4351 | sock_shutdown(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4352 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4353 | int how; |
| 4354 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 4355 | |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 4356 | how = _PyLong_AsInt(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4357 | if (how == -1 && PyErr_Occurred()) |
| 4358 | return NULL; |
| 4359 | Py_BEGIN_ALLOW_THREADS |
| 4360 | res = shutdown(s->sock_fd, how); |
| 4361 | Py_END_ALLOW_THREADS |
| 4362 | if (res < 0) |
| 4363 | return s->errorhandler(); |
| 4364 | Py_INCREF(Py_None); |
| 4365 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4366 | } |
| 4367 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4368 | PyDoc_STRVAR(shutdown_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4369 | "shutdown(flag)\n\ |
| 4370 | \n\ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 4371 | Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\ |
| 4372 | 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] | 4373 | |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 4374 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 4375 | static PyObject* |
| 4376 | sock_ioctl(PySocketSockObject *s, PyObject *arg) |
| 4377 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4378 | unsigned long cmd = SIO_RCVALL; |
| 4379 | PyObject *argO; |
| 4380 | DWORD recv; |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 4381 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4382 | if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO)) |
| 4383 | return NULL; |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 4384 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4385 | switch (cmd) { |
| 4386 | case SIO_RCVALL: { |
| 4387 | unsigned int option = RCVALL_ON; |
| 4388 | if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option)) |
| 4389 | return NULL; |
| 4390 | if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option), |
| 4391 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 4392 | return set_error(); |
| 4393 | } |
| 4394 | return PyLong_FromUnsignedLong(recv); } |
| 4395 | case SIO_KEEPALIVE_VALS: { |
| 4396 | struct tcp_keepalive ka; |
| 4397 | if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd, |
| 4398 | &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval)) |
| 4399 | return NULL; |
| 4400 | if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka), |
| 4401 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 4402 | return set_error(); |
| 4403 | } |
| 4404 | return PyLong_FromUnsignedLong(recv); } |
Steve Dower | ea93ac0 | 2016-06-17 12:52:18 -0700 | [diff] [blame] | 4405 | #if defined(SIO_LOOPBACK_FAST_PATH) |
| 4406 | case SIO_LOOPBACK_FAST_PATH: { |
| 4407 | unsigned int option; |
| 4408 | if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option)) |
| 4409 | return NULL; |
| 4410 | if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option), |
| 4411 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 4412 | return set_error(); |
| 4413 | } |
| 4414 | return PyLong_FromUnsignedLong(recv); } |
| 4415 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4416 | default: |
| 4417 | PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd); |
| 4418 | return NULL; |
| 4419 | } |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 4420 | } |
| 4421 | PyDoc_STRVAR(sock_ioctl_doc, |
| 4422 | "ioctl(cmd, option) -> long\n\ |
| 4423 | \n\ |
Kristján Valur Jónsson | 847ec75 | 2009-09-27 21:10:38 +0000 | [diff] [blame] | 4424 | Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\ |
| 4425 | SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\ |
Steve Dower | ea93ac0 | 2016-06-17 12:52:18 -0700 | [diff] [blame] | 4426 | SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval).\n\ |
| 4427 | SIO_LOOPBACK_FAST_PATH: 'option' is a boolean value, and is disabled by default"); |
Kristján Valur Jónsson | 10f383a | 2012-04-07 11:23:31 +0000 | [diff] [blame] | 4428 | #endif |
| 4429 | |
| 4430 | #if defined(MS_WINDOWS) |
| 4431 | static PyObject* |
| 4432 | sock_share(PySocketSockObject *s, PyObject *arg) |
| 4433 | { |
| 4434 | WSAPROTOCOL_INFO info; |
| 4435 | DWORD processId; |
| 4436 | int result; |
| 4437 | |
| 4438 | if (!PyArg_ParseTuple(arg, "I", &processId)) |
| 4439 | return NULL; |
| 4440 | |
| 4441 | Py_BEGIN_ALLOW_THREADS |
| 4442 | result = WSADuplicateSocket(s->sock_fd, processId, &info); |
| 4443 | Py_END_ALLOW_THREADS |
| 4444 | if (result == SOCKET_ERROR) |
| 4445 | return set_error(); |
| 4446 | return PyBytes_FromStringAndSize((const char*)&info, sizeof(info)); |
| 4447 | } |
| 4448 | PyDoc_STRVAR(sock_share_doc, |
| 4449 | "share(process_id) -> bytes\n\ |
| 4450 | \n\ |
| 4451 | Share the socket with another process. The target process id\n\ |
| 4452 | must be provided and the resulting bytes object passed to the target\n\ |
| 4453 | process. There the shared socket can be instantiated by calling\n\ |
| 4454 | socket.fromshare()."); |
| 4455 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 4456 | |
| 4457 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4458 | |
| 4459 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4460 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4461 | static PyMethodDef sock_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4462 | {"_accept", (PyCFunction)sock_accept, METH_NOARGS, |
| 4463 | accept_doc}, |
| 4464 | {"bind", (PyCFunction)sock_bind, METH_O, |
| 4465 | bind_doc}, |
| 4466 | {"close", (PyCFunction)sock_close, METH_NOARGS, |
| 4467 | close_doc}, |
| 4468 | {"connect", (PyCFunction)sock_connect, METH_O, |
| 4469 | connect_doc}, |
| 4470 | {"connect_ex", (PyCFunction)sock_connect_ex, METH_O, |
| 4471 | connect_ex_doc}, |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 4472 | {"detach", (PyCFunction)sock_detach, METH_NOARGS, |
| 4473 | detach_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4474 | {"fileno", (PyCFunction)sock_fileno, METH_NOARGS, |
| 4475 | fileno_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4476 | #ifdef HAVE_GETPEERNAME |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4477 | {"getpeername", (PyCFunction)sock_getpeername, |
| 4478 | METH_NOARGS, getpeername_doc}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 4479 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4480 | {"getsockname", (PyCFunction)sock_getsockname, |
| 4481 | METH_NOARGS, getsockname_doc}, |
| 4482 | {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS, |
| 4483 | getsockopt_doc}, |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 4484 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4485 | {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS, |
| 4486 | sock_ioctl_doc}, |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 4487 | #endif |
Kristján Valur Jónsson | 10f383a | 2012-04-07 11:23:31 +0000 | [diff] [blame] | 4488 | #if defined(MS_WINDOWS) |
| 4489 | {"share", (PyCFunction)sock_share, METH_VARARGS, |
| 4490 | sock_share_doc}, |
| 4491 | #endif |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 4492 | {"listen", (PyCFunction)sock_listen, METH_VARARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4493 | listen_doc}, |
| 4494 | {"recv", (PyCFunction)sock_recv, METH_VARARGS, |
| 4495 | recv_doc}, |
| 4496 | {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS, |
| 4497 | recv_into_doc}, |
| 4498 | {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS, |
| 4499 | recvfrom_doc}, |
| 4500 | {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS, |
| 4501 | recvfrom_into_doc}, |
| 4502 | {"send", (PyCFunction)sock_send, METH_VARARGS, |
| 4503 | send_doc}, |
| 4504 | {"sendall", (PyCFunction)sock_sendall, METH_VARARGS, |
| 4505 | sendall_doc}, |
| 4506 | {"sendto", (PyCFunction)sock_sendto, METH_VARARGS, |
| 4507 | sendto_doc}, |
| 4508 | {"setblocking", (PyCFunction)sock_setblocking, METH_O, |
| 4509 | setblocking_doc}, |
| 4510 | {"settimeout", (PyCFunction)sock_settimeout, METH_O, |
| 4511 | settimeout_doc}, |
| 4512 | {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS, |
| 4513 | gettimeout_doc}, |
| 4514 | {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS, |
| 4515 | setsockopt_doc}, |
| 4516 | {"shutdown", (PyCFunction)sock_shutdown, METH_O, |
| 4517 | shutdown_doc}, |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4518 | #ifdef CMSG_LEN |
| 4519 | {"recvmsg", (PyCFunction)sock_recvmsg, METH_VARARGS, |
| 4520 | recvmsg_doc}, |
| 4521 | {"recvmsg_into", (PyCFunction)sock_recvmsg_into, METH_VARARGS, |
| 4522 | recvmsg_into_doc,}, |
| 4523 | {"sendmsg", (PyCFunction)sock_sendmsg, METH_VARARGS, |
| 4524 | sendmsg_doc}, |
| 4525 | #endif |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 4526 | #ifdef HAVE_SOCKADDR_ALG |
| 4527 | {"sendmsg_afalg", (PyCFunction)sock_sendmsg_afalg, METH_VARARGS | METH_KEYWORDS, |
| 4528 | sendmsg_afalg_doc}, |
| 4529 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4530 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4531 | }; |
| 4532 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4533 | /* SockObject members */ |
| 4534 | static PyMemberDef sock_memberlist[] = { |
| 4535 | {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"}, |
| 4536 | {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"}, |
| 4537 | {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4538 | {0}, |
| 4539 | }; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4540 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 4541 | static PyGetSetDef sock_getsetlist[] = { |
| 4542 | {"timeout", (getter)sock_gettimeout, NULL, PyDoc_STR("the socket timeout")}, |
| 4543 | {NULL} /* sentinel */ |
| 4544 | }; |
| 4545 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4546 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4547 | First close the file description. */ |
| 4548 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4549 | static void |
Victor Stinner | 19a8e84 | 2016-03-21 16:36:48 +0100 | [diff] [blame] | 4550 | sock_finalize(PySocketSockObject *s) |
| 4551 | { |
| 4552 | SOCKET_T fd; |
| 4553 | PyObject *error_type, *error_value, *error_traceback; |
| 4554 | |
| 4555 | /* Save the current exception, if any. */ |
| 4556 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 4557 | |
Victor Stinner | d3afb62 | 2016-07-22 17:47:09 +0200 | [diff] [blame] | 4558 | if (s->sock_fd != INVALID_SOCKET) { |
Victor Stinner | 19a8e84 | 2016-03-21 16:36:48 +0100 | [diff] [blame] | 4559 | if (PyErr_ResourceWarning((PyObject *)s, 1, "unclosed %R", s)) { |
| 4560 | /* Spurious errors can appear at shutdown */ |
| 4561 | if (PyErr_ExceptionMatches(PyExc_Warning)) { |
| 4562 | PyErr_WriteUnraisable((PyObject *)s); |
| 4563 | } |
| 4564 | } |
| 4565 | |
| 4566 | /* Only close the socket *after* logging the ResourceWarning warning |
| 4567 | to allow the logger to call socket methods like |
| 4568 | socket.getsockname(). If the socket is closed before, socket |
| 4569 | methods fails with the EBADF error. */ |
| 4570 | fd = s->sock_fd; |
Victor Stinner | d3afb62 | 2016-07-22 17:47:09 +0200 | [diff] [blame] | 4571 | s->sock_fd = INVALID_SOCKET; |
Victor Stinner | 19a8e84 | 2016-03-21 16:36:48 +0100 | [diff] [blame] | 4572 | |
| 4573 | /* We do not want to retry upon EINTR: see sock_close() */ |
| 4574 | Py_BEGIN_ALLOW_THREADS |
| 4575 | (void) SOCKETCLOSE(fd); |
| 4576 | Py_END_ALLOW_THREADS |
| 4577 | } |
| 4578 | |
| 4579 | /* Restore the saved exception. */ |
| 4580 | PyErr_Restore(error_type, error_value, error_traceback); |
| 4581 | } |
| 4582 | |
| 4583 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4584 | sock_dealloc(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4585 | { |
Victor Stinner | 19a8e84 | 2016-03-21 16:36:48 +0100 | [diff] [blame] | 4586 | if (PyObject_CallFinalizerFromDealloc((PyObject *)s) < 0) |
| 4587 | return; |
| 4588 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4589 | Py_TYPE(s)->tp_free((PyObject *)s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4590 | } |
| 4591 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4592 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4593 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4594 | sock_repr(PySocketSockObject *s) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4595 | { |
Victor Stinner | e254e53 | 2014-07-26 14:36:55 +0200 | [diff] [blame] | 4596 | long sock_fd; |
| 4597 | /* On Windows, this test is needed because SOCKET_T is unsigned */ |
| 4598 | if (s->sock_fd == INVALID_SOCKET) { |
| 4599 | sock_fd = -1; |
| 4600 | } |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 4601 | #if SIZEOF_SOCKET_T > SIZEOF_LONG |
Victor Stinner | e254e53 | 2014-07-26 14:36:55 +0200 | [diff] [blame] | 4602 | else if (s->sock_fd > LONG_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4603 | /* this can occur on Win64, and actually there is a special |
| 4604 | ugly printf formatter for decimal pointer length integer |
| 4605 | printing, only bother if necessary*/ |
| 4606 | PyErr_SetString(PyExc_OverflowError, |
| 4607 | "no printf formatter to display " |
| 4608 | "the socket descriptor in decimal"); |
| 4609 | return NULL; |
| 4610 | } |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 4611 | #endif |
Victor Stinner | e254e53 | 2014-07-26 14:36:55 +0200 | [diff] [blame] | 4612 | else |
| 4613 | sock_fd = (long)s->sock_fd; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4614 | return PyUnicode_FromFormat( |
| 4615 | "<socket object, fd=%ld, family=%d, type=%d, proto=%d>", |
Victor Stinner | e254e53 | 2014-07-26 14:36:55 +0200 | [diff] [blame] | 4616 | sock_fd, s->sock_family, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4617 | s->sock_type, |
| 4618 | s->sock_proto); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4619 | } |
| 4620 | |
| 4621 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4622 | /* Create a new, uninitialized socket object. */ |
| 4623 | |
| 4624 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4625 | sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4626 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4627 | PyObject *new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4629 | new = type->tp_alloc(type, 0); |
| 4630 | if (new != NULL) { |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 4631 | ((PySocketSockObject *)new)->sock_fd = INVALID_SOCKET; |
Victor Stinner | 9001d80 | 2015-04-06 23:06:01 +0200 | [diff] [blame] | 4632 | ((PySocketSockObject *)new)->sock_timeout = _PyTime_FromSeconds(-1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4633 | ((PySocketSockObject *)new)->errorhandler = &set_error; |
| 4634 | } |
| 4635 | return new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4636 | } |
| 4637 | |
| 4638 | |
| 4639 | /* Initialize a new socket object. */ |
| 4640 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4641 | #ifdef SOCK_CLOEXEC |
| 4642 | /* socket() and socketpair() fail with EINVAL on Linux kernel older |
| 4643 | * than 2.6.27 if SOCK_CLOEXEC flag is set in the socket type. */ |
| 4644 | static int sock_cloexec_works = -1; |
| 4645 | #endif |
| 4646 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4647 | /*ARGSUSED*/ |
| 4648 | static int |
Andrew MacIntyre | 7aec4a2 | 2002-06-13 11:53:52 +0000 | [diff] [blame] | 4649 | sock_initobj(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4650 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4651 | PySocketSockObject *s = (PySocketSockObject *)self; |
| 4652 | PyObject *fdobj = NULL; |
| 4653 | SOCKET_T fd = INVALID_SOCKET; |
| 4654 | int family = AF_INET, type = SOCK_STREAM, proto = 0; |
| 4655 | static char *keywords[] = {"family", "type", "proto", "fileno", 0}; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4656 | #ifndef MS_WINDOWS |
| 4657 | #ifdef SOCK_CLOEXEC |
| 4658 | int *atomic_flag_works = &sock_cloexec_works; |
| 4659 | #else |
| 4660 | int *atomic_flag_works = NULL; |
| 4661 | #endif |
| 4662 | #endif |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4663 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4664 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 4665 | "|iiiO:socket", keywords, |
| 4666 | &family, &type, &proto, &fdobj)) |
| 4667 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4668 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4669 | if (fdobj != NULL && fdobj != Py_None) { |
Kristján Valur Jónsson | 10f383a | 2012-04-07 11:23:31 +0000 | [diff] [blame] | 4670 | #ifdef MS_WINDOWS |
| 4671 | /* recreate a socket that was duplicated */ |
| 4672 | if (PyBytes_Check(fdobj)) { |
| 4673 | WSAPROTOCOL_INFO info; |
| 4674 | if (PyBytes_GET_SIZE(fdobj) != sizeof(info)) { |
| 4675 | PyErr_Format(PyExc_ValueError, |
| 4676 | "socket descriptor string has wrong size, " |
| 4677 | "should be %zu bytes.", sizeof(info)); |
| 4678 | return -1; |
| 4679 | } |
| 4680 | memcpy(&info, PyBytes_AS_STRING(fdobj), sizeof(info)); |
| 4681 | Py_BEGIN_ALLOW_THREADS |
| 4682 | fd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, |
| 4683 | FROM_PROTOCOL_INFO, &info, 0, WSA_FLAG_OVERLAPPED); |
| 4684 | Py_END_ALLOW_THREADS |
| 4685 | if (fd == INVALID_SOCKET) { |
| 4686 | set_error(); |
| 4687 | return -1; |
| 4688 | } |
| 4689 | family = info.iAddressFamily; |
| 4690 | type = info.iSocketType; |
| 4691 | proto = info.iProtocol; |
| 4692 | } |
| 4693 | else |
| 4694 | #endif |
| 4695 | { |
| 4696 | fd = PyLong_AsSocket_t(fdobj); |
| 4697 | if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
| 4698 | return -1; |
| 4699 | if (fd == INVALID_SOCKET) { |
| 4700 | PyErr_SetString(PyExc_ValueError, |
| 4701 | "can't use invalid socket value"); |
| 4702 | return -1; |
| 4703 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4704 | } |
| 4705 | } |
| 4706 | else { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4707 | #ifdef MS_WINDOWS |
| 4708 | /* Windows implementation */ |
| 4709 | #ifndef WSA_FLAG_NO_HANDLE_INHERIT |
| 4710 | #define WSA_FLAG_NO_HANDLE_INHERIT 0x80 |
| 4711 | #endif |
| 4712 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4713 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4714 | if (support_wsa_no_inherit) { |
| 4715 | fd = WSASocket(family, type, proto, |
| 4716 | NULL, 0, |
| 4717 | WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT); |
| 4718 | if (fd == INVALID_SOCKET) { |
| 4719 | /* Windows 7 or Windows 2008 R2 without SP1 or the hotfix */ |
| 4720 | support_wsa_no_inherit = 0; |
| 4721 | fd = socket(family, type, proto); |
| 4722 | } |
| 4723 | } |
| 4724 | else { |
| 4725 | fd = socket(family, type, proto); |
| 4726 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4727 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4729 | if (fd == INVALID_SOCKET) { |
| 4730 | set_error(); |
| 4731 | return -1; |
| 4732 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4733 | |
| 4734 | if (!support_wsa_no_inherit) { |
| 4735 | if (!SetHandleInformation((HANDLE)fd, HANDLE_FLAG_INHERIT, 0)) { |
| 4736 | closesocket(fd); |
| 4737 | PyErr_SetFromWindowsErr(0); |
| 4738 | return -1; |
| 4739 | } |
| 4740 | } |
| 4741 | #else |
| 4742 | /* UNIX */ |
| 4743 | Py_BEGIN_ALLOW_THREADS |
| 4744 | #ifdef SOCK_CLOEXEC |
| 4745 | if (sock_cloexec_works != 0) { |
| 4746 | fd = socket(family, type | SOCK_CLOEXEC, proto); |
| 4747 | if (sock_cloexec_works == -1) { |
| 4748 | if (fd >= 0) { |
| 4749 | sock_cloexec_works = 1; |
| 4750 | } |
| 4751 | else if (errno == EINVAL) { |
| 4752 | /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */ |
| 4753 | sock_cloexec_works = 0; |
| 4754 | fd = socket(family, type, proto); |
| 4755 | } |
| 4756 | } |
| 4757 | } |
| 4758 | else |
| 4759 | #endif |
| 4760 | { |
| 4761 | fd = socket(family, type, proto); |
| 4762 | } |
| 4763 | Py_END_ALLOW_THREADS |
| 4764 | |
| 4765 | if (fd == INVALID_SOCKET) { |
| 4766 | set_error(); |
| 4767 | return -1; |
| 4768 | } |
| 4769 | |
| 4770 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 4771 | SOCKETCLOSE(fd); |
| 4772 | return -1; |
| 4773 | } |
| 4774 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4775 | } |
Christian Heimes | 0b9d64e | 2016-09-09 00:28:57 +0200 | [diff] [blame] | 4776 | if (init_sockobject(s, fd, family, type, proto) == -1) { |
| 4777 | SOCKETCLOSE(fd); |
| 4778 | return -1; |
| 4779 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4781 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4782 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4783 | } |
| 4784 | |
| 4785 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4786 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4787 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4788 | static PyTypeObject sock_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4789 | PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */ |
| 4790 | "_socket.socket", /* tp_name */ |
| 4791 | sizeof(PySocketSockObject), /* tp_basicsize */ |
| 4792 | 0, /* tp_itemsize */ |
| 4793 | (destructor)sock_dealloc, /* tp_dealloc */ |
| 4794 | 0, /* tp_print */ |
| 4795 | 0, /* tp_getattr */ |
| 4796 | 0, /* tp_setattr */ |
| 4797 | 0, /* tp_reserved */ |
| 4798 | (reprfunc)sock_repr, /* tp_repr */ |
| 4799 | 0, /* tp_as_number */ |
| 4800 | 0, /* tp_as_sequence */ |
| 4801 | 0, /* tp_as_mapping */ |
| 4802 | 0, /* tp_hash */ |
| 4803 | 0, /* tp_call */ |
| 4804 | 0, /* tp_str */ |
| 4805 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4806 | 0, /* tp_setattro */ |
| 4807 | 0, /* tp_as_buffer */ |
Victor Stinner | 19a8e84 | 2016-03-21 16:36:48 +0100 | [diff] [blame] | 4808 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
| 4809 | | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4810 | sock_doc, /* tp_doc */ |
| 4811 | 0, /* tp_traverse */ |
| 4812 | 0, /* tp_clear */ |
| 4813 | 0, /* tp_richcompare */ |
| 4814 | 0, /* tp_weaklistoffset */ |
| 4815 | 0, /* tp_iter */ |
| 4816 | 0, /* tp_iternext */ |
| 4817 | sock_methods, /* tp_methods */ |
| 4818 | sock_memberlist, /* tp_members */ |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 4819 | sock_getsetlist, /* tp_getset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4820 | 0, /* tp_base */ |
| 4821 | 0, /* tp_dict */ |
| 4822 | 0, /* tp_descr_get */ |
| 4823 | 0, /* tp_descr_set */ |
| 4824 | 0, /* tp_dictoffset */ |
| 4825 | sock_initobj, /* tp_init */ |
| 4826 | PyType_GenericAlloc, /* tp_alloc */ |
| 4827 | sock_new, /* tp_new */ |
| 4828 | PyObject_Del, /* tp_free */ |
Victor Stinner | 19a8e84 | 2016-03-21 16:36:48 +0100 | [diff] [blame] | 4829 | 0, /* tp_is_gc */ |
| 4830 | 0, /* tp_bases */ |
| 4831 | 0, /* tp_mro */ |
| 4832 | 0, /* tp_cache */ |
| 4833 | 0, /* tp_subclasses */ |
| 4834 | 0, /* tp_weaklist */ |
| 4835 | 0, /* tp_del */ |
| 4836 | 0, /* tp_version_tag */ |
| 4837 | (destructor)sock_finalize, /* tp_finalize */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4838 | }; |
| 4839 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4840 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 4841 | /* Python interface to gethostname(). */ |
| 4842 | |
| 4843 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4844 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4845 | socket_gethostname(PyObject *self, PyObject *unused) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 4846 | { |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4847 | #ifdef MS_WINDOWS |
| 4848 | /* Don't use winsock's gethostname, as this returns the ANSI |
| 4849 | version of the hostname, whereas we need a Unicode string. |
| 4850 | Otherwise, gethostname apparently also returns the DNS name. */ |
Hirokazu Yamamoto | 09fff7a | 2010-11-05 17:24:13 +0000 | [diff] [blame] | 4851 | wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 4852 | DWORD size = Py_ARRAY_LENGTH(buf); |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4853 | wchar_t *name; |
Hirokazu Yamamoto | 09fff7a | 2010-11-05 17:24:13 +0000 | [diff] [blame] | 4854 | PyObject *result; |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4855 | |
| 4856 | if (GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size)) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 4857 | return PyUnicode_FromWideChar(buf, size); |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4858 | |
| 4859 | if (GetLastError() != ERROR_MORE_DATA) |
| 4860 | return PyErr_SetFromWindowsErr(0); |
| 4861 | |
| 4862 | if (size == 0) |
| 4863 | return PyUnicode_New(0, 0); |
| 4864 | |
| 4865 | /* MSDN says ERROR_MORE_DATA may occur because DNS allows longer |
| 4866 | names */ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 4867 | name = PyMem_New(wchar_t, size); |
| 4868 | if (!name) { |
| 4869 | PyErr_NoMemory(); |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4870 | return NULL; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 4871 | } |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4872 | if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname, |
| 4873 | name, |
| 4874 | &size)) |
| 4875 | { |
| 4876 | PyMem_Free(name); |
| 4877 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4878 | } |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4879 | |
| 4880 | result = PyUnicode_FromWideChar(name, size); |
| 4881 | PyMem_Free(name); |
| 4882 | return result; |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4883 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4884 | char buf[1024]; |
| 4885 | int res; |
| 4886 | Py_BEGIN_ALLOW_THREADS |
| 4887 | res = gethostname(buf, (int) sizeof buf - 1); |
| 4888 | Py_END_ALLOW_THREADS |
| 4889 | if (res < 0) |
| 4890 | return set_error(); |
| 4891 | buf[sizeof buf - 1] = '\0'; |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4892 | return PyUnicode_DecodeFSDefault(buf); |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4893 | #endif |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 4894 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 4895 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4896 | PyDoc_STRVAR(gethostname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4897 | "gethostname() -> string\n\ |
| 4898 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4899 | Return the current host name."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4900 | |
Antoine Pitrou | 061cfb5 | 2011-02-28 22:25:22 +0000 | [diff] [blame] | 4901 | #ifdef HAVE_SETHOSTNAME |
| 4902 | PyDoc_STRVAR(sethostname_doc, |
| 4903 | "sethostname(name)\n\n\ |
| 4904 | Sets the hostname to name."); |
| 4905 | |
| 4906 | static PyObject * |
| 4907 | socket_sethostname(PyObject *self, PyObject *args) |
| 4908 | { |
| 4909 | PyObject *hnobj; |
| 4910 | Py_buffer buf; |
| 4911 | int res, flag = 0; |
| 4912 | |
Christian Heimes | d2774c7 | 2013-06-19 02:06:29 +0200 | [diff] [blame] | 4913 | #ifdef _AIX |
| 4914 | /* issue #18259, not declared in any useful header file */ |
| 4915 | extern int sethostname(const char *, size_t); |
| 4916 | #endif |
| 4917 | |
Antoine Pitrou | 061cfb5 | 2011-02-28 22:25:22 +0000 | [diff] [blame] | 4918 | if (!PyArg_ParseTuple(args, "S:sethostname", &hnobj)) { |
| 4919 | PyErr_Clear(); |
| 4920 | if (!PyArg_ParseTuple(args, "O&:sethostname", |
| 4921 | PyUnicode_FSConverter, &hnobj)) |
| 4922 | return NULL; |
| 4923 | flag = 1; |
| 4924 | } |
| 4925 | res = PyObject_GetBuffer(hnobj, &buf, PyBUF_SIMPLE); |
| 4926 | if (!res) { |
| 4927 | res = sethostname(buf.buf, buf.len); |
| 4928 | PyBuffer_Release(&buf); |
| 4929 | } |
| 4930 | if (flag) |
| 4931 | Py_DECREF(hnobj); |
| 4932 | if (res) |
| 4933 | return set_error(); |
| 4934 | Py_RETURN_NONE; |
| 4935 | } |
| 4936 | #endif |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 4937 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4938 | /* Python interface to gethostbyname(name). */ |
| 4939 | |
| 4940 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4941 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4942 | socket_gethostbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4943 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4944 | char *name; |
| 4945 | sock_addr_t addrbuf; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4946 | PyObject *ret = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4947 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4948 | if (!PyArg_ParseTuple(args, "et:gethostbyname", "idna", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4949 | return NULL; |
| 4950 | if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4951 | goto finally; |
| 4952 | ret = makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in)); |
| 4953 | finally: |
| 4954 | PyMem_Free(name); |
| 4955 | return ret; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4956 | } |
| 4957 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4958 | PyDoc_STRVAR(gethostbyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4959 | "gethostbyname(host) -> address\n\ |
| 4960 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4961 | 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] | 4962 | |
| 4963 | |
Victor Stinner | 7240030 | 2016-01-28 15:41:01 +0100 | [diff] [blame] | 4964 | static PyObject* |
| 4965 | sock_decode_hostname(const char *name) |
| 4966 | { |
| 4967 | #ifdef MS_WINDOWS |
| 4968 | /* Issue #26227: gethostbyaddr() returns a string encoded |
| 4969 | * to the ANSI code page */ |
| 4970 | return PyUnicode_DecodeFSDefault(name); |
| 4971 | #else |
| 4972 | /* Decode from UTF-8 */ |
| 4973 | return PyUnicode_FromString(name); |
| 4974 | #endif |
| 4975 | } |
| 4976 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4977 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
| 4978 | |
| 4979 | static PyObject * |
Victor Stinner | 1a62a68 | 2014-08-17 19:33:28 +0200 | [diff] [blame] | 4980 | gethost_common(struct hostent *h, struct sockaddr *addr, size_t alen, int af) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4981 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4982 | char **pch; |
| 4983 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 4984 | PyObject *name_list = (PyObject *)NULL; |
| 4985 | PyObject *addr_list = (PyObject *)NULL; |
| 4986 | PyObject *tmp; |
Victor Stinner | 7240030 | 2016-01-28 15:41:01 +0100 | [diff] [blame] | 4987 | PyObject *name; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4988 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4989 | if (h == NULL) { |
| 4990 | /* Let's get real error message to return */ |
| 4991 | set_herror(h_errno); |
| 4992 | return NULL; |
| 4993 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4994 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4995 | if (h->h_addrtype != af) { |
| 4996 | /* Let's get real error message to return */ |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 4997 | errno = EAFNOSUPPORT; |
| 4998 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4999 | return NULL; |
| 5000 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5001 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5002 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5003 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5004 | case AF_INET: |
| 5005 | if (alen < sizeof(struct sockaddr_in)) |
| 5006 | return NULL; |
| 5007 | break; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5008 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 5009 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5010 | case AF_INET6: |
| 5011 | if (alen < sizeof(struct sockaddr_in6)) |
| 5012 | return NULL; |
| 5013 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5014 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5016 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5017 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5018 | if ((name_list = PyList_New(0)) == NULL) |
| 5019 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5020 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5021 | if ((addr_list = PyList_New(0)) == NULL) |
| 5022 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5023 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5024 | /* SF #1511317: h_aliases can be NULL */ |
| 5025 | if (h->h_aliases) { |
| 5026 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 5027 | int status; |
| 5028 | tmp = PyUnicode_FromString(*pch); |
| 5029 | if (tmp == NULL) |
| 5030 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5032 | status = PyList_Append(name_list, tmp); |
| 5033 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5034 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5035 | if (status) |
| 5036 | goto err; |
| 5037 | } |
| 5038 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5039 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5040 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 5041 | int status; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5042 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5043 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5044 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5045 | case AF_INET: |
| 5046 | { |
| 5047 | struct sockaddr_in sin; |
| 5048 | memset(&sin, 0, sizeof(sin)); |
| 5049 | sin.sin_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5050 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5051 | sin.sin_len = sizeof(sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5052 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5053 | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); |
| 5054 | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5055 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5056 | if (pch == h->h_addr_list && alen >= sizeof(sin)) |
| 5057 | memcpy((char *) addr, &sin, sizeof(sin)); |
| 5058 | break; |
| 5059 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5060 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 5061 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5062 | case AF_INET6: |
| 5063 | { |
| 5064 | struct sockaddr_in6 sin6; |
| 5065 | memset(&sin6, 0, sizeof(sin6)); |
| 5066 | sin6.sin6_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5067 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5068 | sin6.sin6_len = sizeof(sin6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5069 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5070 | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); |
| 5071 | tmp = makeipaddr((struct sockaddr *)&sin6, |
| 5072 | sizeof(sin6)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5073 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5074 | if (pch == h->h_addr_list && alen >= sizeof(sin6)) |
| 5075 | memcpy((char *) addr, &sin6, sizeof(sin6)); |
| 5076 | break; |
| 5077 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5078 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5079 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5080 | default: /* can't happen */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5081 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5082 | "unsupported address family"); |
| 5083 | return NULL; |
| 5084 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5085 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5086 | if (tmp == NULL) |
| 5087 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5088 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5089 | status = PyList_Append(addr_list, tmp); |
| 5090 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5091 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5092 | if (status) |
| 5093 | goto err; |
| 5094 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5095 | |
Victor Stinner | 7240030 | 2016-01-28 15:41:01 +0100 | [diff] [blame] | 5096 | name = sock_decode_hostname(h->h_name); |
| 5097 | if (name == NULL) |
| 5098 | goto err; |
| 5099 | rtn_tuple = Py_BuildValue("NOO", name, name_list, addr_list); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5100 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 5101 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5102 | Py_XDECREF(name_list); |
| 5103 | Py_XDECREF(addr_list); |
| 5104 | return rtn_tuple; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 5105 | } |
| 5106 | |
| 5107 | |
| 5108 | /* Python interface to gethostbyname_ex(name). */ |
| 5109 | |
| 5110 | /*ARGSUSED*/ |
| 5111 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5112 | socket_gethostbyname_ex(PyObject *self, PyObject *args) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 5113 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5114 | char *name; |
| 5115 | struct hostent *h; |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 5116 | sock_addr_t addr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5117 | struct sockaddr *sa; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 5118 | PyObject *ret = NULL; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 5119 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5120 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 5121 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5122 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 5123 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5124 | char buf[16384]; |
| 5125 | int buf_len = (sizeof buf) - 1; |
| 5126 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 5127 | #endif |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 5128 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5129 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 5130 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 5131 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5132 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 5133 | if (!PyArg_ParseTuple(args, "et:gethostbyname_ex", "idna", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5134 | return NULL; |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 5135 | if (setipaddr(name, SAS2SA(&addr), sizeof(addr), AF_INET) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 5136 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5137 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 5138 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 5139 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 5140 | gethostbyname_r(name, &hp_allocated, buf, buf_len, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5141 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 5142 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5143 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 5144 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5145 | memset((void *) &data, '\0', sizeof(data)); |
| 5146 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 5147 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 5148 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 5149 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 5150 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5151 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 5152 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5153 | h = gethostbyname(name); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 5154 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5155 | Py_END_ALLOW_THREADS |
| 5156 | /* Some C libraries would require addr.__ss_family instead of |
| 5157 | addr.ss_family. |
| 5158 | Therefore, we cast the sockaddr_storage into sockaddr to |
| 5159 | access sa_family. */ |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 5160 | sa = SAS2SA(&addr); |
| 5161 | ret = gethost_common(h, SAS2SA(&addr), sizeof(addr), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5162 | sa->sa_family); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 5163 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5164 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 5165 | #endif |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 5166 | finally: |
| 5167 | PyMem_Free(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5168 | return ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 5169 | } |
| 5170 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5171 | PyDoc_STRVAR(ghbn_ex_doc, |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 5172 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
| 5173 | \n\ |
| 5174 | 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] | 5175 | 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] | 5176 | |
| 5177 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 5178 | /* Python interface to gethostbyaddr(IP). */ |
| 5179 | |
| 5180 | /*ARGSUSED*/ |
| 5181 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5182 | socket_gethostbyaddr(PyObject *self, PyObject *args) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 5183 | { |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 5184 | sock_addr_t addr; |
| 5185 | struct sockaddr *sa = SAS2SA(&addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5186 | char *ip_num; |
| 5187 | struct hostent *h; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 5188 | PyObject *ret = NULL; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 5189 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5190 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 5191 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5192 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 5193 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5194 | /* glibcs up to 2.10 assume that the buf argument to |
| 5195 | gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc |
| 5196 | does not ensure. The attribute below instructs the compiler |
| 5197 | to maintain this alignment. */ |
| 5198 | char buf[16384] Py_ALIGNED(8); |
| 5199 | int buf_len = (sizeof buf) - 1; |
| 5200 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 5201 | #endif |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 5202 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5203 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 5204 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 5205 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5206 | char *ap; |
| 5207 | int al; |
| 5208 | int af; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 5209 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 5210 | if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5211 | return NULL; |
| 5212 | af = AF_UNSPEC; |
| 5213 | if (setipaddr(ip_num, sa, sizeof(addr), af) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 5214 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5215 | af = sa->sa_family; |
| 5216 | ap = NULL; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 5217 | /* al = 0; */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5218 | switch (af) { |
| 5219 | case AF_INET: |
| 5220 | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr; |
| 5221 | al = sizeof(((struct sockaddr_in *)sa)->sin_addr); |
| 5222 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 5223 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5224 | case AF_INET6: |
| 5225 | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr; |
| 5226 | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); |
| 5227 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5228 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5229 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5230 | PyErr_SetString(PyExc_OSError, "unsupported address family"); |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 5231 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5232 | } |
| 5233 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 5234 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 5235 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 5236 | gethostbyaddr_r(ap, al, af, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5237 | &hp_allocated, buf, buf_len, |
| 5238 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 5239 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5240 | h = gethostbyaddr_r(ap, al, af, |
| 5241 | &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 5242 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5243 | memset((void *) &data, '\0', sizeof(data)); |
| 5244 | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); |
| 5245 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 5246 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 5247 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 5248 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5249 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 5250 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5251 | h = gethostbyaddr(ap, al, af); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 5252 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5253 | Py_END_ALLOW_THREADS |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 5254 | ret = gethost_common(h, SAS2SA(&addr), sizeof(addr), af); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 5255 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5256 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 5257 | #endif |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 5258 | finally: |
| 5259 | PyMem_Free(ip_num); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5260 | return ret; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 5261 | } |
| 5262 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5263 | PyDoc_STRVAR(gethostbyaddr_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5264 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
| 5265 | \n\ |
| 5266 | 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] | 5267 | 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] | 5268 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 5269 | |
| 5270 | /* Python interface to getservbyname(name). |
| 5271 | This only returns the port number, since the other info is already |
| 5272 | known or not useful (like the list of aliases). */ |
| 5273 | |
| 5274 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 5275 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5276 | socket_getservbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 5277 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5278 | char *name, *proto=NULL; |
| 5279 | struct servent *sp; |
| 5280 | if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto)) |
| 5281 | return NULL; |
| 5282 | Py_BEGIN_ALLOW_THREADS |
| 5283 | sp = getservbyname(name, proto); |
| 5284 | Py_END_ALLOW_THREADS |
| 5285 | if (sp == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5286 | PyErr_SetString(PyExc_OSError, "service/proto not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5287 | return NULL; |
| 5288 | } |
| 5289 | return PyLong_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 5290 | } |
| 5291 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5292 | PyDoc_STRVAR(getservbyname_doc, |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 5293 | "getservbyname(servicename[, protocolname]) -> integer\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5294 | \n\ |
| 5295 | Return a port number from a service name and protocol name.\n\ |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 5296 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 5297 | otherwise any protocol will match."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5298 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 5299 | |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 5300 | /* Python interface to getservbyport(port). |
| 5301 | This only returns the service name, since the other info is already |
| 5302 | known or not useful (like the list of aliases). */ |
| 5303 | |
| 5304 | /*ARGSUSED*/ |
| 5305 | static PyObject * |
| 5306 | socket_getservbyport(PyObject *self, PyObject *args) |
| 5307 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5308 | int port; |
| 5309 | char *proto=NULL; |
| 5310 | struct servent *sp; |
| 5311 | if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto)) |
| 5312 | return NULL; |
| 5313 | if (port < 0 || port > 0xffff) { |
| 5314 | PyErr_SetString( |
| 5315 | PyExc_OverflowError, |
| 5316 | "getservbyport: port must be 0-65535."); |
| 5317 | return NULL; |
| 5318 | } |
| 5319 | Py_BEGIN_ALLOW_THREADS |
| 5320 | sp = getservbyport(htons((short)port), proto); |
| 5321 | Py_END_ALLOW_THREADS |
| 5322 | if (sp == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5323 | PyErr_SetString(PyExc_OSError, "port/proto not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5324 | return NULL; |
| 5325 | } |
| 5326 | return PyUnicode_FromString(sp->s_name); |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 5327 | } |
| 5328 | |
| 5329 | PyDoc_STRVAR(getservbyport_doc, |
| 5330 | "getservbyport(port[, protocolname]) -> string\n\ |
| 5331 | \n\ |
| 5332 | Return the service name from a port number and protocol name.\n\ |
| 5333 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 5334 | otherwise any protocol will match."); |
| 5335 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 5336 | /* Python interface to getprotobyname(name). |
| 5337 | This only returns the protocol number, since the other info is |
| 5338 | already known or not useful (like the list of aliases). */ |
| 5339 | |
| 5340 | /*ARGSUSED*/ |
| 5341 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5342 | socket_getprotobyname(PyObject *self, PyObject *args) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 5343 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5344 | char *name; |
| 5345 | struct protoent *sp; |
| 5346 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) |
| 5347 | return NULL; |
| 5348 | Py_BEGIN_ALLOW_THREADS |
| 5349 | sp = getprotobyname(name); |
| 5350 | Py_END_ALLOW_THREADS |
| 5351 | if (sp == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5352 | PyErr_SetString(PyExc_OSError, "protocol not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5353 | return NULL; |
| 5354 | } |
| 5355 | return PyLong_FromLong((long) sp->p_proto); |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 5356 | } |
| 5357 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5358 | PyDoc_STRVAR(getprotobyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5359 | "getprotobyname(name) -> integer\n\ |
| 5360 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5361 | Return the protocol number for the named protocol. (Rarely used.)"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5362 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 5363 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 5364 | #ifndef NO_DUP |
| 5365 | /* dup() function for socket fds */ |
| 5366 | |
| 5367 | static PyObject * |
| 5368 | socket_dup(PyObject *self, PyObject *fdobj) |
| 5369 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5370 | SOCKET_T fd, newfd; |
| 5371 | PyObject *newfdobj; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5372 | #ifdef MS_WINDOWS |
| 5373 | WSAPROTOCOL_INFO info; |
| 5374 | #endif |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 5375 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5376 | fd = PyLong_AsSocket_t(fdobj); |
| 5377 | if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
| 5378 | return NULL; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 5379 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5380 | #ifdef MS_WINDOWS |
| 5381 | if (WSADuplicateSocket(fd, GetCurrentProcessId(), &info)) |
| 5382 | return set_error(); |
| 5383 | |
| 5384 | newfd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, |
| 5385 | FROM_PROTOCOL_INFO, |
| 5386 | &info, 0, WSA_FLAG_OVERLAPPED); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5387 | if (newfd == INVALID_SOCKET) |
| 5388 | return set_error(); |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 5389 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5390 | if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) { |
| 5391 | closesocket(newfd); |
| 5392 | PyErr_SetFromWindowsErr(0); |
| 5393 | return NULL; |
| 5394 | } |
| 5395 | #else |
| 5396 | /* On UNIX, dup can be used to duplicate the file descriptor of a socket */ |
| 5397 | newfd = _Py_dup(fd); |
| 5398 | if (newfd == INVALID_SOCKET) |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5399 | return NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5400 | #endif |
| 5401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5402 | newfdobj = PyLong_FromSocket_t(newfd); |
| 5403 | if (newfdobj == NULL) |
| 5404 | SOCKETCLOSE(newfd); |
| 5405 | return newfdobj; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 5406 | } |
| 5407 | |
| 5408 | PyDoc_STRVAR(dup_doc, |
| 5409 | "dup(integer) -> integer\n\ |
| 5410 | \n\ |
| 5411 | Duplicate an integer socket file descriptor. This is like os.dup(), but for\n\ |
| 5412 | sockets; on some platforms os.dup() won't work for socket file descriptors."); |
| 5413 | #endif |
| 5414 | |
| 5415 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 5416 | #ifdef HAVE_SOCKETPAIR |
| 5417 | /* Create a pair of sockets using the socketpair() function. |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 5418 | Arguments as for socket() except the default family is AF_UNIX if |
Dave Cole | e8bbfe4 | 2004-08-26 00:51:16 +0000 | [diff] [blame] | 5419 | defined on the platform; otherwise, the default is AF_INET. */ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 5420 | |
| 5421 | /*ARGSUSED*/ |
| 5422 | static PyObject * |
| 5423 | socket_socketpair(PyObject *self, PyObject *args) |
| 5424 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5425 | PySocketSockObject *s0 = NULL, *s1 = NULL; |
| 5426 | SOCKET_T sv[2]; |
| 5427 | int family, type = SOCK_STREAM, proto = 0; |
| 5428 | PyObject *res = NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5429 | #ifdef SOCK_CLOEXEC |
| 5430 | int *atomic_flag_works = &sock_cloexec_works; |
| 5431 | #else |
| 5432 | int *atomic_flag_works = NULL; |
| 5433 | #endif |
| 5434 | int ret; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 5435 | |
| 5436 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5437 | family = AF_UNIX; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 5438 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5439 | family = AF_INET; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 5440 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5441 | if (!PyArg_ParseTuple(args, "|iii:socketpair", |
| 5442 | &family, &type, &proto)) |
| 5443 | return NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5444 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5445 | /* Create a pair of socket fds */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5446 | Py_BEGIN_ALLOW_THREADS |
| 5447 | #ifdef SOCK_CLOEXEC |
| 5448 | if (sock_cloexec_works != 0) { |
| 5449 | ret = socketpair(family, type | SOCK_CLOEXEC, proto, sv); |
| 5450 | if (sock_cloexec_works == -1) { |
| 5451 | if (ret >= 0) { |
| 5452 | sock_cloexec_works = 1; |
| 5453 | } |
| 5454 | else if (errno == EINVAL) { |
| 5455 | /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */ |
| 5456 | sock_cloexec_works = 0; |
| 5457 | ret = socketpair(family, type, proto, sv); |
| 5458 | } |
| 5459 | } |
| 5460 | } |
| 5461 | else |
| 5462 | #endif |
| 5463 | { |
| 5464 | ret = socketpair(family, type, proto, sv); |
| 5465 | } |
| 5466 | Py_END_ALLOW_THREADS |
| 5467 | |
| 5468 | if (ret < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5469 | return set_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5470 | |
| 5471 | if (_Py_set_inheritable(sv[0], 0, atomic_flag_works) < 0) |
| 5472 | goto finally; |
| 5473 | if (_Py_set_inheritable(sv[1], 0, atomic_flag_works) < 0) |
| 5474 | goto finally; |
| 5475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5476 | s0 = new_sockobject(sv[0], family, type, proto); |
| 5477 | if (s0 == NULL) |
| 5478 | goto finally; |
| 5479 | s1 = new_sockobject(sv[1], family, type, proto); |
| 5480 | if (s1 == NULL) |
| 5481 | goto finally; |
| 5482 | res = PyTuple_Pack(2, s0, s1); |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 5483 | |
| 5484 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5485 | if (res == NULL) { |
| 5486 | if (s0 == NULL) |
| 5487 | SOCKETCLOSE(sv[0]); |
| 5488 | if (s1 == NULL) |
| 5489 | SOCKETCLOSE(sv[1]); |
| 5490 | } |
| 5491 | Py_XDECREF(s0); |
| 5492 | Py_XDECREF(s1); |
| 5493 | return res; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 5494 | } |
| 5495 | |
| 5496 | PyDoc_STRVAR(socketpair_doc, |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5497 | "socketpair([family[, type [, proto]]]) -> (socket object, socket object)\n\ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 5498 | \n\ |
| 5499 | Create a pair of socket objects from the sockets returned by the platform\n\ |
| 5500 | socketpair() function.\n\ |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 5501 | 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] | 5502 | 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] | 5503 | |
| 5504 | #endif /* HAVE_SOCKETPAIR */ |
| 5505 | |
| 5506 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 5507 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5508 | socket_ntohs(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 5509 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5510 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 5511 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5512 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { |
| 5513 | return NULL; |
| 5514 | } |
| 5515 | if (x1 < 0) { |
| 5516 | PyErr_SetString(PyExc_OverflowError, |
| 5517 | "can't convert negative number to unsigned long"); |
| 5518 | return NULL; |
| 5519 | } |
| 5520 | x2 = (unsigned int)ntohs((unsigned short)x1); |
| 5521 | return PyLong_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 5522 | } |
| 5523 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5524 | PyDoc_STRVAR(ntohs_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5525 | "ntohs(integer) -> integer\n\ |
| 5526 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5527 | Convert a 16-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5528 | |
| 5529 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 5530 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 5531 | socket_ntohl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 5532 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5533 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 5534 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5535 | if (PyLong_Check(arg)) { |
| 5536 | x = PyLong_AsUnsignedLong(arg); |
| 5537 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 5538 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 5539 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5540 | { |
| 5541 | unsigned long y; |
| 5542 | /* only want the trailing 32 bits */ |
| 5543 | y = x & 0xFFFFFFFFUL; |
| 5544 | if (y ^ x) |
| 5545 | return PyErr_Format(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 5546 | "int larger than 32 bits"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5547 | x = y; |
| 5548 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 5549 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5550 | } |
| 5551 | else |
| 5552 | return PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 5553 | "expected int, %s found", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5554 | Py_TYPE(arg)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5555 | return PyLong_FromUnsignedLong(ntohl(x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 5556 | } |
| 5557 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5558 | PyDoc_STRVAR(ntohl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5559 | "ntohl(integer) -> integer\n\ |
| 5560 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5561 | Convert a 32-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5562 | |
| 5563 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 5564 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5565 | socket_htons(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 5566 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5567 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 5568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5569 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) { |
| 5570 | return NULL; |
| 5571 | } |
| 5572 | if (x1 < 0) { |
| 5573 | PyErr_SetString(PyExc_OverflowError, |
| 5574 | "can't convert negative number to unsigned long"); |
| 5575 | return NULL; |
| 5576 | } |
| 5577 | x2 = (unsigned int)htons((unsigned short)x1); |
| 5578 | return PyLong_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 5579 | } |
| 5580 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5581 | PyDoc_STRVAR(htons_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5582 | "htons(integer) -> integer\n\ |
| 5583 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5584 | Convert a 16-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5585 | |
| 5586 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 5587 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 5588 | socket_htonl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 5589 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5590 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 5591 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5592 | if (PyLong_Check(arg)) { |
| 5593 | x = PyLong_AsUnsignedLong(arg); |
| 5594 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 5595 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 5596 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5597 | { |
| 5598 | unsigned long y; |
| 5599 | /* only want the trailing 32 bits */ |
| 5600 | y = x & 0xFFFFFFFFUL; |
| 5601 | if (y ^ x) |
| 5602 | return PyErr_Format(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 5603 | "int larger than 32 bits"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5604 | x = y; |
| 5605 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 5606 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5607 | } |
| 5608 | else |
| 5609 | return PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 5610 | "expected int, %s found", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5611 | Py_TYPE(arg)->tp_name); |
| 5612 | return PyLong_FromUnsignedLong(htonl((unsigned long)x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 5613 | } |
| 5614 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5615 | PyDoc_STRVAR(htonl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5616 | "htonl(integer) -> integer\n\ |
| 5617 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5618 | Convert a 32-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5619 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5620 | /* socket.inet_aton() and socket.inet_ntoa() functions. */ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5621 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5622 | PyDoc_STRVAR(inet_aton_doc, |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 5623 | "inet_aton(string) -> bytes giving packed 32-bit IP representation\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5624 | \n\ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 5625 | 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] | 5626 | binary format used in low-level network functions."); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5627 | |
| 5628 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5629 | socket_inet_aton(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5630 | { |
Neal Norwitz | 88f115b | 2003-02-13 02:15:42 +0000 | [diff] [blame] | 5631 | #ifdef HAVE_INET_ATON |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5632 | struct in_addr buf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5633 | #endif |
| 5634 | |
| 5635 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 5636 | #if (SIZEOF_INT != 4) |
| 5637 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 5638 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5639 | /* Have to use inet_addr() instead */ |
| 5640 | unsigned int packed_addr; |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 5641 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5642 | char *ip_addr; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 5643 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5644 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) |
| 5645 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 5646 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 5647 | |
| 5648 | #ifdef HAVE_INET_ATON |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5649 | |
| 5650 | #ifdef USE_INET_ATON_WEAKLINK |
| 5651 | if (inet_aton != NULL) { |
| 5652 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5653 | if (inet_aton(ip_addr, &buf)) |
| 5654 | return PyBytes_FromStringAndSize((char *)(&buf), |
| 5655 | sizeof(buf)); |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 5656 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5657 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5658 | "illegal IP address string passed to inet_aton"); |
| 5659 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 5660 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5661 | #ifdef USE_INET_ATON_WEAKLINK |
| 5662 | } else { |
| 5663 | #endif |
| 5664 | |
| 5665 | #endif |
| 5666 | |
| 5667 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
| 5668 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5669 | /* special-case this address as inet_addr might return INADDR_NONE |
| 5670 | * for this */ |
| 5671 | if (strcmp(ip_addr, "255.255.255.255") == 0) { |
Charles-François Natali | 24aa041 | 2013-08-31 14:48:25 +0200 | [diff] [blame] | 5672 | packed_addr = INADDR_BROADCAST; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5673 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5675 | packed_addr = inet_addr(ip_addr); |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 5676 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5677 | if (packed_addr == INADDR_NONE) { /* invalid address */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5678 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5679 | "illegal IP address string passed to inet_aton"); |
| 5680 | return NULL; |
| 5681 | } |
| 5682 | } |
| 5683 | return PyBytes_FromStringAndSize((char *) &packed_addr, |
| 5684 | sizeof(packed_addr)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5685 | |
| 5686 | #ifdef USE_INET_ATON_WEAKLINK |
| 5687 | } |
| 5688 | #endif |
| 5689 | |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 5690 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5691 | } |
| 5692 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5693 | PyDoc_STRVAR(inet_ntoa_doc, |
Fred Drake | e066134 | 2000-03-07 14:05:16 +0000 | [diff] [blame] | 5694 | "inet_ntoa(packed_ip) -> ip_address_string\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5695 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5696 | 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] | 5697 | |
| 5698 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5699 | socket_inet_ntoa(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5700 | { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5701 | Py_buffer packed_ip; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5702 | struct in_addr packed_addr; |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5703 | |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5704 | if (!PyArg_ParseTuple(args, "y*:inet_ntoa", &packed_ip)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5705 | return NULL; |
| 5706 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 5707 | |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5708 | if (packed_ip.len != sizeof(packed_addr)) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5709 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5710 | "packed IP wrong length for inet_ntoa"); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5711 | PyBuffer_Release(&packed_ip); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5712 | return NULL; |
| 5713 | } |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5714 | |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5715 | memcpy(&packed_addr, packed_ip.buf, packed_ip.len); |
| 5716 | PyBuffer_Release(&packed_ip); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5718 | return PyUnicode_FromString(inet_ntoa(packed_addr)); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5719 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5720 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5721 | #if defined(HAVE_INET_PTON) || defined(MS_WINDOWS) |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5722 | |
| 5723 | PyDoc_STRVAR(inet_pton_doc, |
| 5724 | "inet_pton(af, ip) -> packed IP address string\n\ |
| 5725 | \n\ |
| 5726 | Convert an IP address from string format to a packed string suitable\n\ |
| 5727 | for use with low-level network functions."); |
| 5728 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5729 | #endif |
| 5730 | |
| 5731 | #ifdef HAVE_INET_PTON |
| 5732 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5733 | static PyObject * |
| 5734 | socket_inet_pton(PyObject *self, PyObject *args) |
| 5735 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5736 | int af; |
| 5737 | char* ip; |
| 5738 | int retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5739 | #ifdef ENABLE_IPV6 |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 5740 | char packed[Py_MAX(sizeof(struct in_addr), sizeof(struct in6_addr))]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5741 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5742 | char packed[sizeof(struct in_addr)]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5743 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5744 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 5745 | return NULL; |
| 5746 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5747 | |
Martin v. Löwis | 04697e8 | 2004-06-02 12:35:29 +0000 | [diff] [blame] | 5748 | #if !defined(ENABLE_IPV6) && defined(AF_INET6) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5749 | if(af == AF_INET6) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5750 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5751 | "can't use AF_INET6, IPv6 is disabled"); |
| 5752 | return NULL; |
| 5753 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5754 | #endif |
Martin v. Löwis | 1064909 | 2003-08-05 06:25:06 +0000 | [diff] [blame] | 5755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5756 | retval = inet_pton(af, ip, packed); |
| 5757 | if (retval < 0) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5758 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5759 | return NULL; |
| 5760 | } else if (retval == 0) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5761 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5762 | "illegal IP address string passed to inet_pton"); |
| 5763 | return NULL; |
| 5764 | } else if (af == AF_INET) { |
| 5765 | return PyBytes_FromStringAndSize(packed, |
| 5766 | sizeof(struct in_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5767 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5768 | } else if (af == AF_INET6) { |
| 5769 | return PyBytes_FromStringAndSize(packed, |
| 5770 | sizeof(struct in6_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5771 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5772 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5773 | PyErr_SetString(PyExc_OSError, "unknown address family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5774 | return NULL; |
| 5775 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5776 | } |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5777 | #elif defined(MS_WINDOWS) |
| 5778 | |
| 5779 | static PyObject * |
| 5780 | socket_inet_pton(PyObject *self, PyObject *args) |
| 5781 | { |
| 5782 | int af; |
| 5783 | char* ip; |
| 5784 | struct sockaddr_in6 addr; |
| 5785 | INT ret, size; |
| 5786 | |
| 5787 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 5788 | return NULL; |
| 5789 | } |
| 5790 | |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5791 | size = sizeof(addr); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5792 | ret = WSAStringToAddressA(ip, af, NULL, (LPSOCKADDR)&addr, &size); |
| 5793 | |
| 5794 | if (ret) { |
| 5795 | PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError()); |
| 5796 | return NULL; |
| 5797 | } else if(af == AF_INET) { |
| 5798 | struct sockaddr_in *addr4 = (struct sockaddr_in*)&addr; |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5799 | return PyBytes_FromStringAndSize((const char *)&(addr4->sin_addr), |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5800 | sizeof(addr4->sin_addr)); |
| 5801 | } else if (af == AF_INET6) { |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5802 | return PyBytes_FromStringAndSize((const char *)&(addr.sin6_addr), |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5803 | sizeof(addr.sin6_addr)); |
| 5804 | } else { |
| 5805 | PyErr_SetString(PyExc_OSError, "unknown address family"); |
| 5806 | return NULL; |
| 5807 | } |
| 5808 | } |
| 5809 | |
| 5810 | #endif |
| 5811 | |
| 5812 | #if defined(HAVE_INET_PTON) || defined(MS_WINDOWS) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5813 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5814 | PyDoc_STRVAR(inet_ntop_doc, |
| 5815 | "inet_ntop(af, packed_ip) -> string formatted IP address\n\ |
| 5816 | \n\ |
| 5817 | Convert a packed IP address of the given family to string format."); |
| 5818 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5819 | #endif |
| 5820 | |
| 5821 | |
| 5822 | #ifdef HAVE_INET_PTON |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5823 | static PyObject * |
| 5824 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 5825 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5826 | int af; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5827 | Py_buffer packed_ip; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5828 | const char* retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5829 | #ifdef ENABLE_IPV6 |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 5830 | char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5831 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5832 | char ip[INET_ADDRSTRLEN + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5833 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5834 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5835 | /* Guarantee NUL-termination for PyUnicode_FromString() below */ |
| 5836 | memset((void *) &ip[0], '\0', sizeof(ip)); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5837 | |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5838 | if (!PyArg_ParseTuple(args, "iy*:inet_ntop", &af, &packed_ip)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5839 | return NULL; |
| 5840 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5841 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5842 | if (af == AF_INET) { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5843 | if (packed_ip.len != sizeof(struct in_addr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5844 | PyErr_SetString(PyExc_ValueError, |
| 5845 | "invalid length of packed IP address string"); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5846 | PyBuffer_Release(&packed_ip); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5847 | return NULL; |
| 5848 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5849 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5850 | } else if (af == AF_INET6) { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5851 | if (packed_ip.len != sizeof(struct in6_addr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5852 | PyErr_SetString(PyExc_ValueError, |
| 5853 | "invalid length of packed IP address string"); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5854 | PyBuffer_Release(&packed_ip); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5855 | return NULL; |
| 5856 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5857 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5858 | } else { |
| 5859 | PyErr_Format(PyExc_ValueError, |
| 5860 | "unknown address family %d", af); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5861 | PyBuffer_Release(&packed_ip); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5862 | return NULL; |
| 5863 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5864 | |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5865 | retval = inet_ntop(af, packed_ip.buf, ip, sizeof(ip)); |
| 5866 | PyBuffer_Release(&packed_ip); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5867 | if (!retval) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5868 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5869 | return NULL; |
| 5870 | } else { |
| 5871 | return PyUnicode_FromString(retval); |
| 5872 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5873 | } |
| 5874 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5875 | #elif defined(MS_WINDOWS) |
| 5876 | |
| 5877 | static PyObject * |
| 5878 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 5879 | { |
| 5880 | int af; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5881 | Py_buffer packed_ip; |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5882 | struct sockaddr_in6 addr; |
| 5883 | DWORD addrlen, ret, retlen; |
Jason R. Coombs | 8ec784c | 2013-11-10 13:43:22 -0500 | [diff] [blame] | 5884 | #ifdef ENABLE_IPV6 |
| 5885 | char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
| 5886 | #else |
| 5887 | char ip[INET_ADDRSTRLEN + 1]; |
| 5888 | #endif |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5889 | |
| 5890 | /* Guarantee NUL-termination for PyUnicode_FromString() below */ |
| 5891 | memset((void *) &ip[0], '\0', sizeof(ip)); |
| 5892 | |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5893 | if (!PyArg_ParseTuple(args, "iy*:inet_ntop", &af, &packed_ip)) { |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5894 | return NULL; |
| 5895 | } |
| 5896 | |
| 5897 | if (af == AF_INET) { |
| 5898 | struct sockaddr_in * addr4 = (struct sockaddr_in *)&addr; |
| 5899 | |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5900 | if (packed_ip.len != sizeof(struct in_addr)) { |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5901 | PyErr_SetString(PyExc_ValueError, |
| 5902 | "invalid length of packed IP address string"); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5903 | PyBuffer_Release(&packed_ip); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5904 | return NULL; |
| 5905 | } |
| 5906 | memset(addr4, 0, sizeof(struct sockaddr_in)); |
| 5907 | addr4->sin_family = AF_INET; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5908 | memcpy(&(addr4->sin_addr), packed_ip.buf, sizeof(addr4->sin_addr)); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5909 | addrlen = sizeof(struct sockaddr_in); |
| 5910 | } else if (af == AF_INET6) { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5911 | if (packed_ip.len != sizeof(struct in6_addr)) { |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5912 | PyErr_SetString(PyExc_ValueError, |
| 5913 | "invalid length of packed IP address string"); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5914 | PyBuffer_Release(&packed_ip); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5915 | return NULL; |
| 5916 | } |
| 5917 | |
| 5918 | memset(&addr, 0, sizeof(addr)); |
| 5919 | addr.sin6_family = AF_INET6; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5920 | memcpy(&(addr.sin6_addr), packed_ip.buf, sizeof(addr.sin6_addr)); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5921 | addrlen = sizeof(addr); |
| 5922 | } else { |
| 5923 | PyErr_Format(PyExc_ValueError, |
| 5924 | "unknown address family %d", af); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5925 | PyBuffer_Release(&packed_ip); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5926 | return NULL; |
| 5927 | } |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5928 | PyBuffer_Release(&packed_ip); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5929 | |
| 5930 | retlen = sizeof(ip); |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5931 | ret = WSAAddressToStringA((struct sockaddr*)&addr, addrlen, NULL, |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5932 | ip, &retlen); |
| 5933 | |
| 5934 | if (ret) { |
| 5935 | PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError()); |
| 5936 | return NULL; |
| 5937 | } else { |
| 5938 | return PyUnicode_FromString(ip); |
| 5939 | } |
| 5940 | } |
| 5941 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5942 | #endif /* HAVE_INET_PTON */ |
| 5943 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5944 | /* Python interface to getaddrinfo(host, port). */ |
| 5945 | |
| 5946 | /*ARGSUSED*/ |
| 5947 | static PyObject * |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5948 | socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5949 | { |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 5950 | static char* kwnames[] = {"host", "port", "family", "type", "proto", |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5951 | "flags", 0}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5952 | struct addrinfo hints, *res; |
| 5953 | struct addrinfo *res0 = NULL; |
| 5954 | PyObject *hobj = NULL; |
| 5955 | PyObject *pobj = (PyObject *)NULL; |
| 5956 | char pbuf[30]; |
| 5957 | char *hptr, *pptr; |
| 5958 | int family, socktype, protocol, flags; |
| 5959 | int error; |
| 5960 | PyObject *all = (PyObject *)NULL; |
| 5961 | PyObject *idna = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5962 | |
Georg Brandl | 6083a4b | 2013-10-14 06:51:46 +0200 | [diff] [blame] | 5963 | socktype = protocol = flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5964 | family = AF_UNSPEC; |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 5965 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iiii:getaddrinfo", |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5966 | kwnames, &hobj, &pobj, &family, &socktype, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5967 | &protocol, &flags)) { |
| 5968 | return NULL; |
| 5969 | } |
| 5970 | if (hobj == Py_None) { |
| 5971 | hptr = NULL; |
| 5972 | } else if (PyUnicode_Check(hobj)) { |
Victor Stinner | ec1a498 | 2015-09-11 12:37:30 +0200 | [diff] [blame] | 5973 | idna = PyUnicode_AsEncodedString(hobj, "idna", NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5974 | if (!idna) |
| 5975 | return NULL; |
| 5976 | assert(PyBytes_Check(idna)); |
| 5977 | hptr = PyBytes_AS_STRING(idna); |
| 5978 | } else if (PyBytes_Check(hobj)) { |
| 5979 | hptr = PyBytes_AsString(hobj); |
| 5980 | } else { |
| 5981 | PyErr_SetString(PyExc_TypeError, |
| 5982 | "getaddrinfo() argument 1 must be string or None"); |
| 5983 | return NULL; |
| 5984 | } |
| 5985 | if (PyLong_CheckExact(pobj)) { |
| 5986 | long value = PyLong_AsLong(pobj); |
| 5987 | if (value == -1 && PyErr_Occurred()) |
| 5988 | goto err; |
| 5989 | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value); |
| 5990 | pptr = pbuf; |
| 5991 | } else if (PyUnicode_Check(pobj)) { |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 5992 | pptr = PyUnicode_AsUTF8(pobj); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 5993 | if (pptr == NULL) |
| 5994 | goto err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5995 | } else if (PyBytes_Check(pobj)) { |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 5996 | pptr = PyBytes_AS_STRING(pobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5997 | } else if (pobj == Py_None) { |
| 5998 | pptr = (char *)NULL; |
| 5999 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 6000 | PyErr_SetString(PyExc_OSError, "Int or String expected"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6001 | goto err; |
| 6002 | } |
Ronald Oussoren | a822d366 | 2013-06-10 10:36:28 +0200 | [diff] [blame] | 6003 | #if defined(__APPLE__) && defined(AI_NUMERICSERV) |
Ronald Oussoren | 27a4ac5 | 2013-05-24 13:47:37 +0200 | [diff] [blame] | 6004 | if ((flags & AI_NUMERICSERV) && (pptr == NULL || (pptr[0] == '0' && pptr[1] == 0))) { |
| 6005 | /* On OSX upto at least OSX 10.8 getaddrinfo crashes |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 6006 | * if AI_NUMERICSERV is set and the servname is NULL or "0". |
| 6007 | * This workaround avoids a segfault in libsystem. |
| 6008 | */ |
Ronald Oussoren | 27a4ac5 | 2013-05-24 13:47:37 +0200 | [diff] [blame] | 6009 | pptr = "00"; |
| 6010 | } |
| 6011 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6012 | memset(&hints, 0, sizeof(hints)); |
| 6013 | hints.ai_family = family; |
| 6014 | hints.ai_socktype = socktype; |
| 6015 | hints.ai_protocol = protocol; |
| 6016 | hints.ai_flags = flags; |
| 6017 | Py_BEGIN_ALLOW_THREADS |
| 6018 | ACQUIRE_GETADDRINFO_LOCK |
| 6019 | error = getaddrinfo(hptr, pptr, &hints, &res0); |
| 6020 | Py_END_ALLOW_THREADS |
| 6021 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 6022 | if (error) { |
| 6023 | set_gaierror(error); |
| 6024 | goto err; |
| 6025 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6026 | |
Benjamin Peterson | 4fd929b | 2015-04-01 11:16:40 -0400 | [diff] [blame] | 6027 | all = PyList_New(0); |
| 6028 | if (all == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6029 | goto err; |
| 6030 | for (res = res0; res; res = res->ai_next) { |
| 6031 | PyObject *single; |
| 6032 | PyObject *addr = |
| 6033 | makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol); |
| 6034 | if (addr == NULL) |
| 6035 | goto err; |
| 6036 | single = Py_BuildValue("iiisO", res->ai_family, |
| 6037 | res->ai_socktype, res->ai_protocol, |
| 6038 | res->ai_canonname ? res->ai_canonname : "", |
| 6039 | addr); |
| 6040 | Py_DECREF(addr); |
| 6041 | if (single == NULL) |
| 6042 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6043 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6044 | if (PyList_Append(all, single)) |
| 6045 | goto err; |
| 6046 | Py_XDECREF(single); |
| 6047 | } |
| 6048 | Py_XDECREF(idna); |
| 6049 | if (res0) |
| 6050 | freeaddrinfo(res0); |
| 6051 | return all; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6052 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6053 | Py_XDECREF(all); |
| 6054 | Py_XDECREF(idna); |
| 6055 | if (res0) |
| 6056 | freeaddrinfo(res0); |
| 6057 | return (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6058 | } |
| 6059 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6060 | PyDoc_STRVAR(getaddrinfo_doc, |
Berker Peksag | a6ec5ee | 2014-06-30 11:28:40 +0300 | [diff] [blame] | 6061 | "getaddrinfo(host, port [, family, type, proto, flags])\n\ |
| 6062 | -> list of (family, type, proto, canonname, sockaddr)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6063 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6064 | Resolve host and port into addrinfo struct."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6065 | |
| 6066 | /* Python interface to getnameinfo(sa, flags). */ |
| 6067 | |
| 6068 | /*ARGSUSED*/ |
| 6069 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6070 | socket_getnameinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6071 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6072 | PyObject *sa = (PyObject *)NULL; |
| 6073 | int flags; |
| 6074 | char *hostp; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 6075 | int port; |
| 6076 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6077 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; |
| 6078 | struct addrinfo hints, *res = NULL; |
| 6079 | int error; |
| 6080 | PyObject *ret = (PyObject *)NULL; |
Victor Stinner | 7240030 | 2016-01-28 15:41:01 +0100 | [diff] [blame] | 6081 | PyObject *name; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6082 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6083 | flags = flowinfo = scope_id = 0; |
| 6084 | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) |
| 6085 | return NULL; |
| 6086 | if (!PyTuple_Check(sa)) { |
| 6087 | PyErr_SetString(PyExc_TypeError, |
| 6088 | "getnameinfo() argument 1 must be a tuple"); |
| 6089 | return NULL; |
| 6090 | } |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 6091 | if (!PyArg_ParseTuple(sa, "si|II", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6092 | &hostp, &port, &flowinfo, &scope_id)) |
| 6093 | return NULL; |
Charles-François Natali | 9b0c006 | 2012-06-23 10:17:05 +0200 | [diff] [blame] | 6094 | if (flowinfo > 0xfffff) { |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 6095 | PyErr_SetString(PyExc_OverflowError, |
| 6096 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 6097 | return NULL; |
| 6098 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6099 | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); |
| 6100 | memset(&hints, 0, sizeof(hints)); |
| 6101 | hints.ai_family = AF_UNSPEC; |
| 6102 | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ |
Martin v. Löwis | 112c0f3 | 2010-08-25 07:38:15 +0000 | [diff] [blame] | 6103 | hints.ai_flags = AI_NUMERICHOST; /* don't do any name resolution */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6104 | Py_BEGIN_ALLOW_THREADS |
| 6105 | ACQUIRE_GETADDRINFO_LOCK |
| 6106 | error = getaddrinfo(hostp, pbuf, &hints, &res); |
| 6107 | Py_END_ALLOW_THREADS |
| 6108 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 6109 | if (error) { |
| 6110 | set_gaierror(error); |
| 6111 | goto fail; |
| 6112 | } |
| 6113 | if (res->ai_next) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 6114 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6115 | "sockaddr resolved to multiple addresses"); |
| 6116 | goto fail; |
| 6117 | } |
| 6118 | switch (res->ai_family) { |
| 6119 | case AF_INET: |
| 6120 | { |
| 6121 | if (PyTuple_GET_SIZE(sa) != 2) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 6122 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6123 | "IPv4 sockaddr must be 2 tuple"); |
| 6124 | goto fail; |
| 6125 | } |
| 6126 | break; |
| 6127 | } |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 6128 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6129 | case AF_INET6: |
| 6130 | { |
| 6131 | struct sockaddr_in6 *sin6; |
| 6132 | sin6 = (struct sockaddr_in6 *)res->ai_addr; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 6133 | sin6->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6134 | sin6->sin6_scope_id = scope_id; |
| 6135 | break; |
| 6136 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6137 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6138 | } |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 6139 | error = getnameinfo(res->ai_addr, (socklen_t) res->ai_addrlen, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6140 | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); |
| 6141 | if (error) { |
| 6142 | set_gaierror(error); |
| 6143 | goto fail; |
| 6144 | } |
Victor Stinner | 7240030 | 2016-01-28 15:41:01 +0100 | [diff] [blame] | 6145 | |
| 6146 | name = sock_decode_hostname(hbuf); |
| 6147 | if (name == NULL) |
| 6148 | goto fail; |
| 6149 | ret = Py_BuildValue("Ns", name, pbuf); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6150 | |
| 6151 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6152 | if (res) |
| 6153 | freeaddrinfo(res); |
| 6154 | return ret; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6155 | } |
| 6156 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6157 | PyDoc_STRVAR(getnameinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 6158 | "getnameinfo(sockaddr, flags) --> (host, port)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6159 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6160 | Get host and port for a sockaddr."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6161 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 6162 | |
| 6163 | /* Python API to getting and setting the default timeout value. */ |
| 6164 | |
| 6165 | static PyObject * |
| 6166 | socket_getdefaulttimeout(PyObject *self) |
| 6167 | { |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 6168 | if (defaulttimeout < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6169 | Py_INCREF(Py_None); |
| 6170 | return Py_None; |
| 6171 | } |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 6172 | else { |
| 6173 | double seconds = _PyTime_AsSecondsDouble(defaulttimeout); |
| 6174 | return PyFloat_FromDouble(seconds); |
| 6175 | } |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 6176 | } |
| 6177 | |
| 6178 | PyDoc_STRVAR(getdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 6179 | "getdefaulttimeout() -> timeout\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 6180 | \n\ |
Ezio Melotti | 388c945 | 2011-08-14 08:28:57 +0300 | [diff] [blame] | 6181 | 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] | 6182 | A value of None indicates that new socket objects have no timeout.\n\ |
| 6183 | When the socket module is first imported, the default is None."); |
| 6184 | |
| 6185 | static PyObject * |
| 6186 | socket_setdefaulttimeout(PyObject *self, PyObject *arg) |
| 6187 | { |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 6188 | _PyTime_t timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 6189 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 6190 | if (socket_parse_timeout(&timeout, arg) < 0) |
| 6191 | return NULL; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 6192 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6193 | defaulttimeout = timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 6194 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6195 | Py_INCREF(Py_None); |
| 6196 | return Py_None; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 6197 | } |
| 6198 | |
| 6199 | PyDoc_STRVAR(setdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 6200 | "setdefaulttimeout(timeout)\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 6201 | \n\ |
Ezio Melotti | 388c945 | 2011-08-14 08:28:57 +0300 | [diff] [blame] | 6202 | 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] | 6203 | A value of None indicates that new socket objects have no timeout.\n\ |
| 6204 | When the socket module is first imported, the default is None."); |
| 6205 | |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 6206 | #ifdef HAVE_IF_NAMEINDEX |
| 6207 | /* Python API for getting interface indices and names */ |
| 6208 | |
| 6209 | static PyObject * |
| 6210 | socket_if_nameindex(PyObject *self, PyObject *arg) |
| 6211 | { |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 6212 | PyObject *list; |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 6213 | int i; |
| 6214 | struct if_nameindex *ni; |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 6215 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 6216 | ni = if_nameindex(); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 6217 | if (ni == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 6218 | PyErr_SetFromErrno(PyExc_OSError); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 6219 | return NULL; |
| 6220 | } |
| 6221 | |
| 6222 | list = PyList_New(0); |
| 6223 | if (list == NULL) { |
| 6224 | if_freenameindex(ni); |
| 6225 | return NULL; |
| 6226 | } |
| 6227 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 6228 | for (i = 0; ni[i].if_index != 0 && i < INT_MAX; i++) { |
| 6229 | PyObject *ni_tuple = Py_BuildValue("IO&", |
| 6230 | ni[i].if_index, PyUnicode_DecodeFSDefault, ni[i].if_name); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 6231 | |
| 6232 | if (ni_tuple == NULL || PyList_Append(list, ni_tuple) == -1) { |
| 6233 | Py_XDECREF(ni_tuple); |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 6234 | Py_DECREF(list); |
| 6235 | if_freenameindex(ni); |
| 6236 | return NULL; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 6237 | } |
| 6238 | Py_DECREF(ni_tuple); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 6239 | } |
| 6240 | |
| 6241 | if_freenameindex(ni); |
| 6242 | return list; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 6243 | } |
| 6244 | |
| 6245 | PyDoc_STRVAR(if_nameindex_doc, |
| 6246 | "if_nameindex()\n\ |
| 6247 | \n\ |
| 6248 | Returns a list of network interface information (index, name) tuples."); |
| 6249 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 6250 | static PyObject * |
| 6251 | socket_if_nametoindex(PyObject *self, PyObject *args) |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 6252 | { |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 6253 | PyObject *oname; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 6254 | unsigned long index; |
| 6255 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 6256 | if (!PyArg_ParseTuple(args, "O&:if_nametoindex", |
| 6257 | PyUnicode_FSConverter, &oname)) |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 6258 | return NULL; |
| 6259 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 6260 | index = if_nametoindex(PyBytes_AS_STRING(oname)); |
| 6261 | Py_DECREF(oname); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 6262 | if (index == 0) { |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 6263 | /* if_nametoindex() doesn't set errno */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 6264 | PyErr_SetString(PyExc_OSError, "no interface with this name"); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 6265 | return NULL; |
| 6266 | } |
| 6267 | |
| 6268 | return PyLong_FromUnsignedLong(index); |
| 6269 | } |
| 6270 | |
| 6271 | PyDoc_STRVAR(if_nametoindex_doc, |
| 6272 | "if_nametoindex(if_name)\n\ |
| 6273 | \n\ |
| 6274 | Returns the interface index corresponding to the interface name if_name."); |
| 6275 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 6276 | static PyObject * |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 6277 | socket_if_indextoname(PyObject *self, PyObject *arg) |
| 6278 | { |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 6279 | unsigned long index; |
| 6280 | char name[IF_NAMESIZE + 1]; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 6281 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 6282 | index = PyLong_AsUnsignedLong(arg); |
| 6283 | if (index == (unsigned long) -1) |
| 6284 | return NULL; |
| 6285 | |
| 6286 | if (if_indextoname(index, name) == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 6287 | PyErr_SetFromErrno(PyExc_OSError); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 6288 | return NULL; |
| 6289 | } |
| 6290 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 6291 | return PyUnicode_DecodeFSDefault(name); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 6292 | } |
| 6293 | |
| 6294 | PyDoc_STRVAR(if_indextoname_doc, |
| 6295 | "if_indextoname(if_index)\n\ |
| 6296 | \n\ |
| 6297 | Returns the interface name corresponding to the interface index if_index."); |
| 6298 | |
| 6299 | #endif /* HAVE_IF_NAMEINDEX */ |
| 6300 | |
| 6301 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6302 | #ifdef CMSG_LEN |
| 6303 | /* Python interface to CMSG_LEN(length). */ |
| 6304 | |
| 6305 | static PyObject * |
| 6306 | socket_CMSG_LEN(PyObject *self, PyObject *args) |
| 6307 | { |
| 6308 | Py_ssize_t length; |
| 6309 | size_t result; |
| 6310 | |
| 6311 | if (!PyArg_ParseTuple(args, "n:CMSG_LEN", &length)) |
| 6312 | return NULL; |
| 6313 | if (length < 0 || !get_CMSG_LEN(length, &result)) { |
| 6314 | PyErr_Format(PyExc_OverflowError, "CMSG_LEN() argument out of range"); |
| 6315 | return NULL; |
| 6316 | } |
| 6317 | return PyLong_FromSize_t(result); |
| 6318 | } |
| 6319 | |
| 6320 | PyDoc_STRVAR(CMSG_LEN_doc, |
| 6321 | "CMSG_LEN(length) -> control message length\n\ |
| 6322 | \n\ |
| 6323 | Return the total length, without trailing padding, of an ancillary\n\ |
| 6324 | data item with associated data of the given length. This value can\n\ |
| 6325 | often be used as the buffer size for recvmsg() to receive a single\n\ |
| 6326 | item of ancillary data, but RFC 3542 requires portable applications to\n\ |
| 6327 | use CMSG_SPACE() and thus include space for padding, even when the\n\ |
| 6328 | item will be the last in the buffer. Raises OverflowError if length\n\ |
| 6329 | is outside the permissible range of values."); |
| 6330 | |
| 6331 | |
| 6332 | #ifdef CMSG_SPACE |
| 6333 | /* Python interface to CMSG_SPACE(length). */ |
| 6334 | |
| 6335 | static PyObject * |
| 6336 | socket_CMSG_SPACE(PyObject *self, PyObject *args) |
| 6337 | { |
| 6338 | Py_ssize_t length; |
| 6339 | size_t result; |
| 6340 | |
| 6341 | if (!PyArg_ParseTuple(args, "n:CMSG_SPACE", &length)) |
| 6342 | return NULL; |
| 6343 | if (length < 0 || !get_CMSG_SPACE(length, &result)) { |
| 6344 | PyErr_SetString(PyExc_OverflowError, |
| 6345 | "CMSG_SPACE() argument out of range"); |
| 6346 | return NULL; |
| 6347 | } |
| 6348 | return PyLong_FromSize_t(result); |
| 6349 | } |
| 6350 | |
| 6351 | PyDoc_STRVAR(CMSG_SPACE_doc, |
| 6352 | "CMSG_SPACE(length) -> buffer size\n\ |
| 6353 | \n\ |
| 6354 | Return the buffer size needed for recvmsg() to receive an ancillary\n\ |
| 6355 | data item with associated data of the given length, along with any\n\ |
| 6356 | trailing padding. The buffer space needed to receive multiple items\n\ |
| 6357 | is the sum of the CMSG_SPACE() values for their associated data\n\ |
| 6358 | lengths. Raises OverflowError if length is outside the permissible\n\ |
| 6359 | range of values."); |
| 6360 | #endif /* CMSG_SPACE */ |
| 6361 | #endif /* CMSG_LEN */ |
| 6362 | |
| 6363 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 6364 | /* List of functions exported by this module. */ |
| 6365 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6366 | static PyMethodDef socket_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6367 | {"gethostbyname", socket_gethostbyname, |
| 6368 | METH_VARARGS, gethostbyname_doc}, |
| 6369 | {"gethostbyname_ex", socket_gethostbyname_ex, |
| 6370 | METH_VARARGS, ghbn_ex_doc}, |
| 6371 | {"gethostbyaddr", socket_gethostbyaddr, |
| 6372 | METH_VARARGS, gethostbyaddr_doc}, |
| 6373 | {"gethostname", socket_gethostname, |
| 6374 | METH_NOARGS, gethostname_doc}, |
Antoine Pitrou | 061cfb5 | 2011-02-28 22:25:22 +0000 | [diff] [blame] | 6375 | #ifdef HAVE_SETHOSTNAME |
| 6376 | {"sethostname", socket_sethostname, |
| 6377 | METH_VARARGS, sethostname_doc}, |
| 6378 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6379 | {"getservbyname", socket_getservbyname, |
| 6380 | METH_VARARGS, getservbyname_doc}, |
| 6381 | {"getservbyport", socket_getservbyport, |
| 6382 | METH_VARARGS, getservbyport_doc}, |
| 6383 | {"getprotobyname", socket_getprotobyname, |
| 6384 | METH_VARARGS, getprotobyname_doc}, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 6385 | #ifndef NO_DUP |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6386 | {"dup", socket_dup, |
| 6387 | METH_O, dup_doc}, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 6388 | #endif |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 6389 | #ifdef HAVE_SOCKETPAIR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6390 | {"socketpair", socket_socketpair, |
| 6391 | METH_VARARGS, socketpair_doc}, |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 6392 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6393 | {"ntohs", socket_ntohs, |
| 6394 | METH_VARARGS, ntohs_doc}, |
| 6395 | {"ntohl", socket_ntohl, |
| 6396 | METH_O, ntohl_doc}, |
| 6397 | {"htons", socket_htons, |
| 6398 | METH_VARARGS, htons_doc}, |
| 6399 | {"htonl", socket_htonl, |
| 6400 | METH_O, htonl_doc}, |
| 6401 | {"inet_aton", socket_inet_aton, |
| 6402 | METH_VARARGS, inet_aton_doc}, |
| 6403 | {"inet_ntoa", socket_inet_ntoa, |
| 6404 | METH_VARARGS, inet_ntoa_doc}, |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 6405 | #if defined(HAVE_INET_PTON) || defined(MS_WINDOWS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6406 | {"inet_pton", socket_inet_pton, |
| 6407 | METH_VARARGS, inet_pton_doc}, |
| 6408 | {"inet_ntop", socket_inet_ntop, |
| 6409 | METH_VARARGS, inet_ntop_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 6410 | #endif |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 6411 | {"getaddrinfo", (PyCFunction)socket_getaddrinfo, |
| 6412 | METH_VARARGS | METH_KEYWORDS, getaddrinfo_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6413 | {"getnameinfo", socket_getnameinfo, |
| 6414 | METH_VARARGS, getnameinfo_doc}, |
| 6415 | {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout, |
| 6416 | METH_NOARGS, getdefaulttimeout_doc}, |
| 6417 | {"setdefaulttimeout", socket_setdefaulttimeout, |
| 6418 | METH_O, setdefaulttimeout_doc}, |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 6419 | #ifdef HAVE_IF_NAMEINDEX |
| 6420 | {"if_nameindex", socket_if_nameindex, |
| 6421 | METH_NOARGS, if_nameindex_doc}, |
| 6422 | {"if_nametoindex", socket_if_nametoindex, |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 6423 | METH_VARARGS, if_nametoindex_doc}, |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 6424 | {"if_indextoname", socket_if_indextoname, |
| 6425 | METH_O, if_indextoname_doc}, |
| 6426 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6427 | #ifdef CMSG_LEN |
| 6428 | {"CMSG_LEN", socket_CMSG_LEN, |
| 6429 | METH_VARARGS, CMSG_LEN_doc}, |
| 6430 | #ifdef CMSG_SPACE |
| 6431 | {"CMSG_SPACE", socket_CMSG_SPACE, |
| 6432 | METH_VARARGS, CMSG_SPACE_doc}, |
| 6433 | #endif |
| 6434 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6435 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 6436 | }; |
| 6437 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 6438 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6439 | #ifdef MS_WINDOWS |
| 6440 | #define OS_INIT_DEFINED |
| 6441 | |
| 6442 | /* Additional initialization and cleanup for Windows */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 6443 | |
| 6444 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6445 | os_cleanup(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 6446 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6447 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 6448 | } |
| 6449 | |
| 6450 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6451 | os_init(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 6452 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6453 | WSADATA WSAData; |
| 6454 | int ret; |
| 6455 | ret = WSAStartup(0x0101, &WSAData); |
| 6456 | switch (ret) { |
| 6457 | case 0: /* No error */ |
| 6458 | Py_AtExit(os_cleanup); |
| 6459 | return 1; /* Success */ |
| 6460 | case WSASYSNOTREADY: |
| 6461 | PyErr_SetString(PyExc_ImportError, |
| 6462 | "WSAStartup failed: network not ready"); |
| 6463 | break; |
| 6464 | case WSAVERNOTSUPPORTED: |
| 6465 | case WSAEINVAL: |
| 6466 | PyErr_SetString( |
| 6467 | PyExc_ImportError, |
| 6468 | "WSAStartup failed: requested version not supported"); |
| 6469 | break; |
| 6470 | default: |
| 6471 | PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret); |
| 6472 | break; |
| 6473 | } |
| 6474 | return 0; /* Failure */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 6475 | } |
| 6476 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 6477 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 6478 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6479 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6480 | |
| 6481 | #ifndef OS_INIT_DEFINED |
| 6482 | static int |
| 6483 | os_init(void) |
| 6484 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6485 | return 1; /* Success */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6486 | } |
| 6487 | #endif |
| 6488 | |
| 6489 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6490 | /* C API table - always add new things to the end for binary |
| 6491 | compatibility. */ |
| 6492 | static |
| 6493 | PySocketModule_APIObject PySocketModuleAPI = |
| 6494 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6495 | &sock_type, |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 6496 | NULL, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6497 | NULL |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6498 | }; |
| 6499 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 6500 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6501 | /* Initialize the _socket module. |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 6502 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6503 | This module is actually called "_socket", and there's a wrapper |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 6504 | "socket.py" which implements some additional functionality. |
| 6505 | The import of "_socket" may fail with an ImportError exception if |
| 6506 | os-specific initialization fails. On Windows, this does WINSOCK |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 6507 | initialization. When WINSOCK is initialized successfully, a call to |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 6508 | WSACleanup() is scheduled to be made at exit time. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6509 | */ |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 6510 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6511 | PyDoc_STRVAR(socket_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 6512 | "Implementation module for socket operations.\n\ |
| 6513 | \n\ |
| 6514 | See the socket module for documentation."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 6515 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 6516 | static struct PyModuleDef socketmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6517 | PyModuleDef_HEAD_INIT, |
| 6518 | PySocket_MODULE_NAME, |
| 6519 | socket_doc, |
| 6520 | -1, |
| 6521 | socket_methods, |
| 6522 | NULL, |
| 6523 | NULL, |
| 6524 | NULL, |
| 6525 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 6526 | }; |
| 6527 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 6528 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 6529 | PyInit__socket(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 6530 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6531 | PyObject *m, *has_ipv6; |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 6532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6533 | if (!os_init()) |
| 6534 | return NULL; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6535 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6536 | #ifdef MS_WINDOWS |
| 6537 | if (support_wsa_no_inherit == -1) { |
Steve Dower | 65e4cb1 | 2014-11-22 12:54:57 -0800 | [diff] [blame] | 6538 | #if defined(_MSC_VER) && _MSC_VER >= 1800 |
| 6539 | support_wsa_no_inherit = IsWindows7SP1OrGreater(); |
| 6540 | #else |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6541 | DWORD version = GetVersion(); |
| 6542 | DWORD major = (DWORD)LOBYTE(LOWORD(version)); |
| 6543 | DWORD minor = (DWORD)HIBYTE(LOWORD(version)); |
| 6544 | /* need Windows 7 SP1, 2008 R2 SP1 or later */ |
Steve Dower | 65e4cb1 | 2014-11-22 12:54:57 -0800 | [diff] [blame] | 6545 | support_wsa_no_inherit = major > 6 || (major == 6 && minor >= 1); |
| 6546 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6547 | } |
| 6548 | #endif |
| 6549 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6550 | Py_TYPE(&sock_type) = &PyType_Type; |
| 6551 | m = PyModule_Create(&socketmodule); |
| 6552 | if (m == NULL) |
| 6553 | return NULL; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6554 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 6555 | Py_INCREF(PyExc_OSError); |
| 6556 | PySocketModuleAPI.error = PyExc_OSError; |
| 6557 | Py_INCREF(PyExc_OSError); |
| 6558 | PyModule_AddObject(m, "error", PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6559 | socket_herror = PyErr_NewException("socket.herror", |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 6560 | PyExc_OSError, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6561 | if (socket_herror == NULL) |
| 6562 | return NULL; |
| 6563 | Py_INCREF(socket_herror); |
| 6564 | PyModule_AddObject(m, "herror", socket_herror); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 6565 | socket_gaierror = PyErr_NewException("socket.gaierror", PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6566 | NULL); |
| 6567 | if (socket_gaierror == NULL) |
| 6568 | return NULL; |
| 6569 | Py_INCREF(socket_gaierror); |
| 6570 | PyModule_AddObject(m, "gaierror", socket_gaierror); |
| 6571 | socket_timeout = PyErr_NewException("socket.timeout", |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 6572 | PyExc_OSError, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6573 | if (socket_timeout == NULL) |
| 6574 | return NULL; |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 6575 | PySocketModuleAPI.timeout_error = socket_timeout; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6576 | Py_INCREF(socket_timeout); |
| 6577 | PyModule_AddObject(m, "timeout", socket_timeout); |
| 6578 | Py_INCREF((PyObject *)&sock_type); |
| 6579 | if (PyModule_AddObject(m, "SocketType", |
| 6580 | (PyObject *)&sock_type) != 0) |
| 6581 | return NULL; |
| 6582 | Py_INCREF((PyObject *)&sock_type); |
| 6583 | if (PyModule_AddObject(m, "socket", |
| 6584 | (PyObject *)&sock_type) != 0) |
| 6585 | return NULL; |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6586 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 6587 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6588 | has_ipv6 = Py_True; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 6589 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6590 | has_ipv6 = Py_False; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 6591 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6592 | Py_INCREF(has_ipv6); |
| 6593 | PyModule_AddObject(m, "has_ipv6", has_ipv6); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 6594 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6595 | /* Export C API */ |
| 6596 | if (PyModule_AddObject(m, PySocket_CAPI_NAME, |
| 6597 | PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL) |
| 6598 | ) != 0) |
| 6599 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6600 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6601 | /* Address families (we only support AF_INET and AF_UNIX) */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6602 | #ifdef AF_UNSPEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6603 | PyModule_AddIntMacro(m, AF_UNSPEC); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6604 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6605 | PyModule_AddIntMacro(m, AF_INET); |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 6606 | #if defined(AF_UNIX) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6607 | PyModule_AddIntMacro(m, AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6608 | #endif /* AF_UNIX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6609 | #ifdef AF_AX25 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6610 | /* Amateur Radio AX.25 */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6611 | PyModule_AddIntMacro(m, AF_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6612 | #endif |
| 6613 | #ifdef AF_IPX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6614 | PyModule_AddIntMacro(m, AF_IPX); /* Novell IPX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6615 | #endif |
| 6616 | #ifdef AF_APPLETALK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6617 | /* Appletalk DDP */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6618 | PyModule_AddIntMacro(m, AF_APPLETALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6619 | #endif |
| 6620 | #ifdef AF_NETROM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6621 | /* Amateur radio NetROM */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6622 | PyModule_AddIntMacro(m, AF_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6623 | #endif |
| 6624 | #ifdef AF_BRIDGE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6625 | /* Multiprotocol bridge */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6626 | PyModule_AddIntMacro(m, AF_BRIDGE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6627 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6628 | #ifdef AF_ATMPVC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6629 | /* ATM PVCs */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6630 | PyModule_AddIntMacro(m, AF_ATMPVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6631 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6632 | #ifdef AF_AAL5 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6633 | /* Reserved for Werner's ATM */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6634 | PyModule_AddIntMacro(m, AF_AAL5); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6635 | #endif |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 6636 | #ifdef HAVE_SOCKADDR_ALG |
| 6637 | PyModule_AddIntMacro(m, AF_ALG); /* Linux crypto */ |
| 6638 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6639 | #ifdef AF_X25 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6640 | /* Reserved for X.25 project */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6641 | PyModule_AddIntMacro(m, AF_X25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6642 | #endif |
| 6643 | #ifdef AF_INET6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6644 | PyModule_AddIntMacro(m, AF_INET6); /* IP version 6 */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6645 | #endif |
| 6646 | #ifdef AF_ROSE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6647 | /* Amateur Radio X.25 PLP */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6648 | PyModule_AddIntMacro(m, AF_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6649 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6650 | #ifdef AF_DECnet |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6651 | /* Reserved for DECnet project */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6652 | PyModule_AddIntMacro(m, AF_DECnet); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6653 | #endif |
| 6654 | #ifdef AF_NETBEUI |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6655 | /* Reserved for 802.2LLC project */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6656 | PyModule_AddIntMacro(m, AF_NETBEUI); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6657 | #endif |
| 6658 | #ifdef AF_SECURITY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6659 | /* Security callback pseudo AF */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6660 | PyModule_AddIntMacro(m, AF_SECURITY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6661 | #endif |
| 6662 | #ifdef AF_KEY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6663 | /* PF_KEY key management API */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6664 | PyModule_AddIntMacro(m, AF_KEY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6665 | #endif |
| 6666 | #ifdef AF_NETLINK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6667 | /* */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6668 | PyModule_AddIntMacro(m, AF_NETLINK); |
| 6669 | PyModule_AddIntMacro(m, NETLINK_ROUTE); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6670 | #ifdef NETLINK_SKIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6671 | PyModule_AddIntMacro(m, NETLINK_SKIP); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6672 | #endif |
| 6673 | #ifdef NETLINK_W1 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6674 | PyModule_AddIntMacro(m, NETLINK_W1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6675 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6676 | PyModule_AddIntMacro(m, NETLINK_USERSOCK); |
| 6677 | PyModule_AddIntMacro(m, NETLINK_FIREWALL); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 6678 | #ifdef NETLINK_TCPDIAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6679 | PyModule_AddIntMacro(m, NETLINK_TCPDIAG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 6680 | #endif |
| 6681 | #ifdef NETLINK_NFLOG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6682 | PyModule_AddIntMacro(m, NETLINK_NFLOG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 6683 | #endif |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 6684 | #ifdef NETLINK_XFRM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6685 | PyModule_AddIntMacro(m, NETLINK_XFRM); |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 6686 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6687 | #ifdef NETLINK_ARPD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6688 | PyModule_AddIntMacro(m, NETLINK_ARPD); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6689 | #endif |
| 6690 | #ifdef NETLINK_ROUTE6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6691 | PyModule_AddIntMacro(m, NETLINK_ROUTE6); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6692 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6693 | PyModule_AddIntMacro(m, NETLINK_IP6_FW); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6694 | #ifdef NETLINK_DNRTMSG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6695 | PyModule_AddIntMacro(m, NETLINK_DNRTMSG); |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 6696 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6697 | #ifdef NETLINK_TAPBASE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6698 | PyModule_AddIntMacro(m, NETLINK_TAPBASE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6699 | #endif |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 6700 | #ifdef NETLINK_CRYPTO |
| 6701 | PyModule_AddIntMacro(m, NETLINK_CRYPTO); |
| 6702 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6703 | #endif /* AF_NETLINK */ |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6704 | #ifdef AF_ROUTE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6705 | /* Alias to emulate 4.4BSD */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6706 | PyModule_AddIntMacro(m, AF_ROUTE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6707 | #endif |
Giampaolo Rodola' | 80e1c43 | 2013-05-21 21:02:04 +0200 | [diff] [blame] | 6708 | #ifdef AF_LINK |
| 6709 | PyModule_AddIntMacro(m, AF_LINK); |
| 6710 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6711 | #ifdef AF_ASH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6712 | /* Ash */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6713 | PyModule_AddIntMacro(m, AF_ASH); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6714 | #endif |
| 6715 | #ifdef AF_ECONET |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6716 | /* Acorn Econet */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6717 | PyModule_AddIntMacro(m, AF_ECONET); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6718 | #endif |
| 6719 | #ifdef AF_ATMSVC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6720 | /* ATM SVCs */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6721 | PyModule_AddIntMacro(m, AF_ATMSVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6722 | #endif |
| 6723 | #ifdef AF_SNA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6724 | /* Linux SNA Project (nutters!) */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6725 | PyModule_AddIntMacro(m, AF_SNA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6726 | #endif |
| 6727 | #ifdef AF_IRDA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6728 | /* IRDA sockets */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6729 | PyModule_AddIntMacro(m, AF_IRDA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6730 | #endif |
| 6731 | #ifdef AF_PPPOX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6732 | /* PPPoX sockets */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6733 | PyModule_AddIntMacro(m, AF_PPPOX); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6734 | #endif |
| 6735 | #ifdef AF_WANPIPE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6736 | /* Wanpipe API Sockets */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6737 | PyModule_AddIntMacro(m, AF_WANPIPE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6738 | #endif |
| 6739 | #ifdef AF_LLC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6740 | /* Linux LLC */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6741 | PyModule_AddIntMacro(m, AF_LLC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6742 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 6743 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 6744 | #ifdef USE_BLUETOOTH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6745 | PyModule_AddIntMacro(m, AF_BLUETOOTH); |
| 6746 | PyModule_AddIntMacro(m, BTPROTO_L2CAP); |
| 6747 | PyModule_AddIntMacro(m, BTPROTO_HCI); |
| 6748 | PyModule_AddIntMacro(m, SOL_HCI); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 6749 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6750 | PyModule_AddIntMacro(m, HCI_FILTER); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 6751 | #endif |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 6752 | #if !defined(__FreeBSD__) |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 6753 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6754 | PyModule_AddIntMacro(m, HCI_TIME_STAMP); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 6755 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6756 | PyModule_AddIntMacro(m, HCI_DATA_DIR); |
| 6757 | PyModule_AddIntMacro(m, BTPROTO_SCO); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 6758 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6759 | PyModule_AddIntMacro(m, BTPROTO_RFCOMM); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6760 | PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00"); |
| 6761 | 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] | 6762 | #endif |
| 6763 | |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6764 | #ifdef AF_CAN |
| 6765 | /* Controller Area Network */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6766 | PyModule_AddIntMacro(m, AF_CAN); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6767 | #endif |
| 6768 | #ifdef PF_CAN |
| 6769 | /* Controller Area Network */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6770 | PyModule_AddIntMacro(m, PF_CAN); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6771 | #endif |
| 6772 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6773 | /* Reliable Datagram Sockets */ |
| 6774 | #ifdef AF_RDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6775 | PyModule_AddIntMacro(m, AF_RDS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6776 | #endif |
| 6777 | #ifdef PF_RDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6778 | PyModule_AddIntMacro(m, PF_RDS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6779 | #endif |
| 6780 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6781 | /* Kernel event messages */ |
| 6782 | #ifdef PF_SYSTEM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6783 | PyModule_AddIntMacro(m, PF_SYSTEM); |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6784 | #endif |
| 6785 | #ifdef AF_SYSTEM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6786 | PyModule_AddIntMacro(m, AF_SYSTEM); |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6787 | #endif |
| 6788 | |
Antoine Pitrou | b156a46 | 2010-10-27 20:13:57 +0000 | [diff] [blame] | 6789 | #ifdef AF_PACKET |
| 6790 | PyModule_AddIntMacro(m, AF_PACKET); |
| 6791 | #endif |
| 6792 | #ifdef PF_PACKET |
| 6793 | PyModule_AddIntMacro(m, PF_PACKET); |
| 6794 | #endif |
| 6795 | #ifdef PACKET_HOST |
| 6796 | PyModule_AddIntMacro(m, PACKET_HOST); |
| 6797 | #endif |
| 6798 | #ifdef PACKET_BROADCAST |
| 6799 | PyModule_AddIntMacro(m, PACKET_BROADCAST); |
| 6800 | #endif |
| 6801 | #ifdef PACKET_MULTICAST |
| 6802 | PyModule_AddIntMacro(m, PACKET_MULTICAST); |
| 6803 | #endif |
| 6804 | #ifdef PACKET_OTHERHOST |
| 6805 | PyModule_AddIntMacro(m, PACKET_OTHERHOST); |
| 6806 | #endif |
| 6807 | #ifdef PACKET_OUTGOING |
| 6808 | PyModule_AddIntMacro(m, PACKET_OUTGOING); |
| 6809 | #endif |
| 6810 | #ifdef PACKET_LOOPBACK |
| 6811 | PyModule_AddIntMacro(m, PACKET_LOOPBACK); |
| 6812 | #endif |
| 6813 | #ifdef PACKET_FASTROUTE |
| 6814 | PyModule_AddIntMacro(m, PACKET_FASTROUTE); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 6815 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6816 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6817 | #ifdef HAVE_LINUX_TIPC_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6818 | PyModule_AddIntMacro(m, AF_TIPC); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6819 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6820 | /* for addresses */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6821 | PyModule_AddIntMacro(m, TIPC_ADDR_NAMESEQ); |
| 6822 | PyModule_AddIntMacro(m, TIPC_ADDR_NAME); |
| 6823 | PyModule_AddIntMacro(m, TIPC_ADDR_ID); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6824 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6825 | PyModule_AddIntMacro(m, TIPC_ZONE_SCOPE); |
| 6826 | PyModule_AddIntMacro(m, TIPC_CLUSTER_SCOPE); |
| 6827 | PyModule_AddIntMacro(m, TIPC_NODE_SCOPE); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6828 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6829 | /* for setsockopt() */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6830 | PyModule_AddIntMacro(m, SOL_TIPC); |
| 6831 | PyModule_AddIntMacro(m, TIPC_IMPORTANCE); |
| 6832 | PyModule_AddIntMacro(m, TIPC_SRC_DROPPABLE); |
| 6833 | PyModule_AddIntMacro(m, TIPC_DEST_DROPPABLE); |
| 6834 | PyModule_AddIntMacro(m, TIPC_CONN_TIMEOUT); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6835 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6836 | PyModule_AddIntMacro(m, TIPC_LOW_IMPORTANCE); |
| 6837 | PyModule_AddIntMacro(m, TIPC_MEDIUM_IMPORTANCE); |
| 6838 | PyModule_AddIntMacro(m, TIPC_HIGH_IMPORTANCE); |
| 6839 | PyModule_AddIntMacro(m, TIPC_CRITICAL_IMPORTANCE); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6840 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6841 | /* for subscriptions */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6842 | PyModule_AddIntMacro(m, TIPC_SUB_PORTS); |
| 6843 | PyModule_AddIntMacro(m, TIPC_SUB_SERVICE); |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 6844 | #ifdef TIPC_SUB_CANCEL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6845 | /* doesn't seem to be available everywhere */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6846 | PyModule_AddIntMacro(m, TIPC_SUB_CANCEL); |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 6847 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6848 | PyModule_AddIntMacro(m, TIPC_WAIT_FOREVER); |
| 6849 | PyModule_AddIntMacro(m, TIPC_PUBLISHED); |
| 6850 | PyModule_AddIntMacro(m, TIPC_WITHDRAWN); |
| 6851 | PyModule_AddIntMacro(m, TIPC_SUBSCR_TIMEOUT); |
| 6852 | PyModule_AddIntMacro(m, TIPC_CFG_SRV); |
| 6853 | PyModule_AddIntMacro(m, TIPC_TOP_SRV); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6854 | #endif |
| 6855 | |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 6856 | #ifdef HAVE_SOCKADDR_ALG |
| 6857 | /* Socket options */ |
| 6858 | PyModule_AddIntMacro(m, ALG_SET_KEY); |
| 6859 | PyModule_AddIntMacro(m, ALG_SET_IV); |
| 6860 | PyModule_AddIntMacro(m, ALG_SET_OP); |
| 6861 | PyModule_AddIntMacro(m, ALG_SET_AEAD_ASSOCLEN); |
| 6862 | PyModule_AddIntMacro(m, ALG_SET_AEAD_AUTHSIZE); |
| 6863 | PyModule_AddIntMacro(m, ALG_SET_PUBKEY); |
| 6864 | |
| 6865 | /* Operations */ |
| 6866 | PyModule_AddIntMacro(m, ALG_OP_DECRYPT); |
| 6867 | PyModule_AddIntMacro(m, ALG_OP_ENCRYPT); |
| 6868 | PyModule_AddIntMacro(m, ALG_OP_SIGN); |
| 6869 | PyModule_AddIntMacro(m, ALG_OP_VERIFY); |
| 6870 | #endif |
| 6871 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6872 | /* Socket types */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6873 | PyModule_AddIntMacro(m, SOCK_STREAM); |
| 6874 | PyModule_AddIntMacro(m, SOCK_DGRAM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 6875 | /* We have incomplete socket support. */ |
Berker Peksag | 95e0df8 | 2016-08-08 14:07:05 +0300 | [diff] [blame] | 6876 | #ifdef SOCK_RAW |
| 6877 | /* SOCK_RAW is marked as optional in the POSIX specification */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6878 | PyModule_AddIntMacro(m, SOCK_RAW); |
Berker Peksag | 95e0df8 | 2016-08-08 14:07:05 +0300 | [diff] [blame] | 6879 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6880 | PyModule_AddIntMacro(m, SOCK_SEQPACKET); |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 6881 | #if defined(SOCK_RDM) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6882 | PyModule_AddIntMacro(m, SOCK_RDM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 6883 | #endif |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 6884 | #ifdef SOCK_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6885 | PyModule_AddIntMacro(m, SOCK_CLOEXEC); |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 6886 | #endif |
| 6887 | #ifdef SOCK_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6888 | PyModule_AddIntMacro(m, SOCK_NONBLOCK); |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 6889 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6891 | #ifdef SO_DEBUG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6892 | PyModule_AddIntMacro(m, SO_DEBUG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6893 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6894 | #ifdef SO_ACCEPTCONN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6895 | PyModule_AddIntMacro(m, SO_ACCEPTCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6896 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6897 | #ifdef SO_REUSEADDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6898 | PyModule_AddIntMacro(m, SO_REUSEADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6899 | #endif |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 6900 | #ifdef SO_EXCLUSIVEADDRUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6901 | PyModule_AddIntMacro(m, SO_EXCLUSIVEADDRUSE); |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 6902 | #endif |
| 6903 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6904 | #ifdef SO_KEEPALIVE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6905 | PyModule_AddIntMacro(m, SO_KEEPALIVE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6906 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6907 | #ifdef SO_DONTROUTE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6908 | PyModule_AddIntMacro(m, SO_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6909 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6910 | #ifdef SO_BROADCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6911 | PyModule_AddIntMacro(m, SO_BROADCAST); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6912 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6913 | #ifdef SO_USELOOPBACK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6914 | PyModule_AddIntMacro(m, SO_USELOOPBACK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6915 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6916 | #ifdef SO_LINGER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6917 | PyModule_AddIntMacro(m, SO_LINGER); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6918 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6919 | #ifdef SO_OOBINLINE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6920 | PyModule_AddIntMacro(m, SO_OOBINLINE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6921 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 6922 | #ifndef __GNU__ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6923 | #ifdef SO_REUSEPORT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6924 | PyModule_AddIntMacro(m, SO_REUSEPORT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6925 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 6926 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6927 | #ifdef SO_SNDBUF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6928 | PyModule_AddIntMacro(m, SO_SNDBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6929 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6930 | #ifdef SO_RCVBUF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6931 | PyModule_AddIntMacro(m, SO_RCVBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6932 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6933 | #ifdef SO_SNDLOWAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6934 | PyModule_AddIntMacro(m, SO_SNDLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6935 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6936 | #ifdef SO_RCVLOWAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6937 | PyModule_AddIntMacro(m, SO_RCVLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6938 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6939 | #ifdef SO_SNDTIMEO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6940 | PyModule_AddIntMacro(m, SO_SNDTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6941 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6942 | #ifdef SO_RCVTIMEO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6943 | PyModule_AddIntMacro(m, SO_RCVTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6944 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6945 | #ifdef SO_ERROR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6946 | PyModule_AddIntMacro(m, SO_ERROR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6947 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6948 | #ifdef SO_TYPE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6949 | PyModule_AddIntMacro(m, SO_TYPE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6950 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6951 | #ifdef SO_SETFIB |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6952 | PyModule_AddIntMacro(m, SO_SETFIB); |
Larry Hastings | f0f3795 | 2010-04-02 11:47:10 +0000 | [diff] [blame] | 6953 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6954 | #ifdef SO_PASSCRED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6955 | PyModule_AddIntMacro(m, SO_PASSCRED); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6956 | #endif |
| 6957 | #ifdef SO_PEERCRED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6958 | PyModule_AddIntMacro(m, SO_PEERCRED); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6959 | #endif |
| 6960 | #ifdef LOCAL_PEERCRED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6961 | PyModule_AddIntMacro(m, LOCAL_PEERCRED); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6962 | #endif |
R David Murray | bdfa0eb | 2016-08-23 21:12:40 -0400 | [diff] [blame] | 6963 | #ifdef SO_PASSSEC |
| 6964 | PyModule_AddIntMacro(m, SO_PASSSEC); |
| 6965 | #endif |
| 6966 | #ifdef SO_PEERSEC |
| 6967 | PyModule_AddIntMacro(m, SO_PEERSEC); |
| 6968 | #endif |
Victor Stinner | cb98bed | 2011-10-25 13:45:48 +0200 | [diff] [blame] | 6969 | #ifdef SO_BINDTODEVICE |
| 6970 | PyModule_AddIntMacro(m, SO_BINDTODEVICE); |
| 6971 | #endif |
Benjamin Peterson | 1314ef7 | 2013-11-27 09:18:54 -0600 | [diff] [blame] | 6972 | #ifdef SO_PRIORITY |
| 6973 | PyModule_AddIntMacro(m, SO_PRIORITY); |
| 6974 | #endif |
Charles-François Natali | ea07eb9 | 2014-09-18 23:18:46 +0100 | [diff] [blame] | 6975 | #ifdef SO_MARK |
| 6976 | PyModule_AddIntMacro(m, SO_MARK); |
| 6977 | #endif |
R David Murray | bdfa0eb | 2016-08-23 21:12:40 -0400 | [diff] [blame] | 6978 | #ifdef SO_DOMAIN |
| 6979 | PyModule_AddIntMacro(m, SO_DOMAIN); |
| 6980 | #endif |
| 6981 | #ifdef SO_PROTOCOL |
| 6982 | PyModule_AddIntMacro(m, SO_PROTOCOL); |
| 6983 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6984 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6985 | /* Maximum number of connections for "listen" */ |
| 6986 | #ifdef SOMAXCONN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6987 | PyModule_AddIntMacro(m, SOMAXCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6988 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6989 | PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6990 | #endif |
| 6991 | |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 6992 | /* Ancillary message types */ |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6993 | #ifdef SCM_RIGHTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6994 | PyModule_AddIntMacro(m, SCM_RIGHTS); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6995 | #endif |
| 6996 | #ifdef SCM_CREDENTIALS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6997 | PyModule_AddIntMacro(m, SCM_CREDENTIALS); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6998 | #endif |
| 6999 | #ifdef SCM_CREDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7000 | PyModule_AddIntMacro(m, SCM_CREDS); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 7001 | #endif |
| 7002 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7003 | /* Flags for send, recv */ |
| 7004 | #ifdef MSG_OOB |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7005 | PyModule_AddIntMacro(m, MSG_OOB); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7006 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7007 | #ifdef MSG_PEEK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7008 | PyModule_AddIntMacro(m, MSG_PEEK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7009 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7010 | #ifdef MSG_DONTROUTE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7011 | PyModule_AddIntMacro(m, MSG_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7012 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7013 | #ifdef MSG_DONTWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7014 | PyModule_AddIntMacro(m, MSG_DONTWAIT); |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 7015 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7016 | #ifdef MSG_EOR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7017 | PyModule_AddIntMacro(m, MSG_EOR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7018 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7019 | #ifdef MSG_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7020 | PyModule_AddIntMacro(m, MSG_TRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7021 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7022 | #ifdef MSG_CTRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7023 | PyModule_AddIntMacro(m, MSG_CTRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7024 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7025 | #ifdef MSG_WAITALL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7026 | PyModule_AddIntMacro(m, MSG_WAITALL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7027 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7028 | #ifdef MSG_BTAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7029 | PyModule_AddIntMacro(m, MSG_BTAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7030 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7031 | #ifdef MSG_ETAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7032 | PyModule_AddIntMacro(m, MSG_ETAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7033 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 7034 | #ifdef MSG_NOSIGNAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7035 | PyModule_AddIntMacro(m, MSG_NOSIGNAL); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 7036 | #endif |
| 7037 | #ifdef MSG_NOTIFICATION |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7038 | PyModule_AddIntMacro(m, MSG_NOTIFICATION); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 7039 | #endif |
| 7040 | #ifdef MSG_CMSG_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7041 | PyModule_AddIntMacro(m, MSG_CMSG_CLOEXEC); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 7042 | #endif |
| 7043 | #ifdef MSG_ERRQUEUE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7044 | PyModule_AddIntMacro(m, MSG_ERRQUEUE); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 7045 | #endif |
| 7046 | #ifdef MSG_CONFIRM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7047 | PyModule_AddIntMacro(m, MSG_CONFIRM); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 7048 | #endif |
| 7049 | #ifdef MSG_MORE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7050 | PyModule_AddIntMacro(m, MSG_MORE); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 7051 | #endif |
| 7052 | #ifdef MSG_EOF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7053 | PyModule_AddIntMacro(m, MSG_EOF); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 7054 | #endif |
| 7055 | #ifdef MSG_BCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7056 | PyModule_AddIntMacro(m, MSG_BCAST); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 7057 | #endif |
| 7058 | #ifdef MSG_MCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7059 | PyModule_AddIntMacro(m, MSG_MCAST); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 7060 | #endif |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 7061 | #ifdef MSG_FASTOPEN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7062 | PyModule_AddIntMacro(m, MSG_FASTOPEN); |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 7063 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7065 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 7066 | #ifdef SOL_SOCKET |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7067 | PyModule_AddIntMacro(m, SOL_SOCKET); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7068 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7069 | #ifdef SOL_IP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7070 | PyModule_AddIntMacro(m, SOL_IP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 7071 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7072 | PyModule_AddIntConstant(m, "SOL_IP", 0); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 7073 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7074 | #ifdef SOL_IPX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7075 | PyModule_AddIntMacro(m, SOL_IPX); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 7076 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7077 | #ifdef SOL_AX25 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7078 | PyModule_AddIntMacro(m, SOL_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 7079 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7080 | #ifdef SOL_ATALK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7081 | PyModule_AddIntMacro(m, SOL_ATALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 7082 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7083 | #ifdef SOL_NETROM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7084 | PyModule_AddIntMacro(m, SOL_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 7085 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7086 | #ifdef SOL_ROSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7087 | PyModule_AddIntMacro(m, SOL_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 7088 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7089 | #ifdef SOL_TCP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7090 | PyModule_AddIntMacro(m, SOL_TCP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 7091 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7092 | PyModule_AddIntConstant(m, "SOL_TCP", 6); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 7093 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7094 | #ifdef SOL_UDP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7095 | PyModule_AddIntMacro(m, SOL_UDP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 7096 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7097 | PyModule_AddIntConstant(m, "SOL_UDP", 17); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 7098 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 7099 | #ifdef SOL_CAN_BASE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7100 | PyModule_AddIntMacro(m, SOL_CAN_BASE); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 7101 | #endif |
| 7102 | #ifdef SOL_CAN_RAW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7103 | PyModule_AddIntMacro(m, SOL_CAN_RAW); |
| 7104 | PyModule_AddIntMacro(m, CAN_RAW); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 7105 | #endif |
| 7106 | #ifdef HAVE_LINUX_CAN_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7107 | PyModule_AddIntMacro(m, CAN_EFF_FLAG); |
| 7108 | PyModule_AddIntMacro(m, CAN_RTR_FLAG); |
| 7109 | PyModule_AddIntMacro(m, CAN_ERR_FLAG); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 7110 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7111 | PyModule_AddIntMacro(m, CAN_SFF_MASK); |
| 7112 | PyModule_AddIntMacro(m, CAN_EFF_MASK); |
| 7113 | PyModule_AddIntMacro(m, CAN_ERR_MASK); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 7114 | #endif |
| 7115 | #ifdef HAVE_LINUX_CAN_RAW_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7116 | PyModule_AddIntMacro(m, CAN_RAW_FILTER); |
| 7117 | PyModule_AddIntMacro(m, CAN_RAW_ERR_FILTER); |
| 7118 | PyModule_AddIntMacro(m, CAN_RAW_LOOPBACK); |
| 7119 | PyModule_AddIntMacro(m, CAN_RAW_RECV_OWN_MSGS); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 7120 | #endif |
Larry Hastings | a6cc551 | 2015-04-13 17:48:40 -0400 | [diff] [blame] | 7121 | #ifdef HAVE_LINUX_CAN_RAW_FD_FRAMES |
| 7122 | PyModule_AddIntMacro(m, CAN_RAW_FD_FRAMES); |
| 7123 | #endif |
Charles-François Natali | 773e42d | 2013-02-05 19:42:01 +0100 | [diff] [blame] | 7124 | #ifdef HAVE_LINUX_CAN_BCM_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7125 | PyModule_AddIntMacro(m, CAN_BCM); |
Charles-François Natali | 773e42d | 2013-02-05 19:42:01 +0100 | [diff] [blame] | 7126 | PyModule_AddIntConstant(m, "CAN_BCM_TX_SETUP", TX_SETUP); |
| 7127 | PyModule_AddIntConstant(m, "CAN_BCM_TX_DELETE", TX_DELETE); |
| 7128 | PyModule_AddIntConstant(m, "CAN_BCM_TX_READ", TX_READ); |
| 7129 | PyModule_AddIntConstant(m, "CAN_BCM_TX_SEND", TX_SEND); |
| 7130 | PyModule_AddIntConstant(m, "CAN_BCM_RX_SETUP", RX_SETUP); |
| 7131 | PyModule_AddIntConstant(m, "CAN_BCM_RX_DELETE", RX_DELETE); |
| 7132 | PyModule_AddIntConstant(m, "CAN_BCM_RX_READ", RX_READ); |
| 7133 | PyModule_AddIntConstant(m, "CAN_BCM_TX_STATUS", TX_STATUS); |
| 7134 | PyModule_AddIntConstant(m, "CAN_BCM_TX_EXPIRED", TX_EXPIRED); |
| 7135 | PyModule_AddIntConstant(m, "CAN_BCM_RX_STATUS", RX_STATUS); |
| 7136 | PyModule_AddIntConstant(m, "CAN_BCM_RX_TIMEOUT", RX_TIMEOUT); |
| 7137 | PyModule_AddIntConstant(m, "CAN_BCM_RX_CHANGED", RX_CHANGED); |
| 7138 | #endif |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 7139 | #ifdef SOL_RDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7140 | PyModule_AddIntMacro(m, SOL_RDS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 7141 | #endif |
Christian Heimes | dffa394 | 2016-09-05 23:54:41 +0200 | [diff] [blame] | 7142 | #ifdef HAVE_SOCKADDR_ALG |
| 7143 | PyModule_AddIntMacro(m, SOL_ALG); |
| 7144 | #endif |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 7145 | #ifdef RDS_CANCEL_SENT_TO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7146 | PyModule_AddIntMacro(m, RDS_CANCEL_SENT_TO); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 7147 | #endif |
| 7148 | #ifdef RDS_GET_MR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7149 | PyModule_AddIntMacro(m, RDS_GET_MR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 7150 | #endif |
| 7151 | #ifdef RDS_FREE_MR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7152 | PyModule_AddIntMacro(m, RDS_FREE_MR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 7153 | #endif |
| 7154 | #ifdef RDS_RECVERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7155 | PyModule_AddIntMacro(m, RDS_RECVERR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 7156 | #endif |
| 7157 | #ifdef RDS_CONG_MONITOR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7158 | PyModule_AddIntMacro(m, RDS_CONG_MONITOR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 7159 | #endif |
| 7160 | #ifdef RDS_GET_MR_FOR_DEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7161 | PyModule_AddIntMacro(m, RDS_GET_MR_FOR_DEST); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 7162 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7163 | #ifdef IPPROTO_IP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7164 | PyModule_AddIntMacro(m, IPPROTO_IP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 7165 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7166 | PyModule_AddIntConstant(m, "IPPROTO_IP", 0); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7167 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7168 | #ifdef IPPROTO_HOPOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7169 | PyModule_AddIntMacro(m, IPPROTO_HOPOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7170 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7171 | #ifdef IPPROTO_ICMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7172 | PyModule_AddIntMacro(m, IPPROTO_ICMP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 7173 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7174 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7175 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7176 | #ifdef IPPROTO_IGMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7177 | PyModule_AddIntMacro(m, IPPROTO_IGMP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7178 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7179 | #ifdef IPPROTO_GGP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7180 | PyModule_AddIntMacro(m, IPPROTO_GGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7181 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7182 | #ifdef IPPROTO_IPV4 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7183 | PyModule_AddIntMacro(m, IPPROTO_IPV4); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7184 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7185 | #ifdef IPPROTO_IPV6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7186 | PyModule_AddIntMacro(m, IPPROTO_IPV6); |
Martin v. Löwis | a0f1734 | 2003-10-03 13:56:20 +0000 | [diff] [blame] | 7187 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7188 | #ifdef IPPROTO_IPIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7189 | PyModule_AddIntMacro(m, IPPROTO_IPIP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7190 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7191 | #ifdef IPPROTO_TCP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7192 | PyModule_AddIntMacro(m, IPPROTO_TCP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 7193 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7194 | PyModule_AddIntConstant(m, "IPPROTO_TCP", 6); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7195 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7196 | #ifdef IPPROTO_EGP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7197 | PyModule_AddIntMacro(m, IPPROTO_EGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7198 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7199 | #ifdef IPPROTO_PUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7200 | PyModule_AddIntMacro(m, IPPROTO_PUP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7201 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7202 | #ifdef IPPROTO_UDP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7203 | PyModule_AddIntMacro(m, IPPROTO_UDP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 7204 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7205 | PyModule_AddIntConstant(m, "IPPROTO_UDP", 17); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7206 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7207 | #ifdef IPPROTO_IDP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7208 | PyModule_AddIntMacro(m, IPPROTO_IDP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7209 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7210 | #ifdef IPPROTO_HELLO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7211 | PyModule_AddIntMacro(m, IPPROTO_HELLO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7212 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7213 | #ifdef IPPROTO_ND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7214 | PyModule_AddIntMacro(m, IPPROTO_ND); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7215 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7216 | #ifdef IPPROTO_TP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7217 | PyModule_AddIntMacro(m, IPPROTO_TP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7218 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7219 | #ifdef IPPROTO_IPV6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7220 | PyModule_AddIntMacro(m, IPPROTO_IPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7221 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7222 | #ifdef IPPROTO_ROUTING |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7223 | PyModule_AddIntMacro(m, IPPROTO_ROUTING); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7224 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7225 | #ifdef IPPROTO_FRAGMENT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7226 | PyModule_AddIntMacro(m, IPPROTO_FRAGMENT); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7227 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7228 | #ifdef IPPROTO_RSVP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7229 | PyModule_AddIntMacro(m, IPPROTO_RSVP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7230 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7231 | #ifdef IPPROTO_GRE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7232 | PyModule_AddIntMacro(m, IPPROTO_GRE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7233 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7234 | #ifdef IPPROTO_ESP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7235 | PyModule_AddIntMacro(m, IPPROTO_ESP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7236 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7237 | #ifdef IPPROTO_AH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7238 | PyModule_AddIntMacro(m, IPPROTO_AH); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7239 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7240 | #ifdef IPPROTO_MOBILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7241 | PyModule_AddIntMacro(m, IPPROTO_MOBILE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7242 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7243 | #ifdef IPPROTO_ICMPV6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7244 | PyModule_AddIntMacro(m, IPPROTO_ICMPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7245 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7246 | #ifdef IPPROTO_NONE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7247 | PyModule_AddIntMacro(m, IPPROTO_NONE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7248 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7249 | #ifdef IPPROTO_DSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7250 | PyModule_AddIntMacro(m, IPPROTO_DSTOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7251 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7252 | #ifdef IPPROTO_XTP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7253 | PyModule_AddIntMacro(m, IPPROTO_XTP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7254 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7255 | #ifdef IPPROTO_EON |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7256 | PyModule_AddIntMacro(m, IPPROTO_EON); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7257 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7258 | #ifdef IPPROTO_PIM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7259 | PyModule_AddIntMacro(m, IPPROTO_PIM); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7260 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7261 | #ifdef IPPROTO_IPCOMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7262 | PyModule_AddIntMacro(m, IPPROTO_IPCOMP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7263 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7264 | #ifdef IPPROTO_VRRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7265 | PyModule_AddIntMacro(m, IPPROTO_VRRP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7266 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 7267 | #ifdef IPPROTO_SCTP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7268 | PyModule_AddIntMacro(m, IPPROTO_SCTP); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 7269 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7270 | #ifdef IPPROTO_BIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7271 | PyModule_AddIntMacro(m, IPPROTO_BIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7272 | #endif |
| 7273 | /**/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7274 | #ifdef IPPROTO_RAW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7275 | PyModule_AddIntMacro(m, IPPROTO_RAW); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 7276 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7277 | PyModule_AddIntConstant(m, "IPPROTO_RAW", 255); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7278 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7279 | #ifdef IPPROTO_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7280 | PyModule_AddIntMacro(m, IPPROTO_MAX); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7281 | #endif |
| 7282 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 7283 | #ifdef SYSPROTO_CONTROL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7284 | PyModule_AddIntMacro(m, SYSPROTO_CONTROL); |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 7285 | #endif |
| 7286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7287 | /* Some port configuration */ |
| 7288 | #ifdef IPPORT_RESERVED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7289 | PyModule_AddIntMacro(m, IPPORT_RESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7290 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7291 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7292 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7293 | #ifdef IPPORT_USERRESERVED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7294 | PyModule_AddIntMacro(m, IPPORT_USERRESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7295 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7296 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7297 | #endif |
| 7298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7299 | /* Some reserved IP v.4 addresses */ |
| 7300 | #ifdef INADDR_ANY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7301 | PyModule_AddIntMacro(m, INADDR_ANY); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 7302 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7303 | PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 7304 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7305 | #ifdef INADDR_BROADCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7306 | PyModule_AddIntMacro(m, INADDR_BROADCAST); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 7307 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7308 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 7309 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7310 | #ifdef INADDR_LOOPBACK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7311 | PyModule_AddIntMacro(m, INADDR_LOOPBACK); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 7312 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7313 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 7314 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7315 | #ifdef INADDR_UNSPEC_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7316 | PyModule_AddIntMacro(m, INADDR_UNSPEC_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 7317 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7318 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 7319 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7320 | #ifdef INADDR_ALLHOSTS_GROUP |
| 7321 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", |
| 7322 | INADDR_ALLHOSTS_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 7323 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7324 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 7325 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7326 | #ifdef INADDR_MAX_LOCAL_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7327 | PyModule_AddIntMacro(m, INADDR_MAX_LOCAL_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 7328 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7329 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 7330 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7331 | #ifdef INADDR_NONE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7332 | PyModule_AddIntMacro(m, INADDR_NONE); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 7333 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7334 | PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 7335 | #endif |
| 7336 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7337 | /* IPv4 [gs]etsockopt options */ |
| 7338 | #ifdef IP_OPTIONS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7339 | PyModule_AddIntMacro(m, IP_OPTIONS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7340 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7341 | #ifdef IP_HDRINCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7342 | PyModule_AddIntMacro(m, IP_HDRINCL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7343 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7344 | #ifdef IP_TOS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7345 | PyModule_AddIntMacro(m, IP_TOS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7346 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7347 | #ifdef IP_TTL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7348 | PyModule_AddIntMacro(m, IP_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7349 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7350 | #ifdef IP_RECVOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7351 | PyModule_AddIntMacro(m, IP_RECVOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7352 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7353 | #ifdef IP_RECVRETOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7354 | PyModule_AddIntMacro(m, IP_RECVRETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7355 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7356 | #ifdef IP_RECVDSTADDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7357 | PyModule_AddIntMacro(m, IP_RECVDSTADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7358 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7359 | #ifdef IP_RETOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7360 | PyModule_AddIntMacro(m, IP_RETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7361 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7362 | #ifdef IP_MULTICAST_IF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7363 | PyModule_AddIntMacro(m, IP_MULTICAST_IF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7364 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7365 | #ifdef IP_MULTICAST_TTL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7366 | PyModule_AddIntMacro(m, IP_MULTICAST_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7367 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7368 | #ifdef IP_MULTICAST_LOOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7369 | PyModule_AddIntMacro(m, IP_MULTICAST_LOOP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7370 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7371 | #ifdef IP_ADD_MEMBERSHIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7372 | PyModule_AddIntMacro(m, IP_ADD_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7373 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7374 | #ifdef IP_DROP_MEMBERSHIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7375 | PyModule_AddIntMacro(m, IP_DROP_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 7376 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7377 | #ifdef IP_DEFAULT_MULTICAST_TTL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7378 | PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_TTL); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 7379 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7380 | #ifdef IP_DEFAULT_MULTICAST_LOOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7381 | PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_LOOP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 7382 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7383 | #ifdef IP_MAX_MEMBERSHIPS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7384 | PyModule_AddIntMacro(m, IP_MAX_MEMBERSHIPS); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 7385 | #endif |
Charles-François Natali | 564a42c | 2011-12-17 14:59:56 +0100 | [diff] [blame] | 7386 | #ifdef IP_TRANSPARENT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7387 | PyModule_AddIntMacro(m, IP_TRANSPARENT); |
Charles-François Natali | 564a42c | 2011-12-17 14:59:56 +0100 | [diff] [blame] | 7388 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 7389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7390 | /* IPv6 [gs]etsockopt options, defined in RFC2553 */ |
| 7391 | #ifdef IPV6_JOIN_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7392 | PyModule_AddIntMacro(m, IPV6_JOIN_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7393 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7394 | #ifdef IPV6_LEAVE_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7395 | PyModule_AddIntMacro(m, IPV6_LEAVE_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7396 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7397 | #ifdef IPV6_MULTICAST_HOPS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7398 | PyModule_AddIntMacro(m, IPV6_MULTICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7399 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7400 | #ifdef IPV6_MULTICAST_IF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7401 | PyModule_AddIntMacro(m, IPV6_MULTICAST_IF); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7402 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7403 | #ifdef IPV6_MULTICAST_LOOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7404 | PyModule_AddIntMacro(m, IPV6_MULTICAST_LOOP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7405 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7406 | #ifdef IPV6_UNICAST_HOPS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7407 | PyModule_AddIntMacro(m, IPV6_UNICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7408 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7409 | /* Additional IPV6 socket options, defined in RFC 3493 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7410 | #ifdef IPV6_V6ONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7411 | PyModule_AddIntMacro(m, IPV6_V6ONLY); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7412 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7413 | /* Advanced IPV6 socket options, from RFC 3542 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7414 | #ifdef IPV6_CHECKSUM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7415 | PyModule_AddIntMacro(m, IPV6_CHECKSUM); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7416 | #endif |
| 7417 | #ifdef IPV6_DONTFRAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7418 | PyModule_AddIntMacro(m, IPV6_DONTFRAG); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7419 | #endif |
| 7420 | #ifdef IPV6_DSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7421 | PyModule_AddIntMacro(m, IPV6_DSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7422 | #endif |
| 7423 | #ifdef IPV6_HOPLIMIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7424 | PyModule_AddIntMacro(m, IPV6_HOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7425 | #endif |
| 7426 | #ifdef IPV6_HOPOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7427 | PyModule_AddIntMacro(m, IPV6_HOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7428 | #endif |
| 7429 | #ifdef IPV6_NEXTHOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7430 | PyModule_AddIntMacro(m, IPV6_NEXTHOP); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7431 | #endif |
| 7432 | #ifdef IPV6_PATHMTU |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7433 | PyModule_AddIntMacro(m, IPV6_PATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7434 | #endif |
| 7435 | #ifdef IPV6_PKTINFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7436 | PyModule_AddIntMacro(m, IPV6_PKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7437 | #endif |
| 7438 | #ifdef IPV6_RECVDSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7439 | PyModule_AddIntMacro(m, IPV6_RECVDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7440 | #endif |
| 7441 | #ifdef IPV6_RECVHOPLIMIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7442 | PyModule_AddIntMacro(m, IPV6_RECVHOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7443 | #endif |
| 7444 | #ifdef IPV6_RECVHOPOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7445 | PyModule_AddIntMacro(m, IPV6_RECVHOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7446 | #endif |
| 7447 | #ifdef IPV6_RECVPKTINFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7448 | PyModule_AddIntMacro(m, IPV6_RECVPKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7449 | #endif |
| 7450 | #ifdef IPV6_RECVRTHDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7451 | PyModule_AddIntMacro(m, IPV6_RECVRTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7452 | #endif |
| 7453 | #ifdef IPV6_RECVTCLASS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7454 | PyModule_AddIntMacro(m, IPV6_RECVTCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7455 | #endif |
| 7456 | #ifdef IPV6_RTHDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7457 | PyModule_AddIntMacro(m, IPV6_RTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7458 | #endif |
| 7459 | #ifdef IPV6_RTHDRDSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7460 | PyModule_AddIntMacro(m, IPV6_RTHDRDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7461 | #endif |
| 7462 | #ifdef IPV6_RTHDR_TYPE_0 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7463 | PyModule_AddIntMacro(m, IPV6_RTHDR_TYPE_0); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7464 | #endif |
| 7465 | #ifdef IPV6_RECVPATHMTU |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7466 | PyModule_AddIntMacro(m, IPV6_RECVPATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7467 | #endif |
| 7468 | #ifdef IPV6_TCLASS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7469 | PyModule_AddIntMacro(m, IPV6_TCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7470 | #endif |
| 7471 | #ifdef IPV6_USE_MIN_MTU |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7472 | PyModule_AddIntMacro(m, IPV6_USE_MIN_MTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7473 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 7474 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7475 | /* TCP options */ |
| 7476 | #ifdef TCP_NODELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7477 | PyModule_AddIntMacro(m, TCP_NODELAY); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 7478 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7479 | #ifdef TCP_MAXSEG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7480 | PyModule_AddIntMacro(m, TCP_MAXSEG); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 7481 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7482 | #ifdef TCP_CORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7483 | PyModule_AddIntMacro(m, TCP_CORK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 7484 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7485 | #ifdef TCP_KEEPIDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7486 | PyModule_AddIntMacro(m, TCP_KEEPIDLE); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 7487 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7488 | #ifdef TCP_KEEPINTVL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7489 | PyModule_AddIntMacro(m, TCP_KEEPINTVL); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 7490 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7491 | #ifdef TCP_KEEPCNT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7492 | PyModule_AddIntMacro(m, TCP_KEEPCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 7493 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7494 | #ifdef TCP_SYNCNT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7495 | PyModule_AddIntMacro(m, TCP_SYNCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 7496 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7497 | #ifdef TCP_LINGER2 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7498 | PyModule_AddIntMacro(m, TCP_LINGER2); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 7499 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7500 | #ifdef TCP_DEFER_ACCEPT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7501 | PyModule_AddIntMacro(m, TCP_DEFER_ACCEPT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 7502 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7503 | #ifdef TCP_WINDOW_CLAMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7504 | PyModule_AddIntMacro(m, TCP_WINDOW_CLAMP); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 7505 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7506 | #ifdef TCP_INFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7507 | PyModule_AddIntMacro(m, TCP_INFO); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 7508 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7509 | #ifdef TCP_QUICKACK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7510 | PyModule_AddIntMacro(m, TCP_QUICKACK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 7511 | #endif |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 7512 | #ifdef TCP_FASTOPEN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7513 | PyModule_AddIntMacro(m, TCP_FASTOPEN); |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 7514 | #endif |
Victor Stinner | 11dd604 | 2016-11-29 16:55:04 +0100 | [diff] [blame] | 7515 | #ifdef TCP_CONGESTION |
| 7516 | PyModule_AddIntMacro(m, TCP_CONGESTION); |
| 7517 | #endif |
| 7518 | #ifdef TCP_USER_TIMEOUT |
| 7519 | PyModule_AddIntMacro(m, TCP_USER_TIMEOUT); |
| 7520 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 7521 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7522 | /* IPX options */ |
| 7523 | #ifdef IPX_TYPE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7524 | PyModule_AddIntMacro(m, IPX_TYPE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 7525 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 7526 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 7527 | /* Reliable Datagram Sockets */ |
| 7528 | #ifdef RDS_CMSG_RDMA_ARGS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7529 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_ARGS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 7530 | #endif |
| 7531 | #ifdef RDS_CMSG_RDMA_DEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7532 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_DEST); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 7533 | #endif |
| 7534 | #ifdef RDS_CMSG_RDMA_MAP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7535 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_MAP); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 7536 | #endif |
| 7537 | #ifdef RDS_CMSG_RDMA_STATUS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7538 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_STATUS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 7539 | #endif |
| 7540 | #ifdef RDS_CMSG_RDMA_UPDATE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7541 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_UPDATE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 7542 | #endif |
| 7543 | #ifdef RDS_RDMA_READWRITE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7544 | PyModule_AddIntMacro(m, RDS_RDMA_READWRITE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 7545 | #endif |
| 7546 | #ifdef RDS_RDMA_FENCE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7547 | PyModule_AddIntMacro(m, RDS_RDMA_FENCE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 7548 | #endif |
| 7549 | #ifdef RDS_RDMA_INVALIDATE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7550 | PyModule_AddIntMacro(m, RDS_RDMA_INVALIDATE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 7551 | #endif |
| 7552 | #ifdef RDS_RDMA_USE_ONCE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7553 | PyModule_AddIntMacro(m, RDS_RDMA_USE_ONCE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 7554 | #endif |
| 7555 | #ifdef RDS_RDMA_DONTWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7556 | PyModule_AddIntMacro(m, RDS_RDMA_DONTWAIT); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 7557 | #endif |
| 7558 | #ifdef RDS_RDMA_NOTIFY_ME |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7559 | PyModule_AddIntMacro(m, RDS_RDMA_NOTIFY_ME); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 7560 | #endif |
| 7561 | #ifdef RDS_RDMA_SILENT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7562 | PyModule_AddIntMacro(m, RDS_RDMA_SILENT); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 7563 | #endif |
| 7564 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7565 | /* get{addr,name}info parameters */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7566 | #ifdef EAI_ADDRFAMILY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7567 | PyModule_AddIntMacro(m, EAI_ADDRFAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7568 | #endif |
| 7569 | #ifdef EAI_AGAIN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7570 | PyModule_AddIntMacro(m, EAI_AGAIN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7571 | #endif |
| 7572 | #ifdef EAI_BADFLAGS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7573 | PyModule_AddIntMacro(m, EAI_BADFLAGS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7574 | #endif |
| 7575 | #ifdef EAI_FAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7576 | PyModule_AddIntMacro(m, EAI_FAIL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7577 | #endif |
| 7578 | #ifdef EAI_FAMILY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7579 | PyModule_AddIntMacro(m, EAI_FAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7580 | #endif |
| 7581 | #ifdef EAI_MEMORY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7582 | PyModule_AddIntMacro(m, EAI_MEMORY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7583 | #endif |
| 7584 | #ifdef EAI_NODATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7585 | PyModule_AddIntMacro(m, EAI_NODATA); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7586 | #endif |
| 7587 | #ifdef EAI_NONAME |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7588 | PyModule_AddIntMacro(m, EAI_NONAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7589 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7590 | #ifdef EAI_OVERFLOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7591 | PyModule_AddIntMacro(m, EAI_OVERFLOW); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7592 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7593 | #ifdef EAI_SERVICE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7594 | PyModule_AddIntMacro(m, EAI_SERVICE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7595 | #endif |
| 7596 | #ifdef EAI_SOCKTYPE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7597 | PyModule_AddIntMacro(m, EAI_SOCKTYPE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7598 | #endif |
| 7599 | #ifdef EAI_SYSTEM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7600 | PyModule_AddIntMacro(m, EAI_SYSTEM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7601 | #endif |
| 7602 | #ifdef EAI_BADHINTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7603 | PyModule_AddIntMacro(m, EAI_BADHINTS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7604 | #endif |
| 7605 | #ifdef EAI_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7606 | PyModule_AddIntMacro(m, EAI_PROTOCOL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7607 | #endif |
| 7608 | #ifdef EAI_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7609 | PyModule_AddIntMacro(m, EAI_MAX); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7610 | #endif |
| 7611 | #ifdef AI_PASSIVE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7612 | PyModule_AddIntMacro(m, AI_PASSIVE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7613 | #endif |
| 7614 | #ifdef AI_CANONNAME |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7615 | PyModule_AddIntMacro(m, AI_CANONNAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7616 | #endif |
| 7617 | #ifdef AI_NUMERICHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7618 | PyModule_AddIntMacro(m, AI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7619 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7620 | #ifdef AI_NUMERICSERV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7621 | PyModule_AddIntMacro(m, AI_NUMERICSERV); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 7622 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7623 | #ifdef AI_MASK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7624 | PyModule_AddIntMacro(m, AI_MASK); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7625 | #endif |
| 7626 | #ifdef AI_ALL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7627 | PyModule_AddIntMacro(m, AI_ALL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7628 | #endif |
| 7629 | #ifdef AI_V4MAPPED_CFG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7630 | PyModule_AddIntMacro(m, AI_V4MAPPED_CFG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7631 | #endif |
| 7632 | #ifdef AI_ADDRCONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7633 | PyModule_AddIntMacro(m, AI_ADDRCONFIG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7634 | #endif |
| 7635 | #ifdef AI_V4MAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7636 | PyModule_AddIntMacro(m, AI_V4MAPPED); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7637 | #endif |
| 7638 | #ifdef AI_DEFAULT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7639 | PyModule_AddIntMacro(m, AI_DEFAULT); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7640 | #endif |
| 7641 | #ifdef NI_MAXHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7642 | PyModule_AddIntMacro(m, NI_MAXHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7643 | #endif |
| 7644 | #ifdef NI_MAXSERV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7645 | PyModule_AddIntMacro(m, NI_MAXSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7646 | #endif |
| 7647 | #ifdef NI_NOFQDN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7648 | PyModule_AddIntMacro(m, NI_NOFQDN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7649 | #endif |
| 7650 | #ifdef NI_NUMERICHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7651 | PyModule_AddIntMacro(m, NI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7652 | #endif |
| 7653 | #ifdef NI_NAMEREQD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7654 | PyModule_AddIntMacro(m, NI_NAMEREQD); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7655 | #endif |
| 7656 | #ifdef NI_NUMERICSERV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7657 | PyModule_AddIntMacro(m, NI_NUMERICSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7658 | #endif |
| 7659 | #ifdef NI_DGRAM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7660 | PyModule_AddIntMacro(m, NI_DGRAM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7661 | #endif |
| 7662 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7663 | /* shutdown() parameters */ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7664 | #ifdef SHUT_RD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7665 | PyModule_AddIntMacro(m, SHUT_RD); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7666 | #elif defined(SD_RECEIVE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7667 | PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7668 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7669 | PyModule_AddIntConstant(m, "SHUT_RD", 0); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7670 | #endif |
| 7671 | #ifdef SHUT_WR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7672 | PyModule_AddIntMacro(m, SHUT_WR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7673 | #elif defined(SD_SEND) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7674 | PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7675 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7676 | PyModule_AddIntConstant(m, "SHUT_WR", 1); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7677 | #endif |
| 7678 | #ifdef SHUT_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7679 | PyModule_AddIntMacro(m, SHUT_RDWR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7680 | #elif defined(SD_BOTH) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7681 | PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7682 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7683 | PyModule_AddIntConstant(m, "SHUT_RDWR", 2); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7684 | #endif |
| 7685 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 7686 | #ifdef SIO_RCVALL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7687 | { |
Steve Dower | ea93ac0 | 2016-06-17 12:52:18 -0700 | [diff] [blame] | 7688 | DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS, |
| 7689 | #if defined(SIO_LOOPBACK_FAST_PATH) |
Berker Peksag | 3583c3b | 2016-06-18 16:43:25 +0300 | [diff] [blame] | 7690 | SIO_LOOPBACK_FAST_PATH |
Steve Dower | ea93ac0 | 2016-06-17 12:52:18 -0700 | [diff] [blame] | 7691 | #endif |
Berker Peksag | 3583c3b | 2016-06-18 16:43:25 +0300 | [diff] [blame] | 7692 | }; |
Steve Dower | ea93ac0 | 2016-06-17 12:52:18 -0700 | [diff] [blame] | 7693 | const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS", |
| 7694 | #if defined(SIO_LOOPBACK_FAST_PATH) |
Berker Peksag | 3583c3b | 2016-06-18 16:43:25 +0300 | [diff] [blame] | 7695 | "SIO_LOOPBACK_FAST_PATH" |
Steve Dower | ea93ac0 | 2016-06-17 12:52:18 -0700 | [diff] [blame] | 7696 | #endif |
Berker Peksag | 3583c3b | 2016-06-18 16:43:25 +0300 | [diff] [blame] | 7697 | }; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7698 | int i; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 7699 | for(i = 0; i<Py_ARRAY_LENGTH(codes); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7700 | PyObject *tmp; |
| 7701 | tmp = PyLong_FromUnsignedLong(codes[i]); |
| 7702 | if (tmp == NULL) |
| 7703 | return NULL; |
| 7704 | PyModule_AddObject(m, names[i], tmp); |
| 7705 | } |
| 7706 | } |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7707 | PyModule_AddIntMacro(m, RCVALL_OFF); |
| 7708 | PyModule_AddIntMacro(m, RCVALL_ON); |
| 7709 | PyModule_AddIntMacro(m, RCVALL_SOCKETLEVELONLY); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 7710 | #ifdef RCVALL_IPLEVEL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7711 | PyModule_AddIntMacro(m, RCVALL_IPLEVEL); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 7712 | #endif |
| 7713 | #ifdef RCVALL_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7714 | PyModule_AddIntMacro(m, RCVALL_MAX); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 7715 | #endif |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 7716 | #endif /* _MSTCPIP_ */ |
| 7717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7718 | /* Initialize gethostbyname lock */ |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 7719 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7720 | netdb_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 7721 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7722 | return m; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 7723 | } |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7724 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 7725 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7726 | #ifndef HAVE_INET_PTON |
Christian Heimes | 96e7b3d | 2007-11-20 06:51:17 +0000 | [diff] [blame] | 7727 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 7728 | |
| 7729 | /* Simplistic emulation code for inet_pton that only works for IPv4 */ |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 7730 | /* These are not exposed because they do not set errno properly */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 7731 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 7732 | int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 7733 | inet_pton(int af, const char *src, void *dst) |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7734 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7735 | if (af == AF_INET) { |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 7736 | #if (SIZEOF_INT != 4) |
| 7737 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 7738 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7739 | unsigned int packed_addr; |
| 7740 | packed_addr = inet_addr(src); |
| 7741 | if (packed_addr == INADDR_NONE) |
| 7742 | return 0; |
| 7743 | memcpy(dst, &packed_addr, 4); |
| 7744 | return 1; |
| 7745 | } |
| 7746 | /* Should set errno to EAFNOSUPPORT */ |
| 7747 | return -1; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7748 | } |
| 7749 | |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 7750 | const char * |
| 7751 | 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] | 7752 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7753 | if (af == AF_INET) { |
| 7754 | struct in_addr packed_addr; |
| 7755 | if (size < 16) |
| 7756 | /* Should set errno to ENOSPC. */ |
| 7757 | return NULL; |
| 7758 | memcpy(&packed_addr, src, sizeof(packed_addr)); |
| 7759 | return strncpy(dst, inet_ntoa(packed_addr), size); |
| 7760 | } |
| 7761 | /* Should set errno to EAFNOSUPPORT */ |
| 7762 | return NULL; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7763 | } |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 7764 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7765 | #endif |
Christian Heimes | b615069 | 2007-11-15 23:37:07 +0000 | [diff] [blame] | 7766 | #endif |