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__ |
| 87 | /* |
| 88 | * inet_aton is not available on OSX 10.3, yet we want to use a binary |
| 89 | * that was build on 10.4 or later to work on that release, weak linking |
| 90 | * comes to the rescue. |
| 91 | */ |
| 92 | # pragma weak inet_aton |
| 93 | #endif |
| 94 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 95 | #include "Python.h" |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 96 | #include "structmember.h" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 97 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 98 | /* Socket object documentation */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 99 | PyDoc_STRVAR(sock_doc, |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 100 | "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] | 101 | \n\ |
| 102 | Open a socket of the given type. The family argument specifies the\n\ |
| 103 | address family; it defaults to AF_INET. The type argument specifies\n\ |
| 104 | whether this is a stream (SOCK_STREAM, this is the default)\n\ |
| 105 | or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\ |
| 106 | specifying the default protocol. Keyword arguments are accepted.\n\ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 107 | The socket is created as non-inheritable.\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 108 | \n\ |
| 109 | A socket object represents one endpoint of a network connection.\n\ |
| 110 | \n\ |
| 111 | Methods of socket objects (keyword arguments not allowed):\n\ |
| 112 | \n\ |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 113 | _accept() -- accept connection, returning new socket fd and client address\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 114 | bind(addr) -- bind the socket to a local address\n\ |
| 115 | close() -- close the socket\n\ |
| 116 | connect(addr) -- connect the socket to a remote address\n\ |
| 117 | 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] | 118 | dup() -- return a new socket fd duplicated from fileno()\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 119 | fileno() -- return underlying file descriptor\n\ |
| 120 | getpeername() -- return remote address [*]\n\ |
| 121 | getsockname() -- return local address\n\ |
| 122 | getsockopt(level, optname[, buflen]) -- get socket options\n\ |
| 123 | gettimeout() -- return timeout or None\n\ |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 124 | listen([n]) -- start listening for incoming connections\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 125 | recv(buflen[, flags]) -- receive data\n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 126 | recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 127 | recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 128 | recvfrom_into(buffer[, nbytes, [, flags])\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 129 | -- receive data and sender\'s address (into a buffer)\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 130 | sendall(data[, flags]) -- send all data\n\ |
| 131 | send(data[, flags]) -- send data, may not send all of it\n\ |
| 132 | sendto(data[, flags], addr) -- send data to a given address\n\ |
| 133 | setblocking(0 | 1) -- set or clear the blocking I/O flag\n\ |
| 134 | setsockopt(level, optname, value) -- set socket options\n\ |
| 135 | settimeout(None | float) -- set or clear the timeout\n\ |
| 136 | shutdown(how) -- shut down traffic in one or both directions\n\ |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 137 | if_nameindex() -- return all network interface indices and names\n\ |
Gregory P. Smith | b6471db | 2011-05-22 22:47:55 -0700 | [diff] [blame] | 138 | if_nametoindex(name) -- return the corresponding interface index\n\ |
| 139 | if_indextoname(index) -- return the corresponding interface name\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 140 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 141 | [*] not available on all platforms!"); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 142 | |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 143 | /* XXX This is a terrible mess of platform-dependent preprocessor hacks. |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 144 | I hope some day someone can clean this up please... */ |
| 145 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 146 | /* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure |
| 147 | script doesn't get this right, so we hardcode some platform checks below. |
| 148 | On the other hand, not all Linux versions agree, so there the settings |
| 149 | computed by the configure script are needed! */ |
| 150 | |
| 151 | #ifndef linux |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 152 | # undef HAVE_GETHOSTBYNAME_R_3_ARG |
| 153 | # undef HAVE_GETHOSTBYNAME_R_5_ARG |
| 154 | # undef HAVE_GETHOSTBYNAME_R_6_ARG |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 155 | #endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 156 | |
Victor Stinner | 710d27e | 2011-08-23 10:57:32 +0200 | [diff] [blame] | 157 | #if defined(__OpenBSD__) |
| 158 | # include <sys/uio.h> |
| 159 | #endif |
| 160 | |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 161 | #ifndef WITH_THREAD |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 162 | # undef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 163 | #endif |
| 164 | |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 165 | #ifdef HAVE_GETHOSTBYNAME_R |
Jesus Cea | 736e7fc | 2011-03-14 17:36:54 +0100 | [diff] [blame] | 166 | # if defined(_AIX) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 167 | # define HAVE_GETHOSTBYNAME_R_3_ARG |
| 168 | # elif defined(__sun) || defined(__sgi) |
| 169 | # define HAVE_GETHOSTBYNAME_R_5_ARG |
| 170 | # elif defined(linux) |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 171 | /* Rely on the configure script */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 172 | # else |
| 173 | # undef HAVE_GETHOSTBYNAME_R |
| 174 | # endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 175 | #endif |
| 176 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 177 | #if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \ |
| 178 | !defined(MS_WINDOWS) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 179 | # define USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 180 | #endif |
| 181 | |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 182 | /* To use __FreeBSD_version */ |
| 183 | #ifdef HAVE_SYS_PARAM_H |
| 184 | #include <sys/param.h> |
| 185 | #endif |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 186 | /* On systems on which getaddrinfo() is believed to not be thread-safe, |
Just van Rossum | 09aecd7 | 2003-05-09 08:03:44 +0000 | [diff] [blame] | 187 | (this includes the getaddrinfo emulation) protect access with a lock. */ |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 188 | #if defined(WITH_THREAD) && (defined(__APPLE__) || \ |
| 189 | (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 190 | defined(__OpenBSD__) || defined(__NetBSD__) || \ |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 191 | !defined(HAVE_GETADDRINFO)) |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 192 | #define USE_GETADDRINFO_LOCK |
| 193 | #endif |
| 194 | |
| 195 | #ifdef USE_GETADDRINFO_LOCK |
| 196 | #define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1); |
| 197 | #define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock); |
| 198 | #else |
| 199 | #define ACQUIRE_GETADDRINFO_LOCK |
| 200 | #define RELEASE_GETADDRINFO_LOCK |
| 201 | #endif |
| 202 | |
| 203 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 204 | # include "pythread.h" |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 205 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 206 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 207 | #if defined(PYCC_VACPP) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 208 | # include <types.h> |
| 209 | # include <io.h> |
| 210 | # include <sys/ioctl.h> |
| 211 | # include <utils.h> |
| 212 | # include <ctype.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 213 | #endif |
| 214 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 215 | #ifdef __APPLE__ |
| 216 | # include <sys/ioctl.h> |
| 217 | #endif |
| 218 | |
| 219 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 220 | #if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 221 | /* make sure that the reentrant (gethostbyaddr_r etc) |
| 222 | functions are declared correctly if compiling with |
| 223 | MIPSPro 7.x in ANSI C mode (default) */ |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 224 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 225 | /* XXX Using _SGIAPI is the wrong thing, |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 226 | but I don't know what the right thing is. */ |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 227 | #undef _SGIAPI /* to avoid warning */ |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 228 | #define _SGIAPI 1 |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 229 | |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 230 | #undef _XOPEN_SOURCE |
| 231 | #include <sys/socket.h> |
| 232 | #include <sys/types.h> |
| 233 | #include <netinet/in.h> |
| 234 | #ifdef _SS_ALIGNSIZE |
| 235 | #define HAVE_GETADDRINFO 1 |
| 236 | #define HAVE_GETNAMEINFO 1 |
| 237 | #endif |
| 238 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 239 | #define HAVE_INET_PTON |
| 240 | #include <netdb.h> |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 241 | #endif |
| 242 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 243 | /* Irix 6.5 fails to define this variable at all. This is needed |
| 244 | 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] | 245 | are just busted. Same thing for Solaris. */ |
| 246 | #if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN) |
Anthony Baxter | bab23cf | 2003-10-04 08:00:49 +0000 | [diff] [blame] | 247 | #define INET_ADDRSTRLEN 16 |
| 248 | #endif |
| 249 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 250 | /* Generic includes */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 251 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 252 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 253 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 254 | |
Gregory P. Smith | 3b1f2c3 | 2011-05-15 12:18:23 -0700 | [diff] [blame] | 255 | #ifdef HAVE_SYS_SOCKET_H |
| 256 | #include <sys/socket.h> |
| 257 | #endif |
| 258 | |
| 259 | #ifdef HAVE_NET_IF_H |
| 260 | #include <net/if.h> |
| 261 | #endif |
| 262 | |
Marc-André Lemburg | 976ade6 | 2002-02-16 18:47:07 +0000 | [diff] [blame] | 263 | /* Generic socket object definitions and includes */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 264 | #define PySocket_BUILDING_SOCKET |
Marc-André Lemburg | bb8b78b | 2002-02-16 18:44:52 +0000 | [diff] [blame] | 265 | #include "socketmodule.h" |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 266 | |
| 267 | /* Addressing includes */ |
| 268 | |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 269 | #ifndef MS_WINDOWS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 270 | |
| 271 | /* Non-MS WINDOWS includes */ |
| 272 | # include <netdb.h> |
Nick Coghlan | 4ecf6cf | 2011-08-22 15:45:19 +1000 | [diff] [blame] | 273 | # include <unistd.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 274 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 275 | /* Headers needed for inet_ntoa() and inet_addr() */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 276 | # include <arpa/inet.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 277 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 278 | # include <fcntl.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 279 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 280 | #else |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 281 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 282 | /* MS_WINDOWS includes */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 283 | # ifdef HAVE_FCNTL_H |
| 284 | # include <fcntl.h> |
| 285 | # endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 286 | |
Steve Dower | 65e4cb1 | 2014-11-22 12:54:57 -0800 | [diff] [blame] | 287 | #if defined(_MSC_VER) && _MSC_VER >= 1800 |
| 288 | /* Provides the IsWindows7SP1OrGreater() function */ |
| 289 | #include <VersionHelpers.h> |
| 290 | #endif |
| 291 | |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 292 | #endif |
| 293 | |
Skip Montanaro | 7befb99 | 2004-02-10 16:50:21 +0000 | [diff] [blame] | 294 | #include <stddef.h> |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 295 | |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 296 | #ifndef O_NONBLOCK |
| 297 | # define O_NONBLOCK O_NDELAY |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 298 | #endif |
| 299 | |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 300 | /* include Python's addrinfo.h unless it causes trouble */ |
| 301 | #if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE) |
| 302 | /* Do not include addinfo.h on some newer IRIX versions. |
| 303 | * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21, |
| 304 | * for example, but not by 6.5.10. |
| 305 | */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 306 | #elif defined(_MSC_VER) && _MSC_VER>1201 |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 307 | /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and |
| 308 | * EAI_* constants are defined in (the already included) ws2tcpip.h. |
| 309 | */ |
| 310 | #else |
| 311 | # include "addrinfo.h" |
| 312 | #endif |
Jason Tishler | c246cb7 | 2004-08-09 13:25:59 +0000 | [diff] [blame] | 313 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 314 | #ifndef HAVE_INET_PTON |
Christian Heimes | 96e7b3d | 2007-11-20 06:51:17 +0000 | [diff] [blame] | 315 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 316 | int inet_pton(int af, const char *src, void *dst); |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 317 | 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] | 318 | #endif |
Christian Heimes | b615069 | 2007-11-15 23:37:07 +0000 | [diff] [blame] | 319 | #endif |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 320 | |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 321 | #ifdef __APPLE__ |
| 322 | /* On OS X, getaddrinfo returns no error indication of lookup |
| 323 | failure, so we must use the emulation instead of the libinfo |
| 324 | implementation. Unfortunately, performing an autoconf test |
| 325 | for this bug would require DNS access for the machine performing |
| 326 | the configuration, which is not acceptable. Therefore, we |
| 327 | determine the bug just by checking for __APPLE__. If this bug |
| 328 | gets ever fixed, perhaps checking for sys/version.h would be |
| 329 | appropriate, which is 10/0 on the system with the bug. */ |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 330 | #ifndef HAVE_GETNAMEINFO |
| 331 | /* This bug seems to be fixed in Jaguar. Ths easiest way I could |
| 332 | Find to check for Jaguar is that it has getnameinfo(), which |
| 333 | older releases don't have */ |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 334 | #undef HAVE_GETADDRINFO |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 335 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 336 | |
| 337 | #ifdef HAVE_INET_ATON |
| 338 | #define USE_INET_ATON_WEAKLINK |
| 339 | #endif |
| 340 | |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 341 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 342 | |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 343 | /* 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] | 344 | #if !defined(HAVE_GETADDRINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 345 | /* avoid clashes with the C library definition of the symbol. */ |
| 346 | #define getaddrinfo fake_getaddrinfo |
| 347 | #define gai_strerror fake_gai_strerror |
| 348 | #define freeaddrinfo fake_freeaddrinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 349 | #include "getaddrinfo.c" |
| 350 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 351 | #if !defined(HAVE_GETNAMEINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 352 | #define getnameinfo fake_getnameinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 353 | #include "getnameinfo.c" |
| 354 | #endif |
| 355 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 356 | #ifdef MS_WINDOWS |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 357 | #define SOCKETCLOSE closesocket |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 358 | #endif |
| 359 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 360 | #ifdef MS_WIN32 |
Kristján Valur Jónsson | 310052c | 2012-03-31 13:35:00 +0000 | [diff] [blame] | 361 | #undef EAFNOSUPPORT |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 362 | #define EAFNOSUPPORT WSAEAFNOSUPPORT |
| 363 | #define snprintf _snprintf |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 364 | #endif |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 365 | |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 366 | #ifndef SOCKETCLOSE |
| 367 | #define SOCKETCLOSE close |
| 368 | #endif |
| 369 | |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 370 | #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] | 371 | #define USE_BLUETOOTH 1 |
| 372 | #if defined(__FreeBSD__) |
| 373 | #define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP |
| 374 | #define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 375 | #define BTPROTO_HCI BLUETOOTH_PROTO_HCI |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 376 | #define SOL_HCI SOL_HCI_RAW |
| 377 | #define HCI_FILTER SO_HCI_RAW_FILTER |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 378 | #define sockaddr_l2 sockaddr_l2cap |
| 379 | #define sockaddr_rc sockaddr_rfcomm |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 380 | #define hci_dev hci_node |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 381 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb) |
| 382 | #define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 383 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 384 | #elif defined(__NetBSD__) || defined(__DragonFly__) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 385 | #define sockaddr_l2 sockaddr_bt |
| 386 | #define sockaddr_rc sockaddr_bt |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 387 | #define sockaddr_hci sockaddr_bt |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 388 | #define sockaddr_sco sockaddr_bt |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 389 | #define SOL_HCI BTPROTO_HCI |
| 390 | #define HCI_DATA_DIR SO_HCI_DIRECTION |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 391 | #define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb) |
| 392 | #define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 393 | #define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 394 | #define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 395 | #else |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 396 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb) |
| 397 | #define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 398 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 399 | #define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb) |
| 400 | #endif |
| 401 | #endif |
| 402 | |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 403 | /* Convert "sock_addr_t *" to "struct sockaddr *". */ |
| 404 | #define SAS2SA(x) (&((x)->sa)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 405 | |
Martin v. Löwis | e941617 | 2003-05-03 10:12:45 +0000 | [diff] [blame] | 406 | /* |
| 407 | * Constants for getnameinfo() |
| 408 | */ |
| 409 | #if !defined(NI_MAXHOST) |
| 410 | #define NI_MAXHOST 1025 |
| 411 | #endif |
| 412 | #if !defined(NI_MAXSERV) |
| 413 | #define NI_MAXSERV 32 |
| 414 | #endif |
| 415 | |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 416 | #ifndef INVALID_SOCKET /* MS defines this */ |
| 417 | #define INVALID_SOCKET (-1) |
| 418 | #endif |
| 419 | |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 420 | #ifndef INADDR_NONE |
| 421 | #define INADDR_NONE (-1) |
| 422 | #endif |
| 423 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 424 | /* XXX There's a problem here: *static* functions are not supposed to have |
| 425 | a Py prefix (or use CapitalizedWords). Later... */ |
| 426 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 427 | /* Global variable holding the exception type for errors detected |
| 428 | by this module (but not argument type or memory errors, etc.). */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 429 | static PyObject *socket_herror; |
| 430 | static PyObject *socket_gaierror; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 431 | static PyObject *socket_timeout; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 432 | |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 433 | /* A forward reference to the socket type object. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 434 | The sock_type variable contains pointers to various functions, |
| 435 | some of which call new_sockobject(), which uses sock_type, so |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 436 | there has to be a circular reference. */ |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 437 | static PyTypeObject sock_type; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 438 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 439 | #if defined(HAVE_POLL_H) |
| 440 | #include <poll.h> |
| 441 | #elif defined(HAVE_SYS_POLL_H) |
| 442 | #include <sys/poll.h> |
| 443 | #endif |
| 444 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 445 | /* Largest value to try to store in a socklen_t (used when handling |
| 446 | ancillary data). POSIX requires socklen_t to hold at least |
| 447 | (2**31)-1 and recommends against storing larger values, but |
| 448 | socklen_t was originally int in the BSD interface, so to be on the |
| 449 | safe side we use the smaller of (2**31)-1 and INT_MAX. */ |
| 450 | #if INT_MAX > 0x7fffffff |
| 451 | #define SOCKLEN_T_LIMIT 0x7fffffff |
| 452 | #else |
| 453 | #define SOCKLEN_T_LIMIT INT_MAX |
| 454 | #endif |
| 455 | |
Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 456 | #ifdef HAVE_POLL |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 457 | /* Instead of select(), we'll use poll() since poll() works on any fd. */ |
| 458 | #define IS_SELECTABLE(s) 1 |
| 459 | /* 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] | 460 | #else |
Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 461 | /* If there's no timeout left, we don't have to call select, so it's a safe, |
| 462 | * little white lie. */ |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 463 | #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] | 464 | #endif |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 465 | |
| 466 | static PyObject* |
| 467 | select_error(void) |
| 468 | { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 469 | PyErr_SetString(PyExc_OSError, "unable to select on socket"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | return NULL; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 473 | #ifdef MS_WINDOWS |
Antoine Pitrou | c8f0196 | 2010-09-28 22:03:27 +0000 | [diff] [blame] | 474 | #ifndef WSAEAGAIN |
| 475 | #define WSAEAGAIN WSAEWOULDBLOCK |
| 476 | #endif |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 477 | #define CHECK_ERRNO(expected) \ |
| 478 | (WSAGetLastError() == WSA ## expected) |
| 479 | #else |
| 480 | #define CHECK_ERRNO(expected) \ |
| 481 | (errno == expected) |
| 482 | #endif |
| 483 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 484 | #ifdef MS_WINDOWS |
| 485 | /* Does WSASocket() support the WSA_FLAG_NO_HANDLE_INHERIT flag? */ |
| 486 | static int support_wsa_no_inherit = -1; |
| 487 | #endif |
| 488 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 489 | /* Convenience function to raise an error according to errno |
| 490 | and return a NULL pointer from a function. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 491 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 492 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 493 | set_error(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 494 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 495 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | int err_no = WSAGetLastError(); |
| 497 | /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which |
| 498 | recognizes the error codes used by both GetLastError() and |
| 499 | WSAGetLastError */ |
| 500 | if (err_no) |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 501 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, err_no); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 502 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 503 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 504 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 507 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 508 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 509 | set_herror(int h_error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 510 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 511 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 512 | |
| 513 | #ifdef HAVE_HSTRERROR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 514 | v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 515 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 516 | v = Py_BuildValue("(is)", h_error, "host not found"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 517 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | if (v != NULL) { |
| 519 | PyErr_SetObject(socket_herror, v); |
| 520 | Py_DECREF(v); |
| 521 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 523 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | |
| 527 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 528 | set_gaierror(int error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 529 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 530 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 531 | |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 532 | #ifdef EAI_SYSTEM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 533 | /* EAI_SYSTEM is not available on Windows XP. */ |
| 534 | if (error == EAI_SYSTEM) |
| 535 | return set_error(); |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 536 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 537 | |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 538 | #ifdef HAVE_GAI_STRERROR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 | v = Py_BuildValue("(is)", error, gai_strerror(error)); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 540 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 541 | v = Py_BuildValue("(is)", error, "getaddrinfo failed"); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 542 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 543 | if (v != NULL) { |
| 544 | PyErr_SetObject(socket_gaierror, v); |
| 545 | Py_DECREF(v); |
| 546 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 548 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 551 | /* Function to perform the setting of socket blocking mode |
| 552 | internally. block = (1 | 0). */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 553 | static int |
| 554 | internal_setblocking(PySocketSockObject *s, int block) |
| 555 | { |
Victor Stinner | b6dab6b | 2014-07-23 22:56:55 +0200 | [diff] [blame] | 556 | #ifdef MS_WINDOWS |
| 557 | u_long arg; |
| 558 | #endif |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 559 | #if !defined(MS_WINDOWS) \ |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 560 | && !((defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO))) |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 561 | int delay_flag, new_delay_flag; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 562 | #endif |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 563 | #ifdef SOCK_NONBLOCK |
| 564 | if (block) |
| 565 | s->sock_type &= (~SOCK_NONBLOCK); |
| 566 | else |
| 567 | s->sock_type |= SOCK_NONBLOCK; |
| 568 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 569 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 570 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 571 | #ifndef MS_WINDOWS |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 572 | #if (defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 573 | block = !block; |
| 574 | ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block); |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 575 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 576 | delay_flag = fcntl(s->sock_fd, F_GETFL, 0); |
| 577 | if (block) |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 578 | new_delay_flag = delay_flag & (~O_NONBLOCK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 579 | else |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 580 | new_delay_flag = delay_flag | O_NONBLOCK; |
| 581 | if (new_delay_flag != delay_flag) |
| 582 | fcntl(s->sock_fd, F_SETFL, new_delay_flag); |
| 583 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 584 | #else /* MS_WINDOWS */ |
Victor Stinner | b6dab6b | 2014-07-23 22:56:55 +0200 | [diff] [blame] | 585 | arg = !block; |
| 586 | ioctlsocket(s->sock_fd, FIONBIO, &arg); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 587 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 588 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 589 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 590 | /* Since these don't return anything */ |
| 591 | return 1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 594 | static int |
Victor Stinner | e6951c6 | 2015-03-31 13:50:44 +0200 | [diff] [blame^] | 595 | internal_select_impl(PySocketSockObject *s, int writing, _PyTime_t interval) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 596 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 597 | int n; |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 598 | #ifdef HAVE_POLL |
| 599 | struct pollfd pollfd; |
Victor Stinner | e6951c6 | 2015-03-31 13:50:44 +0200 | [diff] [blame^] | 600 | _PyTime_t ms; |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 601 | #else |
| 602 | fd_set fds; |
| 603 | struct timeval tv; |
| 604 | #endif |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 605 | |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 606 | #ifdef WITH_THREAD |
| 607 | /* must be called with the GIL held */ |
| 608 | assert(PyGILState_Check()); |
| 609 | #endif |
| 610 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 611 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 612 | if (s->sock_timeout <= 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 613 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 614 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 615 | /* Guard against closed socket */ |
| 616 | if (s->sock_fd < 0) |
| 617 | return 0; |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 618 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 619 | /* Handling this condition here simplifies the select loops */ |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 620 | if (interval < 0) |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 621 | return 1; |
| 622 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | /* Prefer poll, if available, since you can poll() any fd |
| 624 | * which can't be done with select(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 625 | #ifdef HAVE_POLL |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 626 | pollfd.fd = s->sock_fd; |
| 627 | pollfd.events = writing ? POLLOUT : POLLIN; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 628 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 629 | /* s->sock_timeout is in seconds, timeout in ms */ |
Victor Stinner | e6951c6 | 2015-03-31 13:50:44 +0200 | [diff] [blame^] | 630 | ms = _PyTime_AsMilliseconds(interval, _PyTime_ROUND_CEILING); |
| 631 | assert(ms <= INT_MAX); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 632 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 633 | Py_BEGIN_ALLOW_THREADS; |
Victor Stinner | e6951c6 | 2015-03-31 13:50:44 +0200 | [diff] [blame^] | 634 | n = poll(&pollfd, 1, (int)ms); |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 635 | Py_END_ALLOW_THREADS; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 636 | #else |
Victor Stinner | 869e177 | 2015-03-30 03:49:14 +0200 | [diff] [blame] | 637 | _PyTime_AsTimeval_noraise(interval, &tv, _PyTime_ROUND_CEILING); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 638 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 639 | FD_ZERO(&fds); |
| 640 | FD_SET(s->sock_fd, &fds); |
| 641 | |
| 642 | /* See if the socket is ready */ |
| 643 | Py_BEGIN_ALLOW_THREADS; |
| 644 | if (writing) |
| 645 | n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 646 | NULL, &fds, NULL, &tv); |
| 647 | else |
| 648 | n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 649 | &fds, NULL, NULL, &tv); |
| 650 | Py_END_ALLOW_THREADS; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 651 | #endif |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 653 | if (n < 0) |
| 654 | return -1; |
| 655 | if (n == 0) |
| 656 | return 1; |
| 657 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Victor Stinner | e6951c6 | 2015-03-31 13:50:44 +0200 | [diff] [blame^] | 660 | /* Do a select()/poll() on the socket, if necessary (sock_timeout > 0). |
| 661 | The argument writing indicates the direction. |
| 662 | This does not raise an exception; we'll let our caller do that |
| 663 | after they've reacquired the interpreter lock. |
| 664 | Returns 1 on timeout, -1 on error, 0 otherwise. */ |
| 665 | static int |
| 666 | internal_select(PySocketSockObject *s, int writing, _PyTime_t interval) |
| 667 | { |
| 668 | return internal_select_impl(s, writing, interval); |
| 669 | } |
| 670 | |
| 671 | static int |
| 672 | internal_connect_select(PySocketSockObject *s) |
| 673 | { |
| 674 | return internal_select(s, 1, s->sock_timeout); |
| 675 | } |
| 676 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 677 | /* |
| 678 | Two macros for automatic retry of select() in case of false positives |
| 679 | (for example, select() could indicate a socket is ready for reading |
| 680 | but the data then discarded by the OS because of a wrong checksum). |
| 681 | Here is an example of use: |
| 682 | |
| 683 | BEGIN_SELECT_LOOP(s) |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 684 | |
Victor Stinner | 391fa71 | 2015-03-31 13:15:31 +0200 | [diff] [blame] | 685 | timeout = internal_select(s, 0, interval); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 686 | |
| 687 | if (!timeout) { |
| 688 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 689 | outlen = recv(s->sock_fd, cbuf, len, flags); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 690 | Py_END_ALLOW_THREADS |
| 691 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 692 | if (timeout == 1) { |
| 693 | PyErr_SetString(socket_timeout, "timed out"); |
| 694 | return -1; |
| 695 | } |
| 696 | END_SELECT_LOOP(s) |
| 697 | */ |
| 698 | |
| 699 | #define BEGIN_SELECT_LOOP(s) \ |
| 700 | { \ |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 701 | _PyTime_t deadline = 0; \ |
| 702 | _PyTime_t interval = s->sock_timeout; \ |
| 703 | int has_timeout = (s->sock_timeout > 0); \ |
| 704 | if (has_timeout) \ |
| 705 | deadline = _PyTime_GetMonotonicClock() + interval; \ |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 706 | while (1) { \ |
| 707 | errno = 0; \ |
| 708 | |
| 709 | #define END_SELECT_LOOP(s) \ |
| 710 | if (!has_timeout || \ |
| 711 | (!CHECK_ERRNO(EWOULDBLOCK) && !CHECK_ERRNO(EAGAIN))) \ |
| 712 | break; \ |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 713 | interval = deadline - _PyTime_GetMonotonicClock(); \ |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 714 | } \ |
| 715 | } \ |
| 716 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 717 | /* Initialize a new socket object. */ |
| 718 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 719 | static _PyTime_t defaulttimeout = -1; /* Default timeout for new sockets */ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 720 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 721 | static void |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 722 | init_sockobject(PySocketSockObject *s, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 723 | SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 724 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 725 | s->sock_fd = fd; |
| 726 | s->sock_family = family; |
| 727 | s->sock_type = type; |
| 728 | s->sock_proto = proto; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 729 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 730 | s->errorhandler = &set_error; |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 731 | #ifdef SOCK_NONBLOCK |
| 732 | if (type & SOCK_NONBLOCK) |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 733 | s->sock_timeout = 0; |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 734 | else |
| 735 | #endif |
| 736 | { |
| 737 | s->sock_timeout = defaulttimeout; |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 738 | if (defaulttimeout >= 0) |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 739 | internal_setblocking(s, 0); |
| 740 | } |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 741 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 745 | /* Create a new socket object. |
| 746 | This just creates the object and initializes it. |
| 747 | If the creation fails, return NULL and set an exception (implicit |
| 748 | in NEWOBJ()). */ |
| 749 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 750 | static PySocketSockObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 751 | new_sockobject(SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 752 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 753 | PySocketSockObject *s; |
| 754 | s = (PySocketSockObject *) |
| 755 | PyType_GenericNew(&sock_type, NULL, NULL); |
| 756 | if (s != NULL) |
| 757 | init_sockobject(s, fd, family, type, proto); |
| 758 | return s; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 761 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 762 | /* Lock to allow python interpreter to continue, but only allow one |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 763 | thread to be in gethostbyname or getaddrinfo */ |
| 764 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Matthias Klose | d182a6c | 2012-08-14 17:29:04 +0200 | [diff] [blame] | 765 | static PyThread_type_lock netdb_lock; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 766 | #endif |
| 767 | |
| 768 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 769 | /* Convert a string specifying a host name or one of a few symbolic |
| 770 | names to a numeric IP address. This usually calls gethostbyname() |
| 771 | to do the work; the names "" and "<broadcast>" are special. |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 772 | Return the length (IPv4 should be 4 bytes), or negative if |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 773 | an error occurred; then an exception is raised. */ |
| 774 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 775 | static int |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 776 | setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 777 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 778 | struct addrinfo hints, *res; |
| 779 | int error; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 781 | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
| 782 | if (name[0] == '\0') { |
| 783 | int siz; |
| 784 | memset(&hints, 0, sizeof(hints)); |
| 785 | hints.ai_family = af; |
| 786 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
| 787 | hints.ai_flags = AI_PASSIVE; |
| 788 | Py_BEGIN_ALLOW_THREADS |
| 789 | ACQUIRE_GETADDRINFO_LOCK |
| 790 | error = getaddrinfo(NULL, "0", &hints, &res); |
| 791 | Py_END_ALLOW_THREADS |
| 792 | /* We assume that those thread-unsafe getaddrinfo() versions |
| 793 | *are* safe regarding their return value, ie. that a |
| 794 | subsequent call to getaddrinfo() does not destroy the |
| 795 | outcome of the first call. */ |
| 796 | RELEASE_GETADDRINFO_LOCK |
| 797 | if (error) { |
| 798 | set_gaierror(error); |
| 799 | return -1; |
| 800 | } |
| 801 | switch (res->ai_family) { |
| 802 | case AF_INET: |
| 803 | siz = 4; |
| 804 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 805 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 806 | case AF_INET6: |
| 807 | siz = 16; |
| 808 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 809 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 810 | default: |
| 811 | freeaddrinfo(res); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 812 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 813 | "unsupported address family"); |
| 814 | return -1; |
| 815 | } |
| 816 | if (res->ai_next) { |
| 817 | freeaddrinfo(res); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 818 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 819 | "wildcard resolved to multiple address"); |
| 820 | return -1; |
| 821 | } |
| 822 | if (res->ai_addrlen < addr_ret_size) |
| 823 | addr_ret_size = res->ai_addrlen; |
| 824 | memcpy(addr_ret, res->ai_addr, addr_ret_size); |
| 825 | freeaddrinfo(res); |
| 826 | return siz; |
| 827 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 828 | /* special-case broadcast - inet_addr() below can return INADDR_NONE for |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 829 | * this */ |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 830 | if (strcmp(name, "255.255.255.255") == 0 || |
| 831 | strcmp(name, "<broadcast>") == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 832 | struct sockaddr_in *sin; |
| 833 | if (af != AF_INET && af != AF_UNSPEC) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 834 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 835 | "address family mismatched"); |
| 836 | return -1; |
| 837 | } |
| 838 | sin = (struct sockaddr_in *)addr_ret; |
| 839 | memset((void *) sin, '\0', sizeof(*sin)); |
| 840 | sin->sin_family = AF_INET; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 841 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 | sin->sin_len = sizeof(*sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 843 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 844 | sin->sin_addr.s_addr = INADDR_BROADCAST; |
| 845 | return sizeof(sin->sin_addr); |
| 846 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 847 | |
| 848 | /* avoid a name resolution in case of numeric address */ |
| 849 | #ifdef HAVE_INET_PTON |
| 850 | /* check for an IPv4 address */ |
| 851 | if (af == AF_UNSPEC || af == AF_INET) { |
| 852 | struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret; |
| 853 | memset(sin, 0, sizeof(*sin)); |
| 854 | if (inet_pton(AF_INET, name, &sin->sin_addr) > 0) { |
| 855 | sin->sin_family = AF_INET; |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 856 | #ifdef HAVE_SOCKADDR_SA_LEN |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 857 | sin->sin_len = sizeof(*sin); |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 858 | #endif |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 859 | return 4; |
| 860 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 861 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 862 | #ifdef ENABLE_IPV6 |
| 863 | /* check for an IPv6 address - if the address contains a scope ID, we |
| 864 | * fallback to getaddrinfo(), which can handle translation from interface |
| 865 | * name to interface index */ |
| 866 | if ((af == AF_UNSPEC || af == AF_INET6) && !strchr(name, '%')) { |
| 867 | struct sockaddr_in6 *sin = (struct sockaddr_in6 *)addr_ret; |
| 868 | memset(sin, 0, sizeof(*sin)); |
| 869 | if (inet_pton(AF_INET6, name, &sin->sin6_addr) > 0) { |
| 870 | sin->sin6_family = AF_INET6; |
| 871 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 872 | sin->sin6_len = sizeof(*sin); |
| 873 | #endif |
| 874 | return 16; |
| 875 | } |
| 876 | } |
| 877 | #endif /* ENABLE_IPV6 */ |
| 878 | #else /* HAVE_INET_PTON */ |
| 879 | /* check for an IPv4 address */ |
| 880 | if (af == AF_INET || af == AF_UNSPEC) { |
| 881 | struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret; |
| 882 | memset(sin, 0, sizeof(*sin)); |
| 883 | if ((sin->sin_addr.s_addr = inet_addr(name)) != INADDR_NONE) { |
| 884 | sin->sin_family = AF_INET; |
| 885 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 886 | sin->sin_len = sizeof(*sin); |
| 887 | #endif |
| 888 | return 4; |
| 889 | } |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 890 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 891 | #endif /* HAVE_INET_PTON */ |
| 892 | |
| 893 | /* perform a name resolution */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 894 | memset(&hints, 0, sizeof(hints)); |
| 895 | hints.ai_family = af; |
| 896 | Py_BEGIN_ALLOW_THREADS |
| 897 | ACQUIRE_GETADDRINFO_LOCK |
| 898 | error = getaddrinfo(name, NULL, &hints, &res); |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 899 | #if defined(__digital__) && defined(__unix__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 900 | if (error == EAI_NONAME && af == AF_UNSPEC) { |
| 901 | /* On Tru64 V5.1, numeric-to-addr conversion fails |
| 902 | if no address family is given. Assume IPv4 for now.*/ |
| 903 | hints.ai_family = AF_INET; |
| 904 | error = getaddrinfo(name, NULL, &hints, &res); |
| 905 | } |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 906 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 907 | Py_END_ALLOW_THREADS |
| 908 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 909 | if (error) { |
| 910 | set_gaierror(error); |
| 911 | return -1; |
| 912 | } |
| 913 | if (res->ai_addrlen < addr_ret_size) |
| 914 | addr_ret_size = res->ai_addrlen; |
| 915 | memcpy((char *) addr_ret, res->ai_addr, addr_ret_size); |
| 916 | freeaddrinfo(res); |
| 917 | switch (addr_ret->sa_family) { |
| 918 | case AF_INET: |
| 919 | return 4; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 920 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | case AF_INET6: |
| 922 | return 16; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 923 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 925 | PyErr_SetString(PyExc_OSError, "unknown address family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 926 | return -1; |
| 927 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 930 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 931 | /* Create a string object representing an IP address. |
| 932 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 933 | size numbers). */ |
| 934 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 935 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 936 | makeipaddr(struct sockaddr *addr, int addrlen) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 937 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 938 | char buf[NI_MAXHOST]; |
| 939 | int error; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 940 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 941 | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, |
| 942 | NI_NUMERICHOST); |
| 943 | if (error) { |
| 944 | set_gaierror(error); |
| 945 | return NULL; |
| 946 | } |
| 947 | return PyUnicode_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 951 | #ifdef USE_BLUETOOTH |
| 952 | /* Convert a string representation of a Bluetooth address into a numeric |
| 953 | address. Returns the length (6), or raises an exception and returns -1 if |
| 954 | an error occurred. */ |
| 955 | |
| 956 | static int |
| 957 | setbdaddr(char *name, bdaddr_t *bdaddr) |
| 958 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 959 | unsigned int b0, b1, b2, b3, b4, b5; |
| 960 | char ch; |
| 961 | int n; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | n = sscanf(name, "%X:%X:%X:%X:%X:%X%c", |
| 964 | &b5, &b4, &b3, &b2, &b1, &b0, &ch); |
| 965 | if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) { |
| 966 | bdaddr->b[0] = b0; |
| 967 | bdaddr->b[1] = b1; |
| 968 | bdaddr->b[2] = b2; |
| 969 | bdaddr->b[3] = b3; |
| 970 | bdaddr->b[4] = b4; |
| 971 | bdaddr->b[5] = b5; |
| 972 | return 6; |
| 973 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 974 | PyErr_SetString(PyExc_OSError, "bad bluetooth address"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 975 | return -1; |
| 976 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | /* Create a string representation of the Bluetooth address. This is always a |
| 980 | string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal |
| 981 | value (zero padded if necessary). */ |
| 982 | |
| 983 | static PyObject * |
| 984 | makebdaddr(bdaddr_t *bdaddr) |
| 985 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 986 | char buf[(6 * 2) + 5 + 1]; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 987 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 989 | bdaddr->b[5], bdaddr->b[4], bdaddr->b[3], |
| 990 | bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]); |
| 991 | return PyUnicode_FromString(buf); |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 992 | } |
| 993 | #endif |
| 994 | |
| 995 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 996 | /* Create an object representing the given socket address, |
| 997 | suitable for passing it back to bind(), connect() etc. |
| 998 | The family field of the sockaddr structure is inspected |
| 999 | to determine what kind of address it really is. */ |
| 1000 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1001 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1002 | static PyObject * |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 1003 | 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] | 1004 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1005 | if (addrlen == 0) { |
| 1006 | /* No address -- may be recvfrom() from known socket */ |
| 1007 | Py_INCREF(Py_None); |
| 1008 | return Py_None; |
| 1009 | } |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1010 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | switch (addr->sa_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1013 | case AF_INET: |
| 1014 | { |
| 1015 | struct sockaddr_in *a; |
| 1016 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1017 | PyObject *ret = NULL; |
| 1018 | if (addrobj) { |
| 1019 | a = (struct sockaddr_in *)addr; |
| 1020 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 1021 | Py_DECREF(addrobj); |
| 1022 | } |
| 1023 | return ret; |
| 1024 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1025 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1026 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | case AF_UNIX: |
| 1028 | { |
| 1029 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1030 | #ifdef linux |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1031 | if (a->sun_path[0] == 0) { /* Linux abstract namespace */ |
| 1032 | addrlen -= offsetof(struct sockaddr_un, sun_path); |
Charles-François Natali | b10c71d | 2013-05-21 10:45:46 +0200 | [diff] [blame] | 1033 | return PyBytes_FromStringAndSize(a->sun_path, addrlen); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1034 | } |
| 1035 | else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1036 | #endif /* linux */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1037 | { |
| 1038 | /* regular NULL-terminated string */ |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1039 | return PyUnicode_DecodeFSDefault(a->sun_path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1040 | } |
| 1041 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1042 | #endif /* AF_UNIX */ |
| 1043 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1044 | #if defined(AF_NETLINK) |
| 1045 | case AF_NETLINK: |
| 1046 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1047 | struct sockaddr_nl *a = (struct sockaddr_nl *) addr; |
| 1048 | return Py_BuildValue("II", a->nl_pid, a->nl_groups); |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1049 | } |
| 1050 | #endif /* AF_NETLINK */ |
| 1051 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1052 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1053 | case AF_INET6: |
| 1054 | { |
| 1055 | struct sockaddr_in6 *a; |
| 1056 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1057 | PyObject *ret = NULL; |
| 1058 | if (addrobj) { |
| 1059 | a = (struct sockaddr_in6 *)addr; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1060 | ret = Py_BuildValue("OiII", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1061 | addrobj, |
| 1062 | ntohs(a->sin6_port), |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1063 | ntohl(a->sin6_flowinfo), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1064 | a->sin6_scope_id); |
| 1065 | Py_DECREF(addrobj); |
| 1066 | } |
| 1067 | return ret; |
| 1068 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1069 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1070 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1071 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1072 | case AF_BLUETOOTH: |
| 1073 | switch (proto) { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1074 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | case BTPROTO_L2CAP: |
| 1076 | { |
| 1077 | struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr; |
| 1078 | PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr)); |
| 1079 | PyObject *ret = NULL; |
| 1080 | if (addrobj) { |
| 1081 | ret = Py_BuildValue("Oi", |
| 1082 | addrobj, |
| 1083 | _BT_L2_MEMB(a, psm)); |
| 1084 | Py_DECREF(addrobj); |
| 1085 | } |
| 1086 | return ret; |
| 1087 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1088 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1089 | case BTPROTO_RFCOMM: |
| 1090 | { |
| 1091 | struct sockaddr_rc *a = (struct sockaddr_rc *) addr; |
| 1092 | PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr)); |
| 1093 | PyObject *ret = NULL; |
| 1094 | if (addrobj) { |
| 1095 | ret = Py_BuildValue("Oi", |
| 1096 | addrobj, |
| 1097 | _BT_RC_MEMB(a, channel)); |
| 1098 | Py_DECREF(addrobj); |
| 1099 | } |
| 1100 | return ret; |
| 1101 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1103 | case BTPROTO_HCI: |
| 1104 | { |
| 1105 | struct sockaddr_hci *a = (struct sockaddr_hci *) addr; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1106 | #if defined(__NetBSD__) || defined(__DragonFly__) |
| 1107 | return makebdaddr(&_BT_HCI_MEMB(a, bdaddr)); |
| 1108 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1109 | PyObject *ret = NULL; |
| 1110 | ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev)); |
| 1111 | return ret; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1112 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1113 | } |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1114 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1115 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1116 | case BTPROTO_SCO: |
| 1117 | { |
| 1118 | struct sockaddr_sco *a = (struct sockaddr_sco *) addr; |
| 1119 | return makebdaddr(&_BT_SCO_MEMB(a, bdaddr)); |
| 1120 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1121 | #endif |
| 1122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1123 | default: |
| 1124 | PyErr_SetString(PyExc_ValueError, |
| 1125 | "Unknown Bluetooth protocol"); |
| 1126 | return NULL; |
| 1127 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1128 | #endif |
| 1129 | |
Antoine Pitrou | b156a46 | 2010-10-27 20:13:57 +0000 | [diff] [blame] | 1130 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1131 | case AF_PACKET: |
| 1132 | { |
| 1133 | struct sockaddr_ll *a = (struct sockaddr_ll *)addr; |
| 1134 | char *ifname = ""; |
| 1135 | struct ifreq ifr; |
| 1136 | /* need to look up interface name give index */ |
| 1137 | if (a->sll_ifindex) { |
| 1138 | ifr.ifr_ifindex = a->sll_ifindex; |
| 1139 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
| 1140 | ifname = ifr.ifr_name; |
| 1141 | } |
| 1142 | return Py_BuildValue("shbhy#", |
| 1143 | ifname, |
| 1144 | ntohs(a->sll_protocol), |
| 1145 | a->sll_pkttype, |
| 1146 | a->sll_hatype, |
| 1147 | a->sll_addr, |
| 1148 | a->sll_halen); |
| 1149 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1150 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1151 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1152 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1153 | case AF_TIPC: |
| 1154 | { |
| 1155 | struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr; |
| 1156 | if (a->addrtype == TIPC_ADDR_NAMESEQ) { |
| 1157 | return Py_BuildValue("IIIII", |
| 1158 | a->addrtype, |
| 1159 | a->addr.nameseq.type, |
| 1160 | a->addr.nameseq.lower, |
| 1161 | a->addr.nameseq.upper, |
| 1162 | a->scope); |
| 1163 | } else if (a->addrtype == TIPC_ADDR_NAME) { |
| 1164 | return Py_BuildValue("IIIII", |
| 1165 | a->addrtype, |
| 1166 | a->addr.name.name.type, |
| 1167 | a->addr.name.name.instance, |
| 1168 | a->addr.name.name.instance, |
| 1169 | a->scope); |
| 1170 | } else if (a->addrtype == TIPC_ADDR_ID) { |
| 1171 | return Py_BuildValue("IIIII", |
| 1172 | a->addrtype, |
| 1173 | a->addr.id.node, |
| 1174 | a->addr.id.ref, |
| 1175 | 0, |
| 1176 | a->scope); |
| 1177 | } else { |
| 1178 | PyErr_SetString(PyExc_ValueError, |
| 1179 | "Invalid address type"); |
| 1180 | return NULL; |
| 1181 | } |
| 1182 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1183 | #endif |
| 1184 | |
Vinay Sajip | ecfc98c | 2014-03-20 12:42:42 +0000 | [diff] [blame] | 1185 | #ifdef AF_CAN |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1186 | case AF_CAN: |
| 1187 | { |
| 1188 | struct sockaddr_can *a = (struct sockaddr_can *)addr; |
| 1189 | char *ifname = ""; |
| 1190 | struct ifreq ifr; |
| 1191 | /* need to look up interface name given index */ |
| 1192 | if (a->can_ifindex) { |
| 1193 | ifr.ifr_ifindex = a->can_ifindex; |
| 1194 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
| 1195 | ifname = ifr.ifr_name; |
| 1196 | } |
| 1197 | |
| 1198 | return Py_BuildValue("O&h", PyUnicode_DecodeFSDefault, |
| 1199 | ifname, |
| 1200 | a->can_family); |
| 1201 | } |
| 1202 | #endif |
| 1203 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1204 | #ifdef PF_SYSTEM |
| 1205 | case PF_SYSTEM: |
| 1206 | switch(proto) { |
| 1207 | #ifdef SYSPROTO_CONTROL |
| 1208 | case SYSPROTO_CONTROL: |
| 1209 | { |
| 1210 | struct sockaddr_ctl *a = (struct sockaddr_ctl *)addr; |
| 1211 | return Py_BuildValue("(II)", a->sc_id, a->sc_unit); |
| 1212 | } |
| 1213 | #endif |
| 1214 | default: |
| 1215 | PyErr_SetString(PyExc_ValueError, |
| 1216 | "Invalid address type"); |
| 1217 | return 0; |
| 1218 | } |
| 1219 | #endif |
| 1220 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1221 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1222 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1223 | default: |
| 1224 | /* If we don't know the address family, don't raise an |
| 1225 | exception -- return it as an (int, bytes) tuple. */ |
| 1226 | return Py_BuildValue("iy#", |
| 1227 | addr->sa_family, |
| 1228 | addr->sa_data, |
| 1229 | sizeof(addr->sa_data)); |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1230 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1231 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1234 | /* Helper for getsockaddrarg: bypass IDNA for ASCII-only host names |
| 1235 | (in particular, numeric IP addresses). */ |
| 1236 | struct maybe_idna { |
| 1237 | PyObject *obj; |
| 1238 | char *buf; |
| 1239 | }; |
| 1240 | |
| 1241 | static void |
| 1242 | idna_cleanup(struct maybe_idna *data) |
| 1243 | { |
| 1244 | Py_CLEAR(data->obj); |
| 1245 | } |
| 1246 | |
| 1247 | static int |
| 1248 | idna_converter(PyObject *obj, struct maybe_idna *data) |
| 1249 | { |
| 1250 | size_t len; |
| 1251 | PyObject *obj2, *obj3; |
| 1252 | if (obj == NULL) { |
| 1253 | idna_cleanup(data); |
| 1254 | return 1; |
| 1255 | } |
| 1256 | data->obj = NULL; |
| 1257 | len = -1; |
| 1258 | if (PyBytes_Check(obj)) { |
| 1259 | data->buf = PyBytes_AsString(obj); |
| 1260 | len = PyBytes_Size(obj); |
| 1261 | } |
| 1262 | else if (PyByteArray_Check(obj)) { |
| 1263 | data->buf = PyByteArray_AsString(obj); |
| 1264 | len = PyByteArray_Size(obj); |
| 1265 | } |
| 1266 | else if (PyUnicode_Check(obj) && PyUnicode_READY(obj) == 0 && PyUnicode_IS_COMPACT_ASCII(obj)) { |
| 1267 | data->buf = PyUnicode_DATA(obj); |
| 1268 | len = PyUnicode_GET_LENGTH(obj); |
| 1269 | } |
| 1270 | else { |
| 1271 | obj2 = PyUnicode_FromObject(obj); |
| 1272 | if (!obj2) { |
| 1273 | PyErr_Format(PyExc_TypeError, "string or unicode text buffer expected, not %s", |
| 1274 | obj->ob_type->tp_name); |
| 1275 | return 0; |
| 1276 | } |
| 1277 | obj3 = PyUnicode_AsEncodedString(obj2, "idna", NULL); |
| 1278 | Py_DECREF(obj2); |
| 1279 | if (!obj3) { |
| 1280 | PyErr_SetString(PyExc_TypeError, "encoding of hostname failed"); |
| 1281 | return 0; |
| 1282 | } |
| 1283 | if (!PyBytes_Check(obj3)) { |
Martin v. Löwis | 17fd1e1 | 2014-08-05 16:13:50 +0200 | [diff] [blame] | 1284 | Py_DECREF(obj3); |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1285 | PyErr_SetString(PyExc_TypeError, "encoding of hostname failed to return bytes"); |
| 1286 | return 0; |
| 1287 | } |
| 1288 | data->obj = obj3; |
| 1289 | data->buf = PyBytes_AS_STRING(obj3); |
| 1290 | len = PyBytes_GET_SIZE(obj3); |
| 1291 | } |
| 1292 | if (strlen(data->buf) != len) { |
| 1293 | Py_CLEAR(data->obj); |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1294 | 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] | 1295 | return 0; |
| 1296 | } |
| 1297 | return Py_CLEANUP_SUPPORTED; |
Victor Stinner | 1a62a68 | 2014-08-17 19:33:28 +0200 | [diff] [blame] | 1298 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1299 | |
| 1300 | /* Parse a socket address argument according to the socket object's |
| 1301 | address family. Return 1 if the address was in the proper format, |
| 1302 | 0 of not. The address is returned through addr_ret, its length |
| 1303 | through len_ret. */ |
| 1304 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1305 | static int |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1306 | getsockaddrarg(PySocketSockObject *s, PyObject *args, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1307 | struct sockaddr *addr_ret, int *len_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1308 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1309 | switch (s->sock_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1310 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1311 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1312 | case AF_UNIX: |
| 1313 | { |
| 1314 | struct sockaddr_un* addr; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1315 | Py_buffer path; |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1316 | int retval = 0; |
| 1317 | |
| 1318 | /* PEP 383. Not using PyUnicode_FSConverter since we need to |
| 1319 | allow embedded nulls on Linux. */ |
| 1320 | if (PyUnicode_Check(args)) { |
| 1321 | if ((args = PyUnicode_EncodeFSDefault(args)) == NULL) |
| 1322 | return 0; |
| 1323 | } |
| 1324 | else |
| 1325 | Py_INCREF(args); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1326 | if (!PyArg_Parse(args, "y*", &path)) { |
| 1327 | Py_DECREF(args); |
| 1328 | return retval; |
| 1329 | } |
| 1330 | assert(path.len >= 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1332 | addr = (struct sockaddr_un*)addr_ret; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1333 | #ifdef linux |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1334 | if (path.len > 0 && *(const char *)path.buf == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1335 | /* Linux abstract namespace extension */ |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1336 | if ((size_t)path.len > sizeof addr->sun_path) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1337 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1338 | "AF_UNIX path too long"); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1339 | goto unix_out; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1340 | } |
| 1341 | } |
| 1342 | else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1343 | #endif /* linux */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1344 | { |
| 1345 | /* regular NULL-terminated string */ |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1346 | if ((size_t)path.len >= sizeof addr->sun_path) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1347 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1348 | "AF_UNIX path too long"); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1349 | goto unix_out; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1350 | } |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1351 | addr->sun_path[path.len] = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1352 | } |
| 1353 | addr->sun_family = s->sock_family; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1354 | memcpy(addr->sun_path, path.buf, path.len); |
| 1355 | *len_ret = path.len + offsetof(struct sockaddr_un, sun_path); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1356 | retval = 1; |
| 1357 | unix_out: |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1358 | PyBuffer_Release(&path); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1359 | Py_DECREF(args); |
| 1360 | return retval; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1361 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1362 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1363 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1364 | #if defined(AF_NETLINK) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1365 | case AF_NETLINK: |
| 1366 | { |
| 1367 | struct sockaddr_nl* addr; |
| 1368 | int pid, groups; |
| 1369 | addr = (struct sockaddr_nl *)addr_ret; |
| 1370 | if (!PyTuple_Check(args)) { |
| 1371 | PyErr_Format( |
| 1372 | PyExc_TypeError, |
| 1373 | "getsockaddrarg: " |
| 1374 | "AF_NETLINK address must be tuple, not %.500s", |
| 1375 | Py_TYPE(args)->tp_name); |
| 1376 | return 0; |
| 1377 | } |
| 1378 | if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups)) |
| 1379 | return 0; |
| 1380 | addr->nl_family = AF_NETLINK; |
| 1381 | addr->nl_pid = pid; |
| 1382 | addr->nl_groups = groups; |
| 1383 | *len_ret = sizeof(*addr); |
| 1384 | return 1; |
| 1385 | } |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1386 | #endif |
| 1387 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 1388 | #ifdef AF_RDS |
| 1389 | case AF_RDS: |
| 1390 | /* RDS sockets use sockaddr_in: fall-through */ |
| 1391 | #endif |
| 1392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1393 | case AF_INET: |
| 1394 | { |
| 1395 | struct sockaddr_in* addr; |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1396 | struct maybe_idna host = {NULL, NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1397 | int port, result; |
| 1398 | if (!PyTuple_Check(args)) { |
| 1399 | PyErr_Format( |
| 1400 | PyExc_TypeError, |
| 1401 | "getsockaddrarg: " |
| 1402 | "AF_INET address must be tuple, not %.500s", |
| 1403 | Py_TYPE(args)->tp_name); |
| 1404 | return 0; |
| 1405 | } |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1406 | if (!PyArg_ParseTuple(args, "O&i:getsockaddrarg", |
| 1407 | idna_converter, &host, &port)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1408 | return 0; |
| 1409 | addr=(struct sockaddr_in*)addr_ret; |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1410 | result = setipaddr(host.buf, (struct sockaddr *)addr, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1411 | sizeof(*addr), AF_INET); |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1412 | idna_cleanup(&host); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1413 | if (result < 0) |
| 1414 | return 0; |
| 1415 | if (port < 0 || port > 0xffff) { |
| 1416 | PyErr_SetString( |
| 1417 | PyExc_OverflowError, |
| 1418 | "getsockaddrarg: port must be 0-65535."); |
| 1419 | return 0; |
| 1420 | } |
| 1421 | addr->sin_family = AF_INET; |
| 1422 | addr->sin_port = htons((short)port); |
| 1423 | *len_ret = sizeof *addr; |
| 1424 | return 1; |
| 1425 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1426 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1427 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1428 | case AF_INET6: |
| 1429 | { |
| 1430 | struct sockaddr_in6* addr; |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1431 | struct maybe_idna host = {NULL, NULL}; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1432 | int port, result; |
| 1433 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1434 | flowinfo = scope_id = 0; |
| 1435 | if (!PyTuple_Check(args)) { |
| 1436 | PyErr_Format( |
| 1437 | PyExc_TypeError, |
| 1438 | "getsockaddrarg: " |
| 1439 | "AF_INET6 address must be tuple, not %.500s", |
| 1440 | Py_TYPE(args)->tp_name); |
| 1441 | return 0; |
| 1442 | } |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1443 | if (!PyArg_ParseTuple(args, "O&i|II", |
| 1444 | idna_converter, &host, &port, &flowinfo, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1445 | &scope_id)) { |
| 1446 | return 0; |
| 1447 | } |
| 1448 | addr = (struct sockaddr_in6*)addr_ret; |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1449 | result = setipaddr(host.buf, (struct sockaddr *)addr, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1450 | sizeof(*addr), AF_INET6); |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1451 | idna_cleanup(&host); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1452 | if (result < 0) |
| 1453 | return 0; |
| 1454 | if (port < 0 || port > 0xffff) { |
| 1455 | PyErr_SetString( |
| 1456 | PyExc_OverflowError, |
| 1457 | "getsockaddrarg: port must be 0-65535."); |
| 1458 | return 0; |
| 1459 | } |
Charles-François Natali | 9b0c006 | 2012-06-23 10:17:05 +0200 | [diff] [blame] | 1460 | if (flowinfo > 0xfffff) { |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1461 | PyErr_SetString( |
| 1462 | PyExc_OverflowError, |
| 1463 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 1464 | return 0; |
| 1465 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1466 | addr->sin6_family = s->sock_family; |
| 1467 | addr->sin6_port = htons((short)port); |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1468 | addr->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1469 | addr->sin6_scope_id = scope_id; |
| 1470 | *len_ret = sizeof *addr; |
| 1471 | return 1; |
| 1472 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1473 | #endif |
| 1474 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1475 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1476 | case AF_BLUETOOTH: |
| 1477 | { |
| 1478 | switch (s->sock_proto) { |
| 1479 | case BTPROTO_L2CAP: |
| 1480 | { |
| 1481 | struct sockaddr_l2 *addr; |
| 1482 | char *straddr; |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1483 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1484 | addr = (struct sockaddr_l2 *)addr_ret; |
| 1485 | memset(addr, 0, sizeof(struct sockaddr_l2)); |
| 1486 | _BT_L2_MEMB(addr, family) = AF_BLUETOOTH; |
| 1487 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1488 | &_BT_L2_MEMB(addr, psm))) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1489 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1490 | "wrong format"); |
| 1491 | return 0; |
| 1492 | } |
| 1493 | if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0) |
| 1494 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1496 | *len_ret = sizeof *addr; |
| 1497 | return 1; |
| 1498 | } |
| 1499 | case BTPROTO_RFCOMM: |
| 1500 | { |
| 1501 | struct sockaddr_rc *addr; |
| 1502 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1504 | addr = (struct sockaddr_rc *)addr_ret; |
| 1505 | _BT_RC_MEMB(addr, family) = AF_BLUETOOTH; |
| 1506 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1507 | &_BT_RC_MEMB(addr, channel))) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1508 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1509 | "wrong format"); |
| 1510 | return 0; |
| 1511 | } |
| 1512 | if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0) |
| 1513 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1515 | *len_ret = sizeof *addr; |
| 1516 | return 1; |
| 1517 | } |
| 1518 | case BTPROTO_HCI: |
| 1519 | { |
| 1520 | struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1521 | #if defined(__NetBSD__) || defined(__DragonFly__) |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1522 | char *straddr = PyBytes_AS_STRING(args); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1523 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1524 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1525 | if (straddr == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1526 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1527 | "wrong format"); |
| 1528 | return 0; |
| 1529 | } |
| 1530 | if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0) |
| 1531 | return 0; |
| 1532 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1533 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
| 1534 | if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1535 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1536 | "wrong format"); |
| 1537 | return 0; |
| 1538 | } |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1539 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1540 | *len_ret = sizeof *addr; |
| 1541 | return 1; |
| 1542 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1543 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1544 | case BTPROTO_SCO: |
| 1545 | { |
| 1546 | struct sockaddr_sco *addr; |
| 1547 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1549 | addr = (struct sockaddr_sco *)addr_ret; |
| 1550 | _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; |
| 1551 | if (!PyBytes_Check(args)) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1552 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1553 | "wrong format"); |
| 1554 | return 0; |
| 1555 | } |
| 1556 | straddr = PyBytes_AS_STRING(args); |
| 1557 | if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0) |
| 1558 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1559 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1560 | *len_ret = sizeof *addr; |
| 1561 | return 1; |
| 1562 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1563 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1564 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1565 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: unknown Bluetooth protocol"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1566 | return 0; |
| 1567 | } |
| 1568 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1569 | #endif |
| 1570 | |
Antoine Pitrou | b156a46 | 2010-10-27 20:13:57 +0000 | [diff] [blame] | 1571 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1572 | case AF_PACKET: |
| 1573 | { |
| 1574 | struct sockaddr_ll* addr; |
| 1575 | struct ifreq ifr; |
| 1576 | char *interfaceName; |
| 1577 | int protoNumber; |
| 1578 | int hatype = 0; |
| 1579 | int pkttype = 0; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1580 | Py_buffer haddr = {NULL, NULL}; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1581 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1582 | if (!PyTuple_Check(args)) { |
| 1583 | PyErr_Format( |
| 1584 | PyExc_TypeError, |
| 1585 | "getsockaddrarg: " |
| 1586 | "AF_PACKET address must be tuple, not %.500s", |
| 1587 | Py_TYPE(args)->tp_name); |
| 1588 | return 0; |
| 1589 | } |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1590 | if (!PyArg_ParseTuple(args, "si|iiy*", &interfaceName, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1591 | &protoNumber, &pkttype, &hatype, |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1592 | &haddr)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1593 | return 0; |
| 1594 | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name)); |
| 1595 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
| 1596 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
| 1597 | s->errorhandler(); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1598 | PyBuffer_Release(&haddr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1599 | return 0; |
| 1600 | } |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1601 | if (haddr.buf && haddr.len > 8) { |
| 1602 | PyErr_SetString(PyExc_ValueError, |
| 1603 | "Hardware address must be 8 bytes or less"); |
| 1604 | PyBuffer_Release(&haddr); |
| 1605 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1606 | } |
| 1607 | if (protoNumber < 0 || protoNumber > 0xffff) { |
| 1608 | PyErr_SetString( |
| 1609 | PyExc_OverflowError, |
| 1610 | "getsockaddrarg: protoNumber must be 0-65535."); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1611 | PyBuffer_Release(&haddr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1612 | return 0; |
| 1613 | } |
| 1614 | addr = (struct sockaddr_ll*)addr_ret; |
| 1615 | addr->sll_family = AF_PACKET; |
| 1616 | addr->sll_protocol = htons((short)protoNumber); |
| 1617 | addr->sll_ifindex = ifr.ifr_ifindex; |
| 1618 | addr->sll_pkttype = pkttype; |
| 1619 | addr->sll_hatype = hatype; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1620 | if (haddr.buf) { |
| 1621 | memcpy(&addr->sll_addr, haddr.buf, haddr.len); |
| 1622 | addr->sll_halen = haddr.len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1623 | } |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1624 | else |
| 1625 | addr->sll_halen = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1626 | *len_ret = sizeof *addr; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1627 | PyBuffer_Release(&haddr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1628 | return 1; |
| 1629 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1630 | #endif |
| 1631 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1632 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1633 | case AF_TIPC: |
| 1634 | { |
| 1635 | unsigned int atype, v1, v2, v3; |
| 1636 | unsigned int scope = TIPC_CLUSTER_SCOPE; |
| 1637 | struct sockaddr_tipc *addr; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1638 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1639 | if (!PyTuple_Check(args)) { |
| 1640 | PyErr_Format( |
| 1641 | PyExc_TypeError, |
| 1642 | "getsockaddrarg: " |
| 1643 | "AF_TIPC address must be tuple, not %.500s", |
| 1644 | Py_TYPE(args)->tp_name); |
| 1645 | return 0; |
| 1646 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1647 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1648 | if (!PyArg_ParseTuple(args, |
| 1649 | "IIII|I;Invalid TIPC address format", |
| 1650 | &atype, &v1, &v2, &v3, &scope)) |
| 1651 | return 0; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1653 | addr = (struct sockaddr_tipc *) addr_ret; |
| 1654 | memset(addr, 0, sizeof(struct sockaddr_tipc)); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1656 | addr->family = AF_TIPC; |
| 1657 | addr->scope = scope; |
| 1658 | addr->addrtype = atype; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1659 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1660 | if (atype == TIPC_ADDR_NAMESEQ) { |
| 1661 | addr->addr.nameseq.type = v1; |
| 1662 | addr->addr.nameseq.lower = v2; |
| 1663 | addr->addr.nameseq.upper = v3; |
| 1664 | } else if (atype == TIPC_ADDR_NAME) { |
| 1665 | addr->addr.name.name.type = v1; |
| 1666 | addr->addr.name.name.instance = v2; |
| 1667 | } else if (atype == TIPC_ADDR_ID) { |
| 1668 | addr->addr.id.node = v1; |
| 1669 | addr->addr.id.ref = v2; |
| 1670 | } else { |
| 1671 | /* Shouldn't happen */ |
| 1672 | PyErr_SetString(PyExc_TypeError, "Invalid address type"); |
| 1673 | return 0; |
| 1674 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1675 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1676 | *len_ret = sizeof(*addr); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1677 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1678 | return 1; |
| 1679 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1680 | #endif |
| 1681 | |
Vinay Sajip | ed6783f | 2014-03-21 11:44:32 +0000 | [diff] [blame] | 1682 | #if defined(AF_CAN) && defined(CAN_RAW) && defined(CAN_BCM) |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1683 | case AF_CAN: |
| 1684 | switch (s->sock_proto) { |
| 1685 | case CAN_RAW: |
Charles-François Natali | 773e42d | 2013-02-05 19:42:01 +0100 | [diff] [blame] | 1686 | /* fall-through */ |
| 1687 | case CAN_BCM: |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1688 | { |
| 1689 | struct sockaddr_can *addr; |
| 1690 | PyObject *interfaceName; |
| 1691 | struct ifreq ifr; |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1692 | Py_ssize_t len; |
| 1693 | |
Benjamin Peterson | 18b7191 | 2013-05-16 15:29:44 -0500 | [diff] [blame] | 1694 | addr = (struct sockaddr_can *)addr_ret; |
| 1695 | |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1696 | if (!PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter, |
| 1697 | &interfaceName)) |
| 1698 | return 0; |
| 1699 | |
| 1700 | len = PyBytes_GET_SIZE(interfaceName); |
| 1701 | |
| 1702 | if (len == 0) { |
| 1703 | ifr.ifr_ifindex = 0; |
Victor Stinner | 1a62a68 | 2014-08-17 19:33:28 +0200 | [diff] [blame] | 1704 | } else if ((size_t)len < sizeof(ifr.ifr_name)) { |
Christian Heimes | 15b6885 | 2012-09-10 01:25:50 +0200 | [diff] [blame] | 1705 | strncpy(ifr.ifr_name, PyBytes_AS_STRING(interfaceName), sizeof(ifr.ifr_name)); |
| 1706 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1707 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
| 1708 | s->errorhandler(); |
| 1709 | Py_DECREF(interfaceName); |
| 1710 | return 0; |
| 1711 | } |
| 1712 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1713 | PyErr_SetString(PyExc_OSError, |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1714 | "AF_CAN interface name too long"); |
| 1715 | Py_DECREF(interfaceName); |
| 1716 | return 0; |
| 1717 | } |
| 1718 | |
| 1719 | addr->can_family = AF_CAN; |
| 1720 | addr->can_ifindex = ifr.ifr_ifindex; |
| 1721 | |
| 1722 | *len_ret = sizeof(*addr); |
| 1723 | Py_DECREF(interfaceName); |
| 1724 | return 1; |
| 1725 | } |
| 1726 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1727 | PyErr_SetString(PyExc_OSError, |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1728 | "getsockaddrarg: unsupported CAN protocol"); |
| 1729 | return 0; |
| 1730 | } |
| 1731 | #endif |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1732 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1733 | #ifdef PF_SYSTEM |
| 1734 | case PF_SYSTEM: |
| 1735 | switch (s->sock_proto) { |
| 1736 | #ifdef SYSPROTO_CONTROL |
| 1737 | case SYSPROTO_CONTROL: |
| 1738 | { |
| 1739 | struct sockaddr_ctl *addr; |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1740 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1741 | addr = (struct sockaddr_ctl *)addr_ret; |
| 1742 | addr->sc_family = AF_SYSTEM; |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1743 | addr->ss_sysaddr = AF_SYS_CONTROL; |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1744 | |
| 1745 | if (PyUnicode_Check(args)) { |
| 1746 | struct ctl_info info; |
| 1747 | PyObject *ctl_name; |
| 1748 | |
| 1749 | if (!PyArg_Parse(args, "O&", |
| 1750 | PyUnicode_FSConverter, &ctl_name)) { |
| 1751 | return 0; |
| 1752 | } |
| 1753 | |
Victor Stinner | f50e187 | 2015-03-20 11:32:24 +0100 | [diff] [blame] | 1754 | 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] | 1755 | PyErr_SetString(PyExc_ValueError, |
| 1756 | "provided string is too long"); |
| 1757 | Py_DECREF(ctl_name); |
| 1758 | return 0; |
| 1759 | } |
| 1760 | strncpy(info.ctl_name, PyBytes_AS_STRING(ctl_name), |
| 1761 | sizeof(info.ctl_name)); |
| 1762 | Py_DECREF(ctl_name); |
| 1763 | |
| 1764 | if (ioctl(s->sock_fd, CTLIOCGINFO, &info)) { |
| 1765 | PyErr_SetString(PyExc_OSError, |
| 1766 | "cannot find kernel control with provided name"); |
| 1767 | return 0; |
| 1768 | } |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1769 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1770 | addr->sc_id = info.ctl_id; |
| 1771 | addr->sc_unit = 0; |
| 1772 | } else if (!PyArg_ParseTuple(args, "II", |
| 1773 | &(addr->sc_id), &(addr->sc_unit))) { |
| 1774 | PyErr_SetString(PyExc_TypeError, "getsockaddrarg: " |
| 1775 | "expected str or tuple of two ints"); |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1776 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1777 | return 0; |
| 1778 | } |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1779 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1780 | *len_ret = sizeof(*addr); |
| 1781 | return 1; |
| 1782 | } |
| 1783 | #endif |
| 1784 | default: |
| 1785 | PyErr_SetString(PyExc_OSError, |
| 1786 | "getsockaddrarg: unsupported PF_SYSTEM protocol"); |
| 1787 | return 0; |
| 1788 | } |
| 1789 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1790 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1791 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1793 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1794 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: bad family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1795 | return 0; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1797 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1800 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1801 | /* 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] | 1802 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 1803 | through len_ret. */ |
| 1804 | |
| 1805 | static int |
Peter Schneider-Kamp | 618e25d | 2000-07-11 23:00:12 +0000 | [diff] [blame] | 1806 | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1807 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1808 | switch (s->sock_family) { |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1809 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1810 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1811 | case AF_UNIX: |
| 1812 | { |
| 1813 | *len_ret = sizeof (struct sockaddr_un); |
| 1814 | return 1; |
| 1815 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1816 | #endif /* AF_UNIX */ |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1817 | #if defined(AF_NETLINK) |
| 1818 | case AF_NETLINK: |
| 1819 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1820 | *len_ret = sizeof (struct sockaddr_nl); |
| 1821 | return 1; |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1822 | } |
| 1823 | #endif |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1824 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 1825 | #ifdef AF_RDS |
| 1826 | case AF_RDS: |
| 1827 | /* RDS sockets use sockaddr_in: fall-through */ |
| 1828 | #endif |
| 1829 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1830 | case AF_INET: |
| 1831 | { |
| 1832 | *len_ret = sizeof (struct sockaddr_in); |
| 1833 | return 1; |
| 1834 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1835 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1836 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1837 | case AF_INET6: |
| 1838 | { |
| 1839 | *len_ret = sizeof (struct sockaddr_in6); |
| 1840 | return 1; |
| 1841 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1842 | #endif |
| 1843 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1844 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1845 | case AF_BLUETOOTH: |
| 1846 | { |
| 1847 | switch(s->sock_proto) |
| 1848 | { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1849 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1850 | case BTPROTO_L2CAP: |
| 1851 | *len_ret = sizeof (struct sockaddr_l2); |
| 1852 | return 1; |
| 1853 | case BTPROTO_RFCOMM: |
| 1854 | *len_ret = sizeof (struct sockaddr_rc); |
| 1855 | return 1; |
| 1856 | case BTPROTO_HCI: |
| 1857 | *len_ret = sizeof (struct sockaddr_hci); |
| 1858 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1859 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1860 | case BTPROTO_SCO: |
| 1861 | *len_ret = sizeof (struct sockaddr_sco); |
| 1862 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1863 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1864 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1865 | PyErr_SetString(PyExc_OSError, "getsockaddrlen: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1866 | "unknown BT protocol"); |
| 1867 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1868 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1869 | } |
| 1870 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1871 | #endif |
| 1872 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1873 | #ifdef HAVE_NETPACKET_PACKET_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1874 | case AF_PACKET: |
| 1875 | { |
| 1876 | *len_ret = sizeof (struct sockaddr_ll); |
| 1877 | return 1; |
| 1878 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1879 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1880 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1881 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1882 | case AF_TIPC: |
| 1883 | { |
| 1884 | *len_ret = sizeof (struct sockaddr_tipc); |
| 1885 | return 1; |
| 1886 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1887 | #endif |
| 1888 | |
Vinay Sajip | ecfc98c | 2014-03-20 12:42:42 +0000 | [diff] [blame] | 1889 | #ifdef AF_CAN |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1890 | case AF_CAN: |
| 1891 | { |
| 1892 | *len_ret = sizeof (struct sockaddr_can); |
| 1893 | return 1; |
| 1894 | } |
| 1895 | #endif |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1896 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1897 | #ifdef PF_SYSTEM |
| 1898 | case PF_SYSTEM: |
| 1899 | switch(s->sock_proto) { |
| 1900 | #ifdef SYSPROTO_CONTROL |
| 1901 | case SYSPROTO_CONTROL: |
| 1902 | *len_ret = sizeof (struct sockaddr_ctl); |
| 1903 | return 1; |
| 1904 | #endif |
| 1905 | default: |
| 1906 | PyErr_SetString(PyExc_OSError, "getsockaddrlen: " |
| 1907 | "unknown PF_SYSTEM protocol"); |
| 1908 | return 0; |
| 1909 | } |
| 1910 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1911 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1912 | /* More cases here... */ |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1914 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1915 | PyErr_SetString(PyExc_OSError, "getsockaddrlen: bad family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1916 | return 0; |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1918 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 1922 | /* Support functions for the sendmsg() and recvmsg[_into]() methods. |
| 1923 | Currently, these methods are only compiled if the RFC 2292/3542 |
| 1924 | CMSG_LEN() macro is available. Older systems seem to have used |
| 1925 | sizeof(struct cmsghdr) + (length) where CMSG_LEN() is used now, so |
| 1926 | it may be possible to define CMSG_LEN() that way if it's not |
| 1927 | provided. Some architectures might need extra padding after the |
| 1928 | cmsghdr, however, and CMSG_LEN() would have to take account of |
| 1929 | this. */ |
| 1930 | #ifdef CMSG_LEN |
| 1931 | /* If length is in range, set *result to CMSG_LEN(length) and return |
| 1932 | true; otherwise, return false. */ |
| 1933 | static int |
| 1934 | get_CMSG_LEN(size_t length, size_t *result) |
| 1935 | { |
| 1936 | size_t tmp; |
| 1937 | |
| 1938 | if (length > (SOCKLEN_T_LIMIT - CMSG_LEN(0))) |
| 1939 | return 0; |
| 1940 | tmp = CMSG_LEN(length); |
| 1941 | if (tmp > SOCKLEN_T_LIMIT || tmp < length) |
| 1942 | return 0; |
| 1943 | *result = tmp; |
| 1944 | return 1; |
| 1945 | } |
| 1946 | |
| 1947 | #ifdef CMSG_SPACE |
| 1948 | /* If length is in range, set *result to CMSG_SPACE(length) and return |
| 1949 | true; otherwise, return false. */ |
| 1950 | static int |
| 1951 | get_CMSG_SPACE(size_t length, size_t *result) |
| 1952 | { |
| 1953 | size_t tmp; |
| 1954 | |
| 1955 | /* Use CMSG_SPACE(1) here in order to take account of the padding |
| 1956 | necessary before *and* after the data. */ |
| 1957 | if (length > (SOCKLEN_T_LIMIT - CMSG_SPACE(1))) |
| 1958 | return 0; |
| 1959 | tmp = CMSG_SPACE(length); |
| 1960 | if (tmp > SOCKLEN_T_LIMIT || tmp < length) |
| 1961 | return 0; |
| 1962 | *result = tmp; |
| 1963 | return 1; |
| 1964 | } |
| 1965 | #endif |
| 1966 | |
| 1967 | /* Return true iff msg->msg_controllen is valid, cmsgh is a valid |
| 1968 | pointer in msg->msg_control with at least "space" bytes after it, |
| 1969 | and its cmsg_len member inside the buffer. */ |
| 1970 | static int |
| 1971 | cmsg_min_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t space) |
| 1972 | { |
| 1973 | size_t cmsg_offset; |
| 1974 | static const size_t cmsg_len_end = (offsetof(struct cmsghdr, cmsg_len) + |
| 1975 | sizeof(cmsgh->cmsg_len)); |
| 1976 | |
Charles-François Natali | 466517d | 2011-08-28 18:23:43 +0200 | [diff] [blame] | 1977 | /* Note that POSIX allows msg_controllen to be of signed type. */ |
Brett Cannon | b05cbe6 | 2014-01-07 17:01:01 -0500 | [diff] [blame] | 1978 | if (cmsgh == NULL || msg->msg_control == NULL) |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 1979 | return 0; |
Brett Cannon | b05cbe6 | 2014-01-07 17:01:01 -0500 | [diff] [blame] | 1980 | /* Note that POSIX allows msg_controllen to be of a signed type. This is |
| 1981 | annoying under OS X as it's unsigned there and so it triggers a |
| 1982 | tautological comparison warning under Clang when compared against 0. |
| 1983 | Since the check is valid on other platforms, silence the warning under |
| 1984 | Clang. */ |
| 1985 | #ifdef __clang__ |
| 1986 | #pragma clang diagnostic push |
| 1987 | #pragma clang diagnostic ignored "-Wtautological-compare" |
| 1988 | #endif |
Serhiy Storchaka | caa01f8 | 2015-03-05 23:42:24 +0200 | [diff] [blame] | 1989 | #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))) |
Serhiy Storchaka | b48af34 | 2015-02-26 15:27:57 +0200 | [diff] [blame] | 1990 | #pragma GCC diagnostic push |
| 1991 | #pragma GCC diagnostic ignored "-Wtype-limits" |
| 1992 | #endif |
Brett Cannon | b05cbe6 | 2014-01-07 17:01:01 -0500 | [diff] [blame] | 1993 | if (msg->msg_controllen < 0) |
| 1994 | return 0; |
Serhiy Storchaka | caa01f8 | 2015-03-05 23:42:24 +0200 | [diff] [blame] | 1995 | #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))) |
Serhiy Storchaka | b48af34 | 2015-02-26 15:27:57 +0200 | [diff] [blame] | 1996 | #pragma GCC diagnostic pop |
| 1997 | #endif |
Brett Cannon | b05cbe6 | 2014-01-07 17:01:01 -0500 | [diff] [blame] | 1998 | #ifdef __clang__ |
| 1999 | #pragma clang diagnostic pop |
| 2000 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2001 | if (space < cmsg_len_end) |
| 2002 | space = cmsg_len_end; |
| 2003 | cmsg_offset = (char *)cmsgh - (char *)msg->msg_control; |
| 2004 | return (cmsg_offset <= (size_t)-1 - space && |
| 2005 | cmsg_offset + space <= msg->msg_controllen); |
| 2006 | } |
| 2007 | |
| 2008 | /* If pointer CMSG_DATA(cmsgh) is in buffer msg->msg_control, set |
| 2009 | *space to number of bytes following it in the buffer and return |
| 2010 | true; otherwise, return false. Assumes cmsgh, msg->msg_control and |
| 2011 | msg->msg_controllen are valid. */ |
| 2012 | static int |
| 2013 | get_cmsg_data_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *space) |
| 2014 | { |
| 2015 | size_t data_offset; |
| 2016 | char *data_ptr; |
| 2017 | |
| 2018 | if ((data_ptr = (char *)CMSG_DATA(cmsgh)) == NULL) |
| 2019 | return 0; |
| 2020 | data_offset = data_ptr - (char *)msg->msg_control; |
| 2021 | if (data_offset > msg->msg_controllen) |
| 2022 | return 0; |
| 2023 | *space = msg->msg_controllen - data_offset; |
| 2024 | return 1; |
| 2025 | } |
| 2026 | |
| 2027 | /* If cmsgh is invalid or not contained in the buffer pointed to by |
| 2028 | msg->msg_control, return -1. If cmsgh is valid and its associated |
| 2029 | data is entirely contained in the buffer, set *data_len to the |
| 2030 | length of the associated data and return 0. If only part of the |
| 2031 | associated data is contained in the buffer but cmsgh is otherwise |
| 2032 | valid, set *data_len to the length contained in the buffer and |
| 2033 | return 1. */ |
| 2034 | static int |
| 2035 | get_cmsg_data_len(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *data_len) |
| 2036 | { |
| 2037 | size_t space, cmsg_data_len; |
| 2038 | |
| 2039 | if (!cmsg_min_space(msg, cmsgh, CMSG_LEN(0)) || |
| 2040 | cmsgh->cmsg_len < CMSG_LEN(0)) |
| 2041 | return -1; |
| 2042 | cmsg_data_len = cmsgh->cmsg_len - CMSG_LEN(0); |
| 2043 | if (!get_cmsg_data_space(msg, cmsgh, &space)) |
| 2044 | return -1; |
| 2045 | if (space >= cmsg_data_len) { |
| 2046 | *data_len = cmsg_data_len; |
| 2047 | return 0; |
| 2048 | } |
| 2049 | *data_len = space; |
| 2050 | return 1; |
| 2051 | } |
| 2052 | #endif /* CMSG_LEN */ |
| 2053 | |
| 2054 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2055 | /* s._accept() -> (fd, address) */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2056 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2057 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2058 | sock_accept(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2059 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2060 | sock_addr_t addrbuf; |
| 2061 | SOCKET_T newfd = INVALID_SOCKET; |
| 2062 | socklen_t addrlen; |
| 2063 | PyObject *sock = NULL; |
| 2064 | PyObject *addr = NULL; |
| 2065 | PyObject *res = NULL; |
| 2066 | int timeout; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2067 | int async_err = 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2068 | #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) |
| 2069 | /* accept4() is available on Linux 2.6.28+ and glibc 2.10 */ |
| 2070 | static int accept4_works = -1; |
| 2071 | #endif |
| 2072 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2073 | if (!getsockaddrlen(s, &addrlen)) |
| 2074 | return NULL; |
| 2075 | memset(&addrbuf, 0, addrlen); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2076 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2077 | if (!IS_SELECTABLE(s)) |
| 2078 | return select_error(); |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2079 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2080 | BEGIN_SELECT_LOOP(s) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2081 | do { |
Victor Stinner | 391fa71 | 2015-03-31 13:15:31 +0200 | [diff] [blame] | 2082 | timeout = internal_select(s, 0, interval); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2083 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2084 | if (!timeout) { |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2085 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2086 | #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2087 | if (accept4_works != 0) { |
| 2088 | newfd = accept4(s->sock_fd, SAS2SA(&addrbuf), &addrlen, |
| 2089 | SOCK_CLOEXEC); |
| 2090 | if (newfd == INVALID_SOCKET && accept4_works == -1) { |
| 2091 | /* On Linux older than 2.6.28, accept4() fails with ENOSYS */ |
| 2092 | accept4_works = (errno != ENOSYS); |
| 2093 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2094 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2095 | if (accept4_works == 0) |
| 2096 | newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2097 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2098 | newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2099 | #endif |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2100 | Py_END_ALLOW_THREADS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2101 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2102 | } while (newfd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2103 | if (timeout == 1) { |
| 2104 | PyErr_SetString(socket_timeout, "timed out"); |
| 2105 | return NULL; |
| 2106 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2107 | END_SELECT_LOOP(s) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2109 | if (newfd == INVALID_SOCKET) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2110 | return (!async_err) ? s->errorhandler() : NULL; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2111 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2112 | #ifdef MS_WINDOWS |
| 2113 | if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) { |
| 2114 | PyErr_SetFromWindowsErr(0); |
| 2115 | SOCKETCLOSE(newfd); |
| 2116 | goto finally; |
| 2117 | } |
| 2118 | #else |
| 2119 | |
| 2120 | #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) |
| 2121 | if (!accept4_works) |
| 2122 | #endif |
| 2123 | { |
| 2124 | if (_Py_set_inheritable(newfd, 0, NULL) < 0) { |
| 2125 | SOCKETCLOSE(newfd); |
| 2126 | goto finally; |
| 2127 | } |
| 2128 | } |
| 2129 | #endif |
| 2130 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2131 | sock = PyLong_FromSocket_t(newfd); |
| 2132 | if (sock == NULL) { |
| 2133 | SOCKETCLOSE(newfd); |
| 2134 | goto finally; |
| 2135 | } |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2137 | addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 2138 | addrlen, s->sock_proto); |
| 2139 | if (addr == NULL) |
| 2140 | goto finally; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2141 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2142 | res = PyTuple_Pack(2, sock, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2143 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2144 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2145 | Py_XDECREF(sock); |
| 2146 | Py_XDECREF(addr); |
| 2147 | return res; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2150 | PyDoc_STRVAR(accept_doc, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2151 | "_accept() -> (integer, address info)\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2152 | \n\ |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2153 | Wait for an incoming connection. Return a new socket file descriptor\n\ |
| 2154 | representing the connection, and the address of the client.\n\ |
| 2155 | For IP sockets, the address info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2156 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2157 | /* s.setblocking(flag) method. Argument: |
| 2158 | False -- non-blocking mode; same as settimeout(0) |
| 2159 | True -- blocking mode; same as settimeout(None) |
| 2160 | */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2161 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2162 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2163 | sock_setblocking(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2164 | { |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 2165 | long block; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2167 | block = PyLong_AsLong(arg); |
| 2168 | if (block == -1 && PyErr_Occurred()) |
| 2169 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2170 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2171 | s->sock_timeout = block ? -1 : 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2172 | internal_setblocking(s, block); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2174 | Py_INCREF(Py_None); |
| 2175 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2176 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2177 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2178 | PyDoc_STRVAR(setblocking_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2179 | "setblocking(flag)\n\ |
| 2180 | \n\ |
| 2181 | 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] | 2182 | setblocking(True) is equivalent to settimeout(None);\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2183 | setblocking(False) is equivalent to settimeout(0.0)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2184 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2185 | static int |
| 2186 | socket_parse_timeout(_PyTime_t *timeout, PyObject *timeout_obj) |
| 2187 | { |
| 2188 | #ifdef MS_WINDOWS |
| 2189 | struct timeval tv; |
| 2190 | #endif |
| 2191 | int overflow = 0; |
| 2192 | |
| 2193 | if (timeout_obj == Py_None) { |
| 2194 | *timeout = -1; |
| 2195 | return 0; |
| 2196 | } |
| 2197 | |
Victor Stinner | 869e177 | 2015-03-30 03:49:14 +0200 | [diff] [blame] | 2198 | if (_PyTime_FromSecondsObject(timeout, |
| 2199 | timeout_obj, _PyTime_ROUND_CEILING) < 0) |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2200 | return -1; |
| 2201 | |
| 2202 | if (*timeout < 0) { |
| 2203 | PyErr_SetString(PyExc_ValueError, "Timeout value out of range"); |
| 2204 | return -1; |
| 2205 | } |
| 2206 | |
| 2207 | #ifdef MS_WINDOWS |
Victor Stinner | 869e177 | 2015-03-30 03:49:14 +0200 | [diff] [blame] | 2208 | overflow = (_PyTime_AsTimeval(timeout, &tv, _PyTime_ROUND_CEILING) < 0); |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2209 | #endif |
| 2210 | #ifndef HAVE_POLL |
Victor Stinner | 869e177 | 2015-03-30 03:49:14 +0200 | [diff] [blame] | 2211 | timeout = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING); |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2212 | overflow = (timeout > INT_MAX); |
| 2213 | #endif |
| 2214 | if (overflow) { |
| 2215 | PyErr_SetString(PyExc_OverflowError, |
| 2216 | "timeout doesn't fit into C timeval"); |
| 2217 | return -1; |
| 2218 | } |
| 2219 | |
| 2220 | return 0; |
| 2221 | } |
| 2222 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2223 | /* s.settimeout(timeout) method. Argument: |
| 2224 | None -- no timeout, blocking mode; same as setblocking(True) |
| 2225 | 0.0 -- non-blocking mode; same as setblocking(False) |
| 2226 | > 0 -- timeout mode; operations time out after timeout seconds |
| 2227 | < 0 -- illegal; raises an exception |
| 2228 | */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2229 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2230 | sock_settimeout(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2231 | { |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2232 | _PyTime_t timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2233 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2234 | if (socket_parse_timeout(&timeout, arg) < 0) |
| 2235 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2237 | s->sock_timeout = timeout; |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2238 | internal_setblocking(s, timeout < 0); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2240 | Py_INCREF(Py_None); |
| 2241 | return Py_None; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2244 | PyDoc_STRVAR(settimeout_doc, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2245 | "settimeout(timeout)\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2246 | \n\ |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2247 | Set a timeout on socket operations. 'timeout' can be a float,\n\ |
| 2248 | giving in seconds, or None. Setting a timeout of None disables\n\ |
| 2249 | the timeout feature and is equivalent to setblocking(1).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2250 | Setting a timeout of zero is the same as setblocking(0)."); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2251 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2252 | /* s.gettimeout() method. |
| 2253 | Returns the timeout associated with a socket. */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2254 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2255 | sock_gettimeout(PySocketSockObject *s) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2256 | { |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2257 | if (s->sock_timeout < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2258 | Py_INCREF(Py_None); |
| 2259 | return Py_None; |
| 2260 | } |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2261 | else { |
| 2262 | double seconds = _PyTime_AsSecondsDouble(s->sock_timeout); |
| 2263 | return PyFloat_FromDouble(seconds); |
| 2264 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2267 | PyDoc_STRVAR(gettimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2268 | "gettimeout() -> timeout\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2269 | \n\ |
Ezio Melotti | 388c945 | 2011-08-14 08:28:57 +0300 | [diff] [blame] | 2270 | Returns the timeout in seconds (float) associated with socket \n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2271 | 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] | 2272 | operations are disabled."); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2273 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 2274 | /* s.setsockopt() method. |
| 2275 | With an integer third argument, sets an integer option. |
| 2276 | With a string third argument, sets an option from a buffer; |
| 2277 | use optional built-in module 'struct' to encode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2278 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2279 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2280 | sock_setsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2281 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2282 | int level; |
| 2283 | int optname; |
| 2284 | int res; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 2285 | Py_buffer optval; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2286 | int flag; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2288 | if (PyArg_ParseTuple(args, "iii:setsockopt", |
| 2289 | &level, &optname, &flag)) { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 2290 | res = setsockopt(s->sock_fd, level, optname, &flag, sizeof flag); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2291 | } |
| 2292 | else { |
| 2293 | PyErr_Clear(); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 2294 | if (!PyArg_ParseTuple(args, "iiy*:setsockopt", |
| 2295 | &level, &optname, &optval)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2296 | return NULL; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 2297 | res = setsockopt(s->sock_fd, level, optname, optval.buf, optval.len); |
| 2298 | PyBuffer_Release(&optval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2299 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2300 | if (res < 0) |
| 2301 | return s->errorhandler(); |
| 2302 | Py_INCREF(Py_None); |
| 2303 | return Py_None; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2306 | PyDoc_STRVAR(setsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2307 | "setsockopt(level, option, value)\n\ |
| 2308 | \n\ |
| 2309 | Set a socket option. See the Unix manual for level and option.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2310 | The value argument can either be an integer or a string."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2311 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2312 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 2313 | /* s.getsockopt() method. |
| 2314 | With two arguments, retrieves an integer option. |
| 2315 | With a third integer argument, retrieves a string buffer of that size; |
| 2316 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2317 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2318 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2319 | sock_getsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2320 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2321 | int level; |
| 2322 | int optname; |
| 2323 | int res; |
| 2324 | PyObject *buf; |
| 2325 | socklen_t buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2327 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt", |
| 2328 | &level, &optname, &buflen)) |
| 2329 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2330 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2331 | if (buflen == 0) { |
| 2332 | int flag = 0; |
| 2333 | socklen_t flagsize = sizeof flag; |
| 2334 | res = getsockopt(s->sock_fd, level, optname, |
| 2335 | (void *)&flag, &flagsize); |
| 2336 | if (res < 0) |
| 2337 | return s->errorhandler(); |
| 2338 | return PyLong_FromLong(flag); |
| 2339 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2340 | if (buflen <= 0 || buflen > 1024) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2341 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2342 | "getsockopt buflen out of range"); |
| 2343 | return NULL; |
| 2344 | } |
| 2345 | buf = PyBytes_FromStringAndSize((char *)NULL, buflen); |
| 2346 | if (buf == NULL) |
| 2347 | return NULL; |
| 2348 | res = getsockopt(s->sock_fd, level, optname, |
| 2349 | (void *)PyBytes_AS_STRING(buf), &buflen); |
| 2350 | if (res < 0) { |
| 2351 | Py_DECREF(buf); |
| 2352 | return s->errorhandler(); |
| 2353 | } |
| 2354 | _PyBytes_Resize(&buf, buflen); |
| 2355 | return buf; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2358 | PyDoc_STRVAR(getsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2359 | "getsockopt(level, option[, buffersize]) -> value\n\ |
| 2360 | \n\ |
| 2361 | Get a socket option. See the Unix manual for level and option.\n\ |
| 2362 | 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] | 2363 | string of that length; otherwise it is an integer."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2364 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2365 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2366 | /* s.bind(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2367 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2368 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2369 | sock_bind(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2370 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2371 | sock_addr_t addrbuf; |
| 2372 | int addrlen; |
| 2373 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2375 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2376 | return NULL; |
| 2377 | Py_BEGIN_ALLOW_THREADS |
| 2378 | res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen); |
| 2379 | Py_END_ALLOW_THREADS |
| 2380 | if (res < 0) |
| 2381 | return s->errorhandler(); |
| 2382 | Py_INCREF(Py_None); |
| 2383 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2386 | PyDoc_STRVAR(bind_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2387 | "bind(address)\n\ |
| 2388 | \n\ |
| 2389 | 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] | 2390 | 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] | 2391 | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2392 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2393 | |
| 2394 | /* s.close() method. |
| 2395 | Set the file descriptor to -1 so operations tried subsequently |
| 2396 | will surely fail. */ |
| 2397 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2398 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2399 | sock_close(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2400 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2401 | SOCKET_T fd; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2402 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2403 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 2404 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 2405 | * for more details. |
| 2406 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2407 | if ((fd = s->sock_fd) != -1) { |
| 2408 | s->sock_fd = -1; |
| 2409 | Py_BEGIN_ALLOW_THREADS |
| 2410 | (void) SOCKETCLOSE(fd); |
| 2411 | Py_END_ALLOW_THREADS |
| 2412 | } |
| 2413 | Py_INCREF(Py_None); |
| 2414 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2417 | PyDoc_STRVAR(close_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2418 | "close()\n\ |
| 2419 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2420 | Close the socket. It cannot be used after this call."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2421 | |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2422 | static PyObject * |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2423 | sock_detach(PySocketSockObject *s) |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2424 | { |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2425 | SOCKET_T fd = s->sock_fd; |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2426 | s->sock_fd = -1; |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2427 | return PyLong_FromSocket_t(fd); |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2428 | } |
| 2429 | |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2430 | PyDoc_STRVAR(detach_doc, |
| 2431 | "detach()\n\ |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2432 | \n\ |
Guido van Rossum | 638aebd | 2013-12-04 11:50:09 -0800 | [diff] [blame] | 2433 | Close the socket object without closing the underlying file descriptor.\n\ |
| 2434 | 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] | 2435 | can be reused for other purposes. The file descriptor is returned."); |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2436 | |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2437 | static int |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2438 | internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2439 | int *timeoutp) |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2440 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2441 | int res, timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2442 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2443 | timeout = 0; |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2444 | |
| 2445 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2446 | res = connect(s->sock_fd, addr, addrlen); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2447 | Py_END_ALLOW_THREADS |
| 2448 | |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2449 | #ifdef MS_WINDOWS |
| 2450 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2451 | if (s->sock_timeout > 0 |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2452 | && res < 0 && WSAGetLastError() == WSAEWOULDBLOCK |
| 2453 | && IS_SELECTABLE(s)) { |
| 2454 | /* This is a mess. Best solution: trust select */ |
| 2455 | fd_set fds; |
| 2456 | fd_set fds_exc; |
| 2457 | struct timeval tv; |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2458 | int conv; |
| 2459 | |
Victor Stinner | 869e177 | 2015-03-30 03:49:14 +0200 | [diff] [blame] | 2460 | _PyTime_AsTimeval_noraise(s->sock_timeout, &tv, _PyTime_ROUND_CEILING); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2461 | |
| 2462 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2463 | FD_ZERO(&fds); |
| 2464 | FD_SET(s->sock_fd, &fds); |
| 2465 | FD_ZERO(&fds_exc); |
| 2466 | FD_SET(s->sock_fd, &fds_exc); |
| 2467 | res = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 2468 | NULL, &fds, &fds_exc, &tv); |
| 2469 | Py_END_ALLOW_THREADS |
| 2470 | |
| 2471 | if (res == 0) { |
| 2472 | res = WSAEWOULDBLOCK; |
| 2473 | timeout = 1; |
| 2474 | } else if (res > 0) { |
| 2475 | if (FD_ISSET(s->sock_fd, &fds)) |
| 2476 | /* The socket is in the writable set - this |
| 2477 | means connected */ |
| 2478 | res = 0; |
| 2479 | else { |
| 2480 | /* As per MS docs, we need to call getsockopt() |
| 2481 | to get the underlying error */ |
| 2482 | int res_size = sizeof res; |
| 2483 | /* It must be in the exception set */ |
| 2484 | assert(FD_ISSET(s->sock_fd, &fds_exc)); |
| 2485 | if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR, |
| 2486 | (char *)&res, &res_size)) |
| 2487 | /* getsockopt also clears WSAGetLastError, |
| 2488 | so reset it back. */ |
| 2489 | WSASetLastError(res); |
| 2490 | else |
| 2491 | res = WSAGetLastError(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2492 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2493 | } |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2494 | /* else if (res < 0) an error occurred */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2495 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2497 | if (res < 0) |
| 2498 | res = WSAGetLastError(); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2499 | |
| 2500 | #else |
| 2501 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2502 | if (s->sock_timeout > 0 |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2503 | && res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) { |
| 2504 | |
Victor Stinner | e6951c6 | 2015-03-31 13:50:44 +0200 | [diff] [blame^] | 2505 | timeout = internal_connect_select(s); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2506 | |
| 2507 | if (timeout == 0) { |
| 2508 | /* Bug #1019808: in case of an EINPROGRESS, |
| 2509 | use getsockopt(SO_ERROR) to get the real |
| 2510 | error. */ |
| 2511 | socklen_t res_size = sizeof res; |
| 2512 | (void)getsockopt(s->sock_fd, SOL_SOCKET, |
| 2513 | SO_ERROR, &res, &res_size); |
| 2514 | if (res == EISCONN) |
| 2515 | res = 0; |
| 2516 | errno = res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2517 | } |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2518 | else if (timeout == -1) { |
| 2519 | res = errno; /* had error */ |
| 2520 | } |
| 2521 | else |
| 2522 | res = EWOULDBLOCK; /* timed out */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2523 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2524 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2525 | if (res < 0) |
| 2526 | res = errno; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2527 | |
| 2528 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2529 | *timeoutp = timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2530 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2531 | assert(res >= 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2532 | return res; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2533 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2534 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2535 | /* s.connect(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2536 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2537 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2538 | sock_connect(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2539 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2540 | sock_addr_t addrbuf; |
| 2541 | int addrlen; |
| 2542 | int res; |
| 2543 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2544 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2545 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2546 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2548 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2549 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2550 | if (timeout == 1) { |
| 2551 | PyErr_SetString(socket_timeout, "timed out"); |
| 2552 | return NULL; |
| 2553 | } |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2554 | if (res < 0) |
| 2555 | return NULL; |
| 2556 | if (res != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2557 | return s->errorhandler(); |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2558 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2559 | Py_INCREF(Py_None); |
| 2560 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2563 | PyDoc_STRVAR(connect_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2564 | "connect(address)\n\ |
| 2565 | \n\ |
| 2566 | 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] | 2567 | is a pair (host, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2568 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2569 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2570 | /* s.connect_ex(sockaddr) method */ |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2571 | |
| 2572 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2573 | sock_connect_ex(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2574 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2575 | sock_addr_t addrbuf; |
| 2576 | int addrlen; |
| 2577 | int res; |
| 2578 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2579 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2580 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2581 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2582 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2583 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2584 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2585 | if (res < 0) |
| 2586 | return NULL; |
| 2587 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2588 | /* Signals are not errors (though they may raise exceptions). Adapted |
| 2589 | from PyErr_SetFromErrnoWithFilenameObject(). */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2590 | if (res == EINTR && PyErr_CheckSignals()) |
| 2591 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2592 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2593 | return PyLong_FromLong((long) res); |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2596 | PyDoc_STRVAR(connect_ex_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2597 | "connect_ex(address) -> errno\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2598 | \n\ |
| 2599 | 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] | 2600 | instead of raising an exception when an error occurs."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2601 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2602 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2603 | /* s.fileno() method */ |
| 2604 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2605 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2606 | sock_fileno(PySocketSockObject *s) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2607 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2608 | return PyLong_FromSocket_t(s->sock_fd); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2611 | PyDoc_STRVAR(fileno_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2612 | "fileno() -> integer\n\ |
| 2613 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2614 | Return the integer file descriptor of the socket."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2615 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2616 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2617 | /* s.getsockname() method */ |
| 2618 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2619 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2620 | sock_getsockname(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2621 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2622 | sock_addr_t addrbuf; |
| 2623 | int res; |
| 2624 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2625 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2626 | if (!getsockaddrlen(s, &addrlen)) |
| 2627 | return NULL; |
| 2628 | memset(&addrbuf, 0, addrlen); |
| 2629 | Py_BEGIN_ALLOW_THREADS |
| 2630 | res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2631 | Py_END_ALLOW_THREADS |
| 2632 | if (res < 0) |
| 2633 | return s->errorhandler(); |
| 2634 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2635 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2636 | } |
| 2637 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2638 | PyDoc_STRVAR(getsockname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2639 | "getsockname() -> address info\n\ |
| 2640 | \n\ |
| 2641 | 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] | 2642 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2643 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2645 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2646 | /* s.getpeername() method */ |
| 2647 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2648 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2649 | sock_getpeername(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2650 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2651 | sock_addr_t addrbuf; |
| 2652 | int res; |
| 2653 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2654 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2655 | if (!getsockaddrlen(s, &addrlen)) |
| 2656 | return NULL; |
| 2657 | memset(&addrbuf, 0, addrlen); |
| 2658 | Py_BEGIN_ALLOW_THREADS |
| 2659 | res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2660 | Py_END_ALLOW_THREADS |
| 2661 | if (res < 0) |
| 2662 | return s->errorhandler(); |
| 2663 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2664 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2665 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2666 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2667 | PyDoc_STRVAR(getpeername_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2668 | "getpeername() -> address info\n\ |
| 2669 | \n\ |
| 2670 | 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] | 2671 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2672 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2673 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2674 | |
| 2675 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2676 | /* s.listen(n) method */ |
| 2677 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2678 | static PyObject * |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2679 | sock_listen(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2680 | { |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2681 | /* We try to choose a default backlog high enough to avoid connection drops |
| 2682 | * for common workloads, yet not too high to limit resource usage. */ |
| 2683 | int backlog = Py_MIN(SOMAXCONN, 128); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2684 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2685 | |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2686 | if (!PyArg_ParseTuple(args, "|i:listen", &backlog)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2687 | return NULL; |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2688 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2689 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 1be815a | 2011-05-10 19:16:29 +0200 | [diff] [blame] | 2690 | /* To avoid problems on systems that don't allow a negative backlog |
| 2691 | * (which doesn't make sense anyway) we force a minimum value of 0. */ |
| 2692 | if (backlog < 0) |
| 2693 | backlog = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2694 | res = listen(s->sock_fd, backlog); |
| 2695 | Py_END_ALLOW_THREADS |
| 2696 | if (res < 0) |
| 2697 | return s->errorhandler(); |
| 2698 | Py_INCREF(Py_None); |
| 2699 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2702 | PyDoc_STRVAR(listen_doc, |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2703 | "listen([backlog])\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2704 | \n\ |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2705 | Enable a server to accept connections. If backlog is specified, it must be\n\ |
| 2706 | 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] | 2707 | 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] | 2708 | connections. If not specified, a default reasonable value is chosen."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2709 | |
| 2710 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2711 | /* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2712 | * 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] | 2713 | * 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] | 2714 | * 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] | 2715 | * 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] | 2716 | * also possible that we return a number of bytes smaller than the request |
| 2717 | * bytes. |
| 2718 | */ |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2719 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2720 | static Py_ssize_t |
| 2721 | 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] | 2722 | { |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2723 | Py_ssize_t outlen = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2724 | int timeout; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2725 | int async_err = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2726 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2727 | if (!IS_SELECTABLE(s)) { |
| 2728 | select_error(); |
| 2729 | return -1; |
| 2730 | } |
| 2731 | if (len == 0) { |
| 2732 | /* If 0 bytes were requested, do nothing. */ |
| 2733 | return 0; |
| 2734 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2735 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2736 | BEGIN_SELECT_LOOP(s) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2737 | do { |
Victor Stinner | 391fa71 | 2015-03-31 13:15:31 +0200 | [diff] [blame] | 2738 | timeout = internal_select(s, 0, interval); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2739 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2740 | if (!timeout) { |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2741 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 2742 | #ifdef MS_WINDOWS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2743 | if (len > INT_MAX) |
| 2744 | len = INT_MAX; |
| 2745 | outlen = recv(s->sock_fd, cbuf, (int)len, flags); |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 2746 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2747 | outlen = recv(s->sock_fd, cbuf, len, flags); |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 2748 | #endif |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2749 | Py_END_ALLOW_THREADS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2750 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2751 | } while (outlen < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2752 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2753 | if (timeout == 1) { |
| 2754 | PyErr_SetString(socket_timeout, "timed out"); |
| 2755 | return -1; |
| 2756 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2757 | END_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2758 | if (outlen < 0) { |
| 2759 | /* Note: the call to errorhandler() ALWAYS indirectly returned |
| 2760 | NULL, so ignore its return value */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2761 | if (!async_err) |
| 2762 | s->errorhandler(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2763 | return -1; |
| 2764 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2765 | return outlen; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2768 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2769 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2770 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2771 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2772 | sock_recv(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2773 | { |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2774 | Py_ssize_t recvlen, outlen; |
| 2775 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2776 | PyObject *buf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2777 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2778 | if (!PyArg_ParseTuple(args, "n|i:recv", &recvlen, &flags)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2779 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2781 | if (recvlen < 0) { |
| 2782 | PyErr_SetString(PyExc_ValueError, |
| 2783 | "negative buffersize in recv"); |
| 2784 | return NULL; |
| 2785 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2786 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2787 | /* Allocate a new string. */ |
| 2788 | buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
| 2789 | if (buf == NULL) |
| 2790 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2791 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2792 | /* Call the guts */ |
| 2793 | outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags); |
| 2794 | if (outlen < 0) { |
| 2795 | /* An error occurred, release the string and return an |
| 2796 | error. */ |
| 2797 | Py_DECREF(buf); |
| 2798 | return NULL; |
| 2799 | } |
| 2800 | if (outlen != recvlen) { |
| 2801 | /* We did not read as many bytes as we anticipated, resize the |
| 2802 | string if possible and be successful. */ |
| 2803 | _PyBytes_Resize(&buf, outlen); |
| 2804 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2805 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2806 | return buf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2807 | } |
| 2808 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2809 | PyDoc_STRVAR(recv_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2810 | "recv(buffersize[, flags]) -> data\n\ |
| 2811 | \n\ |
| 2812 | Receive up to buffersize bytes from the socket. For the optional flags\n\ |
| 2813 | argument, see the Unix manual. When no data is available, block until\n\ |
| 2814 | 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] | 2815 | 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] | 2816 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2817 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2818 | /* s.recv_into(buffer, [nbytes [,flags]]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2819 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2820 | static PyObject* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2821 | sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2822 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2823 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2824 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2825 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2826 | Py_buffer pbuf; |
| 2827 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2828 | Py_ssize_t buflen, readlen, recvlen = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2829 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2830 | /* Get the buffer's memory */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2831 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recv_into", kwlist, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2832 | &pbuf, &recvlen, &flags)) |
| 2833 | return NULL; |
| 2834 | buf = pbuf.buf; |
| 2835 | buflen = pbuf.len; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2836 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2837 | if (recvlen < 0) { |
| 2838 | PyBuffer_Release(&pbuf); |
| 2839 | PyErr_SetString(PyExc_ValueError, |
| 2840 | "negative buffersize in recv_into"); |
| 2841 | return NULL; |
| 2842 | } |
| 2843 | if (recvlen == 0) { |
| 2844 | /* If nbytes was not specified, use the buffer's length */ |
| 2845 | recvlen = buflen; |
| 2846 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2847 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2848 | /* Check if the buffer is large enough */ |
| 2849 | if (buflen < recvlen) { |
| 2850 | PyBuffer_Release(&pbuf); |
| 2851 | PyErr_SetString(PyExc_ValueError, |
| 2852 | "buffer too small for requested bytes"); |
| 2853 | return NULL; |
| 2854 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2855 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2856 | /* Call the guts */ |
| 2857 | readlen = sock_recv_guts(s, buf, recvlen, flags); |
| 2858 | if (readlen < 0) { |
| 2859 | /* Return an error. */ |
| 2860 | PyBuffer_Release(&pbuf); |
| 2861 | return NULL; |
| 2862 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2863 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2864 | PyBuffer_Release(&pbuf); |
| 2865 | /* Return the number of bytes read. Note that we do not do anything |
| 2866 | special here in the case that readlen < recvlen. */ |
| 2867 | return PyLong_FromSsize_t(readlen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2870 | PyDoc_STRVAR(recv_into_doc, |
| 2871 | "recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2872 | \n\ |
| 2873 | A version of recv() that stores its data into a buffer rather than creating \n\ |
| 2874 | a new string. Receive up to buffersize bytes from the socket. If buffersize \n\ |
| 2875 | is not specified (or 0), receive up to the size available in the given buffer.\n\ |
| 2876 | \n\ |
| 2877 | See recv() for documentation about the flags."); |
| 2878 | |
| 2879 | |
| 2880 | /* |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 2881 | * This is the guts of the recvfrom() and recvfrom_into() methods, which reads |
| 2882 | * into a char buffer. If you have any inc/def ref to do to the objects that |
| 2883 | * 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] | 2884 | * 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] | 2885 | * that it is also possible that we return a number of bytes smaller than the |
| 2886 | * request bytes. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2887 | * |
| 2888 | * 'addr' is a return value for the address object. Note that you must decref |
| 2889 | * it yourself. |
| 2890 | */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2891 | static Py_ssize_t |
| 2892 | 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] | 2893 | PyObject** addr) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2894 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2895 | sock_addr_t addrbuf; |
| 2896 | int timeout; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2897 | Py_ssize_t n = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2898 | socklen_t addrlen; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2899 | int async_err = 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2900 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2901 | *addr = NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2903 | if (!getsockaddrlen(s, &addrlen)) |
| 2904 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2906 | if (!IS_SELECTABLE(s)) { |
| 2907 | select_error(); |
| 2908 | return -1; |
| 2909 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2910 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2911 | BEGIN_SELECT_LOOP(s) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2912 | do { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2913 | memset(&addrbuf, 0, addrlen); |
Victor Stinner | 391fa71 | 2015-03-31 13:15:31 +0200 | [diff] [blame] | 2914 | timeout = internal_select(s, 0, interval); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2915 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2916 | if (!timeout) { |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2917 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 2918 | #ifdef MS_WINDOWS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2919 | if (len > INT_MAX) |
| 2920 | len = INT_MAX; |
| 2921 | n = recvfrom(s->sock_fd, cbuf, (int)len, flags, |
| 2922 | (void *) &addrbuf, &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2923 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2924 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2925 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2926 | #endif |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2927 | Py_END_ALLOW_THREADS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2928 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2929 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2930 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2931 | if (timeout == 1) { |
| 2932 | PyErr_SetString(socket_timeout, "timed out"); |
| 2933 | return -1; |
| 2934 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2935 | END_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2936 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2937 | if (!async_err) |
| 2938 | s->errorhandler(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2939 | return -1; |
| 2940 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2941 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2942 | if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 2943 | addrlen, s->sock_proto))) |
| 2944 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2945 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2946 | return n; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
| 2949 | /* s.recvfrom(nbytes [,flags]) method */ |
| 2950 | |
| 2951 | static PyObject * |
| 2952 | sock_recvfrom(PySocketSockObject *s, PyObject *args) |
| 2953 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2954 | PyObject *buf = NULL; |
| 2955 | PyObject *addr = NULL; |
| 2956 | PyObject *ret = NULL; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2957 | int flags = 0; |
| 2958 | Py_ssize_t recvlen, outlen; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2959 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2960 | if (!PyArg_ParseTuple(args, "n|i:recvfrom", &recvlen, &flags)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2961 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2963 | if (recvlen < 0) { |
| 2964 | PyErr_SetString(PyExc_ValueError, |
| 2965 | "negative buffersize in recvfrom"); |
| 2966 | return NULL; |
| 2967 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2968 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2969 | buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
| 2970 | if (buf == NULL) |
| 2971 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2972 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2973 | outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf), |
| 2974 | recvlen, flags, &addr); |
| 2975 | if (outlen < 0) { |
| 2976 | goto finally; |
| 2977 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2979 | if (outlen != recvlen) { |
| 2980 | /* We did not read as many bytes as we anticipated, resize the |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 2981 | string if possible and be successful. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2982 | if (_PyBytes_Resize(&buf, outlen) < 0) |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 2983 | /* Oopsy, not so successful after all. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2984 | goto finally; |
| 2985 | } |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2986 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2987 | ret = PyTuple_Pack(2, buf, addr); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2988 | |
| 2989 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2990 | Py_XDECREF(buf); |
| 2991 | Py_XDECREF(addr); |
| 2992 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2995 | PyDoc_STRVAR(recvfrom_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2996 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
| 2997 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2998 | 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] | 2999 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3000 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3001 | /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3002 | |
| 3003 | static PyObject * |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3004 | sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3005 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3006 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3007 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3008 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3009 | Py_buffer pbuf; |
| 3010 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3011 | Py_ssize_t readlen, buflen, recvlen = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3013 | PyObject *addr = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3014 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3015 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recvfrom_into", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3016 | kwlist, &pbuf, |
| 3017 | &recvlen, &flags)) |
| 3018 | return NULL; |
| 3019 | buf = pbuf.buf; |
| 3020 | buflen = pbuf.len; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3021 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3022 | if (recvlen < 0) { |
| 3023 | PyBuffer_Release(&pbuf); |
| 3024 | PyErr_SetString(PyExc_ValueError, |
| 3025 | "negative buffersize in recvfrom_into"); |
| 3026 | return NULL; |
| 3027 | } |
| 3028 | if (recvlen == 0) { |
| 3029 | /* If nbytes was not specified, use the buffer's length */ |
| 3030 | recvlen = buflen; |
Benjamin Peterson | c6b37e2 | 2014-01-13 23:14:42 -0500 | [diff] [blame] | 3031 | } else if (recvlen > buflen) { |
| 3032 | PyBuffer_Release(&pbuf); |
| 3033 | PyErr_SetString(PyExc_ValueError, |
| 3034 | "nbytes is greater than the length of the buffer"); |
| 3035 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3036 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3037 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3038 | readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr); |
| 3039 | if (readlen < 0) { |
| 3040 | PyBuffer_Release(&pbuf); |
| 3041 | /* Return an error */ |
| 3042 | Py_XDECREF(addr); |
| 3043 | return NULL; |
| 3044 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3046 | PyBuffer_Release(&pbuf); |
| 3047 | /* Return the number of bytes read and the address. Note that we do |
| 3048 | not do anything special here in the case that readlen < recvlen. */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3049 | return Py_BuildValue("nN", readlen, addr); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3050 | } |
| 3051 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3052 | PyDoc_STRVAR(recvfrom_into_doc, |
| 3053 | "recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3054 | \n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3055 | 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] | 3056 | |
| 3057 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3058 | /* The sendmsg() and recvmsg[_into]() methods require a working |
| 3059 | CMSG_LEN(). See the comment near get_CMSG_LEN(). */ |
| 3060 | #ifdef CMSG_LEN |
| 3061 | /* |
| 3062 | * Call recvmsg() with the supplied iovec structures, flags, and |
| 3063 | * ancillary data buffer size (controllen). Returns the tuple return |
| 3064 | * value for recvmsg() or recvmsg_into(), with the first item provided |
| 3065 | * by the supplied makeval() function. makeval() will be called with |
| 3066 | * the length read and makeval_data as arguments, and must return a |
| 3067 | * new reference (which will be decrefed if there is a subsequent |
| 3068 | * error). On error, closes any file descriptors received via |
| 3069 | * SCM_RIGHTS. |
| 3070 | */ |
| 3071 | static PyObject * |
| 3072 | sock_recvmsg_guts(PySocketSockObject *s, struct iovec *iov, int iovlen, |
| 3073 | int flags, Py_ssize_t controllen, |
| 3074 | PyObject *(*makeval)(ssize_t, void *), void *makeval_data) |
| 3075 | { |
| 3076 | ssize_t bytes_received = -1; |
| 3077 | int timeout; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3078 | int async_err = 0; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3079 | sock_addr_t addrbuf; |
| 3080 | socklen_t addrbuflen; |
Charles-François Natali | e9e95ae | 2011-08-24 21:40:53 +0200 | [diff] [blame] | 3081 | struct msghdr msg = {0}; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3082 | PyObject *cmsg_list = NULL, *retval = NULL; |
| 3083 | void *controlbuf = NULL; |
| 3084 | struct cmsghdr *cmsgh; |
| 3085 | size_t cmsgdatalen = 0; |
| 3086 | int cmsg_status; |
| 3087 | |
| 3088 | /* XXX: POSIX says that msg_name and msg_namelen "shall be |
| 3089 | ignored" when the socket is connected (Linux fills them in |
| 3090 | anyway for AF_UNIX sockets at least). Normally msg_namelen |
| 3091 | seems to be set to 0 if there's no address, but try to |
| 3092 | initialize msg_name to something that won't be mistaken for a |
| 3093 | real address if that doesn't happen. */ |
| 3094 | if (!getsockaddrlen(s, &addrbuflen)) |
| 3095 | return NULL; |
| 3096 | memset(&addrbuf, 0, addrbuflen); |
| 3097 | SAS2SA(&addrbuf)->sa_family = AF_UNSPEC; |
| 3098 | |
| 3099 | if (controllen < 0 || controllen > SOCKLEN_T_LIMIT) { |
| 3100 | PyErr_SetString(PyExc_ValueError, |
| 3101 | "invalid ancillary data buffer length"); |
| 3102 | return NULL; |
| 3103 | } |
| 3104 | if (controllen > 0 && (controlbuf = PyMem_Malloc(controllen)) == NULL) |
| 3105 | return PyErr_NoMemory(); |
| 3106 | |
| 3107 | /* Make the system call. */ |
| 3108 | if (!IS_SELECTABLE(s)) { |
| 3109 | select_error(); |
| 3110 | goto finally; |
| 3111 | } |
| 3112 | |
| 3113 | BEGIN_SELECT_LOOP(s) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3114 | do { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3115 | msg.msg_name = SAS2SA(&addrbuf); |
| 3116 | msg.msg_namelen = addrbuflen; |
| 3117 | msg.msg_iov = iov; |
| 3118 | msg.msg_iovlen = iovlen; |
| 3119 | msg.msg_control = controlbuf; |
| 3120 | msg.msg_controllen = controllen; |
Victor Stinner | 391fa71 | 2015-03-31 13:15:31 +0200 | [diff] [blame] | 3121 | timeout = internal_select(s, 0, interval); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3122 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3123 | if (timeout == 1) { |
| 3124 | PyErr_SetString(socket_timeout, "timed out"); |
| 3125 | goto finally; |
| 3126 | } |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3127 | |
| 3128 | if (!timeout) { |
| 3129 | Py_BEGIN_ALLOW_THREADS; |
| 3130 | bytes_received = recvmsg(s->sock_fd, &msg, flags); |
| 3131 | Py_END_ALLOW_THREADS; |
| 3132 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3133 | } while (bytes_received < 0 && errno == EINTR && |
| 3134 | !(async_err = PyErr_CheckSignals())); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3135 | END_SELECT_LOOP(s) |
| 3136 | |
| 3137 | if (bytes_received < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3138 | if (!async_err) |
| 3139 | s->errorhandler(); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3140 | goto finally; |
| 3141 | } |
| 3142 | |
| 3143 | /* Make list of (level, type, data) tuples from control messages. */ |
| 3144 | if ((cmsg_list = PyList_New(0)) == NULL) |
| 3145 | goto err_closefds; |
| 3146 | /* Check for empty ancillary data as old CMSG_FIRSTHDR() |
| 3147 | implementations didn't do so. */ |
| 3148 | for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL); |
| 3149 | cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) { |
| 3150 | PyObject *bytes, *tuple; |
| 3151 | int tmp; |
| 3152 | |
| 3153 | cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen); |
| 3154 | if (cmsg_status != 0) { |
| 3155 | if (PyErr_WarnEx(PyExc_RuntimeWarning, |
| 3156 | "received malformed or improperly-truncated " |
| 3157 | "ancillary data", 1) == -1) |
| 3158 | goto err_closefds; |
| 3159 | } |
| 3160 | if (cmsg_status < 0) |
| 3161 | break; |
| 3162 | if (cmsgdatalen > PY_SSIZE_T_MAX) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3163 | PyErr_SetString(PyExc_OSError, "control message too long"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3164 | goto err_closefds; |
| 3165 | } |
| 3166 | |
| 3167 | bytes = PyBytes_FromStringAndSize((char *)CMSG_DATA(cmsgh), |
| 3168 | cmsgdatalen); |
| 3169 | tuple = Py_BuildValue("iiN", (int)cmsgh->cmsg_level, |
| 3170 | (int)cmsgh->cmsg_type, bytes); |
| 3171 | if (tuple == NULL) |
| 3172 | goto err_closefds; |
| 3173 | tmp = PyList_Append(cmsg_list, tuple); |
| 3174 | Py_DECREF(tuple); |
| 3175 | if (tmp != 0) |
| 3176 | goto err_closefds; |
| 3177 | |
| 3178 | if (cmsg_status != 0) |
| 3179 | break; |
| 3180 | } |
| 3181 | |
| 3182 | retval = Py_BuildValue("NOiN", |
| 3183 | (*makeval)(bytes_received, makeval_data), |
| 3184 | cmsg_list, |
| 3185 | (int)msg.msg_flags, |
| 3186 | makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 3187 | ((msg.msg_namelen > addrbuflen) ? |
| 3188 | addrbuflen : msg.msg_namelen), |
| 3189 | s->sock_proto)); |
| 3190 | if (retval == NULL) |
| 3191 | goto err_closefds; |
| 3192 | |
| 3193 | finally: |
| 3194 | Py_XDECREF(cmsg_list); |
| 3195 | PyMem_Free(controlbuf); |
| 3196 | return retval; |
| 3197 | |
| 3198 | err_closefds: |
| 3199 | #ifdef SCM_RIGHTS |
| 3200 | /* Close all descriptors coming from SCM_RIGHTS, so they don't leak. */ |
| 3201 | for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL); |
| 3202 | cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) { |
| 3203 | cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen); |
| 3204 | if (cmsg_status < 0) |
| 3205 | break; |
| 3206 | if (cmsgh->cmsg_level == SOL_SOCKET && |
| 3207 | cmsgh->cmsg_type == SCM_RIGHTS) { |
| 3208 | size_t numfds; |
| 3209 | int *fdp; |
| 3210 | |
| 3211 | numfds = cmsgdatalen / sizeof(int); |
| 3212 | fdp = (int *)CMSG_DATA(cmsgh); |
| 3213 | while (numfds-- > 0) |
| 3214 | close(*fdp++); |
| 3215 | } |
| 3216 | if (cmsg_status != 0) |
| 3217 | break; |
| 3218 | } |
| 3219 | #endif /* SCM_RIGHTS */ |
| 3220 | goto finally; |
| 3221 | } |
| 3222 | |
| 3223 | |
| 3224 | static PyObject * |
| 3225 | makeval_recvmsg(ssize_t received, void *data) |
| 3226 | { |
| 3227 | PyObject **buf = data; |
| 3228 | |
| 3229 | if (received < PyBytes_GET_SIZE(*buf)) |
| 3230 | _PyBytes_Resize(buf, received); |
| 3231 | Py_XINCREF(*buf); |
| 3232 | return *buf; |
| 3233 | } |
| 3234 | |
| 3235 | /* s.recvmsg(bufsize[, ancbufsize[, flags]]) method */ |
| 3236 | |
| 3237 | static PyObject * |
| 3238 | sock_recvmsg(PySocketSockObject *s, PyObject *args) |
| 3239 | { |
| 3240 | Py_ssize_t bufsize, ancbufsize = 0; |
| 3241 | int flags = 0; |
| 3242 | struct iovec iov; |
| 3243 | PyObject *buf = NULL, *retval = NULL; |
| 3244 | |
| 3245 | if (!PyArg_ParseTuple(args, "n|ni:recvmsg", &bufsize, &ancbufsize, &flags)) |
| 3246 | return NULL; |
| 3247 | |
| 3248 | if (bufsize < 0) { |
| 3249 | PyErr_SetString(PyExc_ValueError, "negative buffer size in recvmsg()"); |
| 3250 | return NULL; |
| 3251 | } |
| 3252 | if ((buf = PyBytes_FromStringAndSize(NULL, bufsize)) == NULL) |
| 3253 | return NULL; |
| 3254 | iov.iov_base = PyBytes_AS_STRING(buf); |
| 3255 | iov.iov_len = bufsize; |
| 3256 | |
| 3257 | /* Note that we're passing a pointer to *our pointer* to the bytes |
| 3258 | object here (&buf); makeval_recvmsg() may incref the object, or |
| 3259 | deallocate it and set our pointer to NULL. */ |
| 3260 | retval = sock_recvmsg_guts(s, &iov, 1, flags, ancbufsize, |
| 3261 | &makeval_recvmsg, &buf); |
| 3262 | Py_XDECREF(buf); |
| 3263 | return retval; |
| 3264 | } |
| 3265 | |
| 3266 | PyDoc_STRVAR(recvmsg_doc, |
| 3267 | "recvmsg(bufsize[, ancbufsize[, flags]]) -> (data, ancdata, msg_flags, address)\n\ |
| 3268 | \n\ |
| 3269 | Receive normal data (up to bufsize bytes) and ancillary data from the\n\ |
| 3270 | socket. The ancbufsize argument sets the size in bytes of the\n\ |
| 3271 | internal buffer used to receive the ancillary data; it defaults to 0,\n\ |
| 3272 | meaning that no ancillary data will be received. Appropriate buffer\n\ |
| 3273 | sizes for ancillary data can be calculated using CMSG_SPACE() or\n\ |
| 3274 | CMSG_LEN(), and items which do not fit into the buffer might be\n\ |
| 3275 | truncated or discarded. The flags argument defaults to 0 and has the\n\ |
| 3276 | same meaning as for recv().\n\ |
| 3277 | \n\ |
| 3278 | The return value is a 4-tuple: (data, ancdata, msg_flags, address).\n\ |
| 3279 | The data item is a bytes object holding the non-ancillary data\n\ |
| 3280 | received. The ancdata item is a list of zero or more tuples\n\ |
| 3281 | (cmsg_level, cmsg_type, cmsg_data) representing the ancillary data\n\ |
| 3282 | (control messages) received: cmsg_level and cmsg_type are integers\n\ |
| 3283 | specifying the protocol level and protocol-specific type respectively,\n\ |
| 3284 | and cmsg_data is a bytes object holding the associated data. The\n\ |
| 3285 | msg_flags item is the bitwise OR of various flags indicating\n\ |
| 3286 | conditions on the received message; see your system documentation for\n\ |
| 3287 | details. If the receiving socket is unconnected, address is the\n\ |
| 3288 | address of the sending socket, if available; otherwise, its value is\n\ |
| 3289 | unspecified.\n\ |
| 3290 | \n\ |
| 3291 | If recvmsg() raises an exception after the system call returns, it\n\ |
| 3292 | will first attempt to close any file descriptors received via the\n\ |
| 3293 | SCM_RIGHTS mechanism."); |
| 3294 | |
| 3295 | |
| 3296 | static PyObject * |
| 3297 | makeval_recvmsg_into(ssize_t received, void *data) |
| 3298 | { |
| 3299 | return PyLong_FromSsize_t(received); |
| 3300 | } |
| 3301 | |
| 3302 | /* s.recvmsg_into(buffers[, ancbufsize[, flags]]) method */ |
| 3303 | |
| 3304 | static PyObject * |
| 3305 | sock_recvmsg_into(PySocketSockObject *s, PyObject *args) |
| 3306 | { |
| 3307 | Py_ssize_t ancbufsize = 0; |
| 3308 | int flags = 0; |
| 3309 | struct iovec *iovs = NULL; |
| 3310 | Py_ssize_t i, nitems, nbufs = 0; |
| 3311 | Py_buffer *bufs = NULL; |
| 3312 | PyObject *buffers_arg, *fast, *retval = NULL; |
| 3313 | |
| 3314 | if (!PyArg_ParseTuple(args, "O|ni:recvmsg_into", |
| 3315 | &buffers_arg, &ancbufsize, &flags)) |
| 3316 | return NULL; |
| 3317 | |
| 3318 | if ((fast = PySequence_Fast(buffers_arg, |
| 3319 | "recvmsg_into() argument 1 must be an " |
| 3320 | "iterable")) == NULL) |
| 3321 | return NULL; |
| 3322 | nitems = PySequence_Fast_GET_SIZE(fast); |
| 3323 | if (nitems > INT_MAX) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3324 | PyErr_SetString(PyExc_OSError, "recvmsg_into() argument 1 is too long"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3325 | goto finally; |
| 3326 | } |
| 3327 | |
| 3328 | /* Fill in an iovec for each item, and save the Py_buffer |
| 3329 | structs to release afterwards. */ |
| 3330 | if (nitems > 0 && ((iovs = PyMem_New(struct iovec, nitems)) == NULL || |
| 3331 | (bufs = PyMem_New(Py_buffer, nitems)) == NULL)) { |
| 3332 | PyErr_NoMemory(); |
| 3333 | goto finally; |
| 3334 | } |
| 3335 | for (; nbufs < nitems; nbufs++) { |
| 3336 | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(fast, nbufs), |
| 3337 | "w*;recvmsg_into() argument 1 must be an iterable " |
| 3338 | "of single-segment read-write buffers", |
| 3339 | &bufs[nbufs])) |
| 3340 | goto finally; |
| 3341 | iovs[nbufs].iov_base = bufs[nbufs].buf; |
| 3342 | iovs[nbufs].iov_len = bufs[nbufs].len; |
| 3343 | } |
| 3344 | |
| 3345 | retval = sock_recvmsg_guts(s, iovs, nitems, flags, ancbufsize, |
| 3346 | &makeval_recvmsg_into, NULL); |
| 3347 | finally: |
| 3348 | for (i = 0; i < nbufs; i++) |
| 3349 | PyBuffer_Release(&bufs[i]); |
| 3350 | PyMem_Free(bufs); |
| 3351 | PyMem_Free(iovs); |
| 3352 | Py_DECREF(fast); |
| 3353 | return retval; |
| 3354 | } |
| 3355 | |
| 3356 | PyDoc_STRVAR(recvmsg_into_doc, |
| 3357 | "recvmsg_into(buffers[, ancbufsize[, flags]]) -> (nbytes, ancdata, msg_flags, address)\n\ |
| 3358 | \n\ |
| 3359 | Receive normal data and ancillary data from the socket, scattering the\n\ |
| 3360 | non-ancillary data into a series of buffers. The buffers argument\n\ |
| 3361 | must be an iterable of objects that export writable buffers\n\ |
| 3362 | (e.g. bytearray objects); these will be filled with successive chunks\n\ |
| 3363 | of the non-ancillary data until it has all been written or there are\n\ |
| 3364 | no more buffers. The ancbufsize argument sets the size in bytes of\n\ |
| 3365 | the internal buffer used to receive the ancillary data; it defaults to\n\ |
| 3366 | 0, meaning that no ancillary data will be received. Appropriate\n\ |
| 3367 | buffer sizes for ancillary data can be calculated using CMSG_SPACE()\n\ |
| 3368 | or CMSG_LEN(), and items which do not fit into the buffer might be\n\ |
| 3369 | truncated or discarded. The flags argument defaults to 0 and has the\n\ |
| 3370 | same meaning as for recv().\n\ |
| 3371 | \n\ |
| 3372 | The return value is a 4-tuple: (nbytes, ancdata, msg_flags, address).\n\ |
| 3373 | The nbytes item is the total number of bytes of non-ancillary data\n\ |
| 3374 | written into the buffers. The ancdata item is a list of zero or more\n\ |
| 3375 | tuples (cmsg_level, cmsg_type, cmsg_data) representing the ancillary\n\ |
| 3376 | data (control messages) received: cmsg_level and cmsg_type are\n\ |
| 3377 | integers specifying the protocol level and protocol-specific type\n\ |
| 3378 | respectively, and cmsg_data is a bytes object holding the associated\n\ |
| 3379 | data. The msg_flags item is the bitwise OR of various flags\n\ |
| 3380 | indicating conditions on the received message; see your system\n\ |
| 3381 | documentation for details. If the receiving socket is unconnected,\n\ |
| 3382 | address is the address of the sending socket, if available; otherwise,\n\ |
| 3383 | its value is unspecified.\n\ |
| 3384 | \n\ |
| 3385 | If recvmsg_into() raises an exception after the system call returns,\n\ |
| 3386 | it will first attempt to close any file descriptors received via the\n\ |
| 3387 | SCM_RIGHTS mechanism."); |
| 3388 | #endif /* CMSG_LEN */ |
| 3389 | |
| 3390 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 3391 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3392 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3393 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3394 | sock_send(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3395 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3396 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3397 | Py_ssize_t len, n = -1; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3398 | int async_err = 0; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3399 | int flags = 0, timeout; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3400 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3402 | if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags)) |
| 3403 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3405 | if (!IS_SELECTABLE(s)) { |
| 3406 | PyBuffer_Release(&pbuf); |
| 3407 | return select_error(); |
| 3408 | } |
| 3409 | buf = pbuf.buf; |
| 3410 | len = pbuf.len; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 3411 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3412 | BEGIN_SELECT_LOOP(s) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3413 | do { |
Victor Stinner | 391fa71 | 2015-03-31 13:15:31 +0200 | [diff] [blame] | 3414 | timeout = internal_select(s, 1, interval); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3415 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3416 | if (!timeout) { |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3417 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 3418 | #ifdef MS_WINDOWS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3419 | if (len > INT_MAX) |
| 3420 | len = INT_MAX; |
| 3421 | n = send(s->sock_fd, buf, (int)len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3422 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3423 | n = send(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3424 | #endif |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3425 | Py_END_ALLOW_THREADS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3426 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3427 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3428 | if (timeout == 1) { |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3429 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3430 | PyErr_SetString(socket_timeout, "timed out"); |
| 3431 | return NULL; |
| 3432 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3433 | END_SELECT_LOOP(s) |
| 3434 | |
| 3435 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3436 | if (n < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3437 | return (!async_err) ? s->errorhandler() : NULL; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3438 | return PyLong_FromSsize_t(n); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3439 | } |
| 3440 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3441 | PyDoc_STRVAR(send_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3442 | "send(data[, flags]) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3443 | \n\ |
| 3444 | 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] | 3445 | 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] | 3446 | 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] | 3447 | |
| 3448 | |
| 3449 | /* s.sendall(data [,flags]) method */ |
| 3450 | |
| 3451 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3452 | sock_sendall(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3453 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3454 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3455 | Py_ssize_t len, n = -1; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3456 | int async_err = 0; |
| 3457 | int flags = 0, timeout; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3458 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3459 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3460 | if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags)) |
| 3461 | return NULL; |
| 3462 | buf = pbuf.buf; |
| 3463 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3464 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3465 | if (!IS_SELECTABLE(s)) { |
| 3466 | PyBuffer_Release(&pbuf); |
| 3467 | return select_error(); |
| 3468 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 3469 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3470 | do { |
Victor Stinner | 391fa71 | 2015-03-31 13:15:31 +0200 | [diff] [blame] | 3471 | timeout = internal_select(s, 1, s->sock_timeout); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3473 | n = -1; |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3474 | if (!timeout) { |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3475 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 3476 | #ifdef MS_WINDOWS |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 3477 | if (len > INT_MAX) |
| 3478 | len = INT_MAX; |
| 3479 | n = send(s->sock_fd, buf, (int)len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3480 | #else |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3481 | n = send(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3482 | #endif |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3483 | Py_END_ALLOW_THREADS |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3484 | } |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3485 | if (timeout == 1) { |
| 3486 | PyBuffer_Release(&pbuf); |
| 3487 | PyErr_SetString(socket_timeout, "timed out"); |
| 3488 | return NULL; |
| 3489 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3490 | if (n >= 0) { |
| 3491 | buf += n; |
| 3492 | len -= n; |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3493 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3494 | } while (len > 0 && (n >= 0 || errno == EINTR) && |
| 3495 | !(async_err = PyErr_CheckSignals())); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3496 | PyBuffer_Release(&pbuf); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3497 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3498 | if (n < 0 || async_err) |
| 3499 | return (!async_err) ? s->errorhandler() : NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3501 | Py_INCREF(Py_None); |
| 3502 | return Py_None; |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3503 | } |
| 3504 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3505 | PyDoc_STRVAR(sendall_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3506 | "sendall(data[, flags])\n\ |
| 3507 | \n\ |
| 3508 | Send a data string to the socket. For the optional flags\n\ |
| 3509 | argument, see the Unix manual. This calls send() repeatedly\n\ |
| 3510 | 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] | 3511 | to tell how much data has been sent."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3512 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3513 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 3514 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3515 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3516 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3517 | sock_sendto(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3518 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3519 | Py_buffer pbuf; |
| 3520 | PyObject *addro; |
| 3521 | char *buf; |
Antoine Pitrou | 5e98141 | 2011-03-17 22:38:37 +0100 | [diff] [blame] | 3522 | Py_ssize_t len, arglen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3523 | sock_addr_t addrbuf; |
| 3524 | int addrlen, n = -1, flags, timeout; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3525 | int async_err = 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3527 | flags = 0; |
Antoine Pitrou | 5e98141 | 2011-03-17 22:38:37 +0100 | [diff] [blame] | 3528 | arglen = PyTuple_Size(args); |
| 3529 | switch (arglen) { |
| 3530 | case 2: |
| 3531 | PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro); |
| 3532 | break; |
| 3533 | case 3: |
| 3534 | PyArg_ParseTuple(args, "y*iO:sendto", |
| 3535 | &pbuf, &flags, &addro); |
| 3536 | break; |
| 3537 | default: |
| 3538 | PyErr_Format(PyExc_TypeError, |
| 3539 | "sendto() takes 2 or 3 arguments (%d given)", |
| 3540 | arglen); |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 3541 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3542 | } |
Antoine Pitrou | 5e98141 | 2011-03-17 22:38:37 +0100 | [diff] [blame] | 3543 | if (PyErr_Occurred()) |
| 3544 | return NULL; |
| 3545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3546 | buf = pbuf.buf; |
| 3547 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3549 | if (!IS_SELECTABLE(s)) { |
| 3550 | PyBuffer_Release(&pbuf); |
| 3551 | return select_error(); |
| 3552 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 3553 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3554 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) { |
| 3555 | PyBuffer_Release(&pbuf); |
| 3556 | return NULL; |
| 3557 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3558 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3559 | BEGIN_SELECT_LOOP(s) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3560 | do { |
Victor Stinner | 391fa71 | 2015-03-31 13:15:31 +0200 | [diff] [blame] | 3561 | timeout = internal_select(s, 1, interval); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3562 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3563 | if (!timeout) { |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3564 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 3565 | #ifdef MS_WINDOWS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3566 | if (len > INT_MAX) |
| 3567 | len = INT_MAX; |
| 3568 | n = sendto(s->sock_fd, buf, (int)len, flags, |
| 3569 | SAS2SA(&addrbuf), addrlen); |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 3570 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3571 | n = sendto(s->sock_fd, buf, len, flags, |
| 3572 | SAS2SA(&addrbuf), addrlen); |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 3573 | #endif |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3574 | Py_END_ALLOW_THREADS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3575 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3576 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3578 | if (timeout == 1) { |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3579 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3580 | PyErr_SetString(socket_timeout, "timed out"); |
| 3581 | return NULL; |
| 3582 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3583 | END_SELECT_LOOP(s) |
| 3584 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3585 | if (n < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3586 | return (!async_err) ? s->errorhandler() : NULL; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3587 | return PyLong_FromSsize_t(n); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3588 | } |
| 3589 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3590 | PyDoc_STRVAR(sendto_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3591 | "sendto(data[, flags], address) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3592 | \n\ |
| 3593 | 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] | 3594 | For IP sockets, the address is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3595 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3596 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3597 | /* The sendmsg() and recvmsg[_into]() methods require a working |
| 3598 | CMSG_LEN(). See the comment near get_CMSG_LEN(). */ |
| 3599 | #ifdef CMSG_LEN |
| 3600 | /* s.sendmsg(buffers[, ancdata[, flags[, address]]]) method */ |
| 3601 | |
| 3602 | static PyObject * |
| 3603 | sock_sendmsg(PySocketSockObject *s, PyObject *args) |
| 3604 | { |
| 3605 | Py_ssize_t i, ndataparts, ndatabufs = 0, ncmsgs, ncmsgbufs = 0; |
| 3606 | Py_buffer *databufs = NULL; |
| 3607 | struct iovec *iovs = NULL; |
| 3608 | sock_addr_t addrbuf; |
Charles-François Natali | e9e95ae | 2011-08-24 21:40:53 +0200 | [diff] [blame] | 3609 | struct msghdr msg = {0}; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3610 | struct cmsginfo { |
| 3611 | int level; |
| 3612 | int type; |
| 3613 | Py_buffer data; |
| 3614 | } *cmsgs = NULL; |
| 3615 | void *controlbuf = NULL; |
| 3616 | size_t controllen, controllen_last; |
| 3617 | ssize_t bytes_sent = -1; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3618 | int async_err = 0; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3619 | int addrlen, timeout, flags = 0; |
| 3620 | PyObject *data_arg, *cmsg_arg = NULL, *addr_arg = NULL, *data_fast = NULL, |
| 3621 | *cmsg_fast = NULL, *retval = NULL; |
| 3622 | |
| 3623 | if (!PyArg_ParseTuple(args, "O|OiO:sendmsg", |
| 3624 | &data_arg, &cmsg_arg, &flags, &addr_arg)) |
| 3625 | return NULL; |
| 3626 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3627 | /* Parse destination address. */ |
| 3628 | if (addr_arg != NULL && addr_arg != Py_None) { |
| 3629 | if (!getsockaddrarg(s, addr_arg, SAS2SA(&addrbuf), &addrlen)) |
| 3630 | goto finally; |
| 3631 | msg.msg_name = &addrbuf; |
| 3632 | msg.msg_namelen = addrlen; |
| 3633 | } |
| 3634 | |
| 3635 | /* Fill in an iovec for each message part, and save the Py_buffer |
| 3636 | structs to release afterwards. */ |
| 3637 | if ((data_fast = PySequence_Fast(data_arg, |
| 3638 | "sendmsg() argument 1 must be an " |
| 3639 | "iterable")) == NULL) |
| 3640 | goto finally; |
| 3641 | ndataparts = PySequence_Fast_GET_SIZE(data_fast); |
| 3642 | if (ndataparts > INT_MAX) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3643 | PyErr_SetString(PyExc_OSError, "sendmsg() argument 1 is too long"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3644 | goto finally; |
| 3645 | } |
| 3646 | msg.msg_iovlen = ndataparts; |
| 3647 | if (ndataparts > 0 && |
| 3648 | ((msg.msg_iov = iovs = PyMem_New(struct iovec, ndataparts)) == NULL || |
| 3649 | (databufs = PyMem_New(Py_buffer, ndataparts)) == NULL)) { |
| 3650 | PyErr_NoMemory(); |
| 3651 | goto finally; |
| 3652 | } |
| 3653 | for (; ndatabufs < ndataparts; ndatabufs++) { |
| 3654 | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(data_fast, ndatabufs), |
| 3655 | "y*;sendmsg() argument 1 must be an iterable of " |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 3656 | "bytes-like objects", |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3657 | &databufs[ndatabufs])) |
| 3658 | goto finally; |
| 3659 | iovs[ndatabufs].iov_base = databufs[ndatabufs].buf; |
| 3660 | iovs[ndatabufs].iov_len = databufs[ndatabufs].len; |
| 3661 | } |
| 3662 | |
| 3663 | if (cmsg_arg == NULL) |
| 3664 | ncmsgs = 0; |
| 3665 | else { |
| 3666 | if ((cmsg_fast = PySequence_Fast(cmsg_arg, |
| 3667 | "sendmsg() argument 2 must be an " |
| 3668 | "iterable")) == NULL) |
| 3669 | goto finally; |
| 3670 | ncmsgs = PySequence_Fast_GET_SIZE(cmsg_fast); |
| 3671 | } |
| 3672 | |
| 3673 | #ifndef CMSG_SPACE |
| 3674 | if (ncmsgs > 1) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3675 | PyErr_SetString(PyExc_OSError, |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3676 | "sending multiple control messages is not supported " |
| 3677 | "on this system"); |
| 3678 | goto finally; |
| 3679 | } |
| 3680 | #endif |
| 3681 | /* Save level, type and Py_buffer for each control message, |
| 3682 | and calculate total size. */ |
| 3683 | if (ncmsgs > 0 && (cmsgs = PyMem_New(struct cmsginfo, ncmsgs)) == NULL) { |
| 3684 | PyErr_NoMemory(); |
| 3685 | goto finally; |
| 3686 | } |
| 3687 | controllen = controllen_last = 0; |
| 3688 | while (ncmsgbufs < ncmsgs) { |
| 3689 | size_t bufsize, space; |
| 3690 | |
| 3691 | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(cmsg_fast, ncmsgbufs), |
| 3692 | "(iiy*):[sendmsg() ancillary data items]", |
| 3693 | &cmsgs[ncmsgbufs].level, |
| 3694 | &cmsgs[ncmsgbufs].type, |
| 3695 | &cmsgs[ncmsgbufs].data)) |
| 3696 | goto finally; |
| 3697 | bufsize = cmsgs[ncmsgbufs++].data.len; |
| 3698 | |
| 3699 | #ifdef CMSG_SPACE |
| 3700 | if (!get_CMSG_SPACE(bufsize, &space)) { |
| 3701 | #else |
| 3702 | if (!get_CMSG_LEN(bufsize, &space)) { |
| 3703 | #endif |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3704 | PyErr_SetString(PyExc_OSError, "ancillary data item too large"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3705 | goto finally; |
| 3706 | } |
| 3707 | controllen += space; |
| 3708 | if (controllen > SOCKLEN_T_LIMIT || controllen < controllen_last) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3709 | PyErr_SetString(PyExc_OSError, "too much ancillary data"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3710 | goto finally; |
| 3711 | } |
| 3712 | controllen_last = controllen; |
| 3713 | } |
| 3714 | |
| 3715 | /* Construct ancillary data block from control message info. */ |
| 3716 | if (ncmsgbufs > 0) { |
| 3717 | struct cmsghdr *cmsgh = NULL; |
| 3718 | |
| 3719 | if ((msg.msg_control = controlbuf = |
| 3720 | PyMem_Malloc(controllen)) == NULL) { |
| 3721 | PyErr_NoMemory(); |
| 3722 | goto finally; |
| 3723 | } |
| 3724 | msg.msg_controllen = controllen; |
| 3725 | |
| 3726 | /* Need to zero out the buffer as a workaround for glibc's |
| 3727 | CMSG_NXTHDR() implementation. After getting the pointer to |
| 3728 | the next header, it checks its (uninitialized) cmsg_len |
| 3729 | member to see if the "message" fits in the buffer, and |
| 3730 | returns NULL if it doesn't. Zero-filling the buffer |
Terry Jan Reedy | 0f84764 | 2013-03-11 18:34:00 -0400 | [diff] [blame] | 3731 | ensures that this doesn't happen. */ |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3732 | memset(controlbuf, 0, controllen); |
| 3733 | |
| 3734 | for (i = 0; i < ncmsgbufs; i++) { |
| 3735 | size_t msg_len, data_len = cmsgs[i].data.len; |
| 3736 | int enough_space = 0; |
| 3737 | |
| 3738 | cmsgh = (i == 0) ? CMSG_FIRSTHDR(&msg) : CMSG_NXTHDR(&msg, cmsgh); |
| 3739 | if (cmsgh == NULL) { |
| 3740 | PyErr_Format(PyExc_RuntimeError, |
| 3741 | "unexpected NULL result from %s()", |
| 3742 | (i == 0) ? "CMSG_FIRSTHDR" : "CMSG_NXTHDR"); |
| 3743 | goto finally; |
| 3744 | } |
| 3745 | if (!get_CMSG_LEN(data_len, &msg_len)) { |
| 3746 | PyErr_SetString(PyExc_RuntimeError, |
| 3747 | "item size out of range for CMSG_LEN()"); |
| 3748 | goto finally; |
| 3749 | } |
| 3750 | if (cmsg_min_space(&msg, cmsgh, msg_len)) { |
| 3751 | size_t space; |
| 3752 | |
| 3753 | cmsgh->cmsg_len = msg_len; |
| 3754 | if (get_cmsg_data_space(&msg, cmsgh, &space)) |
| 3755 | enough_space = (space >= data_len); |
| 3756 | } |
| 3757 | if (!enough_space) { |
| 3758 | PyErr_SetString(PyExc_RuntimeError, |
| 3759 | "ancillary data does not fit in calculated " |
| 3760 | "space"); |
| 3761 | goto finally; |
| 3762 | } |
| 3763 | cmsgh->cmsg_level = cmsgs[i].level; |
| 3764 | cmsgh->cmsg_type = cmsgs[i].type; |
| 3765 | memcpy(CMSG_DATA(cmsgh), cmsgs[i].data.buf, data_len); |
| 3766 | } |
| 3767 | } |
| 3768 | |
| 3769 | /* Make the system call. */ |
| 3770 | if (!IS_SELECTABLE(s)) { |
| 3771 | select_error(); |
| 3772 | goto finally; |
| 3773 | } |
| 3774 | |
| 3775 | BEGIN_SELECT_LOOP(s) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3776 | do { |
Victor Stinner | 391fa71 | 2015-03-31 13:15:31 +0200 | [diff] [blame] | 3777 | timeout = internal_select(s, 1, interval); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3778 | |
| 3779 | if (!timeout) { |
| 3780 | Py_BEGIN_ALLOW_THREADS; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3781 | bytes_sent = sendmsg(s->sock_fd, &msg, flags); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3782 | Py_END_ALLOW_THREADS; |
| 3783 | } |
| 3784 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3785 | if (timeout == 1) { |
| 3786 | PyErr_SetString(socket_timeout, "timed out"); |
| 3787 | goto finally; |
| 3788 | } |
| 3789 | } while (bytes_sent < 0 && errno == EINTR && |
| 3790 | !(async_err = PyErr_CheckSignals())); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3791 | END_SELECT_LOOP(s) |
| 3792 | |
| 3793 | if (bytes_sent < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3794 | if (!async_err) |
| 3795 | s->errorhandler(); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3796 | goto finally; |
| 3797 | } |
| 3798 | retval = PyLong_FromSsize_t(bytes_sent); |
| 3799 | |
| 3800 | finally: |
| 3801 | PyMem_Free(controlbuf); |
| 3802 | for (i = 0; i < ncmsgbufs; i++) |
| 3803 | PyBuffer_Release(&cmsgs[i].data); |
| 3804 | PyMem_Free(cmsgs); |
| 3805 | Py_XDECREF(cmsg_fast); |
| 3806 | for (i = 0; i < ndatabufs; i++) |
| 3807 | PyBuffer_Release(&databufs[i]); |
| 3808 | PyMem_Free(databufs); |
| 3809 | PyMem_Free(iovs); |
| 3810 | Py_XDECREF(data_fast); |
| 3811 | return retval; |
| 3812 | } |
| 3813 | |
| 3814 | PyDoc_STRVAR(sendmsg_doc, |
| 3815 | "sendmsg(buffers[, ancdata[, flags[, address]]]) -> count\n\ |
| 3816 | \n\ |
| 3817 | Send normal and ancillary data to the socket, gathering the\n\ |
| 3818 | non-ancillary data from a series of buffers and concatenating it into\n\ |
| 3819 | a single message. The buffers argument specifies the non-ancillary\n\ |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 3820 | 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] | 3821 | The ancdata argument specifies the ancillary data (control messages)\n\ |
| 3822 | as an iterable of zero or more tuples (cmsg_level, cmsg_type,\n\ |
| 3823 | cmsg_data), where cmsg_level and cmsg_type are integers specifying the\n\ |
| 3824 | protocol level and protocol-specific type respectively, and cmsg_data\n\ |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 3825 | is a bytes-like object holding the associated data. The flags\n\ |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3826 | argument defaults to 0 and has the same meaning as for send(). If\n\ |
| 3827 | address is supplied and not None, it sets a destination address for\n\ |
| 3828 | the message. The return value is the number of bytes of non-ancillary\n\ |
| 3829 | data sent."); |
| 3830 | #endif /* CMSG_LEN */ |
| 3831 | |
| 3832 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3833 | /* s.shutdown(how) method */ |
| 3834 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3835 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3836 | sock_shutdown(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3837 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3838 | int how; |
| 3839 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 3840 | |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 3841 | how = _PyLong_AsInt(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3842 | if (how == -1 && PyErr_Occurred()) |
| 3843 | return NULL; |
| 3844 | Py_BEGIN_ALLOW_THREADS |
| 3845 | res = shutdown(s->sock_fd, how); |
| 3846 | Py_END_ALLOW_THREADS |
| 3847 | if (res < 0) |
| 3848 | return s->errorhandler(); |
| 3849 | Py_INCREF(Py_None); |
| 3850 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3851 | } |
| 3852 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3853 | PyDoc_STRVAR(shutdown_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3854 | "shutdown(flag)\n\ |
| 3855 | \n\ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 3856 | Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\ |
| 3857 | 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] | 3858 | |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 3859 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3860 | static PyObject* |
| 3861 | sock_ioctl(PySocketSockObject *s, PyObject *arg) |
| 3862 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3863 | unsigned long cmd = SIO_RCVALL; |
| 3864 | PyObject *argO; |
| 3865 | DWORD recv; |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3866 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3867 | if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO)) |
| 3868 | return NULL; |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3870 | switch (cmd) { |
| 3871 | case SIO_RCVALL: { |
| 3872 | unsigned int option = RCVALL_ON; |
| 3873 | if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option)) |
| 3874 | return NULL; |
| 3875 | if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option), |
| 3876 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 3877 | return set_error(); |
| 3878 | } |
| 3879 | return PyLong_FromUnsignedLong(recv); } |
| 3880 | case SIO_KEEPALIVE_VALS: { |
| 3881 | struct tcp_keepalive ka; |
| 3882 | if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd, |
| 3883 | &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval)) |
| 3884 | return NULL; |
| 3885 | if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka), |
| 3886 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 3887 | return set_error(); |
| 3888 | } |
| 3889 | return PyLong_FromUnsignedLong(recv); } |
| 3890 | default: |
| 3891 | PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd); |
| 3892 | return NULL; |
| 3893 | } |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3894 | } |
| 3895 | PyDoc_STRVAR(sock_ioctl_doc, |
| 3896 | "ioctl(cmd, option) -> long\n\ |
| 3897 | \n\ |
Kristján Valur Jónsson | 847ec75 | 2009-09-27 21:10:38 +0000 | [diff] [blame] | 3898 | Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\ |
| 3899 | SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\ |
| 3900 | SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval)."); |
Kristján Valur Jónsson | 10f383a | 2012-04-07 11:23:31 +0000 | [diff] [blame] | 3901 | #endif |
| 3902 | |
| 3903 | #if defined(MS_WINDOWS) |
| 3904 | static PyObject* |
| 3905 | sock_share(PySocketSockObject *s, PyObject *arg) |
| 3906 | { |
| 3907 | WSAPROTOCOL_INFO info; |
| 3908 | DWORD processId; |
| 3909 | int result; |
| 3910 | |
| 3911 | if (!PyArg_ParseTuple(arg, "I", &processId)) |
| 3912 | return NULL; |
| 3913 | |
| 3914 | Py_BEGIN_ALLOW_THREADS |
| 3915 | result = WSADuplicateSocket(s->sock_fd, processId, &info); |
| 3916 | Py_END_ALLOW_THREADS |
| 3917 | if (result == SOCKET_ERROR) |
| 3918 | return set_error(); |
| 3919 | return PyBytes_FromStringAndSize((const char*)&info, sizeof(info)); |
| 3920 | } |
| 3921 | PyDoc_STRVAR(sock_share_doc, |
| 3922 | "share(process_id) -> bytes\n\ |
| 3923 | \n\ |
| 3924 | Share the socket with another process. The target process id\n\ |
| 3925 | must be provided and the resulting bytes object passed to the target\n\ |
| 3926 | process. There the shared socket can be instantiated by calling\n\ |
| 3927 | socket.fromshare()."); |
| 3928 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3929 | |
| 3930 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3931 | |
| 3932 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3933 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3934 | static PyMethodDef sock_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3935 | {"_accept", (PyCFunction)sock_accept, METH_NOARGS, |
| 3936 | accept_doc}, |
| 3937 | {"bind", (PyCFunction)sock_bind, METH_O, |
| 3938 | bind_doc}, |
| 3939 | {"close", (PyCFunction)sock_close, METH_NOARGS, |
| 3940 | close_doc}, |
| 3941 | {"connect", (PyCFunction)sock_connect, METH_O, |
| 3942 | connect_doc}, |
| 3943 | {"connect_ex", (PyCFunction)sock_connect_ex, METH_O, |
| 3944 | connect_ex_doc}, |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 3945 | {"detach", (PyCFunction)sock_detach, METH_NOARGS, |
| 3946 | detach_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3947 | {"fileno", (PyCFunction)sock_fileno, METH_NOARGS, |
| 3948 | fileno_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3949 | #ifdef HAVE_GETPEERNAME |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3950 | {"getpeername", (PyCFunction)sock_getpeername, |
| 3951 | METH_NOARGS, getpeername_doc}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 3952 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3953 | {"getsockname", (PyCFunction)sock_getsockname, |
| 3954 | METH_NOARGS, getsockname_doc}, |
| 3955 | {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS, |
| 3956 | getsockopt_doc}, |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 3957 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3958 | {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS, |
| 3959 | sock_ioctl_doc}, |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3960 | #endif |
Kristján Valur Jónsson | 10f383a | 2012-04-07 11:23:31 +0000 | [diff] [blame] | 3961 | #if defined(MS_WINDOWS) |
| 3962 | {"share", (PyCFunction)sock_share, METH_VARARGS, |
| 3963 | sock_share_doc}, |
| 3964 | #endif |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 3965 | {"listen", (PyCFunction)sock_listen, METH_VARARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3966 | listen_doc}, |
| 3967 | {"recv", (PyCFunction)sock_recv, METH_VARARGS, |
| 3968 | recv_doc}, |
| 3969 | {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS, |
| 3970 | recv_into_doc}, |
| 3971 | {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS, |
| 3972 | recvfrom_doc}, |
| 3973 | {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS, |
| 3974 | recvfrom_into_doc}, |
| 3975 | {"send", (PyCFunction)sock_send, METH_VARARGS, |
| 3976 | send_doc}, |
| 3977 | {"sendall", (PyCFunction)sock_sendall, METH_VARARGS, |
| 3978 | sendall_doc}, |
| 3979 | {"sendto", (PyCFunction)sock_sendto, METH_VARARGS, |
| 3980 | sendto_doc}, |
| 3981 | {"setblocking", (PyCFunction)sock_setblocking, METH_O, |
| 3982 | setblocking_doc}, |
| 3983 | {"settimeout", (PyCFunction)sock_settimeout, METH_O, |
| 3984 | settimeout_doc}, |
| 3985 | {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS, |
| 3986 | gettimeout_doc}, |
| 3987 | {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS, |
| 3988 | setsockopt_doc}, |
| 3989 | {"shutdown", (PyCFunction)sock_shutdown, METH_O, |
| 3990 | shutdown_doc}, |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3991 | #ifdef CMSG_LEN |
| 3992 | {"recvmsg", (PyCFunction)sock_recvmsg, METH_VARARGS, |
| 3993 | recvmsg_doc}, |
| 3994 | {"recvmsg_into", (PyCFunction)sock_recvmsg_into, METH_VARARGS, |
| 3995 | recvmsg_into_doc,}, |
| 3996 | {"sendmsg", (PyCFunction)sock_sendmsg, METH_VARARGS, |
| 3997 | sendmsg_doc}, |
| 3998 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3999 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4000 | }; |
| 4001 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4002 | /* SockObject members */ |
| 4003 | static PyMemberDef sock_memberlist[] = { |
| 4004 | {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"}, |
| 4005 | {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"}, |
| 4006 | {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4007 | {0}, |
| 4008 | }; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4009 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 4010 | static PyGetSetDef sock_getsetlist[] = { |
| 4011 | {"timeout", (getter)sock_gettimeout, NULL, PyDoc_STR("the socket timeout")}, |
| 4012 | {NULL} /* sentinel */ |
| 4013 | }; |
| 4014 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4015 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4016 | First close the file description. */ |
| 4017 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4018 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4019 | sock_dealloc(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4020 | { |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 4021 | if (s->sock_fd != -1) { |
| 4022 | PyObject *exc, *val, *tb; |
| 4023 | Py_ssize_t old_refcount = Py_REFCNT(s); |
| 4024 | ++Py_REFCNT(s); |
| 4025 | PyErr_Fetch(&exc, &val, &tb); |
| 4026 | if (PyErr_WarnFormat(PyExc_ResourceWarning, 1, |
| 4027 | "unclosed %R", s)) |
| 4028 | /* Spurious errors can appear at shutdown */ |
| 4029 | if (PyErr_ExceptionMatches(PyExc_Warning)) |
| 4030 | PyErr_WriteUnraisable((PyObject *) s); |
| 4031 | PyErr_Restore(exc, val, tb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4032 | (void) SOCKETCLOSE(s->sock_fd); |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 4033 | Py_REFCNT(s) = old_refcount; |
| 4034 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4035 | Py_TYPE(s)->tp_free((PyObject *)s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4036 | } |
| 4037 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4038 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4039 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4040 | sock_repr(PySocketSockObject *s) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4041 | { |
Victor Stinner | e254e53 | 2014-07-26 14:36:55 +0200 | [diff] [blame] | 4042 | long sock_fd; |
| 4043 | /* On Windows, this test is needed because SOCKET_T is unsigned */ |
| 4044 | if (s->sock_fd == INVALID_SOCKET) { |
| 4045 | sock_fd = -1; |
| 4046 | } |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 4047 | #if SIZEOF_SOCKET_T > SIZEOF_LONG |
Victor Stinner | e254e53 | 2014-07-26 14:36:55 +0200 | [diff] [blame] | 4048 | else if (s->sock_fd > LONG_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4049 | /* this can occur on Win64, and actually there is a special |
| 4050 | ugly printf formatter for decimal pointer length integer |
| 4051 | printing, only bother if necessary*/ |
| 4052 | PyErr_SetString(PyExc_OverflowError, |
| 4053 | "no printf formatter to display " |
| 4054 | "the socket descriptor in decimal"); |
| 4055 | return NULL; |
| 4056 | } |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 4057 | #endif |
Victor Stinner | e254e53 | 2014-07-26 14:36:55 +0200 | [diff] [blame] | 4058 | else |
| 4059 | sock_fd = (long)s->sock_fd; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4060 | return PyUnicode_FromFormat( |
| 4061 | "<socket object, fd=%ld, family=%d, type=%d, proto=%d>", |
Victor Stinner | e254e53 | 2014-07-26 14:36:55 +0200 | [diff] [blame] | 4062 | sock_fd, s->sock_family, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4063 | s->sock_type, |
| 4064 | s->sock_proto); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4065 | } |
| 4066 | |
| 4067 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4068 | /* Create a new, uninitialized socket object. */ |
| 4069 | |
| 4070 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4071 | sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4072 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4073 | PyObject *new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4074 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4075 | new = type->tp_alloc(type, 0); |
| 4076 | if (new != NULL) { |
| 4077 | ((PySocketSockObject *)new)->sock_fd = -1; |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 4078 | ((PySocketSockObject *)new)->sock_timeout = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4079 | ((PySocketSockObject *)new)->errorhandler = &set_error; |
| 4080 | } |
| 4081 | return new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4082 | } |
| 4083 | |
| 4084 | |
| 4085 | /* Initialize a new socket object. */ |
| 4086 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4087 | #ifdef SOCK_CLOEXEC |
| 4088 | /* socket() and socketpair() fail with EINVAL on Linux kernel older |
| 4089 | * than 2.6.27 if SOCK_CLOEXEC flag is set in the socket type. */ |
| 4090 | static int sock_cloexec_works = -1; |
| 4091 | #endif |
| 4092 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4093 | /*ARGSUSED*/ |
| 4094 | static int |
Andrew MacIntyre | 7aec4a2 | 2002-06-13 11:53:52 +0000 | [diff] [blame] | 4095 | sock_initobj(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4096 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4097 | PySocketSockObject *s = (PySocketSockObject *)self; |
| 4098 | PyObject *fdobj = NULL; |
| 4099 | SOCKET_T fd = INVALID_SOCKET; |
| 4100 | int family = AF_INET, type = SOCK_STREAM, proto = 0; |
| 4101 | static char *keywords[] = {"family", "type", "proto", "fileno", 0}; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4102 | #ifndef MS_WINDOWS |
| 4103 | #ifdef SOCK_CLOEXEC |
| 4104 | int *atomic_flag_works = &sock_cloexec_works; |
| 4105 | #else |
| 4106 | int *atomic_flag_works = NULL; |
| 4107 | #endif |
| 4108 | #endif |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4109 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4110 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 4111 | "|iiiO:socket", keywords, |
| 4112 | &family, &type, &proto, &fdobj)) |
| 4113 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4115 | if (fdobj != NULL && fdobj != Py_None) { |
Kristján Valur Jónsson | 10f383a | 2012-04-07 11:23:31 +0000 | [diff] [blame] | 4116 | #ifdef MS_WINDOWS |
| 4117 | /* recreate a socket that was duplicated */ |
| 4118 | if (PyBytes_Check(fdobj)) { |
| 4119 | WSAPROTOCOL_INFO info; |
| 4120 | if (PyBytes_GET_SIZE(fdobj) != sizeof(info)) { |
| 4121 | PyErr_Format(PyExc_ValueError, |
| 4122 | "socket descriptor string has wrong size, " |
| 4123 | "should be %zu bytes.", sizeof(info)); |
| 4124 | return -1; |
| 4125 | } |
| 4126 | memcpy(&info, PyBytes_AS_STRING(fdobj), sizeof(info)); |
| 4127 | Py_BEGIN_ALLOW_THREADS |
| 4128 | fd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, |
| 4129 | FROM_PROTOCOL_INFO, &info, 0, WSA_FLAG_OVERLAPPED); |
| 4130 | Py_END_ALLOW_THREADS |
| 4131 | if (fd == INVALID_SOCKET) { |
| 4132 | set_error(); |
| 4133 | return -1; |
| 4134 | } |
| 4135 | family = info.iAddressFamily; |
| 4136 | type = info.iSocketType; |
| 4137 | proto = info.iProtocol; |
| 4138 | } |
| 4139 | else |
| 4140 | #endif |
| 4141 | { |
| 4142 | fd = PyLong_AsSocket_t(fdobj); |
| 4143 | if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
| 4144 | return -1; |
| 4145 | if (fd == INVALID_SOCKET) { |
| 4146 | PyErr_SetString(PyExc_ValueError, |
| 4147 | "can't use invalid socket value"); |
| 4148 | return -1; |
| 4149 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4150 | } |
| 4151 | } |
| 4152 | else { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4153 | #ifdef MS_WINDOWS |
| 4154 | /* Windows implementation */ |
| 4155 | #ifndef WSA_FLAG_NO_HANDLE_INHERIT |
| 4156 | #define WSA_FLAG_NO_HANDLE_INHERIT 0x80 |
| 4157 | #endif |
| 4158 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4159 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4160 | if (support_wsa_no_inherit) { |
| 4161 | fd = WSASocket(family, type, proto, |
| 4162 | NULL, 0, |
| 4163 | WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT); |
| 4164 | if (fd == INVALID_SOCKET) { |
| 4165 | /* Windows 7 or Windows 2008 R2 without SP1 or the hotfix */ |
| 4166 | support_wsa_no_inherit = 0; |
| 4167 | fd = socket(family, type, proto); |
| 4168 | } |
| 4169 | } |
| 4170 | else { |
| 4171 | fd = socket(family, type, proto); |
| 4172 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4173 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4175 | if (fd == INVALID_SOCKET) { |
| 4176 | set_error(); |
| 4177 | return -1; |
| 4178 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4179 | |
| 4180 | if (!support_wsa_no_inherit) { |
| 4181 | if (!SetHandleInformation((HANDLE)fd, HANDLE_FLAG_INHERIT, 0)) { |
| 4182 | closesocket(fd); |
| 4183 | PyErr_SetFromWindowsErr(0); |
| 4184 | return -1; |
| 4185 | } |
| 4186 | } |
| 4187 | #else |
| 4188 | /* UNIX */ |
| 4189 | Py_BEGIN_ALLOW_THREADS |
| 4190 | #ifdef SOCK_CLOEXEC |
| 4191 | if (sock_cloexec_works != 0) { |
| 4192 | fd = socket(family, type | SOCK_CLOEXEC, proto); |
| 4193 | if (sock_cloexec_works == -1) { |
| 4194 | if (fd >= 0) { |
| 4195 | sock_cloexec_works = 1; |
| 4196 | } |
| 4197 | else if (errno == EINVAL) { |
| 4198 | /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */ |
| 4199 | sock_cloexec_works = 0; |
| 4200 | fd = socket(family, type, proto); |
| 4201 | } |
| 4202 | } |
| 4203 | } |
| 4204 | else |
| 4205 | #endif |
| 4206 | { |
| 4207 | fd = socket(family, type, proto); |
| 4208 | } |
| 4209 | Py_END_ALLOW_THREADS |
| 4210 | |
| 4211 | if (fd == INVALID_SOCKET) { |
| 4212 | set_error(); |
| 4213 | return -1; |
| 4214 | } |
| 4215 | |
| 4216 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 4217 | SOCKETCLOSE(fd); |
| 4218 | return -1; |
| 4219 | } |
| 4220 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4221 | } |
| 4222 | init_sockobject(s, fd, family, type, proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4223 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4224 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4225 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4226 | } |
| 4227 | |
| 4228 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4229 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4230 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4231 | static PyTypeObject sock_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4232 | PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */ |
| 4233 | "_socket.socket", /* tp_name */ |
| 4234 | sizeof(PySocketSockObject), /* tp_basicsize */ |
| 4235 | 0, /* tp_itemsize */ |
| 4236 | (destructor)sock_dealloc, /* tp_dealloc */ |
| 4237 | 0, /* tp_print */ |
| 4238 | 0, /* tp_getattr */ |
| 4239 | 0, /* tp_setattr */ |
| 4240 | 0, /* tp_reserved */ |
| 4241 | (reprfunc)sock_repr, /* tp_repr */ |
| 4242 | 0, /* tp_as_number */ |
| 4243 | 0, /* tp_as_sequence */ |
| 4244 | 0, /* tp_as_mapping */ |
| 4245 | 0, /* tp_hash */ |
| 4246 | 0, /* tp_call */ |
| 4247 | 0, /* tp_str */ |
| 4248 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4249 | 0, /* tp_setattro */ |
| 4250 | 0, /* tp_as_buffer */ |
| 4251 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 4252 | sock_doc, /* tp_doc */ |
| 4253 | 0, /* tp_traverse */ |
| 4254 | 0, /* tp_clear */ |
| 4255 | 0, /* tp_richcompare */ |
| 4256 | 0, /* tp_weaklistoffset */ |
| 4257 | 0, /* tp_iter */ |
| 4258 | 0, /* tp_iternext */ |
| 4259 | sock_methods, /* tp_methods */ |
| 4260 | sock_memberlist, /* tp_members */ |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 4261 | sock_getsetlist, /* tp_getset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4262 | 0, /* tp_base */ |
| 4263 | 0, /* tp_dict */ |
| 4264 | 0, /* tp_descr_get */ |
| 4265 | 0, /* tp_descr_set */ |
| 4266 | 0, /* tp_dictoffset */ |
| 4267 | sock_initobj, /* tp_init */ |
| 4268 | PyType_GenericAlloc, /* tp_alloc */ |
| 4269 | sock_new, /* tp_new */ |
| 4270 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4271 | }; |
| 4272 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4273 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 4274 | /* Python interface to gethostname(). */ |
| 4275 | |
| 4276 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4277 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4278 | socket_gethostname(PyObject *self, PyObject *unused) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 4279 | { |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4280 | #ifdef MS_WINDOWS |
| 4281 | /* Don't use winsock's gethostname, as this returns the ANSI |
| 4282 | version of the hostname, whereas we need a Unicode string. |
| 4283 | Otherwise, gethostname apparently also returns the DNS name. */ |
Hirokazu Yamamoto | 09fff7a | 2010-11-05 17:24:13 +0000 | [diff] [blame] | 4284 | wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 4285 | DWORD size = Py_ARRAY_LENGTH(buf); |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4286 | wchar_t *name; |
Hirokazu Yamamoto | 09fff7a | 2010-11-05 17:24:13 +0000 | [diff] [blame] | 4287 | PyObject *result; |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4288 | |
| 4289 | if (GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size)) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 4290 | return PyUnicode_FromWideChar(buf, size); |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4291 | |
| 4292 | if (GetLastError() != ERROR_MORE_DATA) |
| 4293 | return PyErr_SetFromWindowsErr(0); |
| 4294 | |
| 4295 | if (size == 0) |
| 4296 | return PyUnicode_New(0, 0); |
| 4297 | |
| 4298 | /* MSDN says ERROR_MORE_DATA may occur because DNS allows longer |
| 4299 | names */ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 4300 | name = PyMem_New(wchar_t, size); |
| 4301 | if (!name) { |
| 4302 | PyErr_NoMemory(); |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4303 | return NULL; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 4304 | } |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4305 | if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname, |
| 4306 | name, |
| 4307 | &size)) |
| 4308 | { |
| 4309 | PyMem_Free(name); |
| 4310 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4311 | } |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4312 | |
| 4313 | result = PyUnicode_FromWideChar(name, size); |
| 4314 | PyMem_Free(name); |
| 4315 | return result; |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4316 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4317 | char buf[1024]; |
| 4318 | int res; |
| 4319 | Py_BEGIN_ALLOW_THREADS |
| 4320 | res = gethostname(buf, (int) sizeof buf - 1); |
| 4321 | Py_END_ALLOW_THREADS |
| 4322 | if (res < 0) |
| 4323 | return set_error(); |
| 4324 | buf[sizeof buf - 1] = '\0'; |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4325 | return PyUnicode_DecodeFSDefault(buf); |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4326 | #endif |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 4327 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 4328 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4329 | PyDoc_STRVAR(gethostname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4330 | "gethostname() -> string\n\ |
| 4331 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4332 | Return the current host name."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4333 | |
Antoine Pitrou | 061cfb5 | 2011-02-28 22:25:22 +0000 | [diff] [blame] | 4334 | #ifdef HAVE_SETHOSTNAME |
| 4335 | PyDoc_STRVAR(sethostname_doc, |
| 4336 | "sethostname(name)\n\n\ |
| 4337 | Sets the hostname to name."); |
| 4338 | |
| 4339 | static PyObject * |
| 4340 | socket_sethostname(PyObject *self, PyObject *args) |
| 4341 | { |
| 4342 | PyObject *hnobj; |
| 4343 | Py_buffer buf; |
| 4344 | int res, flag = 0; |
| 4345 | |
Christian Heimes | d2774c7 | 2013-06-19 02:06:29 +0200 | [diff] [blame] | 4346 | #ifdef _AIX |
| 4347 | /* issue #18259, not declared in any useful header file */ |
| 4348 | extern int sethostname(const char *, size_t); |
| 4349 | #endif |
| 4350 | |
Antoine Pitrou | 061cfb5 | 2011-02-28 22:25:22 +0000 | [diff] [blame] | 4351 | if (!PyArg_ParseTuple(args, "S:sethostname", &hnobj)) { |
| 4352 | PyErr_Clear(); |
| 4353 | if (!PyArg_ParseTuple(args, "O&:sethostname", |
| 4354 | PyUnicode_FSConverter, &hnobj)) |
| 4355 | return NULL; |
| 4356 | flag = 1; |
| 4357 | } |
| 4358 | res = PyObject_GetBuffer(hnobj, &buf, PyBUF_SIMPLE); |
| 4359 | if (!res) { |
| 4360 | res = sethostname(buf.buf, buf.len); |
| 4361 | PyBuffer_Release(&buf); |
| 4362 | } |
| 4363 | if (flag) |
| 4364 | Py_DECREF(hnobj); |
| 4365 | if (res) |
| 4366 | return set_error(); |
| 4367 | Py_RETURN_NONE; |
| 4368 | } |
| 4369 | #endif |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 4370 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4371 | /* Python interface to gethostbyname(name). */ |
| 4372 | |
| 4373 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4374 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4375 | socket_gethostbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4376 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4377 | char *name; |
| 4378 | sock_addr_t addrbuf; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4379 | PyObject *ret = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4380 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4381 | if (!PyArg_ParseTuple(args, "et:gethostbyname", "idna", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4382 | return NULL; |
| 4383 | if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4384 | goto finally; |
| 4385 | ret = makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in)); |
| 4386 | finally: |
| 4387 | PyMem_Free(name); |
| 4388 | return ret; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4389 | } |
| 4390 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4391 | PyDoc_STRVAR(gethostbyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4392 | "gethostbyname(host) -> address\n\ |
| 4393 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4394 | 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] | 4395 | |
| 4396 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4397 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
| 4398 | |
| 4399 | static PyObject * |
Victor Stinner | 1a62a68 | 2014-08-17 19:33:28 +0200 | [diff] [blame] | 4400 | 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] | 4401 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4402 | char **pch; |
| 4403 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 4404 | PyObject *name_list = (PyObject *)NULL; |
| 4405 | PyObject *addr_list = (PyObject *)NULL; |
| 4406 | PyObject *tmp; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4408 | if (h == NULL) { |
| 4409 | /* Let's get real error message to return */ |
| 4410 | set_herror(h_errno); |
| 4411 | return NULL; |
| 4412 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4413 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4414 | if (h->h_addrtype != af) { |
| 4415 | /* Let's get real error message to return */ |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 4416 | errno = EAFNOSUPPORT; |
| 4417 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4418 | return NULL; |
| 4419 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4420 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4421 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4423 | case AF_INET: |
| 4424 | if (alen < sizeof(struct sockaddr_in)) |
| 4425 | return NULL; |
| 4426 | break; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4427 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4428 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4429 | case AF_INET6: |
| 4430 | if (alen < sizeof(struct sockaddr_in6)) |
| 4431 | return NULL; |
| 4432 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4433 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4434 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4435 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4436 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4437 | if ((name_list = PyList_New(0)) == NULL) |
| 4438 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4439 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4440 | if ((addr_list = PyList_New(0)) == NULL) |
| 4441 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4442 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4443 | /* SF #1511317: h_aliases can be NULL */ |
| 4444 | if (h->h_aliases) { |
| 4445 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 4446 | int status; |
| 4447 | tmp = PyUnicode_FromString(*pch); |
| 4448 | if (tmp == NULL) |
| 4449 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4450 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4451 | status = PyList_Append(name_list, tmp); |
| 4452 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4454 | if (status) |
| 4455 | goto err; |
| 4456 | } |
| 4457 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4458 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4459 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 4460 | int status; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4461 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4462 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4463 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4464 | case AF_INET: |
| 4465 | { |
| 4466 | struct sockaddr_in sin; |
| 4467 | memset(&sin, 0, sizeof(sin)); |
| 4468 | sin.sin_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4469 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4470 | sin.sin_len = sizeof(sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4471 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4472 | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); |
| 4473 | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4474 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4475 | if (pch == h->h_addr_list && alen >= sizeof(sin)) |
| 4476 | memcpy((char *) addr, &sin, sizeof(sin)); |
| 4477 | break; |
| 4478 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4479 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4480 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4481 | case AF_INET6: |
| 4482 | { |
| 4483 | struct sockaddr_in6 sin6; |
| 4484 | memset(&sin6, 0, sizeof(sin6)); |
| 4485 | sin6.sin6_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4486 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4487 | sin6.sin6_len = sizeof(sin6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4488 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4489 | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); |
| 4490 | tmp = makeipaddr((struct sockaddr *)&sin6, |
| 4491 | sizeof(sin6)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4492 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4493 | if (pch == h->h_addr_list && alen >= sizeof(sin6)) |
| 4494 | memcpy((char *) addr, &sin6, sizeof(sin6)); |
| 4495 | break; |
| 4496 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4497 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4498 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4499 | default: /* can't happen */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4500 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4501 | "unsupported address family"); |
| 4502 | return NULL; |
| 4503 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4505 | if (tmp == NULL) |
| 4506 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4508 | status = PyList_Append(addr_list, tmp); |
| 4509 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4511 | if (status) |
| 4512 | goto err; |
| 4513 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4515 | rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4516 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4517 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4518 | Py_XDECREF(name_list); |
| 4519 | Py_XDECREF(addr_list); |
| 4520 | return rtn_tuple; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4521 | } |
| 4522 | |
| 4523 | |
| 4524 | /* Python interface to gethostbyname_ex(name). */ |
| 4525 | |
| 4526 | /*ARGSUSED*/ |
| 4527 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4528 | socket_gethostbyname_ex(PyObject *self, PyObject *args) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4529 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4530 | char *name; |
| 4531 | struct hostent *h; |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4532 | sock_addr_t addr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4533 | struct sockaddr *sa; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4534 | PyObject *ret = NULL; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4535 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4536 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4537 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4538 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4539 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4540 | char buf[16384]; |
| 4541 | int buf_len = (sizeof buf) - 1; |
| 4542 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4543 | #endif |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4544 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4545 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4546 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4547 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4548 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4549 | if (!PyArg_ParseTuple(args, "et:gethostbyname_ex", "idna", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4550 | return NULL; |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4551 | if (setipaddr(name, SAS2SA(&addr), sizeof(addr), AF_INET) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4552 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4553 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4554 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4555 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4556 | gethostbyname_r(name, &hp_allocated, buf, buf_len, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4557 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4558 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4559 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4560 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4561 | memset((void *) &data, '\0', sizeof(data)); |
| 4562 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 4563 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4564 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4565 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4566 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4567 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4568 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4569 | h = gethostbyname(name); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4570 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4571 | Py_END_ALLOW_THREADS |
| 4572 | /* Some C libraries would require addr.__ss_family instead of |
| 4573 | addr.ss_family. |
| 4574 | Therefore, we cast the sockaddr_storage into sockaddr to |
| 4575 | access sa_family. */ |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4576 | sa = SAS2SA(&addr); |
| 4577 | ret = gethost_common(h, SAS2SA(&addr), sizeof(addr), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4578 | sa->sa_family); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4579 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4580 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 4581 | #endif |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4582 | finally: |
| 4583 | PyMem_Free(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4584 | return ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4585 | } |
| 4586 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4587 | PyDoc_STRVAR(ghbn_ex_doc, |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4588 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
| 4589 | \n\ |
| 4590 | 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] | 4591 | 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] | 4592 | |
| 4593 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4594 | /* Python interface to gethostbyaddr(IP). */ |
| 4595 | |
| 4596 | /*ARGSUSED*/ |
| 4597 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4598 | socket_gethostbyaddr(PyObject *self, PyObject *args) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4599 | { |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4600 | sock_addr_t addr; |
| 4601 | struct sockaddr *sa = SAS2SA(&addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4602 | char *ip_num; |
| 4603 | struct hostent *h; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4604 | PyObject *ret = NULL; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4605 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4606 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4607 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4608 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4609 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4610 | /* glibcs up to 2.10 assume that the buf argument to |
| 4611 | gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc |
| 4612 | does not ensure. The attribute below instructs the compiler |
| 4613 | to maintain this alignment. */ |
| 4614 | char buf[16384] Py_ALIGNED(8); |
| 4615 | int buf_len = (sizeof buf) - 1; |
| 4616 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4617 | #endif |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4618 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4619 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4620 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4621 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4622 | char *ap; |
| 4623 | int al; |
| 4624 | int af; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4625 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4626 | if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4627 | return NULL; |
| 4628 | af = AF_UNSPEC; |
| 4629 | if (setipaddr(ip_num, sa, sizeof(addr), af) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4630 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4631 | af = sa->sa_family; |
| 4632 | ap = NULL; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 4633 | /* al = 0; */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4634 | switch (af) { |
| 4635 | case AF_INET: |
| 4636 | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr; |
| 4637 | al = sizeof(((struct sockaddr_in *)sa)->sin_addr); |
| 4638 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4639 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4640 | case AF_INET6: |
| 4641 | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr; |
| 4642 | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); |
| 4643 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4644 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4645 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4646 | PyErr_SetString(PyExc_OSError, "unsupported address family"); |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4647 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4648 | } |
| 4649 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4650 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4651 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4652 | gethostbyaddr_r(ap, al, af, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4653 | &hp_allocated, buf, buf_len, |
| 4654 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4655 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4656 | h = gethostbyaddr_r(ap, al, af, |
| 4657 | &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4658 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4659 | memset((void *) &data, '\0', sizeof(data)); |
| 4660 | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); |
| 4661 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4662 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4663 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4664 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4665 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4666 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4667 | h = gethostbyaddr(ap, al, af); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4668 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4669 | Py_END_ALLOW_THREADS |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4670 | ret = gethost_common(h, SAS2SA(&addr), sizeof(addr), af); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4671 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4672 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4673 | #endif |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4674 | finally: |
| 4675 | PyMem_Free(ip_num); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4676 | return ret; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4677 | } |
| 4678 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4679 | PyDoc_STRVAR(gethostbyaddr_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4680 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
| 4681 | \n\ |
| 4682 | 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] | 4683 | 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] | 4684 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4685 | |
| 4686 | /* Python interface to getservbyname(name). |
| 4687 | This only returns the port number, since the other info is already |
| 4688 | known or not useful (like the list of aliases). */ |
| 4689 | |
| 4690 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4691 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4692 | socket_getservbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4693 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4694 | char *name, *proto=NULL; |
| 4695 | struct servent *sp; |
| 4696 | if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto)) |
| 4697 | return NULL; |
| 4698 | Py_BEGIN_ALLOW_THREADS |
| 4699 | sp = getservbyname(name, proto); |
| 4700 | Py_END_ALLOW_THREADS |
| 4701 | if (sp == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4702 | PyErr_SetString(PyExc_OSError, "service/proto not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4703 | return NULL; |
| 4704 | } |
| 4705 | return PyLong_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4706 | } |
| 4707 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4708 | PyDoc_STRVAR(getservbyname_doc, |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4709 | "getservbyname(servicename[, protocolname]) -> integer\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4710 | \n\ |
| 4711 | Return a port number from a service name and protocol name.\n\ |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4712 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 4713 | otherwise any protocol will match."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4714 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4715 | |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4716 | /* Python interface to getservbyport(port). |
| 4717 | This only returns the service name, since the other info is already |
| 4718 | known or not useful (like the list of aliases). */ |
| 4719 | |
| 4720 | /*ARGSUSED*/ |
| 4721 | static PyObject * |
| 4722 | socket_getservbyport(PyObject *self, PyObject *args) |
| 4723 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4724 | int port; |
| 4725 | char *proto=NULL; |
| 4726 | struct servent *sp; |
| 4727 | if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto)) |
| 4728 | return NULL; |
| 4729 | if (port < 0 || port > 0xffff) { |
| 4730 | PyErr_SetString( |
| 4731 | PyExc_OverflowError, |
| 4732 | "getservbyport: port must be 0-65535."); |
| 4733 | return NULL; |
| 4734 | } |
| 4735 | Py_BEGIN_ALLOW_THREADS |
| 4736 | sp = getservbyport(htons((short)port), proto); |
| 4737 | Py_END_ALLOW_THREADS |
| 4738 | if (sp == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4739 | PyErr_SetString(PyExc_OSError, "port/proto not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4740 | return NULL; |
| 4741 | } |
| 4742 | return PyUnicode_FromString(sp->s_name); |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4743 | } |
| 4744 | |
| 4745 | PyDoc_STRVAR(getservbyport_doc, |
| 4746 | "getservbyport(port[, protocolname]) -> string\n\ |
| 4747 | \n\ |
| 4748 | Return the service name from a port number and protocol name.\n\ |
| 4749 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 4750 | otherwise any protocol will match."); |
| 4751 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4752 | /* Python interface to getprotobyname(name). |
| 4753 | This only returns the protocol number, since the other info is |
| 4754 | already known or not useful (like the list of aliases). */ |
| 4755 | |
| 4756 | /*ARGSUSED*/ |
| 4757 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4758 | socket_getprotobyname(PyObject *self, PyObject *args) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4759 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4760 | char *name; |
| 4761 | struct protoent *sp; |
| 4762 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) |
| 4763 | return NULL; |
| 4764 | Py_BEGIN_ALLOW_THREADS |
| 4765 | sp = getprotobyname(name); |
| 4766 | Py_END_ALLOW_THREADS |
| 4767 | if (sp == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4768 | PyErr_SetString(PyExc_OSError, "protocol not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4769 | return NULL; |
| 4770 | } |
| 4771 | return PyLong_FromLong((long) sp->p_proto); |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4772 | } |
| 4773 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4774 | PyDoc_STRVAR(getprotobyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4775 | "getprotobyname(name) -> integer\n\ |
| 4776 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4777 | Return the protocol number for the named protocol. (Rarely used.)"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4778 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4779 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4780 | #ifndef NO_DUP |
| 4781 | /* dup() function for socket fds */ |
| 4782 | |
| 4783 | static PyObject * |
| 4784 | socket_dup(PyObject *self, PyObject *fdobj) |
| 4785 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4786 | SOCKET_T fd, newfd; |
| 4787 | PyObject *newfdobj; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4788 | #ifdef MS_WINDOWS |
| 4789 | WSAPROTOCOL_INFO info; |
| 4790 | #endif |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4791 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4792 | fd = PyLong_AsSocket_t(fdobj); |
| 4793 | if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
| 4794 | return NULL; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4795 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4796 | #ifdef MS_WINDOWS |
| 4797 | if (WSADuplicateSocket(fd, GetCurrentProcessId(), &info)) |
| 4798 | return set_error(); |
| 4799 | |
| 4800 | newfd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, |
| 4801 | FROM_PROTOCOL_INFO, |
| 4802 | &info, 0, WSA_FLAG_OVERLAPPED); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4803 | if (newfd == INVALID_SOCKET) |
| 4804 | return set_error(); |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4805 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4806 | if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) { |
| 4807 | closesocket(newfd); |
| 4808 | PyErr_SetFromWindowsErr(0); |
| 4809 | return NULL; |
| 4810 | } |
| 4811 | #else |
| 4812 | /* On UNIX, dup can be used to duplicate the file descriptor of a socket */ |
| 4813 | newfd = _Py_dup(fd); |
| 4814 | if (newfd == INVALID_SOCKET) |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 4815 | return NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4816 | #endif |
| 4817 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4818 | newfdobj = PyLong_FromSocket_t(newfd); |
| 4819 | if (newfdobj == NULL) |
| 4820 | SOCKETCLOSE(newfd); |
| 4821 | return newfdobj; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4822 | } |
| 4823 | |
| 4824 | PyDoc_STRVAR(dup_doc, |
| 4825 | "dup(integer) -> integer\n\ |
| 4826 | \n\ |
| 4827 | Duplicate an integer socket file descriptor. This is like os.dup(), but for\n\ |
| 4828 | sockets; on some platforms os.dup() won't work for socket file descriptors."); |
| 4829 | #endif |
| 4830 | |
| 4831 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4832 | #ifdef HAVE_SOCKETPAIR |
| 4833 | /* Create a pair of sockets using the socketpair() function. |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 4834 | Arguments as for socket() except the default family is AF_UNIX if |
Dave Cole | e8bbfe4 | 2004-08-26 00:51:16 +0000 | [diff] [blame] | 4835 | defined on the platform; otherwise, the default is AF_INET. */ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4836 | |
| 4837 | /*ARGSUSED*/ |
| 4838 | static PyObject * |
| 4839 | socket_socketpair(PyObject *self, PyObject *args) |
| 4840 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4841 | PySocketSockObject *s0 = NULL, *s1 = NULL; |
| 4842 | SOCKET_T sv[2]; |
| 4843 | int family, type = SOCK_STREAM, proto = 0; |
| 4844 | PyObject *res = NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4845 | #ifdef SOCK_CLOEXEC |
| 4846 | int *atomic_flag_works = &sock_cloexec_works; |
| 4847 | #else |
| 4848 | int *atomic_flag_works = NULL; |
| 4849 | #endif |
| 4850 | int ret; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4851 | |
| 4852 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4853 | family = AF_UNIX; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4854 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4855 | family = AF_INET; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4856 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4857 | if (!PyArg_ParseTuple(args, "|iii:socketpair", |
| 4858 | &family, &type, &proto)) |
| 4859 | return NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4860 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4861 | /* Create a pair of socket fds */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4862 | Py_BEGIN_ALLOW_THREADS |
| 4863 | #ifdef SOCK_CLOEXEC |
| 4864 | if (sock_cloexec_works != 0) { |
| 4865 | ret = socketpair(family, type | SOCK_CLOEXEC, proto, sv); |
| 4866 | if (sock_cloexec_works == -1) { |
| 4867 | if (ret >= 0) { |
| 4868 | sock_cloexec_works = 1; |
| 4869 | } |
| 4870 | else if (errno == EINVAL) { |
| 4871 | /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */ |
| 4872 | sock_cloexec_works = 0; |
| 4873 | ret = socketpair(family, type, proto, sv); |
| 4874 | } |
| 4875 | } |
| 4876 | } |
| 4877 | else |
| 4878 | #endif |
| 4879 | { |
| 4880 | ret = socketpair(family, type, proto, sv); |
| 4881 | } |
| 4882 | Py_END_ALLOW_THREADS |
| 4883 | |
| 4884 | if (ret < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4885 | return set_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4886 | |
| 4887 | if (_Py_set_inheritable(sv[0], 0, atomic_flag_works) < 0) |
| 4888 | goto finally; |
| 4889 | if (_Py_set_inheritable(sv[1], 0, atomic_flag_works) < 0) |
| 4890 | goto finally; |
| 4891 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4892 | s0 = new_sockobject(sv[0], family, type, proto); |
| 4893 | if (s0 == NULL) |
| 4894 | goto finally; |
| 4895 | s1 = new_sockobject(sv[1], family, type, proto); |
| 4896 | if (s1 == NULL) |
| 4897 | goto finally; |
| 4898 | res = PyTuple_Pack(2, s0, s1); |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4899 | |
| 4900 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4901 | if (res == NULL) { |
| 4902 | if (s0 == NULL) |
| 4903 | SOCKETCLOSE(sv[0]); |
| 4904 | if (s1 == NULL) |
| 4905 | SOCKETCLOSE(sv[1]); |
| 4906 | } |
| 4907 | Py_XDECREF(s0); |
| 4908 | Py_XDECREF(s1); |
| 4909 | return res; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4910 | } |
| 4911 | |
| 4912 | PyDoc_STRVAR(socketpair_doc, |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4913 | "socketpair([family[, type [, proto]]]) -> (socket object, socket object)\n\ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4914 | \n\ |
| 4915 | Create a pair of socket objects from the sockets returned by the platform\n\ |
| 4916 | socketpair() function.\n\ |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 4917 | 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] | 4918 | 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] | 4919 | |
| 4920 | #endif /* HAVE_SOCKETPAIR */ |
| 4921 | |
| 4922 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4923 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4924 | socket_ntohs(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4925 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4926 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4927 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4928 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { |
| 4929 | return NULL; |
| 4930 | } |
| 4931 | if (x1 < 0) { |
| 4932 | PyErr_SetString(PyExc_OverflowError, |
| 4933 | "can't convert negative number to unsigned long"); |
| 4934 | return NULL; |
| 4935 | } |
| 4936 | x2 = (unsigned int)ntohs((unsigned short)x1); |
| 4937 | return PyLong_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4938 | } |
| 4939 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4940 | PyDoc_STRVAR(ntohs_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4941 | "ntohs(integer) -> integer\n\ |
| 4942 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4943 | Convert a 16-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4944 | |
| 4945 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4946 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4947 | socket_ntohl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4948 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4949 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4950 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4951 | if (PyLong_Check(arg)) { |
| 4952 | x = PyLong_AsUnsignedLong(arg); |
| 4953 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 4954 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4955 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4956 | { |
| 4957 | unsigned long y; |
| 4958 | /* only want the trailing 32 bits */ |
| 4959 | y = x & 0xFFFFFFFFUL; |
| 4960 | if (y ^ x) |
| 4961 | return PyErr_Format(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4962 | "int larger than 32 bits"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4963 | x = y; |
| 4964 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4965 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4966 | } |
| 4967 | else |
| 4968 | return PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4969 | "expected int, %s found", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4970 | Py_TYPE(arg)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4971 | return PyLong_FromUnsignedLong(ntohl(x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4972 | } |
| 4973 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4974 | PyDoc_STRVAR(ntohl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4975 | "ntohl(integer) -> integer\n\ |
| 4976 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4977 | Convert a 32-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4978 | |
| 4979 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4980 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4981 | socket_htons(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4982 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4983 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4984 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4985 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) { |
| 4986 | return NULL; |
| 4987 | } |
| 4988 | if (x1 < 0) { |
| 4989 | PyErr_SetString(PyExc_OverflowError, |
| 4990 | "can't convert negative number to unsigned long"); |
| 4991 | return NULL; |
| 4992 | } |
| 4993 | x2 = (unsigned int)htons((unsigned short)x1); |
| 4994 | return PyLong_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4995 | } |
| 4996 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4997 | PyDoc_STRVAR(htons_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4998 | "htons(integer) -> integer\n\ |
| 4999 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5000 | Convert a 16-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5001 | |
| 5002 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 5003 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 5004 | socket_htonl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 5005 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5006 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 5007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5008 | if (PyLong_Check(arg)) { |
| 5009 | x = PyLong_AsUnsignedLong(arg); |
| 5010 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 5011 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 5012 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5013 | { |
| 5014 | unsigned long y; |
| 5015 | /* only want the trailing 32 bits */ |
| 5016 | y = x & 0xFFFFFFFFUL; |
| 5017 | if (y ^ x) |
| 5018 | return PyErr_Format(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 5019 | "int larger than 32 bits"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5020 | x = y; |
| 5021 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 5022 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5023 | } |
| 5024 | else |
| 5025 | return PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 5026 | "expected int, %s found", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5027 | Py_TYPE(arg)->tp_name); |
| 5028 | return PyLong_FromUnsignedLong(htonl((unsigned long)x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 5029 | } |
| 5030 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5031 | PyDoc_STRVAR(htonl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5032 | "htonl(integer) -> integer\n\ |
| 5033 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5034 | Convert a 32-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5035 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5036 | /* socket.inet_aton() and socket.inet_ntoa() functions. */ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5037 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5038 | PyDoc_STRVAR(inet_aton_doc, |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 5039 | "inet_aton(string) -> bytes giving packed 32-bit IP representation\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5040 | \n\ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 5041 | 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] | 5042 | binary format used in low-level network functions."); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5043 | |
| 5044 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5045 | socket_inet_aton(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5046 | { |
Neal Norwitz | 88f115b | 2003-02-13 02:15:42 +0000 | [diff] [blame] | 5047 | #ifdef HAVE_INET_ATON |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5048 | struct in_addr buf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5049 | #endif |
| 5050 | |
| 5051 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 5052 | #if (SIZEOF_INT != 4) |
| 5053 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 5054 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5055 | /* Have to use inet_addr() instead */ |
| 5056 | unsigned int packed_addr; |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 5057 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5058 | char *ip_addr; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 5059 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5060 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) |
| 5061 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 5062 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 5063 | |
| 5064 | #ifdef HAVE_INET_ATON |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5065 | |
| 5066 | #ifdef USE_INET_ATON_WEAKLINK |
| 5067 | if (inet_aton != NULL) { |
| 5068 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5069 | if (inet_aton(ip_addr, &buf)) |
| 5070 | return PyBytes_FromStringAndSize((char *)(&buf), |
| 5071 | sizeof(buf)); |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 5072 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5073 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5074 | "illegal IP address string passed to inet_aton"); |
| 5075 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 5076 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5077 | #ifdef USE_INET_ATON_WEAKLINK |
| 5078 | } else { |
| 5079 | #endif |
| 5080 | |
| 5081 | #endif |
| 5082 | |
| 5083 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
| 5084 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5085 | /* special-case this address as inet_addr might return INADDR_NONE |
| 5086 | * for this */ |
| 5087 | if (strcmp(ip_addr, "255.255.255.255") == 0) { |
Charles-François Natali | 24aa041 | 2013-08-31 14:48:25 +0200 | [diff] [blame] | 5088 | packed_addr = INADDR_BROADCAST; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5089 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5090 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5091 | packed_addr = inet_addr(ip_addr); |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 5092 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5093 | if (packed_addr == INADDR_NONE) { /* invalid address */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5094 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5095 | "illegal IP address string passed to inet_aton"); |
| 5096 | return NULL; |
| 5097 | } |
| 5098 | } |
| 5099 | return PyBytes_FromStringAndSize((char *) &packed_addr, |
| 5100 | sizeof(packed_addr)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5101 | |
| 5102 | #ifdef USE_INET_ATON_WEAKLINK |
| 5103 | } |
| 5104 | #endif |
| 5105 | |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 5106 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5107 | } |
| 5108 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5109 | PyDoc_STRVAR(inet_ntoa_doc, |
Fred Drake | e066134 | 2000-03-07 14:05:16 +0000 | [diff] [blame] | 5110 | "inet_ntoa(packed_ip) -> ip_address_string\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5111 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5112 | 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] | 5113 | |
| 5114 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5115 | socket_inet_ntoa(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5116 | { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5117 | Py_buffer packed_ip; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5118 | struct in_addr packed_addr; |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5119 | |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5120 | if (!PyArg_ParseTuple(args, "y*:inet_ntoa", &packed_ip)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5121 | return NULL; |
| 5122 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 5123 | |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5124 | if (packed_ip.len != sizeof(packed_addr)) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5125 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5126 | "packed IP wrong length for inet_ntoa"); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5127 | PyBuffer_Release(&packed_ip); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5128 | return NULL; |
| 5129 | } |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5130 | |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5131 | memcpy(&packed_addr, packed_ip.buf, packed_ip.len); |
| 5132 | PyBuffer_Release(&packed_ip); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5133 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5134 | return PyUnicode_FromString(inet_ntoa(packed_addr)); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5135 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5136 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5137 | #if defined(HAVE_INET_PTON) || defined(MS_WINDOWS) |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5138 | |
| 5139 | PyDoc_STRVAR(inet_pton_doc, |
| 5140 | "inet_pton(af, ip) -> packed IP address string\n\ |
| 5141 | \n\ |
| 5142 | Convert an IP address from string format to a packed string suitable\n\ |
| 5143 | for use with low-level network functions."); |
| 5144 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5145 | #endif |
| 5146 | |
| 5147 | #ifdef HAVE_INET_PTON |
| 5148 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5149 | static PyObject * |
| 5150 | socket_inet_pton(PyObject *self, PyObject *args) |
| 5151 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5152 | int af; |
| 5153 | char* ip; |
| 5154 | int retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5155 | #ifdef ENABLE_IPV6 |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 5156 | 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] | 5157 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5158 | char packed[sizeof(struct in_addr)]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5159 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5160 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 5161 | return NULL; |
| 5162 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5163 | |
Martin v. Löwis | 04697e8 | 2004-06-02 12:35:29 +0000 | [diff] [blame] | 5164 | #if !defined(ENABLE_IPV6) && defined(AF_INET6) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5165 | if(af == AF_INET6) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5166 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5167 | "can't use AF_INET6, IPv6 is disabled"); |
| 5168 | return NULL; |
| 5169 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5170 | #endif |
Martin v. Löwis | 1064909 | 2003-08-05 06:25:06 +0000 | [diff] [blame] | 5171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5172 | retval = inet_pton(af, ip, packed); |
| 5173 | if (retval < 0) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5174 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5175 | return NULL; |
| 5176 | } else if (retval == 0) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5177 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5178 | "illegal IP address string passed to inet_pton"); |
| 5179 | return NULL; |
| 5180 | } else if (af == AF_INET) { |
| 5181 | return PyBytes_FromStringAndSize(packed, |
| 5182 | sizeof(struct in_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5183 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5184 | } else if (af == AF_INET6) { |
| 5185 | return PyBytes_FromStringAndSize(packed, |
| 5186 | sizeof(struct in6_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5187 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5188 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5189 | PyErr_SetString(PyExc_OSError, "unknown address family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5190 | return NULL; |
| 5191 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5192 | } |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5193 | #elif defined(MS_WINDOWS) |
| 5194 | |
| 5195 | static PyObject * |
| 5196 | socket_inet_pton(PyObject *self, PyObject *args) |
| 5197 | { |
| 5198 | int af; |
| 5199 | char* ip; |
| 5200 | struct sockaddr_in6 addr; |
| 5201 | INT ret, size; |
| 5202 | |
| 5203 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 5204 | return NULL; |
| 5205 | } |
| 5206 | |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5207 | size = sizeof(addr); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5208 | ret = WSAStringToAddressA(ip, af, NULL, (LPSOCKADDR)&addr, &size); |
| 5209 | |
| 5210 | if (ret) { |
| 5211 | PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError()); |
| 5212 | return NULL; |
| 5213 | } else if(af == AF_INET) { |
| 5214 | struct sockaddr_in *addr4 = (struct sockaddr_in*)&addr; |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5215 | return PyBytes_FromStringAndSize((const char *)&(addr4->sin_addr), |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5216 | sizeof(addr4->sin_addr)); |
| 5217 | } else if (af == AF_INET6) { |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5218 | return PyBytes_FromStringAndSize((const char *)&(addr.sin6_addr), |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5219 | sizeof(addr.sin6_addr)); |
| 5220 | } else { |
| 5221 | PyErr_SetString(PyExc_OSError, "unknown address family"); |
| 5222 | return NULL; |
| 5223 | } |
| 5224 | } |
| 5225 | |
| 5226 | #endif |
| 5227 | |
| 5228 | #if defined(HAVE_INET_PTON) || defined(MS_WINDOWS) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5229 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5230 | PyDoc_STRVAR(inet_ntop_doc, |
| 5231 | "inet_ntop(af, packed_ip) -> string formatted IP address\n\ |
| 5232 | \n\ |
| 5233 | Convert a packed IP address of the given family to string format."); |
| 5234 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5235 | #endif |
| 5236 | |
| 5237 | |
| 5238 | #ifdef HAVE_INET_PTON |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5239 | static PyObject * |
| 5240 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 5241 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5242 | int af; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5243 | Py_buffer packed_ip; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5244 | const char* retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5245 | #ifdef ENABLE_IPV6 |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 5246 | char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5247 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5248 | char ip[INET_ADDRSTRLEN + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5249 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5251 | /* Guarantee NUL-termination for PyUnicode_FromString() below */ |
| 5252 | memset((void *) &ip[0], '\0', sizeof(ip)); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5253 | |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5254 | if (!PyArg_ParseTuple(args, "iy*:inet_ntop", &af, &packed_ip)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5255 | return NULL; |
| 5256 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5257 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5258 | if (af == AF_INET) { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5259 | if (packed_ip.len != sizeof(struct in_addr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5260 | PyErr_SetString(PyExc_ValueError, |
| 5261 | "invalid length of packed IP address string"); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5262 | PyBuffer_Release(&packed_ip); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5263 | return NULL; |
| 5264 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5265 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5266 | } else if (af == AF_INET6) { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5267 | if (packed_ip.len != sizeof(struct in6_addr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5268 | PyErr_SetString(PyExc_ValueError, |
| 5269 | "invalid length of packed IP address string"); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5270 | PyBuffer_Release(&packed_ip); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5271 | return NULL; |
| 5272 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5273 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5274 | } else { |
| 5275 | PyErr_Format(PyExc_ValueError, |
| 5276 | "unknown address family %d", af); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5277 | PyBuffer_Release(&packed_ip); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5278 | return NULL; |
| 5279 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5280 | |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5281 | retval = inet_ntop(af, packed_ip.buf, ip, sizeof(ip)); |
| 5282 | PyBuffer_Release(&packed_ip); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5283 | if (!retval) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5284 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5285 | return NULL; |
| 5286 | } else { |
| 5287 | return PyUnicode_FromString(retval); |
| 5288 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5290 | /* NOTREACHED */ |
| 5291 | PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop"); |
| 5292 | return NULL; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5293 | } |
| 5294 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5295 | #elif defined(MS_WINDOWS) |
| 5296 | |
| 5297 | static PyObject * |
| 5298 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 5299 | { |
| 5300 | int af; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5301 | Py_buffer packed_ip; |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5302 | struct sockaddr_in6 addr; |
| 5303 | DWORD addrlen, ret, retlen; |
Jason R. Coombs | 8ec784c | 2013-11-10 13:43:22 -0500 | [diff] [blame] | 5304 | #ifdef ENABLE_IPV6 |
| 5305 | char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
| 5306 | #else |
| 5307 | char ip[INET_ADDRSTRLEN + 1]; |
| 5308 | #endif |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5309 | |
| 5310 | /* Guarantee NUL-termination for PyUnicode_FromString() below */ |
| 5311 | memset((void *) &ip[0], '\0', sizeof(ip)); |
| 5312 | |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5313 | if (!PyArg_ParseTuple(args, "iy*:inet_ntop", &af, &packed_ip)) { |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5314 | return NULL; |
| 5315 | } |
| 5316 | |
| 5317 | if (af == AF_INET) { |
| 5318 | struct sockaddr_in * addr4 = (struct sockaddr_in *)&addr; |
| 5319 | |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5320 | if (packed_ip.len != sizeof(struct in_addr)) { |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5321 | PyErr_SetString(PyExc_ValueError, |
| 5322 | "invalid length of packed IP address string"); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5323 | PyBuffer_Release(&packed_ip); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5324 | return NULL; |
| 5325 | } |
| 5326 | memset(addr4, 0, sizeof(struct sockaddr_in)); |
| 5327 | addr4->sin_family = AF_INET; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5328 | memcpy(&(addr4->sin_addr), packed_ip.buf, sizeof(addr4->sin_addr)); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5329 | addrlen = sizeof(struct sockaddr_in); |
| 5330 | } else if (af == AF_INET6) { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5331 | if (packed_ip.len != sizeof(struct in6_addr)) { |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5332 | PyErr_SetString(PyExc_ValueError, |
| 5333 | "invalid length of packed IP address string"); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5334 | PyBuffer_Release(&packed_ip); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5335 | return NULL; |
| 5336 | } |
| 5337 | |
| 5338 | memset(&addr, 0, sizeof(addr)); |
| 5339 | addr.sin6_family = AF_INET6; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5340 | memcpy(&(addr.sin6_addr), packed_ip.buf, sizeof(addr.sin6_addr)); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5341 | addrlen = sizeof(addr); |
| 5342 | } else { |
| 5343 | PyErr_Format(PyExc_ValueError, |
| 5344 | "unknown address family %d", af); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5345 | PyBuffer_Release(&packed_ip); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5346 | return NULL; |
| 5347 | } |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5348 | PyBuffer_Release(&packed_ip); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5349 | |
| 5350 | retlen = sizeof(ip); |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5351 | ret = WSAAddressToStringA((struct sockaddr*)&addr, addrlen, NULL, |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5352 | ip, &retlen); |
| 5353 | |
| 5354 | if (ret) { |
| 5355 | PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError()); |
| 5356 | return NULL; |
| 5357 | } else { |
| 5358 | return PyUnicode_FromString(ip); |
| 5359 | } |
| 5360 | } |
| 5361 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5362 | #endif /* HAVE_INET_PTON */ |
| 5363 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5364 | /* Python interface to getaddrinfo(host, port). */ |
| 5365 | |
| 5366 | /*ARGSUSED*/ |
| 5367 | static PyObject * |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5368 | socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5369 | { |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 5370 | static char* kwnames[] = {"host", "port", "family", "type", "proto", |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5371 | "flags", 0}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5372 | struct addrinfo hints, *res; |
| 5373 | struct addrinfo *res0 = NULL; |
| 5374 | PyObject *hobj = NULL; |
| 5375 | PyObject *pobj = (PyObject *)NULL; |
| 5376 | char pbuf[30]; |
| 5377 | char *hptr, *pptr; |
| 5378 | int family, socktype, protocol, flags; |
| 5379 | int error; |
| 5380 | PyObject *all = (PyObject *)NULL; |
| 5381 | PyObject *idna = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5382 | |
Georg Brandl | 6083a4b | 2013-10-14 06:51:46 +0200 | [diff] [blame] | 5383 | socktype = protocol = flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5384 | family = AF_UNSPEC; |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 5385 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iiii:getaddrinfo", |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5386 | kwnames, &hobj, &pobj, &family, &socktype, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5387 | &protocol, &flags)) { |
| 5388 | return NULL; |
| 5389 | } |
| 5390 | if (hobj == Py_None) { |
| 5391 | hptr = NULL; |
| 5392 | } else if (PyUnicode_Check(hobj)) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5393 | _Py_IDENTIFIER(encode); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 5394 | |
| 5395 | idna = _PyObject_CallMethodId(hobj, &PyId_encode, "s", "idna"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5396 | if (!idna) |
| 5397 | return NULL; |
| 5398 | assert(PyBytes_Check(idna)); |
| 5399 | hptr = PyBytes_AS_STRING(idna); |
| 5400 | } else if (PyBytes_Check(hobj)) { |
| 5401 | hptr = PyBytes_AsString(hobj); |
| 5402 | } else { |
| 5403 | PyErr_SetString(PyExc_TypeError, |
| 5404 | "getaddrinfo() argument 1 must be string or None"); |
| 5405 | return NULL; |
| 5406 | } |
| 5407 | if (PyLong_CheckExact(pobj)) { |
| 5408 | long value = PyLong_AsLong(pobj); |
| 5409 | if (value == -1 && PyErr_Occurred()) |
| 5410 | goto err; |
| 5411 | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value); |
| 5412 | pptr = pbuf; |
| 5413 | } else if (PyUnicode_Check(pobj)) { |
| 5414 | pptr = _PyUnicode_AsString(pobj); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 5415 | if (pptr == NULL) |
| 5416 | goto err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5417 | } else if (PyBytes_Check(pobj)) { |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 5418 | pptr = PyBytes_AS_STRING(pobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5419 | } else if (pobj == Py_None) { |
| 5420 | pptr = (char *)NULL; |
| 5421 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5422 | PyErr_SetString(PyExc_OSError, "Int or String expected"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5423 | goto err; |
| 5424 | } |
Ronald Oussoren | a822d366 | 2013-06-10 10:36:28 +0200 | [diff] [blame] | 5425 | #if defined(__APPLE__) && defined(AI_NUMERICSERV) |
Ronald Oussoren | 27a4ac5 | 2013-05-24 13:47:37 +0200 | [diff] [blame] | 5426 | if ((flags & AI_NUMERICSERV) && (pptr == NULL || (pptr[0] == '0' && pptr[1] == 0))) { |
| 5427 | /* On OSX upto at least OSX 10.8 getaddrinfo crashes |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 5428 | * if AI_NUMERICSERV is set and the servname is NULL or "0". |
| 5429 | * This workaround avoids a segfault in libsystem. |
| 5430 | */ |
Ronald Oussoren | 27a4ac5 | 2013-05-24 13:47:37 +0200 | [diff] [blame] | 5431 | pptr = "00"; |
| 5432 | } |
| 5433 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5434 | memset(&hints, 0, sizeof(hints)); |
| 5435 | hints.ai_family = family; |
| 5436 | hints.ai_socktype = socktype; |
| 5437 | hints.ai_protocol = protocol; |
| 5438 | hints.ai_flags = flags; |
| 5439 | Py_BEGIN_ALLOW_THREADS |
| 5440 | ACQUIRE_GETADDRINFO_LOCK |
| 5441 | error = getaddrinfo(hptr, pptr, &hints, &res0); |
| 5442 | Py_END_ALLOW_THREADS |
| 5443 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 5444 | if (error) { |
| 5445 | set_gaierror(error); |
| 5446 | goto err; |
| 5447 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5448 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5449 | if ((all = PyList_New(0)) == NULL) |
| 5450 | goto err; |
| 5451 | for (res = res0; res; res = res->ai_next) { |
| 5452 | PyObject *single; |
| 5453 | PyObject *addr = |
| 5454 | makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol); |
| 5455 | if (addr == NULL) |
| 5456 | goto err; |
| 5457 | single = Py_BuildValue("iiisO", res->ai_family, |
| 5458 | res->ai_socktype, res->ai_protocol, |
| 5459 | res->ai_canonname ? res->ai_canonname : "", |
| 5460 | addr); |
| 5461 | Py_DECREF(addr); |
| 5462 | if (single == NULL) |
| 5463 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5464 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5465 | if (PyList_Append(all, single)) |
| 5466 | goto err; |
| 5467 | Py_XDECREF(single); |
| 5468 | } |
| 5469 | Py_XDECREF(idna); |
| 5470 | if (res0) |
| 5471 | freeaddrinfo(res0); |
| 5472 | return all; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5473 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5474 | Py_XDECREF(all); |
| 5475 | Py_XDECREF(idna); |
| 5476 | if (res0) |
| 5477 | freeaddrinfo(res0); |
| 5478 | return (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5479 | } |
| 5480 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5481 | PyDoc_STRVAR(getaddrinfo_doc, |
Berker Peksag | a6ec5ee | 2014-06-30 11:28:40 +0300 | [diff] [blame] | 5482 | "getaddrinfo(host, port [, family, type, proto, flags])\n\ |
| 5483 | -> list of (family, type, proto, canonname, sockaddr)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5484 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5485 | Resolve host and port into addrinfo struct."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5486 | |
| 5487 | /* Python interface to getnameinfo(sa, flags). */ |
| 5488 | |
| 5489 | /*ARGSUSED*/ |
| 5490 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5491 | socket_getnameinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5492 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5493 | PyObject *sa = (PyObject *)NULL; |
| 5494 | int flags; |
| 5495 | char *hostp; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5496 | int port; |
| 5497 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5498 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; |
| 5499 | struct addrinfo hints, *res = NULL; |
| 5500 | int error; |
| 5501 | PyObject *ret = (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5503 | flags = flowinfo = scope_id = 0; |
| 5504 | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) |
| 5505 | return NULL; |
| 5506 | if (!PyTuple_Check(sa)) { |
| 5507 | PyErr_SetString(PyExc_TypeError, |
| 5508 | "getnameinfo() argument 1 must be a tuple"); |
| 5509 | return NULL; |
| 5510 | } |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5511 | if (!PyArg_ParseTuple(sa, "si|II", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5512 | &hostp, &port, &flowinfo, &scope_id)) |
| 5513 | return NULL; |
Charles-François Natali | 9b0c006 | 2012-06-23 10:17:05 +0200 | [diff] [blame] | 5514 | if (flowinfo > 0xfffff) { |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5515 | PyErr_SetString(PyExc_OverflowError, |
| 5516 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 5517 | return NULL; |
| 5518 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5519 | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); |
| 5520 | memset(&hints, 0, sizeof(hints)); |
| 5521 | hints.ai_family = AF_UNSPEC; |
| 5522 | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ |
Martin v. Löwis | 112c0f3 | 2010-08-25 07:38:15 +0000 | [diff] [blame] | 5523 | hints.ai_flags = AI_NUMERICHOST; /* don't do any name resolution */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5524 | Py_BEGIN_ALLOW_THREADS |
| 5525 | ACQUIRE_GETADDRINFO_LOCK |
| 5526 | error = getaddrinfo(hostp, pbuf, &hints, &res); |
| 5527 | Py_END_ALLOW_THREADS |
| 5528 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 5529 | if (error) { |
| 5530 | set_gaierror(error); |
| 5531 | goto fail; |
| 5532 | } |
| 5533 | if (res->ai_next) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5534 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5535 | "sockaddr resolved to multiple addresses"); |
| 5536 | goto fail; |
| 5537 | } |
| 5538 | switch (res->ai_family) { |
| 5539 | case AF_INET: |
| 5540 | { |
| 5541 | if (PyTuple_GET_SIZE(sa) != 2) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5542 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5543 | "IPv4 sockaddr must be 2 tuple"); |
| 5544 | goto fail; |
| 5545 | } |
| 5546 | break; |
| 5547 | } |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 5548 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5549 | case AF_INET6: |
| 5550 | { |
| 5551 | struct sockaddr_in6 *sin6; |
| 5552 | sin6 = (struct sockaddr_in6 *)res->ai_addr; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5553 | sin6->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5554 | sin6->sin6_scope_id = scope_id; |
| 5555 | break; |
| 5556 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5557 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5558 | } |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 5559 | error = getnameinfo(res->ai_addr, (socklen_t) res->ai_addrlen, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5560 | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); |
| 5561 | if (error) { |
| 5562 | set_gaierror(error); |
| 5563 | goto fail; |
| 5564 | } |
| 5565 | ret = Py_BuildValue("ss", hbuf, pbuf); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5566 | |
| 5567 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5568 | if (res) |
| 5569 | freeaddrinfo(res); |
| 5570 | return ret; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5571 | } |
| 5572 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5573 | PyDoc_STRVAR(getnameinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5574 | "getnameinfo(sockaddr, flags) --> (host, port)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5575 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5576 | Get host and port for a sockaddr."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5577 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5578 | |
| 5579 | /* Python API to getting and setting the default timeout value. */ |
| 5580 | |
| 5581 | static PyObject * |
| 5582 | socket_getdefaulttimeout(PyObject *self) |
| 5583 | { |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 5584 | if (defaulttimeout < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5585 | Py_INCREF(Py_None); |
| 5586 | return Py_None; |
| 5587 | } |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 5588 | else { |
| 5589 | double seconds = _PyTime_AsSecondsDouble(defaulttimeout); |
| 5590 | return PyFloat_FromDouble(seconds); |
| 5591 | } |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5592 | } |
| 5593 | |
| 5594 | PyDoc_STRVAR(getdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5595 | "getdefaulttimeout() -> timeout\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5596 | \n\ |
Ezio Melotti | 388c945 | 2011-08-14 08:28:57 +0300 | [diff] [blame] | 5597 | 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] | 5598 | A value of None indicates that new socket objects have no timeout.\n\ |
| 5599 | When the socket module is first imported, the default is None."); |
| 5600 | |
| 5601 | static PyObject * |
| 5602 | socket_setdefaulttimeout(PyObject *self, PyObject *arg) |
| 5603 | { |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 5604 | _PyTime_t timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5605 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 5606 | if (socket_parse_timeout(&timeout, arg) < 0) |
| 5607 | return NULL; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5608 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5609 | defaulttimeout = timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5610 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5611 | Py_INCREF(Py_None); |
| 5612 | return Py_None; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5613 | } |
| 5614 | |
| 5615 | PyDoc_STRVAR(setdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5616 | "setdefaulttimeout(timeout)\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5617 | \n\ |
Ezio Melotti | 388c945 | 2011-08-14 08:28:57 +0300 | [diff] [blame] | 5618 | 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] | 5619 | A value of None indicates that new socket objects have no timeout.\n\ |
| 5620 | When the socket module is first imported, the default is None."); |
| 5621 | |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5622 | #ifdef HAVE_IF_NAMEINDEX |
| 5623 | /* Python API for getting interface indices and names */ |
| 5624 | |
| 5625 | static PyObject * |
| 5626 | socket_if_nameindex(PyObject *self, PyObject *arg) |
| 5627 | { |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5628 | PyObject *list; |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5629 | int i; |
| 5630 | struct if_nameindex *ni; |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 5631 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5632 | ni = if_nameindex(); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5633 | if (ni == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5634 | PyErr_SetFromErrno(PyExc_OSError); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5635 | return NULL; |
| 5636 | } |
| 5637 | |
| 5638 | list = PyList_New(0); |
| 5639 | if (list == NULL) { |
| 5640 | if_freenameindex(ni); |
| 5641 | return NULL; |
| 5642 | } |
| 5643 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5644 | for (i = 0; ni[i].if_index != 0 && i < INT_MAX; i++) { |
| 5645 | PyObject *ni_tuple = Py_BuildValue("IO&", |
| 5646 | ni[i].if_index, PyUnicode_DecodeFSDefault, ni[i].if_name); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5647 | |
| 5648 | if (ni_tuple == NULL || PyList_Append(list, ni_tuple) == -1) { |
| 5649 | Py_XDECREF(ni_tuple); |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5650 | Py_DECREF(list); |
| 5651 | if_freenameindex(ni); |
| 5652 | return NULL; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5653 | } |
| 5654 | Py_DECREF(ni_tuple); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5655 | } |
| 5656 | |
| 5657 | if_freenameindex(ni); |
| 5658 | return list; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5659 | } |
| 5660 | |
| 5661 | PyDoc_STRVAR(if_nameindex_doc, |
| 5662 | "if_nameindex()\n\ |
| 5663 | \n\ |
| 5664 | Returns a list of network interface information (index, name) tuples."); |
| 5665 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5666 | static PyObject * |
| 5667 | socket_if_nametoindex(PyObject *self, PyObject *args) |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5668 | { |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5669 | PyObject *oname; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5670 | unsigned long index; |
| 5671 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5672 | if (!PyArg_ParseTuple(args, "O&:if_nametoindex", |
| 5673 | PyUnicode_FSConverter, &oname)) |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5674 | return NULL; |
| 5675 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5676 | index = if_nametoindex(PyBytes_AS_STRING(oname)); |
| 5677 | Py_DECREF(oname); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5678 | if (index == 0) { |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5679 | /* if_nametoindex() doesn't set errno */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5680 | PyErr_SetString(PyExc_OSError, "no interface with this name"); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5681 | return NULL; |
| 5682 | } |
| 5683 | |
| 5684 | return PyLong_FromUnsignedLong(index); |
| 5685 | } |
| 5686 | |
| 5687 | PyDoc_STRVAR(if_nametoindex_doc, |
| 5688 | "if_nametoindex(if_name)\n\ |
| 5689 | \n\ |
| 5690 | Returns the interface index corresponding to the interface name if_name."); |
| 5691 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5692 | static PyObject * |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5693 | socket_if_indextoname(PyObject *self, PyObject *arg) |
| 5694 | { |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5695 | unsigned long index; |
| 5696 | char name[IF_NAMESIZE + 1]; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5697 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5698 | index = PyLong_AsUnsignedLong(arg); |
| 5699 | if (index == (unsigned long) -1) |
| 5700 | return NULL; |
| 5701 | |
| 5702 | if (if_indextoname(index, name) == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5703 | PyErr_SetFromErrno(PyExc_OSError); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5704 | return NULL; |
| 5705 | } |
| 5706 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5707 | return PyUnicode_DecodeFSDefault(name); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5708 | } |
| 5709 | |
| 5710 | PyDoc_STRVAR(if_indextoname_doc, |
| 5711 | "if_indextoname(if_index)\n\ |
| 5712 | \n\ |
| 5713 | Returns the interface name corresponding to the interface index if_index."); |
| 5714 | |
| 5715 | #endif /* HAVE_IF_NAMEINDEX */ |
| 5716 | |
| 5717 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 5718 | #ifdef CMSG_LEN |
| 5719 | /* Python interface to CMSG_LEN(length). */ |
| 5720 | |
| 5721 | static PyObject * |
| 5722 | socket_CMSG_LEN(PyObject *self, PyObject *args) |
| 5723 | { |
| 5724 | Py_ssize_t length; |
| 5725 | size_t result; |
| 5726 | |
| 5727 | if (!PyArg_ParseTuple(args, "n:CMSG_LEN", &length)) |
| 5728 | return NULL; |
| 5729 | if (length < 0 || !get_CMSG_LEN(length, &result)) { |
| 5730 | PyErr_Format(PyExc_OverflowError, "CMSG_LEN() argument out of range"); |
| 5731 | return NULL; |
| 5732 | } |
| 5733 | return PyLong_FromSize_t(result); |
| 5734 | } |
| 5735 | |
| 5736 | PyDoc_STRVAR(CMSG_LEN_doc, |
| 5737 | "CMSG_LEN(length) -> control message length\n\ |
| 5738 | \n\ |
| 5739 | Return the total length, without trailing padding, of an ancillary\n\ |
| 5740 | data item with associated data of the given length. This value can\n\ |
| 5741 | often be used as the buffer size for recvmsg() to receive a single\n\ |
| 5742 | item of ancillary data, but RFC 3542 requires portable applications to\n\ |
| 5743 | use CMSG_SPACE() and thus include space for padding, even when the\n\ |
| 5744 | item will be the last in the buffer. Raises OverflowError if length\n\ |
| 5745 | is outside the permissible range of values."); |
| 5746 | |
| 5747 | |
| 5748 | #ifdef CMSG_SPACE |
| 5749 | /* Python interface to CMSG_SPACE(length). */ |
| 5750 | |
| 5751 | static PyObject * |
| 5752 | socket_CMSG_SPACE(PyObject *self, PyObject *args) |
| 5753 | { |
| 5754 | Py_ssize_t length; |
| 5755 | size_t result; |
| 5756 | |
| 5757 | if (!PyArg_ParseTuple(args, "n:CMSG_SPACE", &length)) |
| 5758 | return NULL; |
| 5759 | if (length < 0 || !get_CMSG_SPACE(length, &result)) { |
| 5760 | PyErr_SetString(PyExc_OverflowError, |
| 5761 | "CMSG_SPACE() argument out of range"); |
| 5762 | return NULL; |
| 5763 | } |
| 5764 | return PyLong_FromSize_t(result); |
| 5765 | } |
| 5766 | |
| 5767 | PyDoc_STRVAR(CMSG_SPACE_doc, |
| 5768 | "CMSG_SPACE(length) -> buffer size\n\ |
| 5769 | \n\ |
| 5770 | Return the buffer size needed for recvmsg() to receive an ancillary\n\ |
| 5771 | data item with associated data of the given length, along with any\n\ |
| 5772 | trailing padding. The buffer space needed to receive multiple items\n\ |
| 5773 | is the sum of the CMSG_SPACE() values for their associated data\n\ |
| 5774 | lengths. Raises OverflowError if length is outside the permissible\n\ |
| 5775 | range of values."); |
| 5776 | #endif /* CMSG_SPACE */ |
| 5777 | #endif /* CMSG_LEN */ |
| 5778 | |
| 5779 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 5780 | /* List of functions exported by this module. */ |
| 5781 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5782 | static PyMethodDef socket_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5783 | {"gethostbyname", socket_gethostbyname, |
| 5784 | METH_VARARGS, gethostbyname_doc}, |
| 5785 | {"gethostbyname_ex", socket_gethostbyname_ex, |
| 5786 | METH_VARARGS, ghbn_ex_doc}, |
| 5787 | {"gethostbyaddr", socket_gethostbyaddr, |
| 5788 | METH_VARARGS, gethostbyaddr_doc}, |
| 5789 | {"gethostname", socket_gethostname, |
| 5790 | METH_NOARGS, gethostname_doc}, |
Antoine Pitrou | 061cfb5 | 2011-02-28 22:25:22 +0000 | [diff] [blame] | 5791 | #ifdef HAVE_SETHOSTNAME |
| 5792 | {"sethostname", socket_sethostname, |
| 5793 | METH_VARARGS, sethostname_doc}, |
| 5794 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5795 | {"getservbyname", socket_getservbyname, |
| 5796 | METH_VARARGS, getservbyname_doc}, |
| 5797 | {"getservbyport", socket_getservbyport, |
| 5798 | METH_VARARGS, getservbyport_doc}, |
| 5799 | {"getprotobyname", socket_getprotobyname, |
| 5800 | METH_VARARGS, getprotobyname_doc}, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 5801 | #ifndef NO_DUP |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5802 | {"dup", socket_dup, |
| 5803 | METH_O, dup_doc}, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 5804 | #endif |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 5805 | #ifdef HAVE_SOCKETPAIR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5806 | {"socketpair", socket_socketpair, |
| 5807 | METH_VARARGS, socketpair_doc}, |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 5808 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5809 | {"ntohs", socket_ntohs, |
| 5810 | METH_VARARGS, ntohs_doc}, |
| 5811 | {"ntohl", socket_ntohl, |
| 5812 | METH_O, ntohl_doc}, |
| 5813 | {"htons", socket_htons, |
| 5814 | METH_VARARGS, htons_doc}, |
| 5815 | {"htonl", socket_htonl, |
| 5816 | METH_O, htonl_doc}, |
| 5817 | {"inet_aton", socket_inet_aton, |
| 5818 | METH_VARARGS, inet_aton_doc}, |
| 5819 | {"inet_ntoa", socket_inet_ntoa, |
| 5820 | METH_VARARGS, inet_ntoa_doc}, |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5821 | #if defined(HAVE_INET_PTON) || defined(MS_WINDOWS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5822 | {"inet_pton", socket_inet_pton, |
| 5823 | METH_VARARGS, inet_pton_doc}, |
| 5824 | {"inet_ntop", socket_inet_ntop, |
| 5825 | METH_VARARGS, inet_ntop_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5826 | #endif |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5827 | {"getaddrinfo", (PyCFunction)socket_getaddrinfo, |
| 5828 | METH_VARARGS | METH_KEYWORDS, getaddrinfo_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5829 | {"getnameinfo", socket_getnameinfo, |
| 5830 | METH_VARARGS, getnameinfo_doc}, |
| 5831 | {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout, |
| 5832 | METH_NOARGS, getdefaulttimeout_doc}, |
| 5833 | {"setdefaulttimeout", socket_setdefaulttimeout, |
| 5834 | METH_O, setdefaulttimeout_doc}, |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5835 | #ifdef HAVE_IF_NAMEINDEX |
| 5836 | {"if_nameindex", socket_if_nameindex, |
| 5837 | METH_NOARGS, if_nameindex_doc}, |
| 5838 | {"if_nametoindex", socket_if_nametoindex, |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5839 | METH_VARARGS, if_nametoindex_doc}, |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5840 | {"if_indextoname", socket_if_indextoname, |
| 5841 | METH_O, if_indextoname_doc}, |
| 5842 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 5843 | #ifdef CMSG_LEN |
| 5844 | {"CMSG_LEN", socket_CMSG_LEN, |
| 5845 | METH_VARARGS, CMSG_LEN_doc}, |
| 5846 | #ifdef CMSG_SPACE |
| 5847 | {"CMSG_SPACE", socket_CMSG_SPACE, |
| 5848 | METH_VARARGS, CMSG_SPACE_doc}, |
| 5849 | #endif |
| 5850 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5851 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5852 | }; |
| 5853 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 5854 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5855 | #ifdef MS_WINDOWS |
| 5856 | #define OS_INIT_DEFINED |
| 5857 | |
| 5858 | /* Additional initialization and cleanup for Windows */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5859 | |
| 5860 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5861 | os_cleanup(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5862 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5863 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5864 | } |
| 5865 | |
| 5866 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5867 | os_init(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5868 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5869 | WSADATA WSAData; |
| 5870 | int ret; |
| 5871 | ret = WSAStartup(0x0101, &WSAData); |
| 5872 | switch (ret) { |
| 5873 | case 0: /* No error */ |
| 5874 | Py_AtExit(os_cleanup); |
| 5875 | return 1; /* Success */ |
| 5876 | case WSASYSNOTREADY: |
| 5877 | PyErr_SetString(PyExc_ImportError, |
| 5878 | "WSAStartup failed: network not ready"); |
| 5879 | break; |
| 5880 | case WSAVERNOTSUPPORTED: |
| 5881 | case WSAEINVAL: |
| 5882 | PyErr_SetString( |
| 5883 | PyExc_ImportError, |
| 5884 | "WSAStartup failed: requested version not supported"); |
| 5885 | break; |
| 5886 | default: |
| 5887 | PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret); |
| 5888 | break; |
| 5889 | } |
| 5890 | return 0; /* Failure */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5891 | } |
| 5892 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 5893 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5894 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5895 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5896 | |
| 5897 | #ifndef OS_INIT_DEFINED |
| 5898 | static int |
| 5899 | os_init(void) |
| 5900 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5901 | return 1; /* Success */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5902 | } |
| 5903 | #endif |
| 5904 | |
| 5905 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5906 | /* C API table - always add new things to the end for binary |
| 5907 | compatibility. */ |
| 5908 | static |
| 5909 | PySocketModule_APIObject PySocketModuleAPI = |
| 5910 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5911 | &sock_type, |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 5912 | NULL, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5913 | NULL |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5914 | }; |
| 5915 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5916 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5917 | /* Initialize the _socket module. |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5918 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5919 | This module is actually called "_socket", and there's a wrapper |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 5920 | "socket.py" which implements some additional functionality. |
| 5921 | The import of "_socket" may fail with an ImportError exception if |
| 5922 | os-specific initialization fails. On Windows, this does WINSOCK |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 5923 | initialization. When WINSOCK is initialized successfully, a call to |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 5924 | WSACleanup() is scheduled to be made at exit time. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5925 | */ |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5926 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5927 | PyDoc_STRVAR(socket_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5928 | "Implementation module for socket operations.\n\ |
| 5929 | \n\ |
| 5930 | See the socket module for documentation."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5931 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5932 | static struct PyModuleDef socketmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5933 | PyModuleDef_HEAD_INIT, |
| 5934 | PySocket_MODULE_NAME, |
| 5935 | socket_doc, |
| 5936 | -1, |
| 5937 | socket_methods, |
| 5938 | NULL, |
| 5939 | NULL, |
| 5940 | NULL, |
| 5941 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5942 | }; |
| 5943 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 5944 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5945 | PyInit__socket(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5946 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5947 | PyObject *m, *has_ipv6; |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5949 | if (!os_init()) |
| 5950 | return NULL; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5951 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5952 | #ifdef MS_WINDOWS |
| 5953 | if (support_wsa_no_inherit == -1) { |
Steve Dower | 65e4cb1 | 2014-11-22 12:54:57 -0800 | [diff] [blame] | 5954 | #if defined(_MSC_VER) && _MSC_VER >= 1800 |
| 5955 | support_wsa_no_inherit = IsWindows7SP1OrGreater(); |
| 5956 | #else |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5957 | DWORD version = GetVersion(); |
| 5958 | DWORD major = (DWORD)LOBYTE(LOWORD(version)); |
| 5959 | DWORD minor = (DWORD)HIBYTE(LOWORD(version)); |
| 5960 | /* need Windows 7 SP1, 2008 R2 SP1 or later */ |
Steve Dower | 65e4cb1 | 2014-11-22 12:54:57 -0800 | [diff] [blame] | 5961 | support_wsa_no_inherit = major > 6 || (major == 6 && minor >= 1); |
| 5962 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5963 | } |
| 5964 | #endif |
| 5965 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5966 | Py_TYPE(&sock_type) = &PyType_Type; |
| 5967 | m = PyModule_Create(&socketmodule); |
| 5968 | if (m == NULL) |
| 5969 | return NULL; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5970 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5971 | Py_INCREF(PyExc_OSError); |
| 5972 | PySocketModuleAPI.error = PyExc_OSError; |
| 5973 | Py_INCREF(PyExc_OSError); |
| 5974 | PyModule_AddObject(m, "error", PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5975 | socket_herror = PyErr_NewException("socket.herror", |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5976 | PyExc_OSError, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5977 | if (socket_herror == NULL) |
| 5978 | return NULL; |
| 5979 | Py_INCREF(socket_herror); |
| 5980 | PyModule_AddObject(m, "herror", socket_herror); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5981 | socket_gaierror = PyErr_NewException("socket.gaierror", PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5982 | NULL); |
| 5983 | if (socket_gaierror == NULL) |
| 5984 | return NULL; |
| 5985 | Py_INCREF(socket_gaierror); |
| 5986 | PyModule_AddObject(m, "gaierror", socket_gaierror); |
| 5987 | socket_timeout = PyErr_NewException("socket.timeout", |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5988 | PyExc_OSError, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5989 | if (socket_timeout == NULL) |
| 5990 | return NULL; |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 5991 | PySocketModuleAPI.timeout_error = socket_timeout; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5992 | Py_INCREF(socket_timeout); |
| 5993 | PyModule_AddObject(m, "timeout", socket_timeout); |
| 5994 | Py_INCREF((PyObject *)&sock_type); |
| 5995 | if (PyModule_AddObject(m, "SocketType", |
| 5996 | (PyObject *)&sock_type) != 0) |
| 5997 | return NULL; |
| 5998 | Py_INCREF((PyObject *)&sock_type); |
| 5999 | if (PyModule_AddObject(m, "socket", |
| 6000 | (PyObject *)&sock_type) != 0) |
| 6001 | return NULL; |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6002 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 6003 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6004 | has_ipv6 = Py_True; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 6005 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6006 | has_ipv6 = Py_False; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 6007 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6008 | Py_INCREF(has_ipv6); |
| 6009 | PyModule_AddObject(m, "has_ipv6", has_ipv6); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 6010 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6011 | /* Export C API */ |
| 6012 | if (PyModule_AddObject(m, PySocket_CAPI_NAME, |
| 6013 | PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL) |
| 6014 | ) != 0) |
| 6015 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6016 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6017 | /* Address families (we only support AF_INET and AF_UNIX) */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6018 | #ifdef AF_UNSPEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6019 | PyModule_AddIntMacro(m, AF_UNSPEC); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6020 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6021 | PyModule_AddIntMacro(m, AF_INET); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6022 | #ifdef AF_INET6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6023 | PyModule_AddIntMacro(m, AF_INET6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6024 | #endif /* AF_INET6 */ |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 6025 | #if defined(AF_UNIX) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6026 | PyModule_AddIntMacro(m, AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6027 | #endif /* AF_UNIX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6028 | #ifdef AF_AX25 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6029 | /* Amateur Radio AX.25 */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6030 | PyModule_AddIntMacro(m, AF_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6031 | #endif |
| 6032 | #ifdef AF_IPX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6033 | PyModule_AddIntMacro(m, AF_IPX); /* Novell IPX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6034 | #endif |
| 6035 | #ifdef AF_APPLETALK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6036 | /* Appletalk DDP */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6037 | PyModule_AddIntMacro(m, AF_APPLETALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6038 | #endif |
| 6039 | #ifdef AF_NETROM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6040 | /* Amateur radio NetROM */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6041 | PyModule_AddIntMacro(m, AF_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6042 | #endif |
| 6043 | #ifdef AF_BRIDGE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6044 | /* Multiprotocol bridge */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6045 | PyModule_AddIntMacro(m, AF_BRIDGE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6046 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6047 | #ifdef AF_ATMPVC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6048 | /* ATM PVCs */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6049 | PyModule_AddIntMacro(m, AF_ATMPVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6050 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6051 | #ifdef AF_AAL5 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6052 | /* Reserved for Werner's ATM */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6053 | PyModule_AddIntMacro(m, AF_AAL5); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6054 | #endif |
| 6055 | #ifdef AF_X25 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6056 | /* Reserved for X.25 project */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6057 | PyModule_AddIntMacro(m, AF_X25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6058 | #endif |
| 6059 | #ifdef AF_INET6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6060 | PyModule_AddIntMacro(m, AF_INET6); /* IP version 6 */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6061 | #endif |
| 6062 | #ifdef AF_ROSE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6063 | /* Amateur Radio X.25 PLP */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6064 | PyModule_AddIntMacro(m, AF_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6065 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6066 | #ifdef AF_DECnet |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6067 | /* Reserved for DECnet project */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6068 | PyModule_AddIntMacro(m, AF_DECnet); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6069 | #endif |
| 6070 | #ifdef AF_NETBEUI |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6071 | /* Reserved for 802.2LLC project */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6072 | PyModule_AddIntMacro(m, AF_NETBEUI); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6073 | #endif |
| 6074 | #ifdef AF_SECURITY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6075 | /* Security callback pseudo AF */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6076 | PyModule_AddIntMacro(m, AF_SECURITY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6077 | #endif |
| 6078 | #ifdef AF_KEY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6079 | /* PF_KEY key management API */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6080 | PyModule_AddIntMacro(m, AF_KEY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6081 | #endif |
| 6082 | #ifdef AF_NETLINK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6083 | /* */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6084 | PyModule_AddIntMacro(m, AF_NETLINK); |
| 6085 | PyModule_AddIntMacro(m, NETLINK_ROUTE); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6086 | #ifdef NETLINK_SKIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6087 | PyModule_AddIntMacro(m, NETLINK_SKIP); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6088 | #endif |
| 6089 | #ifdef NETLINK_W1 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6090 | PyModule_AddIntMacro(m, NETLINK_W1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6091 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6092 | PyModule_AddIntMacro(m, NETLINK_USERSOCK); |
| 6093 | PyModule_AddIntMacro(m, NETLINK_FIREWALL); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 6094 | #ifdef NETLINK_TCPDIAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6095 | PyModule_AddIntMacro(m, NETLINK_TCPDIAG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 6096 | #endif |
| 6097 | #ifdef NETLINK_NFLOG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6098 | PyModule_AddIntMacro(m, NETLINK_NFLOG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 6099 | #endif |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 6100 | #ifdef NETLINK_XFRM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6101 | PyModule_AddIntMacro(m, NETLINK_XFRM); |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 6102 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6103 | #ifdef NETLINK_ARPD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6104 | PyModule_AddIntMacro(m, NETLINK_ARPD); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6105 | #endif |
| 6106 | #ifdef NETLINK_ROUTE6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6107 | PyModule_AddIntMacro(m, NETLINK_ROUTE6); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6108 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6109 | PyModule_AddIntMacro(m, NETLINK_IP6_FW); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6110 | #ifdef NETLINK_DNRTMSG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6111 | PyModule_AddIntMacro(m, NETLINK_DNRTMSG); |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 6112 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6113 | #ifdef NETLINK_TAPBASE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6114 | PyModule_AddIntMacro(m, NETLINK_TAPBASE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6115 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6116 | #endif /* AF_NETLINK */ |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6117 | #ifdef AF_ROUTE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6118 | /* Alias to emulate 4.4BSD */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6119 | PyModule_AddIntMacro(m, AF_ROUTE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6120 | #endif |
Giampaolo Rodola' | 80e1c43 | 2013-05-21 21:02:04 +0200 | [diff] [blame] | 6121 | #ifdef AF_LINK |
| 6122 | PyModule_AddIntMacro(m, AF_LINK); |
| 6123 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6124 | #ifdef AF_ASH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6125 | /* Ash */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6126 | PyModule_AddIntMacro(m, AF_ASH); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6127 | #endif |
| 6128 | #ifdef AF_ECONET |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6129 | /* Acorn Econet */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6130 | PyModule_AddIntMacro(m, AF_ECONET); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6131 | #endif |
| 6132 | #ifdef AF_ATMSVC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6133 | /* ATM SVCs */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6134 | PyModule_AddIntMacro(m, AF_ATMSVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6135 | #endif |
| 6136 | #ifdef AF_SNA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6137 | /* Linux SNA Project (nutters!) */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6138 | PyModule_AddIntMacro(m, AF_SNA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6139 | #endif |
| 6140 | #ifdef AF_IRDA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6141 | /* IRDA sockets */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6142 | PyModule_AddIntMacro(m, AF_IRDA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6143 | #endif |
| 6144 | #ifdef AF_PPPOX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6145 | /* PPPoX sockets */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6146 | PyModule_AddIntMacro(m, AF_PPPOX); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6147 | #endif |
| 6148 | #ifdef AF_WANPIPE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6149 | /* Wanpipe API Sockets */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6150 | PyModule_AddIntMacro(m, AF_WANPIPE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6151 | #endif |
| 6152 | #ifdef AF_LLC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6153 | /* Linux LLC */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6154 | PyModule_AddIntMacro(m, AF_LLC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6155 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 6156 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 6157 | #ifdef USE_BLUETOOTH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6158 | PyModule_AddIntMacro(m, AF_BLUETOOTH); |
| 6159 | PyModule_AddIntMacro(m, BTPROTO_L2CAP); |
| 6160 | PyModule_AddIntMacro(m, BTPROTO_HCI); |
| 6161 | PyModule_AddIntMacro(m, SOL_HCI); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 6162 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6163 | PyModule_AddIntMacro(m, HCI_FILTER); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 6164 | #endif |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 6165 | #if !defined(__FreeBSD__) |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 6166 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6167 | PyModule_AddIntMacro(m, HCI_TIME_STAMP); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 6168 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6169 | PyModule_AddIntMacro(m, HCI_DATA_DIR); |
| 6170 | PyModule_AddIntMacro(m, BTPROTO_SCO); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 6171 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6172 | PyModule_AddIntMacro(m, BTPROTO_RFCOMM); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6173 | PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00"); |
| 6174 | 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] | 6175 | #endif |
| 6176 | |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6177 | #ifdef AF_CAN |
| 6178 | /* Controller Area Network */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6179 | PyModule_AddIntMacro(m, AF_CAN); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6180 | #endif |
| 6181 | #ifdef PF_CAN |
| 6182 | /* Controller Area Network */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6183 | PyModule_AddIntMacro(m, PF_CAN); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6184 | #endif |
| 6185 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6186 | /* Reliable Datagram Sockets */ |
| 6187 | #ifdef AF_RDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6188 | PyModule_AddIntMacro(m, AF_RDS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6189 | #endif |
| 6190 | #ifdef PF_RDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6191 | PyModule_AddIntMacro(m, PF_RDS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6192 | #endif |
| 6193 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6194 | /* Kernel event messages */ |
| 6195 | #ifdef PF_SYSTEM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6196 | PyModule_AddIntMacro(m, PF_SYSTEM); |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6197 | #endif |
| 6198 | #ifdef AF_SYSTEM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6199 | PyModule_AddIntMacro(m, AF_SYSTEM); |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6200 | #endif |
| 6201 | |
Antoine Pitrou | b156a46 | 2010-10-27 20:13:57 +0000 | [diff] [blame] | 6202 | #ifdef AF_PACKET |
| 6203 | PyModule_AddIntMacro(m, AF_PACKET); |
| 6204 | #endif |
| 6205 | #ifdef PF_PACKET |
| 6206 | PyModule_AddIntMacro(m, PF_PACKET); |
| 6207 | #endif |
| 6208 | #ifdef PACKET_HOST |
| 6209 | PyModule_AddIntMacro(m, PACKET_HOST); |
| 6210 | #endif |
| 6211 | #ifdef PACKET_BROADCAST |
| 6212 | PyModule_AddIntMacro(m, PACKET_BROADCAST); |
| 6213 | #endif |
| 6214 | #ifdef PACKET_MULTICAST |
| 6215 | PyModule_AddIntMacro(m, PACKET_MULTICAST); |
| 6216 | #endif |
| 6217 | #ifdef PACKET_OTHERHOST |
| 6218 | PyModule_AddIntMacro(m, PACKET_OTHERHOST); |
| 6219 | #endif |
| 6220 | #ifdef PACKET_OUTGOING |
| 6221 | PyModule_AddIntMacro(m, PACKET_OUTGOING); |
| 6222 | #endif |
| 6223 | #ifdef PACKET_LOOPBACK |
| 6224 | PyModule_AddIntMacro(m, PACKET_LOOPBACK); |
| 6225 | #endif |
| 6226 | #ifdef PACKET_FASTROUTE |
| 6227 | PyModule_AddIntMacro(m, PACKET_FASTROUTE); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 6228 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6229 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6230 | #ifdef HAVE_LINUX_TIPC_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6231 | PyModule_AddIntMacro(m, AF_TIPC); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6232 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6233 | /* for addresses */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6234 | PyModule_AddIntMacro(m, TIPC_ADDR_NAMESEQ); |
| 6235 | PyModule_AddIntMacro(m, TIPC_ADDR_NAME); |
| 6236 | PyModule_AddIntMacro(m, TIPC_ADDR_ID); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6237 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6238 | PyModule_AddIntMacro(m, TIPC_ZONE_SCOPE); |
| 6239 | PyModule_AddIntMacro(m, TIPC_CLUSTER_SCOPE); |
| 6240 | PyModule_AddIntMacro(m, TIPC_NODE_SCOPE); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6242 | /* for setsockopt() */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6243 | PyModule_AddIntMacro(m, SOL_TIPC); |
| 6244 | PyModule_AddIntMacro(m, TIPC_IMPORTANCE); |
| 6245 | PyModule_AddIntMacro(m, TIPC_SRC_DROPPABLE); |
| 6246 | PyModule_AddIntMacro(m, TIPC_DEST_DROPPABLE); |
| 6247 | PyModule_AddIntMacro(m, TIPC_CONN_TIMEOUT); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6248 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6249 | PyModule_AddIntMacro(m, TIPC_LOW_IMPORTANCE); |
| 6250 | PyModule_AddIntMacro(m, TIPC_MEDIUM_IMPORTANCE); |
| 6251 | PyModule_AddIntMacro(m, TIPC_HIGH_IMPORTANCE); |
| 6252 | PyModule_AddIntMacro(m, TIPC_CRITICAL_IMPORTANCE); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6253 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6254 | /* for subscriptions */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6255 | PyModule_AddIntMacro(m, TIPC_SUB_PORTS); |
| 6256 | PyModule_AddIntMacro(m, TIPC_SUB_SERVICE); |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 6257 | #ifdef TIPC_SUB_CANCEL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6258 | /* doesn't seem to be available everywhere */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6259 | PyModule_AddIntMacro(m, TIPC_SUB_CANCEL); |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 6260 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6261 | PyModule_AddIntMacro(m, TIPC_WAIT_FOREVER); |
| 6262 | PyModule_AddIntMacro(m, TIPC_PUBLISHED); |
| 6263 | PyModule_AddIntMacro(m, TIPC_WITHDRAWN); |
| 6264 | PyModule_AddIntMacro(m, TIPC_SUBSCR_TIMEOUT); |
| 6265 | PyModule_AddIntMacro(m, TIPC_CFG_SRV); |
| 6266 | PyModule_AddIntMacro(m, TIPC_TOP_SRV); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6267 | #endif |
| 6268 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6269 | /* Socket types */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6270 | PyModule_AddIntMacro(m, SOCK_STREAM); |
| 6271 | PyModule_AddIntMacro(m, SOCK_DGRAM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 6272 | /* We have incomplete socket support. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6273 | PyModule_AddIntMacro(m, SOCK_RAW); |
| 6274 | PyModule_AddIntMacro(m, SOCK_SEQPACKET); |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 6275 | #if defined(SOCK_RDM) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6276 | PyModule_AddIntMacro(m, SOCK_RDM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 6277 | #endif |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 6278 | #ifdef SOCK_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6279 | PyModule_AddIntMacro(m, SOCK_CLOEXEC); |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 6280 | #endif |
| 6281 | #ifdef SOCK_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6282 | PyModule_AddIntMacro(m, SOCK_NONBLOCK); |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 6283 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6285 | #ifdef SO_DEBUG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6286 | PyModule_AddIntMacro(m, SO_DEBUG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6287 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6288 | #ifdef SO_ACCEPTCONN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6289 | PyModule_AddIntMacro(m, SO_ACCEPTCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6290 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6291 | #ifdef SO_REUSEADDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6292 | PyModule_AddIntMacro(m, SO_REUSEADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6293 | #endif |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 6294 | #ifdef SO_EXCLUSIVEADDRUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6295 | PyModule_AddIntMacro(m, SO_EXCLUSIVEADDRUSE); |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 6296 | #endif |
| 6297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6298 | #ifdef SO_KEEPALIVE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6299 | PyModule_AddIntMacro(m, SO_KEEPALIVE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6300 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6301 | #ifdef SO_DONTROUTE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6302 | PyModule_AddIntMacro(m, SO_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6303 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6304 | #ifdef SO_BROADCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6305 | PyModule_AddIntMacro(m, SO_BROADCAST); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6306 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6307 | #ifdef SO_USELOOPBACK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6308 | PyModule_AddIntMacro(m, SO_USELOOPBACK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6309 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6310 | #ifdef SO_LINGER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6311 | PyModule_AddIntMacro(m, SO_LINGER); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6312 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6313 | #ifdef SO_OOBINLINE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6314 | PyModule_AddIntMacro(m, SO_OOBINLINE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6315 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6316 | #ifdef SO_REUSEPORT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6317 | PyModule_AddIntMacro(m, SO_REUSEPORT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6318 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6319 | #ifdef SO_SNDBUF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6320 | PyModule_AddIntMacro(m, SO_SNDBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6321 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6322 | #ifdef SO_RCVBUF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6323 | PyModule_AddIntMacro(m, SO_RCVBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6324 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6325 | #ifdef SO_SNDLOWAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6326 | PyModule_AddIntMacro(m, SO_SNDLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6327 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6328 | #ifdef SO_RCVLOWAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6329 | PyModule_AddIntMacro(m, SO_RCVLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6330 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6331 | #ifdef SO_SNDTIMEO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6332 | PyModule_AddIntMacro(m, SO_SNDTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6333 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6334 | #ifdef SO_RCVTIMEO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6335 | PyModule_AddIntMacro(m, SO_RCVTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6336 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6337 | #ifdef SO_ERROR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6338 | PyModule_AddIntMacro(m, SO_ERROR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6339 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6340 | #ifdef SO_TYPE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6341 | PyModule_AddIntMacro(m, SO_TYPE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6342 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6343 | #ifdef SO_SETFIB |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6344 | PyModule_AddIntMacro(m, SO_SETFIB); |
Larry Hastings | f0f3795 | 2010-04-02 11:47:10 +0000 | [diff] [blame] | 6345 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6346 | #ifdef SO_PASSCRED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6347 | PyModule_AddIntMacro(m, SO_PASSCRED); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6348 | #endif |
| 6349 | #ifdef SO_PEERCRED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6350 | PyModule_AddIntMacro(m, SO_PEERCRED); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6351 | #endif |
| 6352 | #ifdef LOCAL_PEERCRED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6353 | PyModule_AddIntMacro(m, LOCAL_PEERCRED); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6354 | #endif |
Victor Stinner | cb98bed | 2011-10-25 13:45:48 +0200 | [diff] [blame] | 6355 | #ifdef SO_BINDTODEVICE |
| 6356 | PyModule_AddIntMacro(m, SO_BINDTODEVICE); |
| 6357 | #endif |
Benjamin Peterson | 1314ef7 | 2013-11-27 09:18:54 -0600 | [diff] [blame] | 6358 | #ifdef SO_PRIORITY |
| 6359 | PyModule_AddIntMacro(m, SO_PRIORITY); |
| 6360 | #endif |
Charles-François Natali | ea07eb9 | 2014-09-18 23:18:46 +0100 | [diff] [blame] | 6361 | #ifdef SO_MARK |
| 6362 | PyModule_AddIntMacro(m, SO_MARK); |
| 6363 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6364 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6365 | /* Maximum number of connections for "listen" */ |
| 6366 | #ifdef SOMAXCONN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6367 | PyModule_AddIntMacro(m, SOMAXCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6368 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6369 | PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6370 | #endif |
| 6371 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6372 | /* Ancilliary message types */ |
| 6373 | #ifdef SCM_RIGHTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6374 | PyModule_AddIntMacro(m, SCM_RIGHTS); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6375 | #endif |
| 6376 | #ifdef SCM_CREDENTIALS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6377 | PyModule_AddIntMacro(m, SCM_CREDENTIALS); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6378 | #endif |
| 6379 | #ifdef SCM_CREDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6380 | PyModule_AddIntMacro(m, SCM_CREDS); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6381 | #endif |
| 6382 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6383 | /* Flags for send, recv */ |
| 6384 | #ifdef MSG_OOB |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6385 | PyModule_AddIntMacro(m, MSG_OOB); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6386 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6387 | #ifdef MSG_PEEK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6388 | PyModule_AddIntMacro(m, MSG_PEEK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6389 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6390 | #ifdef MSG_DONTROUTE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6391 | PyModule_AddIntMacro(m, MSG_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6392 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6393 | #ifdef MSG_DONTWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6394 | PyModule_AddIntMacro(m, MSG_DONTWAIT); |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 6395 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6396 | #ifdef MSG_EOR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6397 | PyModule_AddIntMacro(m, MSG_EOR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6398 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6399 | #ifdef MSG_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6400 | PyModule_AddIntMacro(m, MSG_TRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6401 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6402 | #ifdef MSG_CTRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6403 | PyModule_AddIntMacro(m, MSG_CTRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6404 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6405 | #ifdef MSG_WAITALL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6406 | PyModule_AddIntMacro(m, MSG_WAITALL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6407 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6408 | #ifdef MSG_BTAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6409 | PyModule_AddIntMacro(m, MSG_BTAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6410 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6411 | #ifdef MSG_ETAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6412 | PyModule_AddIntMacro(m, MSG_ETAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6413 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6414 | #ifdef MSG_NOSIGNAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6415 | PyModule_AddIntMacro(m, MSG_NOSIGNAL); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6416 | #endif |
| 6417 | #ifdef MSG_NOTIFICATION |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6418 | PyModule_AddIntMacro(m, MSG_NOTIFICATION); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6419 | #endif |
| 6420 | #ifdef MSG_CMSG_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6421 | PyModule_AddIntMacro(m, MSG_CMSG_CLOEXEC); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6422 | #endif |
| 6423 | #ifdef MSG_ERRQUEUE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6424 | PyModule_AddIntMacro(m, MSG_ERRQUEUE); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6425 | #endif |
| 6426 | #ifdef MSG_CONFIRM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6427 | PyModule_AddIntMacro(m, MSG_CONFIRM); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6428 | #endif |
| 6429 | #ifdef MSG_MORE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6430 | PyModule_AddIntMacro(m, MSG_MORE); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6431 | #endif |
| 6432 | #ifdef MSG_EOF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6433 | PyModule_AddIntMacro(m, MSG_EOF); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6434 | #endif |
| 6435 | #ifdef MSG_BCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6436 | PyModule_AddIntMacro(m, MSG_BCAST); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6437 | #endif |
| 6438 | #ifdef MSG_MCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6439 | PyModule_AddIntMacro(m, MSG_MCAST); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6440 | #endif |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 6441 | #ifdef MSG_FASTOPEN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6442 | PyModule_AddIntMacro(m, MSG_FASTOPEN); |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 6443 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6444 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6445 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 6446 | #ifdef SOL_SOCKET |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6447 | PyModule_AddIntMacro(m, SOL_SOCKET); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6448 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6449 | #ifdef SOL_IP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6450 | PyModule_AddIntMacro(m, SOL_IP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6451 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6452 | PyModule_AddIntConstant(m, "SOL_IP", 0); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6453 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6454 | #ifdef SOL_IPX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6455 | PyModule_AddIntMacro(m, SOL_IPX); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6456 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6457 | #ifdef SOL_AX25 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6458 | PyModule_AddIntMacro(m, SOL_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6459 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6460 | #ifdef SOL_ATALK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6461 | PyModule_AddIntMacro(m, SOL_ATALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6462 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6463 | #ifdef SOL_NETROM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6464 | PyModule_AddIntMacro(m, SOL_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6465 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6466 | #ifdef SOL_ROSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6467 | PyModule_AddIntMacro(m, SOL_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6468 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6469 | #ifdef SOL_TCP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6470 | PyModule_AddIntMacro(m, SOL_TCP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6471 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6472 | PyModule_AddIntConstant(m, "SOL_TCP", 6); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6473 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6474 | #ifdef SOL_UDP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6475 | PyModule_AddIntMacro(m, SOL_UDP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6476 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6477 | PyModule_AddIntConstant(m, "SOL_UDP", 17); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6478 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6479 | #ifdef SOL_CAN_BASE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6480 | PyModule_AddIntMacro(m, SOL_CAN_BASE); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6481 | #endif |
| 6482 | #ifdef SOL_CAN_RAW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6483 | PyModule_AddIntMacro(m, SOL_CAN_RAW); |
| 6484 | PyModule_AddIntMacro(m, CAN_RAW); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6485 | #endif |
| 6486 | #ifdef HAVE_LINUX_CAN_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6487 | PyModule_AddIntMacro(m, CAN_EFF_FLAG); |
| 6488 | PyModule_AddIntMacro(m, CAN_RTR_FLAG); |
| 6489 | PyModule_AddIntMacro(m, CAN_ERR_FLAG); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6490 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6491 | PyModule_AddIntMacro(m, CAN_SFF_MASK); |
| 6492 | PyModule_AddIntMacro(m, CAN_EFF_MASK); |
| 6493 | PyModule_AddIntMacro(m, CAN_ERR_MASK); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6494 | #endif |
| 6495 | #ifdef HAVE_LINUX_CAN_RAW_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6496 | PyModule_AddIntMacro(m, CAN_RAW_FILTER); |
| 6497 | PyModule_AddIntMacro(m, CAN_RAW_ERR_FILTER); |
| 6498 | PyModule_AddIntMacro(m, CAN_RAW_LOOPBACK); |
| 6499 | PyModule_AddIntMacro(m, CAN_RAW_RECV_OWN_MSGS); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6500 | #endif |
Charles-François Natali | 773e42d | 2013-02-05 19:42:01 +0100 | [diff] [blame] | 6501 | #ifdef HAVE_LINUX_CAN_BCM_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6502 | PyModule_AddIntMacro(m, CAN_BCM); |
Charles-François Natali | 773e42d | 2013-02-05 19:42:01 +0100 | [diff] [blame] | 6503 | PyModule_AddIntConstant(m, "CAN_BCM_TX_SETUP", TX_SETUP); |
| 6504 | PyModule_AddIntConstant(m, "CAN_BCM_TX_DELETE", TX_DELETE); |
| 6505 | PyModule_AddIntConstant(m, "CAN_BCM_TX_READ", TX_READ); |
| 6506 | PyModule_AddIntConstant(m, "CAN_BCM_TX_SEND", TX_SEND); |
| 6507 | PyModule_AddIntConstant(m, "CAN_BCM_RX_SETUP", RX_SETUP); |
| 6508 | PyModule_AddIntConstant(m, "CAN_BCM_RX_DELETE", RX_DELETE); |
| 6509 | PyModule_AddIntConstant(m, "CAN_BCM_RX_READ", RX_READ); |
| 6510 | PyModule_AddIntConstant(m, "CAN_BCM_TX_STATUS", TX_STATUS); |
| 6511 | PyModule_AddIntConstant(m, "CAN_BCM_TX_EXPIRED", TX_EXPIRED); |
| 6512 | PyModule_AddIntConstant(m, "CAN_BCM_RX_STATUS", RX_STATUS); |
| 6513 | PyModule_AddIntConstant(m, "CAN_BCM_RX_TIMEOUT", RX_TIMEOUT); |
| 6514 | PyModule_AddIntConstant(m, "CAN_BCM_RX_CHANGED", RX_CHANGED); |
| 6515 | #endif |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6516 | #ifdef SOL_RDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6517 | PyModule_AddIntMacro(m, SOL_RDS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6518 | #endif |
| 6519 | #ifdef RDS_CANCEL_SENT_TO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6520 | PyModule_AddIntMacro(m, RDS_CANCEL_SENT_TO); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6521 | #endif |
| 6522 | #ifdef RDS_GET_MR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6523 | PyModule_AddIntMacro(m, RDS_GET_MR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6524 | #endif |
| 6525 | #ifdef RDS_FREE_MR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6526 | PyModule_AddIntMacro(m, RDS_FREE_MR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6527 | #endif |
| 6528 | #ifdef RDS_RECVERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6529 | PyModule_AddIntMacro(m, RDS_RECVERR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6530 | #endif |
| 6531 | #ifdef RDS_CONG_MONITOR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6532 | PyModule_AddIntMacro(m, RDS_CONG_MONITOR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6533 | #endif |
| 6534 | #ifdef RDS_GET_MR_FOR_DEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6535 | PyModule_AddIntMacro(m, RDS_GET_MR_FOR_DEST); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6536 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6537 | #ifdef IPPROTO_IP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6538 | PyModule_AddIntMacro(m, IPPROTO_IP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6539 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6540 | PyModule_AddIntConstant(m, "IPPROTO_IP", 0); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6541 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6542 | #ifdef IPPROTO_HOPOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6543 | PyModule_AddIntMacro(m, IPPROTO_HOPOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6544 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6545 | #ifdef IPPROTO_ICMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6546 | PyModule_AddIntMacro(m, IPPROTO_ICMP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6547 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6548 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6549 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6550 | #ifdef IPPROTO_IGMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6551 | PyModule_AddIntMacro(m, IPPROTO_IGMP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6552 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6553 | #ifdef IPPROTO_GGP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6554 | PyModule_AddIntMacro(m, IPPROTO_GGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6555 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6556 | #ifdef IPPROTO_IPV4 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6557 | PyModule_AddIntMacro(m, IPPROTO_IPV4); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6558 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6559 | #ifdef IPPROTO_IPV6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6560 | PyModule_AddIntMacro(m, IPPROTO_IPV6); |
Martin v. Löwis | a0f1734 | 2003-10-03 13:56:20 +0000 | [diff] [blame] | 6561 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6562 | #ifdef IPPROTO_IPIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6563 | PyModule_AddIntMacro(m, IPPROTO_IPIP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6564 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6565 | #ifdef IPPROTO_TCP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6566 | PyModule_AddIntMacro(m, IPPROTO_TCP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6567 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6568 | PyModule_AddIntConstant(m, "IPPROTO_TCP", 6); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6569 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6570 | #ifdef IPPROTO_EGP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6571 | PyModule_AddIntMacro(m, IPPROTO_EGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6572 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6573 | #ifdef IPPROTO_PUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6574 | PyModule_AddIntMacro(m, IPPROTO_PUP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6575 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6576 | #ifdef IPPROTO_UDP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6577 | PyModule_AddIntMacro(m, IPPROTO_UDP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6578 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6579 | PyModule_AddIntConstant(m, "IPPROTO_UDP", 17); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6580 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6581 | #ifdef IPPROTO_IDP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6582 | PyModule_AddIntMacro(m, IPPROTO_IDP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6583 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6584 | #ifdef IPPROTO_HELLO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6585 | PyModule_AddIntMacro(m, IPPROTO_HELLO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6586 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6587 | #ifdef IPPROTO_ND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6588 | PyModule_AddIntMacro(m, IPPROTO_ND); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6589 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6590 | #ifdef IPPROTO_TP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6591 | PyModule_AddIntMacro(m, IPPROTO_TP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6592 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6593 | #ifdef IPPROTO_IPV6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6594 | PyModule_AddIntMacro(m, IPPROTO_IPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6595 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6596 | #ifdef IPPROTO_ROUTING |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6597 | PyModule_AddIntMacro(m, IPPROTO_ROUTING); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6598 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6599 | #ifdef IPPROTO_FRAGMENT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6600 | PyModule_AddIntMacro(m, IPPROTO_FRAGMENT); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6601 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6602 | #ifdef IPPROTO_RSVP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6603 | PyModule_AddIntMacro(m, IPPROTO_RSVP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6604 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6605 | #ifdef IPPROTO_GRE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6606 | PyModule_AddIntMacro(m, IPPROTO_GRE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6607 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6608 | #ifdef IPPROTO_ESP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6609 | PyModule_AddIntMacro(m, IPPROTO_ESP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6610 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6611 | #ifdef IPPROTO_AH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6612 | PyModule_AddIntMacro(m, IPPROTO_AH); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6613 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6614 | #ifdef IPPROTO_MOBILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6615 | PyModule_AddIntMacro(m, IPPROTO_MOBILE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6616 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6617 | #ifdef IPPROTO_ICMPV6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6618 | PyModule_AddIntMacro(m, IPPROTO_ICMPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6619 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6620 | #ifdef IPPROTO_NONE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6621 | PyModule_AddIntMacro(m, IPPROTO_NONE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6622 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6623 | #ifdef IPPROTO_DSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6624 | PyModule_AddIntMacro(m, IPPROTO_DSTOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6625 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6626 | #ifdef IPPROTO_XTP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6627 | PyModule_AddIntMacro(m, IPPROTO_XTP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6628 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6629 | #ifdef IPPROTO_EON |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6630 | PyModule_AddIntMacro(m, IPPROTO_EON); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6631 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6632 | #ifdef IPPROTO_PIM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6633 | PyModule_AddIntMacro(m, IPPROTO_PIM); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6634 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6635 | #ifdef IPPROTO_IPCOMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6636 | PyModule_AddIntMacro(m, IPPROTO_IPCOMP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6637 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6638 | #ifdef IPPROTO_VRRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6639 | PyModule_AddIntMacro(m, IPPROTO_VRRP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6640 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6641 | #ifdef IPPROTO_SCTP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6642 | PyModule_AddIntMacro(m, IPPROTO_SCTP); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6643 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6644 | #ifdef IPPROTO_BIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6645 | PyModule_AddIntMacro(m, IPPROTO_BIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6646 | #endif |
| 6647 | /**/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6648 | #ifdef IPPROTO_RAW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6649 | PyModule_AddIntMacro(m, IPPROTO_RAW); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6650 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6651 | PyModule_AddIntConstant(m, "IPPROTO_RAW", 255); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6652 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6653 | #ifdef IPPROTO_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6654 | PyModule_AddIntMacro(m, IPPROTO_MAX); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6655 | #endif |
| 6656 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6657 | #ifdef SYSPROTO_CONTROL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6658 | PyModule_AddIntMacro(m, SYSPROTO_CONTROL); |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6659 | #endif |
| 6660 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6661 | /* Some port configuration */ |
| 6662 | #ifdef IPPORT_RESERVED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6663 | PyModule_AddIntMacro(m, IPPORT_RESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6664 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6665 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6666 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6667 | #ifdef IPPORT_USERRESERVED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6668 | PyModule_AddIntMacro(m, IPPORT_USERRESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6669 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6670 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6671 | #endif |
| 6672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6673 | /* Some reserved IP v.4 addresses */ |
| 6674 | #ifdef INADDR_ANY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6675 | PyModule_AddIntMacro(m, INADDR_ANY); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6676 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6677 | PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6678 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6679 | #ifdef INADDR_BROADCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6680 | PyModule_AddIntMacro(m, INADDR_BROADCAST); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6681 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6682 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6683 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6684 | #ifdef INADDR_LOOPBACK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6685 | PyModule_AddIntMacro(m, INADDR_LOOPBACK); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6686 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6687 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6688 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6689 | #ifdef INADDR_UNSPEC_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6690 | PyModule_AddIntMacro(m, INADDR_UNSPEC_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6691 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6692 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6693 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6694 | #ifdef INADDR_ALLHOSTS_GROUP |
| 6695 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", |
| 6696 | INADDR_ALLHOSTS_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6697 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6698 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6699 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6700 | #ifdef INADDR_MAX_LOCAL_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6701 | PyModule_AddIntMacro(m, INADDR_MAX_LOCAL_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6702 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6703 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6704 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6705 | #ifdef INADDR_NONE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6706 | PyModule_AddIntMacro(m, INADDR_NONE); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6707 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6708 | PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6709 | #endif |
| 6710 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6711 | /* IPv4 [gs]etsockopt options */ |
| 6712 | #ifdef IP_OPTIONS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6713 | PyModule_AddIntMacro(m, IP_OPTIONS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6714 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6715 | #ifdef IP_HDRINCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6716 | PyModule_AddIntMacro(m, IP_HDRINCL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6717 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6718 | #ifdef IP_TOS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6719 | PyModule_AddIntMacro(m, IP_TOS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6720 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6721 | #ifdef IP_TTL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6722 | PyModule_AddIntMacro(m, IP_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6723 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6724 | #ifdef IP_RECVOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6725 | PyModule_AddIntMacro(m, IP_RECVOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6726 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6727 | #ifdef IP_RECVRETOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6728 | PyModule_AddIntMacro(m, IP_RECVRETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6729 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6730 | #ifdef IP_RECVDSTADDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6731 | PyModule_AddIntMacro(m, IP_RECVDSTADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6732 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6733 | #ifdef IP_RETOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6734 | PyModule_AddIntMacro(m, IP_RETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6735 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6736 | #ifdef IP_MULTICAST_IF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6737 | PyModule_AddIntMacro(m, IP_MULTICAST_IF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6738 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6739 | #ifdef IP_MULTICAST_TTL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6740 | PyModule_AddIntMacro(m, IP_MULTICAST_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6741 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6742 | #ifdef IP_MULTICAST_LOOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6743 | PyModule_AddIntMacro(m, IP_MULTICAST_LOOP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6744 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6745 | #ifdef IP_ADD_MEMBERSHIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6746 | PyModule_AddIntMacro(m, IP_ADD_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6747 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6748 | #ifdef IP_DROP_MEMBERSHIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6749 | PyModule_AddIntMacro(m, IP_DROP_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6750 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6751 | #ifdef IP_DEFAULT_MULTICAST_TTL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6752 | PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_TTL); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6753 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6754 | #ifdef IP_DEFAULT_MULTICAST_LOOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6755 | PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_LOOP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6756 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6757 | #ifdef IP_MAX_MEMBERSHIPS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6758 | PyModule_AddIntMacro(m, IP_MAX_MEMBERSHIPS); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6759 | #endif |
Charles-François Natali | 564a42c | 2011-12-17 14:59:56 +0100 | [diff] [blame] | 6760 | #ifdef IP_TRANSPARENT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6761 | PyModule_AddIntMacro(m, IP_TRANSPARENT); |
Charles-François Natali | 564a42c | 2011-12-17 14:59:56 +0100 | [diff] [blame] | 6762 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6763 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6764 | /* IPv6 [gs]etsockopt options, defined in RFC2553 */ |
| 6765 | #ifdef IPV6_JOIN_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6766 | PyModule_AddIntMacro(m, IPV6_JOIN_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6767 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6768 | #ifdef IPV6_LEAVE_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6769 | PyModule_AddIntMacro(m, IPV6_LEAVE_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6770 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6771 | #ifdef IPV6_MULTICAST_HOPS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6772 | PyModule_AddIntMacro(m, IPV6_MULTICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6773 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6774 | #ifdef IPV6_MULTICAST_IF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6775 | PyModule_AddIntMacro(m, IPV6_MULTICAST_IF); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6776 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6777 | #ifdef IPV6_MULTICAST_LOOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6778 | PyModule_AddIntMacro(m, IPV6_MULTICAST_LOOP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6779 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6780 | #ifdef IPV6_UNICAST_HOPS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6781 | PyModule_AddIntMacro(m, IPV6_UNICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6782 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6783 | /* Additional IPV6 socket options, defined in RFC 3493 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6784 | #ifdef IPV6_V6ONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6785 | PyModule_AddIntMacro(m, IPV6_V6ONLY); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6786 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6787 | /* Advanced IPV6 socket options, from RFC 3542 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6788 | #ifdef IPV6_CHECKSUM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6789 | PyModule_AddIntMacro(m, IPV6_CHECKSUM); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6790 | #endif |
| 6791 | #ifdef IPV6_DONTFRAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6792 | PyModule_AddIntMacro(m, IPV6_DONTFRAG); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6793 | #endif |
| 6794 | #ifdef IPV6_DSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6795 | PyModule_AddIntMacro(m, IPV6_DSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6796 | #endif |
| 6797 | #ifdef IPV6_HOPLIMIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6798 | PyModule_AddIntMacro(m, IPV6_HOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6799 | #endif |
| 6800 | #ifdef IPV6_HOPOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6801 | PyModule_AddIntMacro(m, IPV6_HOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6802 | #endif |
| 6803 | #ifdef IPV6_NEXTHOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6804 | PyModule_AddIntMacro(m, IPV6_NEXTHOP); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6805 | #endif |
| 6806 | #ifdef IPV6_PATHMTU |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6807 | PyModule_AddIntMacro(m, IPV6_PATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6808 | #endif |
| 6809 | #ifdef IPV6_PKTINFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6810 | PyModule_AddIntMacro(m, IPV6_PKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6811 | #endif |
| 6812 | #ifdef IPV6_RECVDSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6813 | PyModule_AddIntMacro(m, IPV6_RECVDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6814 | #endif |
| 6815 | #ifdef IPV6_RECVHOPLIMIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6816 | PyModule_AddIntMacro(m, IPV6_RECVHOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6817 | #endif |
| 6818 | #ifdef IPV6_RECVHOPOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6819 | PyModule_AddIntMacro(m, IPV6_RECVHOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6820 | #endif |
| 6821 | #ifdef IPV6_RECVPKTINFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6822 | PyModule_AddIntMacro(m, IPV6_RECVPKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6823 | #endif |
| 6824 | #ifdef IPV6_RECVRTHDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6825 | PyModule_AddIntMacro(m, IPV6_RECVRTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6826 | #endif |
| 6827 | #ifdef IPV6_RECVTCLASS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6828 | PyModule_AddIntMacro(m, IPV6_RECVTCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6829 | #endif |
| 6830 | #ifdef IPV6_RTHDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6831 | PyModule_AddIntMacro(m, IPV6_RTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6832 | #endif |
| 6833 | #ifdef IPV6_RTHDRDSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6834 | PyModule_AddIntMacro(m, IPV6_RTHDRDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6835 | #endif |
| 6836 | #ifdef IPV6_RTHDR_TYPE_0 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6837 | PyModule_AddIntMacro(m, IPV6_RTHDR_TYPE_0); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6838 | #endif |
| 6839 | #ifdef IPV6_RECVPATHMTU |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6840 | PyModule_AddIntMacro(m, IPV6_RECVPATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6841 | #endif |
| 6842 | #ifdef IPV6_TCLASS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6843 | PyModule_AddIntMacro(m, IPV6_TCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6844 | #endif |
| 6845 | #ifdef IPV6_USE_MIN_MTU |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6846 | PyModule_AddIntMacro(m, IPV6_USE_MIN_MTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6847 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6848 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6849 | /* TCP options */ |
| 6850 | #ifdef TCP_NODELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6851 | PyModule_AddIntMacro(m, TCP_NODELAY); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6852 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6853 | #ifdef TCP_MAXSEG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6854 | PyModule_AddIntMacro(m, TCP_MAXSEG); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6855 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6856 | #ifdef TCP_CORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6857 | PyModule_AddIntMacro(m, TCP_CORK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6858 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6859 | #ifdef TCP_KEEPIDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6860 | PyModule_AddIntMacro(m, TCP_KEEPIDLE); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6861 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6862 | #ifdef TCP_KEEPINTVL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6863 | PyModule_AddIntMacro(m, TCP_KEEPINTVL); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6864 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6865 | #ifdef TCP_KEEPCNT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6866 | PyModule_AddIntMacro(m, TCP_KEEPCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6867 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6868 | #ifdef TCP_SYNCNT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6869 | PyModule_AddIntMacro(m, TCP_SYNCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6870 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6871 | #ifdef TCP_LINGER2 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6872 | PyModule_AddIntMacro(m, TCP_LINGER2); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6873 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6874 | #ifdef TCP_DEFER_ACCEPT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6875 | PyModule_AddIntMacro(m, TCP_DEFER_ACCEPT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6876 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6877 | #ifdef TCP_WINDOW_CLAMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6878 | PyModule_AddIntMacro(m, TCP_WINDOW_CLAMP); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6879 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6880 | #ifdef TCP_INFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6881 | PyModule_AddIntMacro(m, TCP_INFO); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6882 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6883 | #ifdef TCP_QUICKACK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6884 | PyModule_AddIntMacro(m, TCP_QUICKACK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6885 | #endif |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 6886 | #ifdef TCP_FASTOPEN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6887 | PyModule_AddIntMacro(m, TCP_FASTOPEN); |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 6888 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6889 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6890 | /* IPX options */ |
| 6891 | #ifdef IPX_TYPE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6892 | PyModule_AddIntMacro(m, IPX_TYPE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6893 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 6894 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6895 | /* Reliable Datagram Sockets */ |
| 6896 | #ifdef RDS_CMSG_RDMA_ARGS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6897 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_ARGS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6898 | #endif |
| 6899 | #ifdef RDS_CMSG_RDMA_DEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6900 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_DEST); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6901 | #endif |
| 6902 | #ifdef RDS_CMSG_RDMA_MAP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6903 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_MAP); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6904 | #endif |
| 6905 | #ifdef RDS_CMSG_RDMA_STATUS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6906 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_STATUS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6907 | #endif |
| 6908 | #ifdef RDS_CMSG_RDMA_UPDATE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6909 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_UPDATE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6910 | #endif |
| 6911 | #ifdef RDS_RDMA_READWRITE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6912 | PyModule_AddIntMacro(m, RDS_RDMA_READWRITE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6913 | #endif |
| 6914 | #ifdef RDS_RDMA_FENCE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6915 | PyModule_AddIntMacro(m, RDS_RDMA_FENCE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6916 | #endif |
| 6917 | #ifdef RDS_RDMA_INVALIDATE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6918 | PyModule_AddIntMacro(m, RDS_RDMA_INVALIDATE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6919 | #endif |
| 6920 | #ifdef RDS_RDMA_USE_ONCE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6921 | PyModule_AddIntMacro(m, RDS_RDMA_USE_ONCE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6922 | #endif |
| 6923 | #ifdef RDS_RDMA_DONTWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6924 | PyModule_AddIntMacro(m, RDS_RDMA_DONTWAIT); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6925 | #endif |
| 6926 | #ifdef RDS_RDMA_NOTIFY_ME |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6927 | PyModule_AddIntMacro(m, RDS_RDMA_NOTIFY_ME); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6928 | #endif |
| 6929 | #ifdef RDS_RDMA_SILENT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6930 | PyModule_AddIntMacro(m, RDS_RDMA_SILENT); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6931 | #endif |
| 6932 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6933 | /* get{addr,name}info parameters */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6934 | #ifdef EAI_ADDRFAMILY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6935 | PyModule_AddIntMacro(m, EAI_ADDRFAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6936 | #endif |
| 6937 | #ifdef EAI_AGAIN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6938 | PyModule_AddIntMacro(m, EAI_AGAIN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6939 | #endif |
| 6940 | #ifdef EAI_BADFLAGS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6941 | PyModule_AddIntMacro(m, EAI_BADFLAGS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6942 | #endif |
| 6943 | #ifdef EAI_FAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6944 | PyModule_AddIntMacro(m, EAI_FAIL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6945 | #endif |
| 6946 | #ifdef EAI_FAMILY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6947 | PyModule_AddIntMacro(m, EAI_FAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6948 | #endif |
| 6949 | #ifdef EAI_MEMORY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6950 | PyModule_AddIntMacro(m, EAI_MEMORY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6951 | #endif |
| 6952 | #ifdef EAI_NODATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6953 | PyModule_AddIntMacro(m, EAI_NODATA); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6954 | #endif |
| 6955 | #ifdef EAI_NONAME |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6956 | PyModule_AddIntMacro(m, EAI_NONAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6957 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6958 | #ifdef EAI_OVERFLOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6959 | PyModule_AddIntMacro(m, EAI_OVERFLOW); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6960 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6961 | #ifdef EAI_SERVICE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6962 | PyModule_AddIntMacro(m, EAI_SERVICE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6963 | #endif |
| 6964 | #ifdef EAI_SOCKTYPE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6965 | PyModule_AddIntMacro(m, EAI_SOCKTYPE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6966 | #endif |
| 6967 | #ifdef EAI_SYSTEM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6968 | PyModule_AddIntMacro(m, EAI_SYSTEM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6969 | #endif |
| 6970 | #ifdef EAI_BADHINTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6971 | PyModule_AddIntMacro(m, EAI_BADHINTS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6972 | #endif |
| 6973 | #ifdef EAI_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6974 | PyModule_AddIntMacro(m, EAI_PROTOCOL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6975 | #endif |
| 6976 | #ifdef EAI_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6977 | PyModule_AddIntMacro(m, EAI_MAX); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6978 | #endif |
| 6979 | #ifdef AI_PASSIVE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6980 | PyModule_AddIntMacro(m, AI_PASSIVE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6981 | #endif |
| 6982 | #ifdef AI_CANONNAME |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6983 | PyModule_AddIntMacro(m, AI_CANONNAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6984 | #endif |
| 6985 | #ifdef AI_NUMERICHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6986 | PyModule_AddIntMacro(m, AI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6987 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6988 | #ifdef AI_NUMERICSERV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6989 | PyModule_AddIntMacro(m, AI_NUMERICSERV); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6990 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6991 | #ifdef AI_MASK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6992 | PyModule_AddIntMacro(m, AI_MASK); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6993 | #endif |
| 6994 | #ifdef AI_ALL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6995 | PyModule_AddIntMacro(m, AI_ALL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6996 | #endif |
| 6997 | #ifdef AI_V4MAPPED_CFG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6998 | PyModule_AddIntMacro(m, AI_V4MAPPED_CFG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6999 | #endif |
| 7000 | #ifdef AI_ADDRCONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7001 | PyModule_AddIntMacro(m, AI_ADDRCONFIG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7002 | #endif |
| 7003 | #ifdef AI_V4MAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7004 | PyModule_AddIntMacro(m, AI_V4MAPPED); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7005 | #endif |
| 7006 | #ifdef AI_DEFAULT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7007 | PyModule_AddIntMacro(m, AI_DEFAULT); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7008 | #endif |
| 7009 | #ifdef NI_MAXHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7010 | PyModule_AddIntMacro(m, NI_MAXHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7011 | #endif |
| 7012 | #ifdef NI_MAXSERV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7013 | PyModule_AddIntMacro(m, NI_MAXSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7014 | #endif |
| 7015 | #ifdef NI_NOFQDN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7016 | PyModule_AddIntMacro(m, NI_NOFQDN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7017 | #endif |
| 7018 | #ifdef NI_NUMERICHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7019 | PyModule_AddIntMacro(m, NI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7020 | #endif |
| 7021 | #ifdef NI_NAMEREQD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7022 | PyModule_AddIntMacro(m, NI_NAMEREQD); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7023 | #endif |
| 7024 | #ifdef NI_NUMERICSERV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7025 | PyModule_AddIntMacro(m, NI_NUMERICSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7026 | #endif |
| 7027 | #ifdef NI_DGRAM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7028 | PyModule_AddIntMacro(m, NI_DGRAM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7029 | #endif |
| 7030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7031 | /* shutdown() parameters */ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7032 | #ifdef SHUT_RD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7033 | PyModule_AddIntMacro(m, SHUT_RD); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7034 | #elif defined(SD_RECEIVE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7035 | PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7036 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7037 | PyModule_AddIntConstant(m, "SHUT_RD", 0); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7038 | #endif |
| 7039 | #ifdef SHUT_WR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7040 | PyModule_AddIntMacro(m, SHUT_WR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7041 | #elif defined(SD_SEND) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7042 | PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7043 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7044 | PyModule_AddIntConstant(m, "SHUT_WR", 1); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7045 | #endif |
| 7046 | #ifdef SHUT_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7047 | PyModule_AddIntMacro(m, SHUT_RDWR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7048 | #elif defined(SD_BOTH) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7049 | PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7050 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7051 | PyModule_AddIntConstant(m, "SHUT_RDWR", 2); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7052 | #endif |
| 7053 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 7054 | #ifdef SIO_RCVALL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7055 | { |
| 7056 | DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS}; |
| 7057 | const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"}; |
| 7058 | int i; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 7059 | for(i = 0; i<Py_ARRAY_LENGTH(codes); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7060 | PyObject *tmp; |
| 7061 | tmp = PyLong_FromUnsignedLong(codes[i]); |
| 7062 | if (tmp == NULL) |
| 7063 | return NULL; |
| 7064 | PyModule_AddObject(m, names[i], tmp); |
| 7065 | } |
| 7066 | } |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7067 | PyModule_AddIntMacro(m, RCVALL_OFF); |
| 7068 | PyModule_AddIntMacro(m, RCVALL_ON); |
| 7069 | PyModule_AddIntMacro(m, RCVALL_SOCKETLEVELONLY); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 7070 | #ifdef RCVALL_IPLEVEL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7071 | PyModule_AddIntMacro(m, RCVALL_IPLEVEL); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 7072 | #endif |
| 7073 | #ifdef RCVALL_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7074 | PyModule_AddIntMacro(m, RCVALL_MAX); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 7075 | #endif |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 7076 | #endif /* _MSTCPIP_ */ |
| 7077 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7078 | /* Initialize gethostbyname lock */ |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 7079 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7080 | netdb_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 7081 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7082 | return m; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 7083 | } |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7084 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 7085 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7086 | #ifndef HAVE_INET_PTON |
Christian Heimes | 96e7b3d | 2007-11-20 06:51:17 +0000 | [diff] [blame] | 7087 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 7088 | |
| 7089 | /* Simplistic emulation code for inet_pton that only works for IPv4 */ |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 7090 | /* These are not exposed because they do not set errno properly */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 7091 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 7092 | int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 7093 | inet_pton(int af, const char *src, void *dst) |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7094 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7095 | if (af == AF_INET) { |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 7096 | #if (SIZEOF_INT != 4) |
| 7097 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 7098 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7099 | unsigned int packed_addr; |
| 7100 | packed_addr = inet_addr(src); |
| 7101 | if (packed_addr == INADDR_NONE) |
| 7102 | return 0; |
| 7103 | memcpy(dst, &packed_addr, 4); |
| 7104 | return 1; |
| 7105 | } |
| 7106 | /* Should set errno to EAFNOSUPPORT */ |
| 7107 | return -1; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7108 | } |
| 7109 | |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 7110 | const char * |
| 7111 | 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] | 7112 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7113 | if (af == AF_INET) { |
| 7114 | struct in_addr packed_addr; |
| 7115 | if (size < 16) |
| 7116 | /* Should set errno to ENOSPC. */ |
| 7117 | return NULL; |
| 7118 | memcpy(&packed_addr, src, sizeof(packed_addr)); |
| 7119 | return strncpy(dst, inet_ntoa(packed_addr), size); |
| 7120 | } |
| 7121 | /* Should set errno to EAFNOSUPPORT */ |
| 7122 | return NULL; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7123 | } |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 7124 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7125 | #endif |
Christian Heimes | b615069 | 2007-11-15 23:37:07 +0000 | [diff] [blame] | 7126 | #endif |