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 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 594 | /* Do a select()/poll() on the socket, if necessary (sock_timeout > 0). |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 595 | The argument writing indicates the direction. |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 596 | This does not raise an exception; we'll let our caller do that |
| 597 | after they've reacquired the interpreter lock. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 598 | Returns 1 on timeout, -1 on error, 0 otherwise. */ |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 599 | static int |
Victor Stinner | 391fa71 | 2015-03-31 13:15:31 +0200 | [diff] [blame^] | 600 | internal_select(PySocketSockObject *s, int writing, _PyTime_t interval) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 601 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 602 | int n; |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 603 | #ifdef HAVE_POLL |
| 604 | struct pollfd pollfd; |
| 605 | _PyTime_t timeout; |
| 606 | int timeout_int; |
| 607 | #else |
| 608 | fd_set fds; |
| 609 | struct timeval tv; |
| 610 | #endif |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 611 | |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 612 | #ifdef WITH_THREAD |
| 613 | /* must be called with the GIL held */ |
| 614 | assert(PyGILState_Check()); |
| 615 | #endif |
| 616 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 617 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 618 | if (s->sock_timeout <= 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 620 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 621 | /* Guard against closed socket */ |
| 622 | if (s->sock_fd < 0) |
| 623 | return 0; |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 624 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 625 | /* Handling this condition here simplifies the select loops */ |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 626 | if (interval < 0) |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 627 | return 1; |
| 628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 629 | /* Prefer poll, if available, since you can poll() any fd |
| 630 | * which can't be done with select(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 631 | #ifdef HAVE_POLL |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 632 | pollfd.fd = s->sock_fd; |
| 633 | pollfd.events = writing ? POLLOUT : POLLIN; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 634 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 635 | /* s->sock_timeout is in seconds, timeout in ms */ |
Victor Stinner | 869e177 | 2015-03-30 03:49:14 +0200 | [diff] [blame] | 636 | timeout = _PyTime_AsMilliseconds(interval, _PyTime_ROUND_CEILING); |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 637 | assert(timeout <= INT_MAX); |
| 638 | timeout_int = (int)timeout; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 639 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 640 | Py_BEGIN_ALLOW_THREADS; |
| 641 | n = poll(&pollfd, 1, timeout_int); |
| 642 | Py_END_ALLOW_THREADS; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 643 | #else |
Victor Stinner | 869e177 | 2015-03-30 03:49:14 +0200 | [diff] [blame] | 644 | _PyTime_AsTimeval_noraise(interval, &tv, _PyTime_ROUND_CEILING); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 645 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 646 | FD_ZERO(&fds); |
| 647 | FD_SET(s->sock_fd, &fds); |
| 648 | |
| 649 | /* See if the socket is ready */ |
| 650 | Py_BEGIN_ALLOW_THREADS; |
| 651 | if (writing) |
| 652 | n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 653 | NULL, &fds, NULL, &tv); |
| 654 | else |
| 655 | n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 656 | &fds, NULL, NULL, &tv); |
| 657 | Py_END_ALLOW_THREADS; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 658 | #endif |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 659 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 660 | if (n < 0) |
| 661 | return -1; |
| 662 | if (n == 0) |
| 663 | return 1; |
| 664 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 667 | /* |
| 668 | Two macros for automatic retry of select() in case of false positives |
| 669 | (for example, select() could indicate a socket is ready for reading |
| 670 | but the data then discarded by the OS because of a wrong checksum). |
| 671 | Here is an example of use: |
| 672 | |
| 673 | BEGIN_SELECT_LOOP(s) |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 674 | |
Victor Stinner | 391fa71 | 2015-03-31 13:15:31 +0200 | [diff] [blame^] | 675 | timeout = internal_select(s, 0, interval); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 676 | |
| 677 | if (!timeout) { |
| 678 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 679 | outlen = recv(s->sock_fd, cbuf, len, flags); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 680 | Py_END_ALLOW_THREADS |
| 681 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 682 | if (timeout == 1) { |
| 683 | PyErr_SetString(socket_timeout, "timed out"); |
| 684 | return -1; |
| 685 | } |
| 686 | END_SELECT_LOOP(s) |
| 687 | */ |
| 688 | |
| 689 | #define BEGIN_SELECT_LOOP(s) \ |
| 690 | { \ |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 691 | _PyTime_t deadline = 0; \ |
| 692 | _PyTime_t interval = s->sock_timeout; \ |
| 693 | int has_timeout = (s->sock_timeout > 0); \ |
| 694 | if (has_timeout) \ |
| 695 | deadline = _PyTime_GetMonotonicClock() + interval; \ |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 696 | while (1) { \ |
| 697 | errno = 0; \ |
| 698 | |
| 699 | #define END_SELECT_LOOP(s) \ |
| 700 | if (!has_timeout || \ |
| 701 | (!CHECK_ERRNO(EWOULDBLOCK) && !CHECK_ERRNO(EAGAIN))) \ |
| 702 | break; \ |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 703 | interval = deadline - _PyTime_GetMonotonicClock(); \ |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 704 | } \ |
| 705 | } \ |
| 706 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 707 | /* Initialize a new socket object. */ |
| 708 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 709 | static _PyTime_t defaulttimeout = -1; /* Default timeout for new sockets */ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 710 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 711 | static void |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 712 | init_sockobject(PySocketSockObject *s, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 713 | SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 714 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 715 | s->sock_fd = fd; |
| 716 | s->sock_family = family; |
| 717 | s->sock_type = type; |
| 718 | s->sock_proto = proto; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 720 | s->errorhandler = &set_error; |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 721 | #ifdef SOCK_NONBLOCK |
| 722 | if (type & SOCK_NONBLOCK) |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 723 | s->sock_timeout = 0; |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 724 | else |
| 725 | #endif |
| 726 | { |
| 727 | s->sock_timeout = defaulttimeout; |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 728 | if (defaulttimeout >= 0) |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 729 | internal_setblocking(s, 0); |
| 730 | } |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 731 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 735 | /* Create a new socket object. |
| 736 | This just creates the object and initializes it. |
| 737 | If the creation fails, return NULL and set an exception (implicit |
| 738 | in NEWOBJ()). */ |
| 739 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 740 | static PySocketSockObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 741 | new_sockobject(SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 742 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 743 | PySocketSockObject *s; |
| 744 | s = (PySocketSockObject *) |
| 745 | PyType_GenericNew(&sock_type, NULL, NULL); |
| 746 | if (s != NULL) |
| 747 | init_sockobject(s, fd, family, type, proto); |
| 748 | return s; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 751 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 752 | /* Lock to allow python interpreter to continue, but only allow one |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 753 | thread to be in gethostbyname or getaddrinfo */ |
| 754 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Matthias Klose | d182a6c | 2012-08-14 17:29:04 +0200 | [diff] [blame] | 755 | static PyThread_type_lock netdb_lock; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 756 | #endif |
| 757 | |
| 758 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 759 | /* Convert a string specifying a host name or one of a few symbolic |
| 760 | names to a numeric IP address. This usually calls gethostbyname() |
| 761 | to do the work; the names "" and "<broadcast>" are special. |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 762 | Return the length (IPv4 should be 4 bytes), or negative if |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 763 | an error occurred; then an exception is raised. */ |
| 764 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 765 | static int |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 766 | 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] | 767 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 768 | struct addrinfo hints, *res; |
| 769 | int error; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 771 | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
| 772 | if (name[0] == '\0') { |
| 773 | int siz; |
| 774 | memset(&hints, 0, sizeof(hints)); |
| 775 | hints.ai_family = af; |
| 776 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
| 777 | hints.ai_flags = AI_PASSIVE; |
| 778 | Py_BEGIN_ALLOW_THREADS |
| 779 | ACQUIRE_GETADDRINFO_LOCK |
| 780 | error = getaddrinfo(NULL, "0", &hints, &res); |
| 781 | Py_END_ALLOW_THREADS |
| 782 | /* We assume that those thread-unsafe getaddrinfo() versions |
| 783 | *are* safe regarding their return value, ie. that a |
| 784 | subsequent call to getaddrinfo() does not destroy the |
| 785 | outcome of the first call. */ |
| 786 | RELEASE_GETADDRINFO_LOCK |
| 787 | if (error) { |
| 788 | set_gaierror(error); |
| 789 | return -1; |
| 790 | } |
| 791 | switch (res->ai_family) { |
| 792 | case AF_INET: |
| 793 | siz = 4; |
| 794 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 795 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 796 | case AF_INET6: |
| 797 | siz = 16; |
| 798 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 799 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 800 | default: |
| 801 | freeaddrinfo(res); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 802 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 803 | "unsupported address family"); |
| 804 | return -1; |
| 805 | } |
| 806 | if (res->ai_next) { |
| 807 | freeaddrinfo(res); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 808 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 809 | "wildcard resolved to multiple address"); |
| 810 | return -1; |
| 811 | } |
| 812 | if (res->ai_addrlen < addr_ret_size) |
| 813 | addr_ret_size = res->ai_addrlen; |
| 814 | memcpy(addr_ret, res->ai_addr, addr_ret_size); |
| 815 | freeaddrinfo(res); |
| 816 | return siz; |
| 817 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 818 | /* special-case broadcast - inet_addr() below can return INADDR_NONE for |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 819 | * this */ |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 820 | if (strcmp(name, "255.255.255.255") == 0 || |
| 821 | strcmp(name, "<broadcast>") == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 822 | struct sockaddr_in *sin; |
| 823 | if (af != AF_INET && af != AF_UNSPEC) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 824 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 825 | "address family mismatched"); |
| 826 | return -1; |
| 827 | } |
| 828 | sin = (struct sockaddr_in *)addr_ret; |
| 829 | memset((void *) sin, '\0', sizeof(*sin)); |
| 830 | sin->sin_family = AF_INET; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 831 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 832 | sin->sin_len = sizeof(*sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 833 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 834 | sin->sin_addr.s_addr = INADDR_BROADCAST; |
| 835 | return sizeof(sin->sin_addr); |
| 836 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 837 | |
| 838 | /* avoid a name resolution in case of numeric address */ |
| 839 | #ifdef HAVE_INET_PTON |
| 840 | /* check for an IPv4 address */ |
| 841 | if (af == AF_UNSPEC || af == AF_INET) { |
| 842 | struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret; |
| 843 | memset(sin, 0, sizeof(*sin)); |
| 844 | if (inet_pton(AF_INET, name, &sin->sin_addr) > 0) { |
| 845 | sin->sin_family = AF_INET; |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 846 | #ifdef HAVE_SOCKADDR_SA_LEN |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 847 | sin->sin_len = sizeof(*sin); |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 848 | #endif |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 849 | return 4; |
| 850 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 851 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 852 | #ifdef ENABLE_IPV6 |
| 853 | /* check for an IPv6 address - if the address contains a scope ID, we |
| 854 | * fallback to getaddrinfo(), which can handle translation from interface |
| 855 | * name to interface index */ |
| 856 | if ((af == AF_UNSPEC || af == AF_INET6) && !strchr(name, '%')) { |
| 857 | struct sockaddr_in6 *sin = (struct sockaddr_in6 *)addr_ret; |
| 858 | memset(sin, 0, sizeof(*sin)); |
| 859 | if (inet_pton(AF_INET6, name, &sin->sin6_addr) > 0) { |
| 860 | sin->sin6_family = AF_INET6; |
| 861 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 862 | sin->sin6_len = sizeof(*sin); |
| 863 | #endif |
| 864 | return 16; |
| 865 | } |
| 866 | } |
| 867 | #endif /* ENABLE_IPV6 */ |
| 868 | #else /* HAVE_INET_PTON */ |
| 869 | /* check for an IPv4 address */ |
| 870 | if (af == AF_INET || af == AF_UNSPEC) { |
| 871 | struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret; |
| 872 | memset(sin, 0, sizeof(*sin)); |
| 873 | if ((sin->sin_addr.s_addr = inet_addr(name)) != INADDR_NONE) { |
| 874 | sin->sin_family = AF_INET; |
| 875 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 876 | sin->sin_len = sizeof(*sin); |
| 877 | #endif |
| 878 | return 4; |
| 879 | } |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 880 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 881 | #endif /* HAVE_INET_PTON */ |
| 882 | |
| 883 | /* perform a name resolution */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 884 | memset(&hints, 0, sizeof(hints)); |
| 885 | hints.ai_family = af; |
| 886 | Py_BEGIN_ALLOW_THREADS |
| 887 | ACQUIRE_GETADDRINFO_LOCK |
| 888 | error = getaddrinfo(name, NULL, &hints, &res); |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 889 | #if defined(__digital__) && defined(__unix__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 890 | if (error == EAI_NONAME && af == AF_UNSPEC) { |
| 891 | /* On Tru64 V5.1, numeric-to-addr conversion fails |
| 892 | if no address family is given. Assume IPv4 for now.*/ |
| 893 | hints.ai_family = AF_INET; |
| 894 | error = getaddrinfo(name, NULL, &hints, &res); |
| 895 | } |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 896 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 897 | Py_END_ALLOW_THREADS |
| 898 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 899 | if (error) { |
| 900 | set_gaierror(error); |
| 901 | return -1; |
| 902 | } |
| 903 | if (res->ai_addrlen < addr_ret_size) |
| 904 | addr_ret_size = res->ai_addrlen; |
| 905 | memcpy((char *) addr_ret, res->ai_addr, addr_ret_size); |
| 906 | freeaddrinfo(res); |
| 907 | switch (addr_ret->sa_family) { |
| 908 | case AF_INET: |
| 909 | return 4; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 910 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 911 | case AF_INET6: |
| 912 | return 16; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 913 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 915 | PyErr_SetString(PyExc_OSError, "unknown address family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 916 | return -1; |
| 917 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 920 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 921 | /* Create a string object representing an IP address. |
| 922 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 923 | size numbers). */ |
| 924 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 925 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 926 | makeipaddr(struct sockaddr *addr, int addrlen) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 927 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 928 | char buf[NI_MAXHOST]; |
| 929 | int error; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 930 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 931 | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, |
| 932 | NI_NUMERICHOST); |
| 933 | if (error) { |
| 934 | set_gaierror(error); |
| 935 | return NULL; |
| 936 | } |
| 937 | return PyUnicode_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 941 | #ifdef USE_BLUETOOTH |
| 942 | /* Convert a string representation of a Bluetooth address into a numeric |
| 943 | address. Returns the length (6), or raises an exception and returns -1 if |
| 944 | an error occurred. */ |
| 945 | |
| 946 | static int |
| 947 | setbdaddr(char *name, bdaddr_t *bdaddr) |
| 948 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 949 | unsigned int b0, b1, b2, b3, b4, b5; |
| 950 | char ch; |
| 951 | int n; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 952 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 953 | n = sscanf(name, "%X:%X:%X:%X:%X:%X%c", |
| 954 | &b5, &b4, &b3, &b2, &b1, &b0, &ch); |
| 955 | if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) { |
| 956 | bdaddr->b[0] = b0; |
| 957 | bdaddr->b[1] = b1; |
| 958 | bdaddr->b[2] = b2; |
| 959 | bdaddr->b[3] = b3; |
| 960 | bdaddr->b[4] = b4; |
| 961 | bdaddr->b[5] = b5; |
| 962 | return 6; |
| 963 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 964 | PyErr_SetString(PyExc_OSError, "bad bluetooth address"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 965 | return -1; |
| 966 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | /* Create a string representation of the Bluetooth address. This is always a |
| 970 | string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal |
| 971 | value (zero padded if necessary). */ |
| 972 | |
| 973 | static PyObject * |
| 974 | makebdaddr(bdaddr_t *bdaddr) |
| 975 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 976 | char buf[(6 * 2) + 5 + 1]; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 977 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 978 | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 979 | bdaddr->b[5], bdaddr->b[4], bdaddr->b[3], |
| 980 | bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]); |
| 981 | return PyUnicode_FromString(buf); |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 982 | } |
| 983 | #endif |
| 984 | |
| 985 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 986 | /* Create an object representing the given socket address, |
| 987 | suitable for passing it back to bind(), connect() etc. |
| 988 | The family field of the sockaddr structure is inspected |
| 989 | to determine what kind of address it really is. */ |
| 990 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 991 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 992 | static PyObject * |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 993 | 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] | 994 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 995 | if (addrlen == 0) { |
| 996 | /* No address -- may be recvfrom() from known socket */ |
| 997 | Py_INCREF(Py_None); |
| 998 | return Py_None; |
| 999 | } |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1000 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1001 | switch (addr->sa_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1002 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1003 | case AF_INET: |
| 1004 | { |
| 1005 | struct sockaddr_in *a; |
| 1006 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1007 | PyObject *ret = NULL; |
| 1008 | if (addrobj) { |
| 1009 | a = (struct sockaddr_in *)addr; |
| 1010 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 1011 | Py_DECREF(addrobj); |
| 1012 | } |
| 1013 | return ret; |
| 1014 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1015 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1016 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1017 | case AF_UNIX: |
| 1018 | { |
| 1019 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1020 | #ifdef linux |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1021 | if (a->sun_path[0] == 0) { /* Linux abstract namespace */ |
| 1022 | addrlen -= offsetof(struct sockaddr_un, sun_path); |
Charles-François Natali | b10c71d | 2013-05-21 10:45:46 +0200 | [diff] [blame] | 1023 | return PyBytes_FromStringAndSize(a->sun_path, addrlen); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1024 | } |
| 1025 | else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1026 | #endif /* linux */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | { |
| 1028 | /* regular NULL-terminated string */ |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1029 | return PyUnicode_DecodeFSDefault(a->sun_path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1030 | } |
| 1031 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1032 | #endif /* AF_UNIX */ |
| 1033 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1034 | #if defined(AF_NETLINK) |
| 1035 | case AF_NETLINK: |
| 1036 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1037 | struct sockaddr_nl *a = (struct sockaddr_nl *) addr; |
| 1038 | return Py_BuildValue("II", a->nl_pid, a->nl_groups); |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1039 | } |
| 1040 | #endif /* AF_NETLINK */ |
| 1041 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1042 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1043 | case AF_INET6: |
| 1044 | { |
| 1045 | struct sockaddr_in6 *a; |
| 1046 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1047 | PyObject *ret = NULL; |
| 1048 | if (addrobj) { |
| 1049 | a = (struct sockaddr_in6 *)addr; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1050 | ret = Py_BuildValue("OiII", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1051 | addrobj, |
| 1052 | ntohs(a->sin6_port), |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1053 | ntohl(a->sin6_flowinfo), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1054 | a->sin6_scope_id); |
| 1055 | Py_DECREF(addrobj); |
| 1056 | } |
| 1057 | return ret; |
| 1058 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1059 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1060 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1061 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1062 | case AF_BLUETOOTH: |
| 1063 | switch (proto) { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1065 | case BTPROTO_L2CAP: |
| 1066 | { |
| 1067 | struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr; |
| 1068 | PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr)); |
| 1069 | PyObject *ret = NULL; |
| 1070 | if (addrobj) { |
| 1071 | ret = Py_BuildValue("Oi", |
| 1072 | addrobj, |
| 1073 | _BT_L2_MEMB(a, psm)); |
| 1074 | Py_DECREF(addrobj); |
| 1075 | } |
| 1076 | return ret; |
| 1077 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1078 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | case BTPROTO_RFCOMM: |
| 1080 | { |
| 1081 | struct sockaddr_rc *a = (struct sockaddr_rc *) addr; |
| 1082 | PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr)); |
| 1083 | PyObject *ret = NULL; |
| 1084 | if (addrobj) { |
| 1085 | ret = Py_BuildValue("Oi", |
| 1086 | addrobj, |
| 1087 | _BT_RC_MEMB(a, channel)); |
| 1088 | Py_DECREF(addrobj); |
| 1089 | } |
| 1090 | return ret; |
| 1091 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1092 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1093 | case BTPROTO_HCI: |
| 1094 | { |
| 1095 | struct sockaddr_hci *a = (struct sockaddr_hci *) addr; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1096 | #if defined(__NetBSD__) || defined(__DragonFly__) |
| 1097 | return makebdaddr(&_BT_HCI_MEMB(a, bdaddr)); |
| 1098 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1099 | PyObject *ret = NULL; |
| 1100 | ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev)); |
| 1101 | return ret; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1102 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1103 | } |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1104 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1105 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1106 | case BTPROTO_SCO: |
| 1107 | { |
| 1108 | struct sockaddr_sco *a = (struct sockaddr_sco *) addr; |
| 1109 | return makebdaddr(&_BT_SCO_MEMB(a, bdaddr)); |
| 1110 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1111 | #endif |
| 1112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1113 | default: |
| 1114 | PyErr_SetString(PyExc_ValueError, |
| 1115 | "Unknown Bluetooth protocol"); |
| 1116 | return NULL; |
| 1117 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1118 | #endif |
| 1119 | |
Antoine Pitrou | b156a46 | 2010-10-27 20:13:57 +0000 | [diff] [blame] | 1120 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1121 | case AF_PACKET: |
| 1122 | { |
| 1123 | struct sockaddr_ll *a = (struct sockaddr_ll *)addr; |
| 1124 | char *ifname = ""; |
| 1125 | struct ifreq ifr; |
| 1126 | /* need to look up interface name give index */ |
| 1127 | if (a->sll_ifindex) { |
| 1128 | ifr.ifr_ifindex = a->sll_ifindex; |
| 1129 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
| 1130 | ifname = ifr.ifr_name; |
| 1131 | } |
| 1132 | return Py_BuildValue("shbhy#", |
| 1133 | ifname, |
| 1134 | ntohs(a->sll_protocol), |
| 1135 | a->sll_pkttype, |
| 1136 | a->sll_hatype, |
| 1137 | a->sll_addr, |
| 1138 | a->sll_halen); |
| 1139 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1140 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1141 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1142 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1143 | case AF_TIPC: |
| 1144 | { |
| 1145 | struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr; |
| 1146 | if (a->addrtype == TIPC_ADDR_NAMESEQ) { |
| 1147 | return Py_BuildValue("IIIII", |
| 1148 | a->addrtype, |
| 1149 | a->addr.nameseq.type, |
| 1150 | a->addr.nameseq.lower, |
| 1151 | a->addr.nameseq.upper, |
| 1152 | a->scope); |
| 1153 | } else if (a->addrtype == TIPC_ADDR_NAME) { |
| 1154 | return Py_BuildValue("IIIII", |
| 1155 | a->addrtype, |
| 1156 | a->addr.name.name.type, |
| 1157 | a->addr.name.name.instance, |
| 1158 | a->addr.name.name.instance, |
| 1159 | a->scope); |
| 1160 | } else if (a->addrtype == TIPC_ADDR_ID) { |
| 1161 | return Py_BuildValue("IIIII", |
| 1162 | a->addrtype, |
| 1163 | a->addr.id.node, |
| 1164 | a->addr.id.ref, |
| 1165 | 0, |
| 1166 | a->scope); |
| 1167 | } else { |
| 1168 | PyErr_SetString(PyExc_ValueError, |
| 1169 | "Invalid address type"); |
| 1170 | return NULL; |
| 1171 | } |
| 1172 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1173 | #endif |
| 1174 | |
Vinay Sajip | ecfc98c | 2014-03-20 12:42:42 +0000 | [diff] [blame] | 1175 | #ifdef AF_CAN |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1176 | case AF_CAN: |
| 1177 | { |
| 1178 | struct sockaddr_can *a = (struct sockaddr_can *)addr; |
| 1179 | char *ifname = ""; |
| 1180 | struct ifreq ifr; |
| 1181 | /* need to look up interface name given index */ |
| 1182 | if (a->can_ifindex) { |
| 1183 | ifr.ifr_ifindex = a->can_ifindex; |
| 1184 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
| 1185 | ifname = ifr.ifr_name; |
| 1186 | } |
| 1187 | |
| 1188 | return Py_BuildValue("O&h", PyUnicode_DecodeFSDefault, |
| 1189 | ifname, |
| 1190 | a->can_family); |
| 1191 | } |
| 1192 | #endif |
| 1193 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1194 | #ifdef PF_SYSTEM |
| 1195 | case PF_SYSTEM: |
| 1196 | switch(proto) { |
| 1197 | #ifdef SYSPROTO_CONTROL |
| 1198 | case SYSPROTO_CONTROL: |
| 1199 | { |
| 1200 | struct sockaddr_ctl *a = (struct sockaddr_ctl *)addr; |
| 1201 | return Py_BuildValue("(II)", a->sc_id, a->sc_unit); |
| 1202 | } |
| 1203 | #endif |
| 1204 | default: |
| 1205 | PyErr_SetString(PyExc_ValueError, |
| 1206 | "Invalid address type"); |
| 1207 | return 0; |
| 1208 | } |
| 1209 | #endif |
| 1210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1211 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1213 | default: |
| 1214 | /* If we don't know the address family, don't raise an |
| 1215 | exception -- return it as an (int, bytes) tuple. */ |
| 1216 | return Py_BuildValue("iy#", |
| 1217 | addr->sa_family, |
| 1218 | addr->sa_data, |
| 1219 | sizeof(addr->sa_data)); |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1220 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1221 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1224 | /* Helper for getsockaddrarg: bypass IDNA for ASCII-only host names |
| 1225 | (in particular, numeric IP addresses). */ |
| 1226 | struct maybe_idna { |
| 1227 | PyObject *obj; |
| 1228 | char *buf; |
| 1229 | }; |
| 1230 | |
| 1231 | static void |
| 1232 | idna_cleanup(struct maybe_idna *data) |
| 1233 | { |
| 1234 | Py_CLEAR(data->obj); |
| 1235 | } |
| 1236 | |
| 1237 | static int |
| 1238 | idna_converter(PyObject *obj, struct maybe_idna *data) |
| 1239 | { |
| 1240 | size_t len; |
| 1241 | PyObject *obj2, *obj3; |
| 1242 | if (obj == NULL) { |
| 1243 | idna_cleanup(data); |
| 1244 | return 1; |
| 1245 | } |
| 1246 | data->obj = NULL; |
| 1247 | len = -1; |
| 1248 | if (PyBytes_Check(obj)) { |
| 1249 | data->buf = PyBytes_AsString(obj); |
| 1250 | len = PyBytes_Size(obj); |
| 1251 | } |
| 1252 | else if (PyByteArray_Check(obj)) { |
| 1253 | data->buf = PyByteArray_AsString(obj); |
| 1254 | len = PyByteArray_Size(obj); |
| 1255 | } |
| 1256 | else if (PyUnicode_Check(obj) && PyUnicode_READY(obj) == 0 && PyUnicode_IS_COMPACT_ASCII(obj)) { |
| 1257 | data->buf = PyUnicode_DATA(obj); |
| 1258 | len = PyUnicode_GET_LENGTH(obj); |
| 1259 | } |
| 1260 | else { |
| 1261 | obj2 = PyUnicode_FromObject(obj); |
| 1262 | if (!obj2) { |
| 1263 | PyErr_Format(PyExc_TypeError, "string or unicode text buffer expected, not %s", |
| 1264 | obj->ob_type->tp_name); |
| 1265 | return 0; |
| 1266 | } |
| 1267 | obj3 = PyUnicode_AsEncodedString(obj2, "idna", NULL); |
| 1268 | Py_DECREF(obj2); |
| 1269 | if (!obj3) { |
| 1270 | PyErr_SetString(PyExc_TypeError, "encoding of hostname failed"); |
| 1271 | return 0; |
| 1272 | } |
| 1273 | if (!PyBytes_Check(obj3)) { |
Martin v. Löwis | 17fd1e1 | 2014-08-05 16:13:50 +0200 | [diff] [blame] | 1274 | Py_DECREF(obj3); |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1275 | PyErr_SetString(PyExc_TypeError, "encoding of hostname failed to return bytes"); |
| 1276 | return 0; |
| 1277 | } |
| 1278 | data->obj = obj3; |
| 1279 | data->buf = PyBytes_AS_STRING(obj3); |
| 1280 | len = PyBytes_GET_SIZE(obj3); |
| 1281 | } |
| 1282 | if (strlen(data->buf) != len) { |
| 1283 | Py_CLEAR(data->obj); |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1284 | 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] | 1285 | return 0; |
| 1286 | } |
| 1287 | return Py_CLEANUP_SUPPORTED; |
Victor Stinner | 1a62a68 | 2014-08-17 19:33:28 +0200 | [diff] [blame] | 1288 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1289 | |
| 1290 | /* Parse a socket address argument according to the socket object's |
| 1291 | address family. Return 1 if the address was in the proper format, |
| 1292 | 0 of not. The address is returned through addr_ret, its length |
| 1293 | through len_ret. */ |
| 1294 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1295 | static int |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1296 | getsockaddrarg(PySocketSockObject *s, PyObject *args, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1297 | struct sockaddr *addr_ret, int *len_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1298 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1299 | switch (s->sock_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1300 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1301 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1302 | case AF_UNIX: |
| 1303 | { |
| 1304 | struct sockaddr_un* addr; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1305 | Py_buffer path; |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1306 | int retval = 0; |
| 1307 | |
| 1308 | /* PEP 383. Not using PyUnicode_FSConverter since we need to |
| 1309 | allow embedded nulls on Linux. */ |
| 1310 | if (PyUnicode_Check(args)) { |
| 1311 | if ((args = PyUnicode_EncodeFSDefault(args)) == NULL) |
| 1312 | return 0; |
| 1313 | } |
| 1314 | else |
| 1315 | Py_INCREF(args); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1316 | if (!PyArg_Parse(args, "y*", &path)) { |
| 1317 | Py_DECREF(args); |
| 1318 | return retval; |
| 1319 | } |
| 1320 | assert(path.len >= 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1322 | addr = (struct sockaddr_un*)addr_ret; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1323 | #ifdef linux |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1324 | if (path.len > 0 && *(const char *)path.buf == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1325 | /* Linux abstract namespace extension */ |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1326 | if ((size_t)path.len > sizeof addr->sun_path) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1327 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1328 | "AF_UNIX path too long"); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1329 | goto unix_out; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1330 | } |
| 1331 | } |
| 1332 | else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1333 | #endif /* linux */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1334 | { |
| 1335 | /* regular NULL-terminated string */ |
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 | } |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1341 | addr->sun_path[path.len] = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1342 | } |
| 1343 | addr->sun_family = s->sock_family; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1344 | memcpy(addr->sun_path, path.buf, path.len); |
| 1345 | *len_ret = path.len + offsetof(struct sockaddr_un, sun_path); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1346 | retval = 1; |
| 1347 | unix_out: |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1348 | PyBuffer_Release(&path); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1349 | Py_DECREF(args); |
| 1350 | return retval; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1351 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1352 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1353 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1354 | #if defined(AF_NETLINK) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1355 | case AF_NETLINK: |
| 1356 | { |
| 1357 | struct sockaddr_nl* addr; |
| 1358 | int pid, groups; |
| 1359 | addr = (struct sockaddr_nl *)addr_ret; |
| 1360 | if (!PyTuple_Check(args)) { |
| 1361 | PyErr_Format( |
| 1362 | PyExc_TypeError, |
| 1363 | "getsockaddrarg: " |
| 1364 | "AF_NETLINK address must be tuple, not %.500s", |
| 1365 | Py_TYPE(args)->tp_name); |
| 1366 | return 0; |
| 1367 | } |
| 1368 | if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups)) |
| 1369 | return 0; |
| 1370 | addr->nl_family = AF_NETLINK; |
| 1371 | addr->nl_pid = pid; |
| 1372 | addr->nl_groups = groups; |
| 1373 | *len_ret = sizeof(*addr); |
| 1374 | return 1; |
| 1375 | } |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1376 | #endif |
| 1377 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 1378 | #ifdef AF_RDS |
| 1379 | case AF_RDS: |
| 1380 | /* RDS sockets use sockaddr_in: fall-through */ |
| 1381 | #endif |
| 1382 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1383 | case AF_INET: |
| 1384 | { |
| 1385 | struct sockaddr_in* addr; |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1386 | struct maybe_idna host = {NULL, NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1387 | int port, result; |
| 1388 | if (!PyTuple_Check(args)) { |
| 1389 | PyErr_Format( |
| 1390 | PyExc_TypeError, |
| 1391 | "getsockaddrarg: " |
| 1392 | "AF_INET address must be tuple, not %.500s", |
| 1393 | Py_TYPE(args)->tp_name); |
| 1394 | return 0; |
| 1395 | } |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1396 | if (!PyArg_ParseTuple(args, "O&i:getsockaddrarg", |
| 1397 | idna_converter, &host, &port)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1398 | return 0; |
| 1399 | addr=(struct sockaddr_in*)addr_ret; |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1400 | result = setipaddr(host.buf, (struct sockaddr *)addr, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1401 | sizeof(*addr), AF_INET); |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1402 | idna_cleanup(&host); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1403 | if (result < 0) |
| 1404 | return 0; |
| 1405 | if (port < 0 || port > 0xffff) { |
| 1406 | PyErr_SetString( |
| 1407 | PyExc_OverflowError, |
| 1408 | "getsockaddrarg: port must be 0-65535."); |
| 1409 | return 0; |
| 1410 | } |
| 1411 | addr->sin_family = AF_INET; |
| 1412 | addr->sin_port = htons((short)port); |
| 1413 | *len_ret = sizeof *addr; |
| 1414 | return 1; |
| 1415 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1416 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1417 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1418 | case AF_INET6: |
| 1419 | { |
| 1420 | struct sockaddr_in6* addr; |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1421 | struct maybe_idna host = {NULL, NULL}; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1422 | int port, result; |
| 1423 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1424 | flowinfo = scope_id = 0; |
| 1425 | if (!PyTuple_Check(args)) { |
| 1426 | PyErr_Format( |
| 1427 | PyExc_TypeError, |
| 1428 | "getsockaddrarg: " |
| 1429 | "AF_INET6 address must be tuple, not %.500s", |
| 1430 | Py_TYPE(args)->tp_name); |
| 1431 | return 0; |
| 1432 | } |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1433 | if (!PyArg_ParseTuple(args, "O&i|II", |
| 1434 | idna_converter, &host, &port, &flowinfo, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1435 | &scope_id)) { |
| 1436 | return 0; |
| 1437 | } |
| 1438 | addr = (struct sockaddr_in6*)addr_ret; |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1439 | result = setipaddr(host.buf, (struct sockaddr *)addr, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1440 | sizeof(*addr), AF_INET6); |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1441 | idna_cleanup(&host); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1442 | if (result < 0) |
| 1443 | return 0; |
| 1444 | if (port < 0 || port > 0xffff) { |
| 1445 | PyErr_SetString( |
| 1446 | PyExc_OverflowError, |
| 1447 | "getsockaddrarg: port must be 0-65535."); |
| 1448 | return 0; |
| 1449 | } |
Charles-François Natali | 9b0c006 | 2012-06-23 10:17:05 +0200 | [diff] [blame] | 1450 | if (flowinfo > 0xfffff) { |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1451 | PyErr_SetString( |
| 1452 | PyExc_OverflowError, |
| 1453 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 1454 | return 0; |
| 1455 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1456 | addr->sin6_family = s->sock_family; |
| 1457 | addr->sin6_port = htons((short)port); |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1458 | addr->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1459 | addr->sin6_scope_id = scope_id; |
| 1460 | *len_ret = sizeof *addr; |
| 1461 | return 1; |
| 1462 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1463 | #endif |
| 1464 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1465 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1466 | case AF_BLUETOOTH: |
| 1467 | { |
| 1468 | switch (s->sock_proto) { |
| 1469 | case BTPROTO_L2CAP: |
| 1470 | { |
| 1471 | struct sockaddr_l2 *addr; |
| 1472 | char *straddr; |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1474 | addr = (struct sockaddr_l2 *)addr_ret; |
| 1475 | memset(addr, 0, sizeof(struct sockaddr_l2)); |
| 1476 | _BT_L2_MEMB(addr, family) = AF_BLUETOOTH; |
| 1477 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1478 | &_BT_L2_MEMB(addr, psm))) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1479 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1480 | "wrong format"); |
| 1481 | return 0; |
| 1482 | } |
| 1483 | if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0) |
| 1484 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1485 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1486 | *len_ret = sizeof *addr; |
| 1487 | return 1; |
| 1488 | } |
| 1489 | case BTPROTO_RFCOMM: |
| 1490 | { |
| 1491 | struct sockaddr_rc *addr; |
| 1492 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1494 | addr = (struct sockaddr_rc *)addr_ret; |
| 1495 | _BT_RC_MEMB(addr, family) = AF_BLUETOOTH; |
| 1496 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1497 | &_BT_RC_MEMB(addr, channel))) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1498 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1499 | "wrong format"); |
| 1500 | return 0; |
| 1501 | } |
| 1502 | if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0) |
| 1503 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1505 | *len_ret = sizeof *addr; |
| 1506 | return 1; |
| 1507 | } |
| 1508 | case BTPROTO_HCI: |
| 1509 | { |
| 1510 | struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1511 | #if defined(__NetBSD__) || defined(__DragonFly__) |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1512 | char *straddr = PyBytes_AS_STRING(args); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1513 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1514 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1515 | if (straddr == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1516 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1517 | "wrong format"); |
| 1518 | return 0; |
| 1519 | } |
| 1520 | if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0) |
| 1521 | return 0; |
| 1522 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1523 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
| 1524 | if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1525 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1526 | "wrong format"); |
| 1527 | return 0; |
| 1528 | } |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1529 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1530 | *len_ret = sizeof *addr; |
| 1531 | return 1; |
| 1532 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1533 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1534 | case BTPROTO_SCO: |
| 1535 | { |
| 1536 | struct sockaddr_sco *addr; |
| 1537 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1538 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1539 | addr = (struct sockaddr_sco *)addr_ret; |
| 1540 | _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; |
| 1541 | if (!PyBytes_Check(args)) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1542 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1543 | "wrong format"); |
| 1544 | return 0; |
| 1545 | } |
| 1546 | straddr = PyBytes_AS_STRING(args); |
| 1547 | if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0) |
| 1548 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1549 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1550 | *len_ret = sizeof *addr; |
| 1551 | return 1; |
| 1552 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1553 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1554 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1555 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: unknown Bluetooth protocol"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1556 | return 0; |
| 1557 | } |
| 1558 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1559 | #endif |
| 1560 | |
Antoine Pitrou | b156a46 | 2010-10-27 20:13:57 +0000 | [diff] [blame] | 1561 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1562 | case AF_PACKET: |
| 1563 | { |
| 1564 | struct sockaddr_ll* addr; |
| 1565 | struct ifreq ifr; |
| 1566 | char *interfaceName; |
| 1567 | int protoNumber; |
| 1568 | int hatype = 0; |
| 1569 | int pkttype = 0; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1570 | Py_buffer haddr = {NULL, NULL}; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1571 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1572 | if (!PyTuple_Check(args)) { |
| 1573 | PyErr_Format( |
| 1574 | PyExc_TypeError, |
| 1575 | "getsockaddrarg: " |
| 1576 | "AF_PACKET address must be tuple, not %.500s", |
| 1577 | Py_TYPE(args)->tp_name); |
| 1578 | return 0; |
| 1579 | } |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1580 | if (!PyArg_ParseTuple(args, "si|iiy*", &interfaceName, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1581 | &protoNumber, &pkttype, &hatype, |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1582 | &haddr)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1583 | return 0; |
| 1584 | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name)); |
| 1585 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
| 1586 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
| 1587 | s->errorhandler(); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1588 | PyBuffer_Release(&haddr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1589 | return 0; |
| 1590 | } |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1591 | if (haddr.buf && haddr.len > 8) { |
| 1592 | PyErr_SetString(PyExc_ValueError, |
| 1593 | "Hardware address must be 8 bytes or less"); |
| 1594 | PyBuffer_Release(&haddr); |
| 1595 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1596 | } |
| 1597 | if (protoNumber < 0 || protoNumber > 0xffff) { |
| 1598 | PyErr_SetString( |
| 1599 | PyExc_OverflowError, |
| 1600 | "getsockaddrarg: protoNumber must be 0-65535."); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1601 | PyBuffer_Release(&haddr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1602 | return 0; |
| 1603 | } |
| 1604 | addr = (struct sockaddr_ll*)addr_ret; |
| 1605 | addr->sll_family = AF_PACKET; |
| 1606 | addr->sll_protocol = htons((short)protoNumber); |
| 1607 | addr->sll_ifindex = ifr.ifr_ifindex; |
| 1608 | addr->sll_pkttype = pkttype; |
| 1609 | addr->sll_hatype = hatype; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1610 | if (haddr.buf) { |
| 1611 | memcpy(&addr->sll_addr, haddr.buf, haddr.len); |
| 1612 | addr->sll_halen = haddr.len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1613 | } |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1614 | else |
| 1615 | addr->sll_halen = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1616 | *len_ret = sizeof *addr; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1617 | PyBuffer_Release(&haddr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1618 | return 1; |
| 1619 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1620 | #endif |
| 1621 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1622 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1623 | case AF_TIPC: |
| 1624 | { |
| 1625 | unsigned int atype, v1, v2, v3; |
| 1626 | unsigned int scope = TIPC_CLUSTER_SCOPE; |
| 1627 | struct sockaddr_tipc *addr; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1629 | if (!PyTuple_Check(args)) { |
| 1630 | PyErr_Format( |
| 1631 | PyExc_TypeError, |
| 1632 | "getsockaddrarg: " |
| 1633 | "AF_TIPC address must be tuple, not %.500s", |
| 1634 | Py_TYPE(args)->tp_name); |
| 1635 | return 0; |
| 1636 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1637 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1638 | if (!PyArg_ParseTuple(args, |
| 1639 | "IIII|I;Invalid TIPC address format", |
| 1640 | &atype, &v1, &v2, &v3, &scope)) |
| 1641 | return 0; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1642 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1643 | addr = (struct sockaddr_tipc *) addr_ret; |
| 1644 | memset(addr, 0, sizeof(struct sockaddr_tipc)); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1645 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1646 | addr->family = AF_TIPC; |
| 1647 | addr->scope = scope; |
| 1648 | addr->addrtype = atype; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1649 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1650 | if (atype == TIPC_ADDR_NAMESEQ) { |
| 1651 | addr->addr.nameseq.type = v1; |
| 1652 | addr->addr.nameseq.lower = v2; |
| 1653 | addr->addr.nameseq.upper = v3; |
| 1654 | } else if (atype == TIPC_ADDR_NAME) { |
| 1655 | addr->addr.name.name.type = v1; |
| 1656 | addr->addr.name.name.instance = v2; |
| 1657 | } else if (atype == TIPC_ADDR_ID) { |
| 1658 | addr->addr.id.node = v1; |
| 1659 | addr->addr.id.ref = v2; |
| 1660 | } else { |
| 1661 | /* Shouldn't happen */ |
| 1662 | PyErr_SetString(PyExc_TypeError, "Invalid address type"); |
| 1663 | return 0; |
| 1664 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1665 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1666 | *len_ret = sizeof(*addr); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1667 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1668 | return 1; |
| 1669 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1670 | #endif |
| 1671 | |
Vinay Sajip | ed6783f | 2014-03-21 11:44:32 +0000 | [diff] [blame] | 1672 | #if defined(AF_CAN) && defined(CAN_RAW) && defined(CAN_BCM) |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1673 | case AF_CAN: |
| 1674 | switch (s->sock_proto) { |
| 1675 | case CAN_RAW: |
Charles-François Natali | 773e42d | 2013-02-05 19:42:01 +0100 | [diff] [blame] | 1676 | /* fall-through */ |
| 1677 | case CAN_BCM: |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1678 | { |
| 1679 | struct sockaddr_can *addr; |
| 1680 | PyObject *interfaceName; |
| 1681 | struct ifreq ifr; |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1682 | Py_ssize_t len; |
| 1683 | |
Benjamin Peterson | 18b7191 | 2013-05-16 15:29:44 -0500 | [diff] [blame] | 1684 | addr = (struct sockaddr_can *)addr_ret; |
| 1685 | |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1686 | if (!PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter, |
| 1687 | &interfaceName)) |
| 1688 | return 0; |
| 1689 | |
| 1690 | len = PyBytes_GET_SIZE(interfaceName); |
| 1691 | |
| 1692 | if (len == 0) { |
| 1693 | ifr.ifr_ifindex = 0; |
Victor Stinner | 1a62a68 | 2014-08-17 19:33:28 +0200 | [diff] [blame] | 1694 | } else if ((size_t)len < sizeof(ifr.ifr_name)) { |
Christian Heimes | 15b6885 | 2012-09-10 01:25:50 +0200 | [diff] [blame] | 1695 | strncpy(ifr.ifr_name, PyBytes_AS_STRING(interfaceName), sizeof(ifr.ifr_name)); |
| 1696 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1697 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
| 1698 | s->errorhandler(); |
| 1699 | Py_DECREF(interfaceName); |
| 1700 | return 0; |
| 1701 | } |
| 1702 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1703 | PyErr_SetString(PyExc_OSError, |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1704 | "AF_CAN interface name too long"); |
| 1705 | Py_DECREF(interfaceName); |
| 1706 | return 0; |
| 1707 | } |
| 1708 | |
| 1709 | addr->can_family = AF_CAN; |
| 1710 | addr->can_ifindex = ifr.ifr_ifindex; |
| 1711 | |
| 1712 | *len_ret = sizeof(*addr); |
| 1713 | Py_DECREF(interfaceName); |
| 1714 | return 1; |
| 1715 | } |
| 1716 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1717 | PyErr_SetString(PyExc_OSError, |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1718 | "getsockaddrarg: unsupported CAN protocol"); |
| 1719 | return 0; |
| 1720 | } |
| 1721 | #endif |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1722 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1723 | #ifdef PF_SYSTEM |
| 1724 | case PF_SYSTEM: |
| 1725 | switch (s->sock_proto) { |
| 1726 | #ifdef SYSPROTO_CONTROL |
| 1727 | case SYSPROTO_CONTROL: |
| 1728 | { |
| 1729 | struct sockaddr_ctl *addr; |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1730 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1731 | addr = (struct sockaddr_ctl *)addr_ret; |
| 1732 | addr->sc_family = AF_SYSTEM; |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1733 | addr->ss_sysaddr = AF_SYS_CONTROL; |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1734 | |
| 1735 | if (PyUnicode_Check(args)) { |
| 1736 | struct ctl_info info; |
| 1737 | PyObject *ctl_name; |
| 1738 | |
| 1739 | if (!PyArg_Parse(args, "O&", |
| 1740 | PyUnicode_FSConverter, &ctl_name)) { |
| 1741 | return 0; |
| 1742 | } |
| 1743 | |
Victor Stinner | f50e187 | 2015-03-20 11:32:24 +0100 | [diff] [blame] | 1744 | 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] | 1745 | PyErr_SetString(PyExc_ValueError, |
| 1746 | "provided string is too long"); |
| 1747 | Py_DECREF(ctl_name); |
| 1748 | return 0; |
| 1749 | } |
| 1750 | strncpy(info.ctl_name, PyBytes_AS_STRING(ctl_name), |
| 1751 | sizeof(info.ctl_name)); |
| 1752 | Py_DECREF(ctl_name); |
| 1753 | |
| 1754 | if (ioctl(s->sock_fd, CTLIOCGINFO, &info)) { |
| 1755 | PyErr_SetString(PyExc_OSError, |
| 1756 | "cannot find kernel control with provided name"); |
| 1757 | return 0; |
| 1758 | } |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1759 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1760 | addr->sc_id = info.ctl_id; |
| 1761 | addr->sc_unit = 0; |
| 1762 | } else if (!PyArg_ParseTuple(args, "II", |
| 1763 | &(addr->sc_id), &(addr->sc_unit))) { |
| 1764 | PyErr_SetString(PyExc_TypeError, "getsockaddrarg: " |
| 1765 | "expected str or tuple of two ints"); |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1766 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 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 | *len_ret = sizeof(*addr); |
| 1771 | return 1; |
| 1772 | } |
| 1773 | #endif |
| 1774 | default: |
| 1775 | PyErr_SetString(PyExc_OSError, |
| 1776 | "getsockaddrarg: unsupported PF_SYSTEM protocol"); |
| 1777 | return 0; |
| 1778 | } |
| 1779 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1781 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1782 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1783 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1784 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: bad family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1785 | return 0; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1786 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1787 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1790 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1791 | /* 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] | 1792 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 1793 | through len_ret. */ |
| 1794 | |
| 1795 | static int |
Peter Schneider-Kamp | 618e25d | 2000-07-11 23:00:12 +0000 | [diff] [blame] | 1796 | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1797 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1798 | switch (s->sock_family) { |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1799 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1800 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1801 | case AF_UNIX: |
| 1802 | { |
| 1803 | *len_ret = sizeof (struct sockaddr_un); |
| 1804 | return 1; |
| 1805 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1806 | #endif /* AF_UNIX */ |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1807 | #if defined(AF_NETLINK) |
| 1808 | case AF_NETLINK: |
| 1809 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1810 | *len_ret = sizeof (struct sockaddr_nl); |
| 1811 | return 1; |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1812 | } |
| 1813 | #endif |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1814 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 1815 | #ifdef AF_RDS |
| 1816 | case AF_RDS: |
| 1817 | /* RDS sockets use sockaddr_in: fall-through */ |
| 1818 | #endif |
| 1819 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1820 | case AF_INET: |
| 1821 | { |
| 1822 | *len_ret = sizeof (struct sockaddr_in); |
| 1823 | return 1; |
| 1824 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1825 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1826 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1827 | case AF_INET6: |
| 1828 | { |
| 1829 | *len_ret = sizeof (struct sockaddr_in6); |
| 1830 | return 1; |
| 1831 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1832 | #endif |
| 1833 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1834 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1835 | case AF_BLUETOOTH: |
| 1836 | { |
| 1837 | switch(s->sock_proto) |
| 1838 | { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1839 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1840 | case BTPROTO_L2CAP: |
| 1841 | *len_ret = sizeof (struct sockaddr_l2); |
| 1842 | return 1; |
| 1843 | case BTPROTO_RFCOMM: |
| 1844 | *len_ret = sizeof (struct sockaddr_rc); |
| 1845 | return 1; |
| 1846 | case BTPROTO_HCI: |
| 1847 | *len_ret = sizeof (struct sockaddr_hci); |
| 1848 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1849 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1850 | case BTPROTO_SCO: |
| 1851 | *len_ret = sizeof (struct sockaddr_sco); |
| 1852 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1853 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1854 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1855 | PyErr_SetString(PyExc_OSError, "getsockaddrlen: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1856 | "unknown BT protocol"); |
| 1857 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1858 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1859 | } |
| 1860 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1861 | #endif |
| 1862 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1863 | #ifdef HAVE_NETPACKET_PACKET_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1864 | case AF_PACKET: |
| 1865 | { |
| 1866 | *len_ret = sizeof (struct sockaddr_ll); |
| 1867 | return 1; |
| 1868 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1869 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1870 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1871 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1872 | case AF_TIPC: |
| 1873 | { |
| 1874 | *len_ret = sizeof (struct sockaddr_tipc); |
| 1875 | return 1; |
| 1876 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1877 | #endif |
| 1878 | |
Vinay Sajip | ecfc98c | 2014-03-20 12:42:42 +0000 | [diff] [blame] | 1879 | #ifdef AF_CAN |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1880 | case AF_CAN: |
| 1881 | { |
| 1882 | *len_ret = sizeof (struct sockaddr_can); |
| 1883 | return 1; |
| 1884 | } |
| 1885 | #endif |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1886 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1887 | #ifdef PF_SYSTEM |
| 1888 | case PF_SYSTEM: |
| 1889 | switch(s->sock_proto) { |
| 1890 | #ifdef SYSPROTO_CONTROL |
| 1891 | case SYSPROTO_CONTROL: |
| 1892 | *len_ret = sizeof (struct sockaddr_ctl); |
| 1893 | return 1; |
| 1894 | #endif |
| 1895 | default: |
| 1896 | PyErr_SetString(PyExc_OSError, "getsockaddrlen: " |
| 1897 | "unknown PF_SYSTEM protocol"); |
| 1898 | return 0; |
| 1899 | } |
| 1900 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1901 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1902 | /* More cases here... */ |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1903 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1904 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1905 | PyErr_SetString(PyExc_OSError, "getsockaddrlen: bad family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1906 | return 0; |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1907 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1908 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 1912 | /* Support functions for the sendmsg() and recvmsg[_into]() methods. |
| 1913 | Currently, these methods are only compiled if the RFC 2292/3542 |
| 1914 | CMSG_LEN() macro is available. Older systems seem to have used |
| 1915 | sizeof(struct cmsghdr) + (length) where CMSG_LEN() is used now, so |
| 1916 | it may be possible to define CMSG_LEN() that way if it's not |
| 1917 | provided. Some architectures might need extra padding after the |
| 1918 | cmsghdr, however, and CMSG_LEN() would have to take account of |
| 1919 | this. */ |
| 1920 | #ifdef CMSG_LEN |
| 1921 | /* If length is in range, set *result to CMSG_LEN(length) and return |
| 1922 | true; otherwise, return false. */ |
| 1923 | static int |
| 1924 | get_CMSG_LEN(size_t length, size_t *result) |
| 1925 | { |
| 1926 | size_t tmp; |
| 1927 | |
| 1928 | if (length > (SOCKLEN_T_LIMIT - CMSG_LEN(0))) |
| 1929 | return 0; |
| 1930 | tmp = CMSG_LEN(length); |
| 1931 | if (tmp > SOCKLEN_T_LIMIT || tmp < length) |
| 1932 | return 0; |
| 1933 | *result = tmp; |
| 1934 | return 1; |
| 1935 | } |
| 1936 | |
| 1937 | #ifdef CMSG_SPACE |
| 1938 | /* If length is in range, set *result to CMSG_SPACE(length) and return |
| 1939 | true; otherwise, return false. */ |
| 1940 | static int |
| 1941 | get_CMSG_SPACE(size_t length, size_t *result) |
| 1942 | { |
| 1943 | size_t tmp; |
| 1944 | |
| 1945 | /* Use CMSG_SPACE(1) here in order to take account of the padding |
| 1946 | necessary before *and* after the data. */ |
| 1947 | if (length > (SOCKLEN_T_LIMIT - CMSG_SPACE(1))) |
| 1948 | return 0; |
| 1949 | tmp = CMSG_SPACE(length); |
| 1950 | if (tmp > SOCKLEN_T_LIMIT || tmp < length) |
| 1951 | return 0; |
| 1952 | *result = tmp; |
| 1953 | return 1; |
| 1954 | } |
| 1955 | #endif |
| 1956 | |
| 1957 | /* Return true iff msg->msg_controllen is valid, cmsgh is a valid |
| 1958 | pointer in msg->msg_control with at least "space" bytes after it, |
| 1959 | and its cmsg_len member inside the buffer. */ |
| 1960 | static int |
| 1961 | cmsg_min_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t space) |
| 1962 | { |
| 1963 | size_t cmsg_offset; |
| 1964 | static const size_t cmsg_len_end = (offsetof(struct cmsghdr, cmsg_len) + |
| 1965 | sizeof(cmsgh->cmsg_len)); |
| 1966 | |
Charles-François Natali | 466517d | 2011-08-28 18:23:43 +0200 | [diff] [blame] | 1967 | /* Note that POSIX allows msg_controllen to be of signed type. */ |
Brett Cannon | b05cbe6 | 2014-01-07 17:01:01 -0500 | [diff] [blame] | 1968 | if (cmsgh == NULL || msg->msg_control == NULL) |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 1969 | return 0; |
Brett Cannon | b05cbe6 | 2014-01-07 17:01:01 -0500 | [diff] [blame] | 1970 | /* Note that POSIX allows msg_controllen to be of a signed type. This is |
| 1971 | annoying under OS X as it's unsigned there and so it triggers a |
| 1972 | tautological comparison warning under Clang when compared against 0. |
| 1973 | Since the check is valid on other platforms, silence the warning under |
| 1974 | Clang. */ |
| 1975 | #ifdef __clang__ |
| 1976 | #pragma clang diagnostic push |
| 1977 | #pragma clang diagnostic ignored "-Wtautological-compare" |
| 1978 | #endif |
Serhiy Storchaka | caa01f8 | 2015-03-05 23:42:24 +0200 | [diff] [blame] | 1979 | #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))) |
Serhiy Storchaka | b48af34 | 2015-02-26 15:27:57 +0200 | [diff] [blame] | 1980 | #pragma GCC diagnostic push |
| 1981 | #pragma GCC diagnostic ignored "-Wtype-limits" |
| 1982 | #endif |
Brett Cannon | b05cbe6 | 2014-01-07 17:01:01 -0500 | [diff] [blame] | 1983 | if (msg->msg_controllen < 0) |
| 1984 | return 0; |
Serhiy Storchaka | caa01f8 | 2015-03-05 23:42:24 +0200 | [diff] [blame] | 1985 | #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))) |
Serhiy Storchaka | b48af34 | 2015-02-26 15:27:57 +0200 | [diff] [blame] | 1986 | #pragma GCC diagnostic pop |
| 1987 | #endif |
Brett Cannon | b05cbe6 | 2014-01-07 17:01:01 -0500 | [diff] [blame] | 1988 | #ifdef __clang__ |
| 1989 | #pragma clang diagnostic pop |
| 1990 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 1991 | if (space < cmsg_len_end) |
| 1992 | space = cmsg_len_end; |
| 1993 | cmsg_offset = (char *)cmsgh - (char *)msg->msg_control; |
| 1994 | return (cmsg_offset <= (size_t)-1 - space && |
| 1995 | cmsg_offset + space <= msg->msg_controllen); |
| 1996 | } |
| 1997 | |
| 1998 | /* If pointer CMSG_DATA(cmsgh) is in buffer msg->msg_control, set |
| 1999 | *space to number of bytes following it in the buffer and return |
| 2000 | true; otherwise, return false. Assumes cmsgh, msg->msg_control and |
| 2001 | msg->msg_controllen are valid. */ |
| 2002 | static int |
| 2003 | get_cmsg_data_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *space) |
| 2004 | { |
| 2005 | size_t data_offset; |
| 2006 | char *data_ptr; |
| 2007 | |
| 2008 | if ((data_ptr = (char *)CMSG_DATA(cmsgh)) == NULL) |
| 2009 | return 0; |
| 2010 | data_offset = data_ptr - (char *)msg->msg_control; |
| 2011 | if (data_offset > msg->msg_controllen) |
| 2012 | return 0; |
| 2013 | *space = msg->msg_controllen - data_offset; |
| 2014 | return 1; |
| 2015 | } |
| 2016 | |
| 2017 | /* If cmsgh is invalid or not contained in the buffer pointed to by |
| 2018 | msg->msg_control, return -1. If cmsgh is valid and its associated |
| 2019 | data is entirely contained in the buffer, set *data_len to the |
| 2020 | length of the associated data and return 0. If only part of the |
| 2021 | associated data is contained in the buffer but cmsgh is otherwise |
| 2022 | valid, set *data_len to the length contained in the buffer and |
| 2023 | return 1. */ |
| 2024 | static int |
| 2025 | get_cmsg_data_len(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *data_len) |
| 2026 | { |
| 2027 | size_t space, cmsg_data_len; |
| 2028 | |
| 2029 | if (!cmsg_min_space(msg, cmsgh, CMSG_LEN(0)) || |
| 2030 | cmsgh->cmsg_len < CMSG_LEN(0)) |
| 2031 | return -1; |
| 2032 | cmsg_data_len = cmsgh->cmsg_len - CMSG_LEN(0); |
| 2033 | if (!get_cmsg_data_space(msg, cmsgh, &space)) |
| 2034 | return -1; |
| 2035 | if (space >= cmsg_data_len) { |
| 2036 | *data_len = cmsg_data_len; |
| 2037 | return 0; |
| 2038 | } |
| 2039 | *data_len = space; |
| 2040 | return 1; |
| 2041 | } |
| 2042 | #endif /* CMSG_LEN */ |
| 2043 | |
| 2044 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2045 | /* s._accept() -> (fd, address) */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2046 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2047 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2048 | sock_accept(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2049 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2050 | sock_addr_t addrbuf; |
| 2051 | SOCKET_T newfd = INVALID_SOCKET; |
| 2052 | socklen_t addrlen; |
| 2053 | PyObject *sock = NULL; |
| 2054 | PyObject *addr = NULL; |
| 2055 | PyObject *res = NULL; |
| 2056 | int timeout; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2057 | int async_err = 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2058 | #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) |
| 2059 | /* accept4() is available on Linux 2.6.28+ and glibc 2.10 */ |
| 2060 | static int accept4_works = -1; |
| 2061 | #endif |
| 2062 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2063 | if (!getsockaddrlen(s, &addrlen)) |
| 2064 | return NULL; |
| 2065 | memset(&addrbuf, 0, addrlen); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2066 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2067 | if (!IS_SELECTABLE(s)) |
| 2068 | return select_error(); |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2069 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2070 | BEGIN_SELECT_LOOP(s) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2071 | do { |
Victor Stinner | 391fa71 | 2015-03-31 13:15:31 +0200 | [diff] [blame^] | 2072 | timeout = internal_select(s, 0, interval); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2073 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2074 | if (!timeout) { |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2075 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2076 | #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2077 | if (accept4_works != 0) { |
| 2078 | newfd = accept4(s->sock_fd, SAS2SA(&addrbuf), &addrlen, |
| 2079 | SOCK_CLOEXEC); |
| 2080 | if (newfd == INVALID_SOCKET && accept4_works == -1) { |
| 2081 | /* On Linux older than 2.6.28, accept4() fails with ENOSYS */ |
| 2082 | accept4_works = (errno != ENOSYS); |
| 2083 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2084 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2085 | if (accept4_works == 0) |
| 2086 | newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2087 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2088 | newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2089 | #endif |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2090 | Py_END_ALLOW_THREADS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2091 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2092 | } while (newfd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2093 | if (timeout == 1) { |
| 2094 | PyErr_SetString(socket_timeout, "timed out"); |
| 2095 | return NULL; |
| 2096 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2097 | END_SELECT_LOOP(s) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2098 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2099 | if (newfd == INVALID_SOCKET) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2100 | return (!async_err) ? s->errorhandler() : NULL; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2101 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2102 | #ifdef MS_WINDOWS |
| 2103 | if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) { |
| 2104 | PyErr_SetFromWindowsErr(0); |
| 2105 | SOCKETCLOSE(newfd); |
| 2106 | goto finally; |
| 2107 | } |
| 2108 | #else |
| 2109 | |
| 2110 | #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) |
| 2111 | if (!accept4_works) |
| 2112 | #endif |
| 2113 | { |
| 2114 | if (_Py_set_inheritable(newfd, 0, NULL) < 0) { |
| 2115 | SOCKETCLOSE(newfd); |
| 2116 | goto finally; |
| 2117 | } |
| 2118 | } |
| 2119 | #endif |
| 2120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2121 | sock = PyLong_FromSocket_t(newfd); |
| 2122 | if (sock == NULL) { |
| 2123 | SOCKETCLOSE(newfd); |
| 2124 | goto finally; |
| 2125 | } |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2126 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2127 | addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 2128 | addrlen, s->sock_proto); |
| 2129 | if (addr == NULL) |
| 2130 | goto finally; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2132 | res = PyTuple_Pack(2, sock, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2133 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2134 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2135 | Py_XDECREF(sock); |
| 2136 | Py_XDECREF(addr); |
| 2137 | return res; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2140 | PyDoc_STRVAR(accept_doc, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2141 | "_accept() -> (integer, address info)\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2142 | \n\ |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2143 | Wait for an incoming connection. Return a new socket file descriptor\n\ |
| 2144 | representing the connection, and the address of the client.\n\ |
| 2145 | For IP sockets, the address info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2146 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2147 | /* s.setblocking(flag) method. Argument: |
| 2148 | False -- non-blocking mode; same as settimeout(0) |
| 2149 | True -- blocking mode; same as settimeout(None) |
| 2150 | */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2151 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2152 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2153 | sock_setblocking(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2154 | { |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 2155 | long block; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2156 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2157 | block = PyLong_AsLong(arg); |
| 2158 | if (block == -1 && PyErr_Occurred()) |
| 2159 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2160 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2161 | s->sock_timeout = block ? -1 : 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2162 | internal_setblocking(s, block); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2164 | Py_INCREF(Py_None); |
| 2165 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2166 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2167 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2168 | PyDoc_STRVAR(setblocking_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2169 | "setblocking(flag)\n\ |
| 2170 | \n\ |
| 2171 | 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] | 2172 | setblocking(True) is equivalent to settimeout(None);\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2173 | setblocking(False) is equivalent to settimeout(0.0)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2174 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2175 | static int |
| 2176 | socket_parse_timeout(_PyTime_t *timeout, PyObject *timeout_obj) |
| 2177 | { |
| 2178 | #ifdef MS_WINDOWS |
| 2179 | struct timeval tv; |
| 2180 | #endif |
| 2181 | int overflow = 0; |
| 2182 | |
| 2183 | if (timeout_obj == Py_None) { |
| 2184 | *timeout = -1; |
| 2185 | return 0; |
| 2186 | } |
| 2187 | |
Victor Stinner | 869e177 | 2015-03-30 03:49:14 +0200 | [diff] [blame] | 2188 | if (_PyTime_FromSecondsObject(timeout, |
| 2189 | timeout_obj, _PyTime_ROUND_CEILING) < 0) |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2190 | return -1; |
| 2191 | |
| 2192 | if (*timeout < 0) { |
| 2193 | PyErr_SetString(PyExc_ValueError, "Timeout value out of range"); |
| 2194 | return -1; |
| 2195 | } |
| 2196 | |
| 2197 | #ifdef MS_WINDOWS |
Victor Stinner | 869e177 | 2015-03-30 03:49:14 +0200 | [diff] [blame] | 2198 | overflow = (_PyTime_AsTimeval(timeout, &tv, _PyTime_ROUND_CEILING) < 0); |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2199 | #endif |
| 2200 | #ifndef HAVE_POLL |
Victor Stinner | 869e177 | 2015-03-30 03:49:14 +0200 | [diff] [blame] | 2201 | timeout = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING); |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2202 | overflow = (timeout > INT_MAX); |
| 2203 | #endif |
| 2204 | if (overflow) { |
| 2205 | PyErr_SetString(PyExc_OverflowError, |
| 2206 | "timeout doesn't fit into C timeval"); |
| 2207 | return -1; |
| 2208 | } |
| 2209 | |
| 2210 | return 0; |
| 2211 | } |
| 2212 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2213 | /* s.settimeout(timeout) method. Argument: |
| 2214 | None -- no timeout, blocking mode; same as setblocking(True) |
| 2215 | 0.0 -- non-blocking mode; same as setblocking(False) |
| 2216 | > 0 -- timeout mode; operations time out after timeout seconds |
| 2217 | < 0 -- illegal; raises an exception |
| 2218 | */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2219 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2220 | sock_settimeout(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2221 | { |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2222 | _PyTime_t timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2223 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2224 | if (socket_parse_timeout(&timeout, arg) < 0) |
| 2225 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2227 | s->sock_timeout = timeout; |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2228 | internal_setblocking(s, timeout < 0); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2230 | Py_INCREF(Py_None); |
| 2231 | return Py_None; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2234 | PyDoc_STRVAR(settimeout_doc, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2235 | "settimeout(timeout)\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2236 | \n\ |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2237 | Set a timeout on socket operations. 'timeout' can be a float,\n\ |
| 2238 | giving in seconds, or None. Setting a timeout of None disables\n\ |
| 2239 | the timeout feature and is equivalent to setblocking(1).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2240 | Setting a timeout of zero is the same as setblocking(0)."); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2241 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2242 | /* s.gettimeout() method. |
| 2243 | Returns the timeout associated with a socket. */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2244 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2245 | sock_gettimeout(PySocketSockObject *s) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2246 | { |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2247 | if (s->sock_timeout < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2248 | Py_INCREF(Py_None); |
| 2249 | return Py_None; |
| 2250 | } |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2251 | else { |
| 2252 | double seconds = _PyTime_AsSecondsDouble(s->sock_timeout); |
| 2253 | return PyFloat_FromDouble(seconds); |
| 2254 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2257 | PyDoc_STRVAR(gettimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2258 | "gettimeout() -> timeout\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2259 | \n\ |
Ezio Melotti | 388c945 | 2011-08-14 08:28:57 +0300 | [diff] [blame] | 2260 | Returns the timeout in seconds (float) associated with socket \n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2261 | 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] | 2262 | operations are disabled."); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2263 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 2264 | /* s.setsockopt() method. |
| 2265 | With an integer third argument, sets an integer option. |
| 2266 | With a string third argument, sets an option from a buffer; |
| 2267 | use optional built-in module 'struct' to encode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2268 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2269 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2270 | sock_setsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2271 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2272 | int level; |
| 2273 | int optname; |
| 2274 | int res; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 2275 | Py_buffer optval; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2276 | int flag; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2278 | if (PyArg_ParseTuple(args, "iii:setsockopt", |
| 2279 | &level, &optname, &flag)) { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 2280 | res = setsockopt(s->sock_fd, level, optname, &flag, sizeof flag); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2281 | } |
| 2282 | else { |
| 2283 | PyErr_Clear(); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 2284 | if (!PyArg_ParseTuple(args, "iiy*:setsockopt", |
| 2285 | &level, &optname, &optval)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2286 | return NULL; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 2287 | res = setsockopt(s->sock_fd, level, optname, optval.buf, optval.len); |
| 2288 | PyBuffer_Release(&optval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2289 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2290 | if (res < 0) |
| 2291 | return s->errorhandler(); |
| 2292 | Py_INCREF(Py_None); |
| 2293 | return Py_None; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2296 | PyDoc_STRVAR(setsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2297 | "setsockopt(level, option, value)\n\ |
| 2298 | \n\ |
| 2299 | 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] | 2300 | The value argument can either be an integer or a string."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2301 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2302 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 2303 | /* s.getsockopt() method. |
| 2304 | With two arguments, retrieves an integer option. |
| 2305 | With a third integer argument, retrieves a string buffer of that size; |
| 2306 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2307 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2308 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2309 | sock_getsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2310 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2311 | int level; |
| 2312 | int optname; |
| 2313 | int res; |
| 2314 | PyObject *buf; |
| 2315 | socklen_t buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2316 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2317 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt", |
| 2318 | &level, &optname, &buflen)) |
| 2319 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2321 | if (buflen == 0) { |
| 2322 | int flag = 0; |
| 2323 | socklen_t flagsize = sizeof flag; |
| 2324 | res = getsockopt(s->sock_fd, level, optname, |
| 2325 | (void *)&flag, &flagsize); |
| 2326 | if (res < 0) |
| 2327 | return s->errorhandler(); |
| 2328 | return PyLong_FromLong(flag); |
| 2329 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2330 | if (buflen <= 0 || buflen > 1024) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2331 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2332 | "getsockopt buflen out of range"); |
| 2333 | return NULL; |
| 2334 | } |
| 2335 | buf = PyBytes_FromStringAndSize((char *)NULL, buflen); |
| 2336 | if (buf == NULL) |
| 2337 | return NULL; |
| 2338 | res = getsockopt(s->sock_fd, level, optname, |
| 2339 | (void *)PyBytes_AS_STRING(buf), &buflen); |
| 2340 | if (res < 0) { |
| 2341 | Py_DECREF(buf); |
| 2342 | return s->errorhandler(); |
| 2343 | } |
| 2344 | _PyBytes_Resize(&buf, buflen); |
| 2345 | return buf; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2348 | PyDoc_STRVAR(getsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2349 | "getsockopt(level, option[, buffersize]) -> value\n\ |
| 2350 | \n\ |
| 2351 | Get a socket option. See the Unix manual for level and option.\n\ |
| 2352 | 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] | 2353 | string of that length; otherwise it is an integer."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2354 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2355 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2356 | /* s.bind(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2357 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2358 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2359 | sock_bind(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2360 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2361 | sock_addr_t addrbuf; |
| 2362 | int addrlen; |
| 2363 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2364 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2365 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2366 | return NULL; |
| 2367 | Py_BEGIN_ALLOW_THREADS |
| 2368 | res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen); |
| 2369 | Py_END_ALLOW_THREADS |
| 2370 | if (res < 0) |
| 2371 | return s->errorhandler(); |
| 2372 | Py_INCREF(Py_None); |
| 2373 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2374 | } |
| 2375 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2376 | PyDoc_STRVAR(bind_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2377 | "bind(address)\n\ |
| 2378 | \n\ |
| 2379 | 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] | 2380 | 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] | 2381 | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2382 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2383 | |
| 2384 | /* s.close() method. |
| 2385 | Set the file descriptor to -1 so operations tried subsequently |
| 2386 | will surely fail. */ |
| 2387 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2388 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2389 | sock_close(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2390 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2391 | SOCKET_T fd; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2392 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2393 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 2394 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 2395 | * for more details. |
| 2396 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2397 | if ((fd = s->sock_fd) != -1) { |
| 2398 | s->sock_fd = -1; |
| 2399 | Py_BEGIN_ALLOW_THREADS |
| 2400 | (void) SOCKETCLOSE(fd); |
| 2401 | Py_END_ALLOW_THREADS |
| 2402 | } |
| 2403 | Py_INCREF(Py_None); |
| 2404 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2405 | } |
| 2406 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2407 | PyDoc_STRVAR(close_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2408 | "close()\n\ |
| 2409 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2410 | Close the socket. It cannot be used after this call."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2411 | |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2412 | static PyObject * |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2413 | sock_detach(PySocketSockObject *s) |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2414 | { |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2415 | SOCKET_T fd = s->sock_fd; |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2416 | s->sock_fd = -1; |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2417 | return PyLong_FromSocket_t(fd); |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2418 | } |
| 2419 | |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2420 | PyDoc_STRVAR(detach_doc, |
| 2421 | "detach()\n\ |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2422 | \n\ |
Guido van Rossum | 638aebd | 2013-12-04 11:50:09 -0800 | [diff] [blame] | 2423 | Close the socket object without closing the underlying file descriptor.\n\ |
| 2424 | 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] | 2425 | can be reused for other purposes. The file descriptor is returned."); |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2426 | |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2427 | static int |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2428 | internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2429 | int *timeoutp) |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2430 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2431 | int res, timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2432 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2433 | timeout = 0; |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2434 | |
| 2435 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2436 | res = connect(s->sock_fd, addr, addrlen); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2437 | Py_END_ALLOW_THREADS |
| 2438 | |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2439 | #ifdef MS_WINDOWS |
| 2440 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2441 | if (s->sock_timeout > 0 |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2442 | && res < 0 && WSAGetLastError() == WSAEWOULDBLOCK |
| 2443 | && IS_SELECTABLE(s)) { |
| 2444 | /* This is a mess. Best solution: trust select */ |
| 2445 | fd_set fds; |
| 2446 | fd_set fds_exc; |
| 2447 | struct timeval tv; |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2448 | int conv; |
| 2449 | |
Victor Stinner | 869e177 | 2015-03-30 03:49:14 +0200 | [diff] [blame] | 2450 | _PyTime_AsTimeval_noraise(s->sock_timeout, &tv, _PyTime_ROUND_CEILING); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2451 | |
| 2452 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2453 | FD_ZERO(&fds); |
| 2454 | FD_SET(s->sock_fd, &fds); |
| 2455 | FD_ZERO(&fds_exc); |
| 2456 | FD_SET(s->sock_fd, &fds_exc); |
| 2457 | res = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 2458 | NULL, &fds, &fds_exc, &tv); |
| 2459 | Py_END_ALLOW_THREADS |
| 2460 | |
| 2461 | if (res == 0) { |
| 2462 | res = WSAEWOULDBLOCK; |
| 2463 | timeout = 1; |
| 2464 | } else if (res > 0) { |
| 2465 | if (FD_ISSET(s->sock_fd, &fds)) |
| 2466 | /* The socket is in the writable set - this |
| 2467 | means connected */ |
| 2468 | res = 0; |
| 2469 | else { |
| 2470 | /* As per MS docs, we need to call getsockopt() |
| 2471 | to get the underlying error */ |
| 2472 | int res_size = sizeof res; |
| 2473 | /* It must be in the exception set */ |
| 2474 | assert(FD_ISSET(s->sock_fd, &fds_exc)); |
| 2475 | if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR, |
| 2476 | (char *)&res, &res_size)) |
| 2477 | /* getsockopt also clears WSAGetLastError, |
| 2478 | so reset it back. */ |
| 2479 | WSASetLastError(res); |
| 2480 | else |
| 2481 | res = WSAGetLastError(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2482 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2483 | } |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2484 | /* else if (res < 0) an error occurred */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2485 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2487 | if (res < 0) |
| 2488 | res = WSAGetLastError(); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2489 | |
| 2490 | #else |
| 2491 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2492 | if (s->sock_timeout > 0 |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2493 | && res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) { |
| 2494 | |
Victor Stinner | 391fa71 | 2015-03-31 13:15:31 +0200 | [diff] [blame^] | 2495 | timeout = internal_select(s, 1, s->sock_timeout); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2496 | |
| 2497 | if (timeout == 0) { |
| 2498 | /* Bug #1019808: in case of an EINPROGRESS, |
| 2499 | use getsockopt(SO_ERROR) to get the real |
| 2500 | error. */ |
| 2501 | socklen_t res_size = sizeof res; |
| 2502 | (void)getsockopt(s->sock_fd, SOL_SOCKET, |
| 2503 | SO_ERROR, &res, &res_size); |
| 2504 | if (res == EISCONN) |
| 2505 | res = 0; |
| 2506 | errno = res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2507 | } |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2508 | else if (timeout == -1) { |
| 2509 | res = errno; /* had error */ |
| 2510 | } |
| 2511 | else |
| 2512 | res = EWOULDBLOCK; /* timed out */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2513 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2515 | if (res < 0) |
| 2516 | res = errno; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2517 | |
| 2518 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2519 | *timeoutp = timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2520 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2521 | assert(res >= 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2522 | return res; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2523 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2524 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2525 | /* s.connect(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2526 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2527 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2528 | sock_connect(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2529 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2530 | sock_addr_t addrbuf; |
| 2531 | int addrlen; |
| 2532 | int res; |
| 2533 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2534 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2535 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2536 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2537 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2538 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2539 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2540 | if (timeout == 1) { |
| 2541 | PyErr_SetString(socket_timeout, "timed out"); |
| 2542 | return NULL; |
| 2543 | } |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2544 | if (res < 0) |
| 2545 | return NULL; |
| 2546 | if (res != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2547 | return s->errorhandler(); |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2548 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2549 | Py_INCREF(Py_None); |
| 2550 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2553 | PyDoc_STRVAR(connect_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2554 | "connect(address)\n\ |
| 2555 | \n\ |
| 2556 | 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] | 2557 | is a pair (host, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2558 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2559 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2560 | /* s.connect_ex(sockaddr) method */ |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2561 | |
| 2562 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2563 | sock_connect_ex(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2564 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2565 | sock_addr_t addrbuf; |
| 2566 | int addrlen; |
| 2567 | int res; |
| 2568 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2569 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2570 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2571 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2572 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2573 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2574 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 2575 | if (res < 0) |
| 2576 | return NULL; |
| 2577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2578 | /* Signals are not errors (though they may raise exceptions). Adapted |
| 2579 | from PyErr_SetFromErrnoWithFilenameObject(). */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2580 | if (res == EINTR && PyErr_CheckSignals()) |
| 2581 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2582 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2583 | return PyLong_FromLong((long) res); |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2586 | PyDoc_STRVAR(connect_ex_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2587 | "connect_ex(address) -> errno\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2588 | \n\ |
| 2589 | 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] | 2590 | instead of raising an exception when an error occurs."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2591 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2592 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2593 | /* s.fileno() method */ |
| 2594 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2595 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2596 | sock_fileno(PySocketSockObject *s) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2597 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2598 | return PyLong_FromSocket_t(s->sock_fd); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2601 | PyDoc_STRVAR(fileno_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2602 | "fileno() -> integer\n\ |
| 2603 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2604 | Return the integer file descriptor of the socket."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2605 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2606 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2607 | /* s.getsockname() method */ |
| 2608 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2609 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2610 | sock_getsockname(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2611 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2612 | sock_addr_t addrbuf; |
| 2613 | int res; |
| 2614 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2615 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2616 | if (!getsockaddrlen(s, &addrlen)) |
| 2617 | return NULL; |
| 2618 | memset(&addrbuf, 0, addrlen); |
| 2619 | Py_BEGIN_ALLOW_THREADS |
| 2620 | res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2621 | Py_END_ALLOW_THREADS |
| 2622 | if (res < 0) |
| 2623 | return s->errorhandler(); |
| 2624 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2625 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2628 | PyDoc_STRVAR(getsockname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2629 | "getsockname() -> address info\n\ |
| 2630 | \n\ |
| 2631 | 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] | 2632 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2633 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2634 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2635 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2636 | /* s.getpeername() method */ |
| 2637 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2638 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2639 | sock_getpeername(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2640 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2641 | sock_addr_t addrbuf; |
| 2642 | int res; |
| 2643 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2645 | if (!getsockaddrlen(s, &addrlen)) |
| 2646 | return NULL; |
| 2647 | memset(&addrbuf, 0, addrlen); |
| 2648 | Py_BEGIN_ALLOW_THREADS |
| 2649 | res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2650 | Py_END_ALLOW_THREADS |
| 2651 | if (res < 0) |
| 2652 | return s->errorhandler(); |
| 2653 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2654 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2655 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2656 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2657 | PyDoc_STRVAR(getpeername_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2658 | "getpeername() -> address info\n\ |
| 2659 | \n\ |
| 2660 | 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] | 2661 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2662 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2663 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2664 | |
| 2665 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2666 | /* s.listen(n) method */ |
| 2667 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2668 | static PyObject * |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2669 | sock_listen(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2670 | { |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2671 | /* We try to choose a default backlog high enough to avoid connection drops |
| 2672 | * for common workloads, yet not too high to limit resource usage. */ |
| 2673 | int backlog = Py_MIN(SOMAXCONN, 128); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2674 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2675 | |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2676 | if (!PyArg_ParseTuple(args, "|i:listen", &backlog)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2677 | return NULL; |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2679 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 1be815a | 2011-05-10 19:16:29 +0200 | [diff] [blame] | 2680 | /* To avoid problems on systems that don't allow a negative backlog |
| 2681 | * (which doesn't make sense anyway) we force a minimum value of 0. */ |
| 2682 | if (backlog < 0) |
| 2683 | backlog = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2684 | res = listen(s->sock_fd, backlog); |
| 2685 | Py_END_ALLOW_THREADS |
| 2686 | if (res < 0) |
| 2687 | return s->errorhandler(); |
| 2688 | Py_INCREF(Py_None); |
| 2689 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2692 | PyDoc_STRVAR(listen_doc, |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2693 | "listen([backlog])\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2694 | \n\ |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2695 | Enable a server to accept connections. If backlog is specified, it must be\n\ |
| 2696 | 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] | 2697 | 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] | 2698 | connections. If not specified, a default reasonable value is chosen."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2699 | |
| 2700 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2701 | /* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2702 | * 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] | 2703 | * 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] | 2704 | * 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] | 2705 | * 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] | 2706 | * also possible that we return a number of bytes smaller than the request |
| 2707 | * bytes. |
| 2708 | */ |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2709 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2710 | static Py_ssize_t |
| 2711 | 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] | 2712 | { |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2713 | Py_ssize_t outlen = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2714 | int timeout; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2715 | int async_err = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2716 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2717 | if (!IS_SELECTABLE(s)) { |
| 2718 | select_error(); |
| 2719 | return -1; |
| 2720 | } |
| 2721 | if (len == 0) { |
| 2722 | /* If 0 bytes were requested, do nothing. */ |
| 2723 | return 0; |
| 2724 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2725 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2726 | BEGIN_SELECT_LOOP(s) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2727 | do { |
Victor Stinner | 391fa71 | 2015-03-31 13:15:31 +0200 | [diff] [blame^] | 2728 | timeout = internal_select(s, 0, interval); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2729 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2730 | if (!timeout) { |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2731 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 2732 | #ifdef MS_WINDOWS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2733 | if (len > INT_MAX) |
| 2734 | len = INT_MAX; |
| 2735 | outlen = recv(s->sock_fd, cbuf, (int)len, flags); |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 2736 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2737 | outlen = recv(s->sock_fd, cbuf, len, flags); |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 2738 | #endif |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2739 | Py_END_ALLOW_THREADS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2740 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2741 | } while (outlen < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2742 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2743 | if (timeout == 1) { |
| 2744 | PyErr_SetString(socket_timeout, "timed out"); |
| 2745 | return -1; |
| 2746 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2747 | END_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2748 | if (outlen < 0) { |
| 2749 | /* Note: the call to errorhandler() ALWAYS indirectly returned |
| 2750 | NULL, so ignore its return value */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2751 | if (!async_err) |
| 2752 | s->errorhandler(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2753 | return -1; |
| 2754 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2755 | return outlen; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2758 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2759 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2760 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2761 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2762 | sock_recv(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2763 | { |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2764 | Py_ssize_t recvlen, outlen; |
| 2765 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2766 | PyObject *buf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2767 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2768 | if (!PyArg_ParseTuple(args, "n|i:recv", &recvlen, &flags)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2769 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2771 | if (recvlen < 0) { |
| 2772 | PyErr_SetString(PyExc_ValueError, |
| 2773 | "negative buffersize in recv"); |
| 2774 | return NULL; |
| 2775 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2776 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2777 | /* Allocate a new string. */ |
| 2778 | buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
| 2779 | if (buf == NULL) |
| 2780 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2781 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2782 | /* Call the guts */ |
| 2783 | outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags); |
| 2784 | if (outlen < 0) { |
| 2785 | /* An error occurred, release the string and return an |
| 2786 | error. */ |
| 2787 | Py_DECREF(buf); |
| 2788 | return NULL; |
| 2789 | } |
| 2790 | if (outlen != recvlen) { |
| 2791 | /* We did not read as many bytes as we anticipated, resize the |
| 2792 | string if possible and be successful. */ |
| 2793 | _PyBytes_Resize(&buf, outlen); |
| 2794 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2795 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2796 | return buf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2797 | } |
| 2798 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2799 | PyDoc_STRVAR(recv_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2800 | "recv(buffersize[, flags]) -> data\n\ |
| 2801 | \n\ |
| 2802 | Receive up to buffersize bytes from the socket. For the optional flags\n\ |
| 2803 | argument, see the Unix manual. When no data is available, block until\n\ |
| 2804 | 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] | 2805 | 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] | 2806 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2807 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2808 | /* s.recv_into(buffer, [nbytes [,flags]]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2809 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2810 | static PyObject* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2811 | sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2812 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2813 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2814 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2815 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2816 | Py_buffer pbuf; |
| 2817 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2818 | Py_ssize_t buflen, readlen, recvlen = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2819 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2820 | /* Get the buffer's memory */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2821 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recv_into", kwlist, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2822 | &pbuf, &recvlen, &flags)) |
| 2823 | return NULL; |
| 2824 | buf = pbuf.buf; |
| 2825 | buflen = pbuf.len; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2826 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2827 | if (recvlen < 0) { |
| 2828 | PyBuffer_Release(&pbuf); |
| 2829 | PyErr_SetString(PyExc_ValueError, |
| 2830 | "negative buffersize in recv_into"); |
| 2831 | return NULL; |
| 2832 | } |
| 2833 | if (recvlen == 0) { |
| 2834 | /* If nbytes was not specified, use the buffer's length */ |
| 2835 | recvlen = buflen; |
| 2836 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2837 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2838 | /* Check if the buffer is large enough */ |
| 2839 | if (buflen < recvlen) { |
| 2840 | PyBuffer_Release(&pbuf); |
| 2841 | PyErr_SetString(PyExc_ValueError, |
| 2842 | "buffer too small for requested bytes"); |
| 2843 | return NULL; |
| 2844 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2845 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2846 | /* Call the guts */ |
| 2847 | readlen = sock_recv_guts(s, buf, recvlen, flags); |
| 2848 | if (readlen < 0) { |
| 2849 | /* Return an error. */ |
| 2850 | PyBuffer_Release(&pbuf); |
| 2851 | return NULL; |
| 2852 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2853 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2854 | PyBuffer_Release(&pbuf); |
| 2855 | /* Return the number of bytes read. Note that we do not do anything |
| 2856 | special here in the case that readlen < recvlen. */ |
| 2857 | return PyLong_FromSsize_t(readlen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2860 | PyDoc_STRVAR(recv_into_doc, |
| 2861 | "recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2862 | \n\ |
| 2863 | A version of recv() that stores its data into a buffer rather than creating \n\ |
| 2864 | a new string. Receive up to buffersize bytes from the socket. If buffersize \n\ |
| 2865 | is not specified (or 0), receive up to the size available in the given buffer.\n\ |
| 2866 | \n\ |
| 2867 | See recv() for documentation about the flags."); |
| 2868 | |
| 2869 | |
| 2870 | /* |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 2871 | * This is the guts of the recvfrom() and recvfrom_into() methods, which reads |
| 2872 | * into a char buffer. If you have any inc/def ref to do to the objects that |
| 2873 | * 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] | 2874 | * 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] | 2875 | * that it is also possible that we return a number of bytes smaller than the |
| 2876 | * request bytes. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2877 | * |
| 2878 | * 'addr' is a return value for the address object. Note that you must decref |
| 2879 | * it yourself. |
| 2880 | */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2881 | static Py_ssize_t |
| 2882 | 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] | 2883 | PyObject** addr) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2884 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2885 | sock_addr_t addrbuf; |
| 2886 | int timeout; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2887 | Py_ssize_t n = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2888 | socklen_t addrlen; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2889 | int async_err = 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2891 | *addr = NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2892 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2893 | if (!getsockaddrlen(s, &addrlen)) |
| 2894 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2895 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2896 | if (!IS_SELECTABLE(s)) { |
| 2897 | select_error(); |
| 2898 | return -1; |
| 2899 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2900 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2901 | BEGIN_SELECT_LOOP(s) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2902 | do { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2903 | memset(&addrbuf, 0, addrlen); |
Victor Stinner | 391fa71 | 2015-03-31 13:15:31 +0200 | [diff] [blame^] | 2904 | timeout = internal_select(s, 0, interval); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2905 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2906 | if (!timeout) { |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2907 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 2908 | #ifdef MS_WINDOWS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2909 | if (len > INT_MAX) |
| 2910 | len = INT_MAX; |
| 2911 | n = recvfrom(s->sock_fd, cbuf, (int)len, flags, |
| 2912 | (void *) &addrbuf, &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2913 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2914 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2915 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2916 | #endif |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 2917 | Py_END_ALLOW_THREADS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2918 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2919 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2920 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2921 | if (timeout == 1) { |
| 2922 | PyErr_SetString(socket_timeout, "timed out"); |
| 2923 | return -1; |
| 2924 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2925 | END_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2926 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2927 | if (!async_err) |
| 2928 | s->errorhandler(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2929 | return -1; |
| 2930 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2931 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2932 | if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 2933 | addrlen, s->sock_proto))) |
| 2934 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2935 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2936 | return n; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
| 2939 | /* s.recvfrom(nbytes [,flags]) method */ |
| 2940 | |
| 2941 | static PyObject * |
| 2942 | sock_recvfrom(PySocketSockObject *s, PyObject *args) |
| 2943 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2944 | PyObject *buf = NULL; |
| 2945 | PyObject *addr = NULL; |
| 2946 | PyObject *ret = NULL; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2947 | int flags = 0; |
| 2948 | Py_ssize_t recvlen, outlen; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2949 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2950 | if (!PyArg_ParseTuple(args, "n|i:recvfrom", &recvlen, &flags)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2951 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2952 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2953 | if (recvlen < 0) { |
| 2954 | PyErr_SetString(PyExc_ValueError, |
| 2955 | "negative buffersize in recvfrom"); |
| 2956 | return NULL; |
| 2957 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2958 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2959 | buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
| 2960 | if (buf == NULL) |
| 2961 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2963 | outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf), |
| 2964 | recvlen, flags, &addr); |
| 2965 | if (outlen < 0) { |
| 2966 | goto finally; |
| 2967 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2968 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2969 | if (outlen != recvlen) { |
| 2970 | /* We did not read as many bytes as we anticipated, resize the |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 2971 | string if possible and be successful. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2972 | if (_PyBytes_Resize(&buf, outlen) < 0) |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 2973 | /* Oopsy, not so successful after all. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2974 | goto finally; |
| 2975 | } |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2976 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2977 | ret = PyTuple_Pack(2, buf, addr); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2978 | |
| 2979 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2980 | Py_XDECREF(buf); |
| 2981 | Py_XDECREF(addr); |
| 2982 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2985 | PyDoc_STRVAR(recvfrom_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2986 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
| 2987 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2988 | 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] | 2989 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2990 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2991 | /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2992 | |
| 2993 | static PyObject * |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2994 | sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2995 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2996 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2997 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2998 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2999 | Py_buffer pbuf; |
| 3000 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3001 | Py_ssize_t readlen, buflen, recvlen = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3002 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3003 | PyObject *addr = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3004 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3005 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recvfrom_into", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3006 | kwlist, &pbuf, |
| 3007 | &recvlen, &flags)) |
| 3008 | return NULL; |
| 3009 | buf = pbuf.buf; |
| 3010 | buflen = pbuf.len; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3012 | if (recvlen < 0) { |
| 3013 | PyBuffer_Release(&pbuf); |
| 3014 | PyErr_SetString(PyExc_ValueError, |
| 3015 | "negative buffersize in recvfrom_into"); |
| 3016 | return NULL; |
| 3017 | } |
| 3018 | if (recvlen == 0) { |
| 3019 | /* If nbytes was not specified, use the buffer's length */ |
| 3020 | recvlen = buflen; |
Benjamin Peterson | c6b37e2 | 2014-01-13 23:14:42 -0500 | [diff] [blame] | 3021 | } else if (recvlen > buflen) { |
| 3022 | PyBuffer_Release(&pbuf); |
| 3023 | PyErr_SetString(PyExc_ValueError, |
| 3024 | "nbytes is greater than the length of the buffer"); |
| 3025 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3026 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3027 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3028 | readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr); |
| 3029 | if (readlen < 0) { |
| 3030 | PyBuffer_Release(&pbuf); |
| 3031 | /* Return an error */ |
| 3032 | Py_XDECREF(addr); |
| 3033 | return NULL; |
| 3034 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3035 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3036 | PyBuffer_Release(&pbuf); |
| 3037 | /* Return the number of bytes read and the address. Note that we do |
| 3038 | not do anything special here in the case that readlen < recvlen. */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3039 | return Py_BuildValue("nN", readlen, addr); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3040 | } |
| 3041 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3042 | PyDoc_STRVAR(recvfrom_into_doc, |
| 3043 | "recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3044 | \n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3045 | 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] | 3046 | |
| 3047 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3048 | /* The sendmsg() and recvmsg[_into]() methods require a working |
| 3049 | CMSG_LEN(). See the comment near get_CMSG_LEN(). */ |
| 3050 | #ifdef CMSG_LEN |
| 3051 | /* |
| 3052 | * Call recvmsg() with the supplied iovec structures, flags, and |
| 3053 | * ancillary data buffer size (controllen). Returns the tuple return |
| 3054 | * value for recvmsg() or recvmsg_into(), with the first item provided |
| 3055 | * by the supplied makeval() function. makeval() will be called with |
| 3056 | * the length read and makeval_data as arguments, and must return a |
| 3057 | * new reference (which will be decrefed if there is a subsequent |
| 3058 | * error). On error, closes any file descriptors received via |
| 3059 | * SCM_RIGHTS. |
| 3060 | */ |
| 3061 | static PyObject * |
| 3062 | sock_recvmsg_guts(PySocketSockObject *s, struct iovec *iov, int iovlen, |
| 3063 | int flags, Py_ssize_t controllen, |
| 3064 | PyObject *(*makeval)(ssize_t, void *), void *makeval_data) |
| 3065 | { |
| 3066 | ssize_t bytes_received = -1; |
| 3067 | int timeout; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3068 | int async_err = 0; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3069 | sock_addr_t addrbuf; |
| 3070 | socklen_t addrbuflen; |
Charles-François Natali | e9e95ae | 2011-08-24 21:40:53 +0200 | [diff] [blame] | 3071 | struct msghdr msg = {0}; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3072 | PyObject *cmsg_list = NULL, *retval = NULL; |
| 3073 | void *controlbuf = NULL; |
| 3074 | struct cmsghdr *cmsgh; |
| 3075 | size_t cmsgdatalen = 0; |
| 3076 | int cmsg_status; |
| 3077 | |
| 3078 | /* XXX: POSIX says that msg_name and msg_namelen "shall be |
| 3079 | ignored" when the socket is connected (Linux fills them in |
| 3080 | anyway for AF_UNIX sockets at least). Normally msg_namelen |
| 3081 | seems to be set to 0 if there's no address, but try to |
| 3082 | initialize msg_name to something that won't be mistaken for a |
| 3083 | real address if that doesn't happen. */ |
| 3084 | if (!getsockaddrlen(s, &addrbuflen)) |
| 3085 | return NULL; |
| 3086 | memset(&addrbuf, 0, addrbuflen); |
| 3087 | SAS2SA(&addrbuf)->sa_family = AF_UNSPEC; |
| 3088 | |
| 3089 | if (controllen < 0 || controllen > SOCKLEN_T_LIMIT) { |
| 3090 | PyErr_SetString(PyExc_ValueError, |
| 3091 | "invalid ancillary data buffer length"); |
| 3092 | return NULL; |
| 3093 | } |
| 3094 | if (controllen > 0 && (controlbuf = PyMem_Malloc(controllen)) == NULL) |
| 3095 | return PyErr_NoMemory(); |
| 3096 | |
| 3097 | /* Make the system call. */ |
| 3098 | if (!IS_SELECTABLE(s)) { |
| 3099 | select_error(); |
| 3100 | goto finally; |
| 3101 | } |
| 3102 | |
| 3103 | BEGIN_SELECT_LOOP(s) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3104 | do { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3105 | msg.msg_name = SAS2SA(&addrbuf); |
| 3106 | msg.msg_namelen = addrbuflen; |
| 3107 | msg.msg_iov = iov; |
| 3108 | msg.msg_iovlen = iovlen; |
| 3109 | msg.msg_control = controlbuf; |
| 3110 | msg.msg_controllen = controllen; |
Victor Stinner | 391fa71 | 2015-03-31 13:15:31 +0200 | [diff] [blame^] | 3111 | timeout = internal_select(s, 0, interval); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3112 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3113 | if (timeout == 1) { |
| 3114 | PyErr_SetString(socket_timeout, "timed out"); |
| 3115 | goto finally; |
| 3116 | } |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3117 | |
| 3118 | if (!timeout) { |
| 3119 | Py_BEGIN_ALLOW_THREADS; |
| 3120 | bytes_received = recvmsg(s->sock_fd, &msg, flags); |
| 3121 | Py_END_ALLOW_THREADS; |
| 3122 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3123 | } while (bytes_received < 0 && errno == EINTR && |
| 3124 | !(async_err = PyErr_CheckSignals())); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3125 | END_SELECT_LOOP(s) |
| 3126 | |
| 3127 | if (bytes_received < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3128 | if (!async_err) |
| 3129 | s->errorhandler(); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3130 | goto finally; |
| 3131 | } |
| 3132 | |
| 3133 | /* Make list of (level, type, data) tuples from control messages. */ |
| 3134 | if ((cmsg_list = PyList_New(0)) == NULL) |
| 3135 | goto err_closefds; |
| 3136 | /* Check for empty ancillary data as old CMSG_FIRSTHDR() |
| 3137 | implementations didn't do so. */ |
| 3138 | for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL); |
| 3139 | cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) { |
| 3140 | PyObject *bytes, *tuple; |
| 3141 | int tmp; |
| 3142 | |
| 3143 | cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen); |
| 3144 | if (cmsg_status != 0) { |
| 3145 | if (PyErr_WarnEx(PyExc_RuntimeWarning, |
| 3146 | "received malformed or improperly-truncated " |
| 3147 | "ancillary data", 1) == -1) |
| 3148 | goto err_closefds; |
| 3149 | } |
| 3150 | if (cmsg_status < 0) |
| 3151 | break; |
| 3152 | if (cmsgdatalen > PY_SSIZE_T_MAX) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3153 | PyErr_SetString(PyExc_OSError, "control message too long"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3154 | goto err_closefds; |
| 3155 | } |
| 3156 | |
| 3157 | bytes = PyBytes_FromStringAndSize((char *)CMSG_DATA(cmsgh), |
| 3158 | cmsgdatalen); |
| 3159 | tuple = Py_BuildValue("iiN", (int)cmsgh->cmsg_level, |
| 3160 | (int)cmsgh->cmsg_type, bytes); |
| 3161 | if (tuple == NULL) |
| 3162 | goto err_closefds; |
| 3163 | tmp = PyList_Append(cmsg_list, tuple); |
| 3164 | Py_DECREF(tuple); |
| 3165 | if (tmp != 0) |
| 3166 | goto err_closefds; |
| 3167 | |
| 3168 | if (cmsg_status != 0) |
| 3169 | break; |
| 3170 | } |
| 3171 | |
| 3172 | retval = Py_BuildValue("NOiN", |
| 3173 | (*makeval)(bytes_received, makeval_data), |
| 3174 | cmsg_list, |
| 3175 | (int)msg.msg_flags, |
| 3176 | makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 3177 | ((msg.msg_namelen > addrbuflen) ? |
| 3178 | addrbuflen : msg.msg_namelen), |
| 3179 | s->sock_proto)); |
| 3180 | if (retval == NULL) |
| 3181 | goto err_closefds; |
| 3182 | |
| 3183 | finally: |
| 3184 | Py_XDECREF(cmsg_list); |
| 3185 | PyMem_Free(controlbuf); |
| 3186 | return retval; |
| 3187 | |
| 3188 | err_closefds: |
| 3189 | #ifdef SCM_RIGHTS |
| 3190 | /* Close all descriptors coming from SCM_RIGHTS, so they don't leak. */ |
| 3191 | for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL); |
| 3192 | cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) { |
| 3193 | cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen); |
| 3194 | if (cmsg_status < 0) |
| 3195 | break; |
| 3196 | if (cmsgh->cmsg_level == SOL_SOCKET && |
| 3197 | cmsgh->cmsg_type == SCM_RIGHTS) { |
| 3198 | size_t numfds; |
| 3199 | int *fdp; |
| 3200 | |
| 3201 | numfds = cmsgdatalen / sizeof(int); |
| 3202 | fdp = (int *)CMSG_DATA(cmsgh); |
| 3203 | while (numfds-- > 0) |
| 3204 | close(*fdp++); |
| 3205 | } |
| 3206 | if (cmsg_status != 0) |
| 3207 | break; |
| 3208 | } |
| 3209 | #endif /* SCM_RIGHTS */ |
| 3210 | goto finally; |
| 3211 | } |
| 3212 | |
| 3213 | |
| 3214 | static PyObject * |
| 3215 | makeval_recvmsg(ssize_t received, void *data) |
| 3216 | { |
| 3217 | PyObject **buf = data; |
| 3218 | |
| 3219 | if (received < PyBytes_GET_SIZE(*buf)) |
| 3220 | _PyBytes_Resize(buf, received); |
| 3221 | Py_XINCREF(*buf); |
| 3222 | return *buf; |
| 3223 | } |
| 3224 | |
| 3225 | /* s.recvmsg(bufsize[, ancbufsize[, flags]]) method */ |
| 3226 | |
| 3227 | static PyObject * |
| 3228 | sock_recvmsg(PySocketSockObject *s, PyObject *args) |
| 3229 | { |
| 3230 | Py_ssize_t bufsize, ancbufsize = 0; |
| 3231 | int flags = 0; |
| 3232 | struct iovec iov; |
| 3233 | PyObject *buf = NULL, *retval = NULL; |
| 3234 | |
| 3235 | if (!PyArg_ParseTuple(args, "n|ni:recvmsg", &bufsize, &ancbufsize, &flags)) |
| 3236 | return NULL; |
| 3237 | |
| 3238 | if (bufsize < 0) { |
| 3239 | PyErr_SetString(PyExc_ValueError, "negative buffer size in recvmsg()"); |
| 3240 | return NULL; |
| 3241 | } |
| 3242 | if ((buf = PyBytes_FromStringAndSize(NULL, bufsize)) == NULL) |
| 3243 | return NULL; |
| 3244 | iov.iov_base = PyBytes_AS_STRING(buf); |
| 3245 | iov.iov_len = bufsize; |
| 3246 | |
| 3247 | /* Note that we're passing a pointer to *our pointer* to the bytes |
| 3248 | object here (&buf); makeval_recvmsg() may incref the object, or |
| 3249 | deallocate it and set our pointer to NULL. */ |
| 3250 | retval = sock_recvmsg_guts(s, &iov, 1, flags, ancbufsize, |
| 3251 | &makeval_recvmsg, &buf); |
| 3252 | Py_XDECREF(buf); |
| 3253 | return retval; |
| 3254 | } |
| 3255 | |
| 3256 | PyDoc_STRVAR(recvmsg_doc, |
| 3257 | "recvmsg(bufsize[, ancbufsize[, flags]]) -> (data, ancdata, msg_flags, address)\n\ |
| 3258 | \n\ |
| 3259 | Receive normal data (up to bufsize bytes) and ancillary data from the\n\ |
| 3260 | socket. The ancbufsize argument sets the size in bytes of the\n\ |
| 3261 | internal buffer used to receive the ancillary data; it defaults to 0,\n\ |
| 3262 | meaning that no ancillary data will be received. Appropriate buffer\n\ |
| 3263 | sizes for ancillary data can be calculated using CMSG_SPACE() or\n\ |
| 3264 | CMSG_LEN(), and items which do not fit into the buffer might be\n\ |
| 3265 | truncated or discarded. The flags argument defaults to 0 and has the\n\ |
| 3266 | same meaning as for recv().\n\ |
| 3267 | \n\ |
| 3268 | The return value is a 4-tuple: (data, ancdata, msg_flags, address).\n\ |
| 3269 | The data item is a bytes object holding the non-ancillary data\n\ |
| 3270 | received. The ancdata item is a list of zero or more tuples\n\ |
| 3271 | (cmsg_level, cmsg_type, cmsg_data) representing the ancillary data\n\ |
| 3272 | (control messages) received: cmsg_level and cmsg_type are integers\n\ |
| 3273 | specifying the protocol level and protocol-specific type respectively,\n\ |
| 3274 | and cmsg_data is a bytes object holding the associated data. The\n\ |
| 3275 | msg_flags item is the bitwise OR of various flags indicating\n\ |
| 3276 | conditions on the received message; see your system documentation for\n\ |
| 3277 | details. If the receiving socket is unconnected, address is the\n\ |
| 3278 | address of the sending socket, if available; otherwise, its value is\n\ |
| 3279 | unspecified.\n\ |
| 3280 | \n\ |
| 3281 | If recvmsg() raises an exception after the system call returns, it\n\ |
| 3282 | will first attempt to close any file descriptors received via the\n\ |
| 3283 | SCM_RIGHTS mechanism."); |
| 3284 | |
| 3285 | |
| 3286 | static PyObject * |
| 3287 | makeval_recvmsg_into(ssize_t received, void *data) |
| 3288 | { |
| 3289 | return PyLong_FromSsize_t(received); |
| 3290 | } |
| 3291 | |
| 3292 | /* s.recvmsg_into(buffers[, ancbufsize[, flags]]) method */ |
| 3293 | |
| 3294 | static PyObject * |
| 3295 | sock_recvmsg_into(PySocketSockObject *s, PyObject *args) |
| 3296 | { |
| 3297 | Py_ssize_t ancbufsize = 0; |
| 3298 | int flags = 0; |
| 3299 | struct iovec *iovs = NULL; |
| 3300 | Py_ssize_t i, nitems, nbufs = 0; |
| 3301 | Py_buffer *bufs = NULL; |
| 3302 | PyObject *buffers_arg, *fast, *retval = NULL; |
| 3303 | |
| 3304 | if (!PyArg_ParseTuple(args, "O|ni:recvmsg_into", |
| 3305 | &buffers_arg, &ancbufsize, &flags)) |
| 3306 | return NULL; |
| 3307 | |
| 3308 | if ((fast = PySequence_Fast(buffers_arg, |
| 3309 | "recvmsg_into() argument 1 must be an " |
| 3310 | "iterable")) == NULL) |
| 3311 | return NULL; |
| 3312 | nitems = PySequence_Fast_GET_SIZE(fast); |
| 3313 | if (nitems > INT_MAX) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3314 | PyErr_SetString(PyExc_OSError, "recvmsg_into() argument 1 is too long"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3315 | goto finally; |
| 3316 | } |
| 3317 | |
| 3318 | /* Fill in an iovec for each item, and save the Py_buffer |
| 3319 | structs to release afterwards. */ |
| 3320 | if (nitems > 0 && ((iovs = PyMem_New(struct iovec, nitems)) == NULL || |
| 3321 | (bufs = PyMem_New(Py_buffer, nitems)) == NULL)) { |
| 3322 | PyErr_NoMemory(); |
| 3323 | goto finally; |
| 3324 | } |
| 3325 | for (; nbufs < nitems; nbufs++) { |
| 3326 | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(fast, nbufs), |
| 3327 | "w*;recvmsg_into() argument 1 must be an iterable " |
| 3328 | "of single-segment read-write buffers", |
| 3329 | &bufs[nbufs])) |
| 3330 | goto finally; |
| 3331 | iovs[nbufs].iov_base = bufs[nbufs].buf; |
| 3332 | iovs[nbufs].iov_len = bufs[nbufs].len; |
| 3333 | } |
| 3334 | |
| 3335 | retval = sock_recvmsg_guts(s, iovs, nitems, flags, ancbufsize, |
| 3336 | &makeval_recvmsg_into, NULL); |
| 3337 | finally: |
| 3338 | for (i = 0; i < nbufs; i++) |
| 3339 | PyBuffer_Release(&bufs[i]); |
| 3340 | PyMem_Free(bufs); |
| 3341 | PyMem_Free(iovs); |
| 3342 | Py_DECREF(fast); |
| 3343 | return retval; |
| 3344 | } |
| 3345 | |
| 3346 | PyDoc_STRVAR(recvmsg_into_doc, |
| 3347 | "recvmsg_into(buffers[, ancbufsize[, flags]]) -> (nbytes, ancdata, msg_flags, address)\n\ |
| 3348 | \n\ |
| 3349 | Receive normal data and ancillary data from the socket, scattering the\n\ |
| 3350 | non-ancillary data into a series of buffers. The buffers argument\n\ |
| 3351 | must be an iterable of objects that export writable buffers\n\ |
| 3352 | (e.g. bytearray objects); these will be filled with successive chunks\n\ |
| 3353 | of the non-ancillary data until it has all been written or there are\n\ |
| 3354 | no more buffers. The ancbufsize argument sets the size in bytes of\n\ |
| 3355 | the internal buffer used to receive the ancillary data; it defaults to\n\ |
| 3356 | 0, meaning that no ancillary data will be received. Appropriate\n\ |
| 3357 | buffer sizes for ancillary data can be calculated using CMSG_SPACE()\n\ |
| 3358 | or CMSG_LEN(), and items which do not fit into the buffer might be\n\ |
| 3359 | truncated or discarded. The flags argument defaults to 0 and has the\n\ |
| 3360 | same meaning as for recv().\n\ |
| 3361 | \n\ |
| 3362 | The return value is a 4-tuple: (nbytes, ancdata, msg_flags, address).\n\ |
| 3363 | The nbytes item is the total number of bytes of non-ancillary data\n\ |
| 3364 | written into the buffers. The ancdata item is a list of zero or more\n\ |
| 3365 | tuples (cmsg_level, cmsg_type, cmsg_data) representing the ancillary\n\ |
| 3366 | data (control messages) received: cmsg_level and cmsg_type are\n\ |
| 3367 | integers specifying the protocol level and protocol-specific type\n\ |
| 3368 | respectively, and cmsg_data is a bytes object holding the associated\n\ |
| 3369 | data. The msg_flags item is the bitwise OR of various flags\n\ |
| 3370 | indicating conditions on the received message; see your system\n\ |
| 3371 | documentation for details. If the receiving socket is unconnected,\n\ |
| 3372 | address is the address of the sending socket, if available; otherwise,\n\ |
| 3373 | its value is unspecified.\n\ |
| 3374 | \n\ |
| 3375 | If recvmsg_into() raises an exception after the system call returns,\n\ |
| 3376 | it will first attempt to close any file descriptors received via the\n\ |
| 3377 | SCM_RIGHTS mechanism."); |
| 3378 | #endif /* CMSG_LEN */ |
| 3379 | |
| 3380 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 3381 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3382 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3383 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3384 | sock_send(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3385 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3386 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3387 | Py_ssize_t len, n = -1; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3388 | int async_err = 0; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3389 | int flags = 0, timeout; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3390 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3391 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3392 | if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags)) |
| 3393 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3395 | if (!IS_SELECTABLE(s)) { |
| 3396 | PyBuffer_Release(&pbuf); |
| 3397 | return select_error(); |
| 3398 | } |
| 3399 | buf = pbuf.buf; |
| 3400 | len = pbuf.len; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 3401 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3402 | BEGIN_SELECT_LOOP(s) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3403 | do { |
Victor Stinner | 391fa71 | 2015-03-31 13:15:31 +0200 | [diff] [blame^] | 3404 | timeout = internal_select(s, 1, interval); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3405 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3406 | if (!timeout) { |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3407 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 3408 | #ifdef MS_WINDOWS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3409 | if (len > INT_MAX) |
| 3410 | len = INT_MAX; |
| 3411 | n = send(s->sock_fd, buf, (int)len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3412 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3413 | n = send(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3414 | #endif |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3415 | Py_END_ALLOW_THREADS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3416 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3417 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3418 | if (timeout == 1) { |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3419 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3420 | PyErr_SetString(socket_timeout, "timed out"); |
| 3421 | return NULL; |
| 3422 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3423 | END_SELECT_LOOP(s) |
| 3424 | |
| 3425 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3426 | if (n < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3427 | return (!async_err) ? s->errorhandler() : NULL; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3428 | return PyLong_FromSsize_t(n); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3429 | } |
| 3430 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3431 | PyDoc_STRVAR(send_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3432 | "send(data[, flags]) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3433 | \n\ |
| 3434 | 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] | 3435 | 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] | 3436 | 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] | 3437 | |
| 3438 | |
| 3439 | /* s.sendall(data [,flags]) method */ |
| 3440 | |
| 3441 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3442 | sock_sendall(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3443 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3444 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3445 | Py_ssize_t len, n = -1; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3446 | int async_err = 0; |
| 3447 | int flags = 0, timeout; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3448 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3449 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3450 | if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags)) |
| 3451 | return NULL; |
| 3452 | buf = pbuf.buf; |
| 3453 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3454 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3455 | if (!IS_SELECTABLE(s)) { |
| 3456 | PyBuffer_Release(&pbuf); |
| 3457 | return select_error(); |
| 3458 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 3459 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3460 | do { |
Victor Stinner | 391fa71 | 2015-03-31 13:15:31 +0200 | [diff] [blame^] | 3461 | timeout = internal_select(s, 1, s->sock_timeout); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3462 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3463 | n = -1; |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3464 | if (!timeout) { |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3465 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 3466 | #ifdef MS_WINDOWS |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 3467 | if (len > INT_MAX) |
| 3468 | len = INT_MAX; |
| 3469 | n = send(s->sock_fd, buf, (int)len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3470 | #else |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3471 | n = send(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3472 | #endif |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3473 | Py_END_ALLOW_THREADS |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3474 | } |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3475 | if (timeout == 1) { |
| 3476 | PyBuffer_Release(&pbuf); |
| 3477 | PyErr_SetString(socket_timeout, "timed out"); |
| 3478 | return NULL; |
| 3479 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3480 | if (n >= 0) { |
| 3481 | buf += n; |
| 3482 | len -= n; |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3483 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3484 | } while (len > 0 && (n >= 0 || errno == EINTR) && |
| 3485 | !(async_err = PyErr_CheckSignals())); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3486 | PyBuffer_Release(&pbuf); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3487 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3488 | if (n < 0 || async_err) |
| 3489 | return (!async_err) ? s->errorhandler() : NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3491 | Py_INCREF(Py_None); |
| 3492 | return Py_None; |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3493 | } |
| 3494 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3495 | PyDoc_STRVAR(sendall_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3496 | "sendall(data[, flags])\n\ |
| 3497 | \n\ |
| 3498 | Send a data string to the socket. For the optional flags\n\ |
| 3499 | argument, see the Unix manual. This calls send() repeatedly\n\ |
| 3500 | 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] | 3501 | to tell how much data has been sent."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3502 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3503 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 3504 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3505 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3506 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3507 | sock_sendto(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3508 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3509 | Py_buffer pbuf; |
| 3510 | PyObject *addro; |
| 3511 | char *buf; |
Antoine Pitrou | 5e98141 | 2011-03-17 22:38:37 +0100 | [diff] [blame] | 3512 | Py_ssize_t len, arglen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3513 | sock_addr_t addrbuf; |
| 3514 | int addrlen, n = -1, flags, timeout; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3515 | int async_err = 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3516 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3517 | flags = 0; |
Antoine Pitrou | 5e98141 | 2011-03-17 22:38:37 +0100 | [diff] [blame] | 3518 | arglen = PyTuple_Size(args); |
| 3519 | switch (arglen) { |
| 3520 | case 2: |
| 3521 | PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro); |
| 3522 | break; |
| 3523 | case 3: |
| 3524 | PyArg_ParseTuple(args, "y*iO:sendto", |
| 3525 | &pbuf, &flags, &addro); |
| 3526 | break; |
| 3527 | default: |
| 3528 | PyErr_Format(PyExc_TypeError, |
| 3529 | "sendto() takes 2 or 3 arguments (%d given)", |
| 3530 | arglen); |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 3531 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3532 | } |
Antoine Pitrou | 5e98141 | 2011-03-17 22:38:37 +0100 | [diff] [blame] | 3533 | if (PyErr_Occurred()) |
| 3534 | return NULL; |
| 3535 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3536 | buf = pbuf.buf; |
| 3537 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3538 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3539 | if (!IS_SELECTABLE(s)) { |
| 3540 | PyBuffer_Release(&pbuf); |
| 3541 | return select_error(); |
| 3542 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 3543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3544 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) { |
| 3545 | PyBuffer_Release(&pbuf); |
| 3546 | return NULL; |
| 3547 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3548 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3549 | BEGIN_SELECT_LOOP(s) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3550 | do { |
Victor Stinner | 391fa71 | 2015-03-31 13:15:31 +0200 | [diff] [blame^] | 3551 | timeout = internal_select(s, 1, interval); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3552 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3553 | if (!timeout) { |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3554 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 3555 | #ifdef MS_WINDOWS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3556 | if (len > INT_MAX) |
| 3557 | len = INT_MAX; |
| 3558 | n = sendto(s->sock_fd, buf, (int)len, flags, |
| 3559 | SAS2SA(&addrbuf), addrlen); |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 3560 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3561 | n = sendto(s->sock_fd, buf, len, flags, |
| 3562 | SAS2SA(&addrbuf), addrlen); |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 3563 | #endif |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3564 | Py_END_ALLOW_THREADS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3565 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3566 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3568 | if (timeout == 1) { |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3569 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3570 | PyErr_SetString(socket_timeout, "timed out"); |
| 3571 | return NULL; |
| 3572 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3573 | END_SELECT_LOOP(s) |
| 3574 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3575 | if (n < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3576 | return (!async_err) ? s->errorhandler() : NULL; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3577 | return PyLong_FromSsize_t(n); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3578 | } |
| 3579 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3580 | PyDoc_STRVAR(sendto_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3581 | "sendto(data[, flags], address) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3582 | \n\ |
| 3583 | 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] | 3584 | For IP sockets, the address is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3585 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3586 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3587 | /* The sendmsg() and recvmsg[_into]() methods require a working |
| 3588 | CMSG_LEN(). See the comment near get_CMSG_LEN(). */ |
| 3589 | #ifdef CMSG_LEN |
| 3590 | /* s.sendmsg(buffers[, ancdata[, flags[, address]]]) method */ |
| 3591 | |
| 3592 | static PyObject * |
| 3593 | sock_sendmsg(PySocketSockObject *s, PyObject *args) |
| 3594 | { |
| 3595 | Py_ssize_t i, ndataparts, ndatabufs = 0, ncmsgs, ncmsgbufs = 0; |
| 3596 | Py_buffer *databufs = NULL; |
| 3597 | struct iovec *iovs = NULL; |
| 3598 | sock_addr_t addrbuf; |
Charles-François Natali | e9e95ae | 2011-08-24 21:40:53 +0200 | [diff] [blame] | 3599 | struct msghdr msg = {0}; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3600 | struct cmsginfo { |
| 3601 | int level; |
| 3602 | int type; |
| 3603 | Py_buffer data; |
| 3604 | } *cmsgs = NULL; |
| 3605 | void *controlbuf = NULL; |
| 3606 | size_t controllen, controllen_last; |
| 3607 | ssize_t bytes_sent = -1; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3608 | int async_err = 0; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3609 | int addrlen, timeout, flags = 0; |
| 3610 | PyObject *data_arg, *cmsg_arg = NULL, *addr_arg = NULL, *data_fast = NULL, |
| 3611 | *cmsg_fast = NULL, *retval = NULL; |
| 3612 | |
| 3613 | if (!PyArg_ParseTuple(args, "O|OiO:sendmsg", |
| 3614 | &data_arg, &cmsg_arg, &flags, &addr_arg)) |
| 3615 | return NULL; |
| 3616 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3617 | /* Parse destination address. */ |
| 3618 | if (addr_arg != NULL && addr_arg != Py_None) { |
| 3619 | if (!getsockaddrarg(s, addr_arg, SAS2SA(&addrbuf), &addrlen)) |
| 3620 | goto finally; |
| 3621 | msg.msg_name = &addrbuf; |
| 3622 | msg.msg_namelen = addrlen; |
| 3623 | } |
| 3624 | |
| 3625 | /* Fill in an iovec for each message part, and save the Py_buffer |
| 3626 | structs to release afterwards. */ |
| 3627 | if ((data_fast = PySequence_Fast(data_arg, |
| 3628 | "sendmsg() argument 1 must be an " |
| 3629 | "iterable")) == NULL) |
| 3630 | goto finally; |
| 3631 | ndataparts = PySequence_Fast_GET_SIZE(data_fast); |
| 3632 | if (ndataparts > INT_MAX) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3633 | PyErr_SetString(PyExc_OSError, "sendmsg() argument 1 is too long"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3634 | goto finally; |
| 3635 | } |
| 3636 | msg.msg_iovlen = ndataparts; |
| 3637 | if (ndataparts > 0 && |
| 3638 | ((msg.msg_iov = iovs = PyMem_New(struct iovec, ndataparts)) == NULL || |
| 3639 | (databufs = PyMem_New(Py_buffer, ndataparts)) == NULL)) { |
| 3640 | PyErr_NoMemory(); |
| 3641 | goto finally; |
| 3642 | } |
| 3643 | for (; ndatabufs < ndataparts; ndatabufs++) { |
| 3644 | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(data_fast, ndatabufs), |
| 3645 | "y*;sendmsg() argument 1 must be an iterable of " |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 3646 | "bytes-like objects", |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3647 | &databufs[ndatabufs])) |
| 3648 | goto finally; |
| 3649 | iovs[ndatabufs].iov_base = databufs[ndatabufs].buf; |
| 3650 | iovs[ndatabufs].iov_len = databufs[ndatabufs].len; |
| 3651 | } |
| 3652 | |
| 3653 | if (cmsg_arg == NULL) |
| 3654 | ncmsgs = 0; |
| 3655 | else { |
| 3656 | if ((cmsg_fast = PySequence_Fast(cmsg_arg, |
| 3657 | "sendmsg() argument 2 must be an " |
| 3658 | "iterable")) == NULL) |
| 3659 | goto finally; |
| 3660 | ncmsgs = PySequence_Fast_GET_SIZE(cmsg_fast); |
| 3661 | } |
| 3662 | |
| 3663 | #ifndef CMSG_SPACE |
| 3664 | if (ncmsgs > 1) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3665 | PyErr_SetString(PyExc_OSError, |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3666 | "sending multiple control messages is not supported " |
| 3667 | "on this system"); |
| 3668 | goto finally; |
| 3669 | } |
| 3670 | #endif |
| 3671 | /* Save level, type and Py_buffer for each control message, |
| 3672 | and calculate total size. */ |
| 3673 | if (ncmsgs > 0 && (cmsgs = PyMem_New(struct cmsginfo, ncmsgs)) == NULL) { |
| 3674 | PyErr_NoMemory(); |
| 3675 | goto finally; |
| 3676 | } |
| 3677 | controllen = controllen_last = 0; |
| 3678 | while (ncmsgbufs < ncmsgs) { |
| 3679 | size_t bufsize, space; |
| 3680 | |
| 3681 | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(cmsg_fast, ncmsgbufs), |
| 3682 | "(iiy*):[sendmsg() ancillary data items]", |
| 3683 | &cmsgs[ncmsgbufs].level, |
| 3684 | &cmsgs[ncmsgbufs].type, |
| 3685 | &cmsgs[ncmsgbufs].data)) |
| 3686 | goto finally; |
| 3687 | bufsize = cmsgs[ncmsgbufs++].data.len; |
| 3688 | |
| 3689 | #ifdef CMSG_SPACE |
| 3690 | if (!get_CMSG_SPACE(bufsize, &space)) { |
| 3691 | #else |
| 3692 | if (!get_CMSG_LEN(bufsize, &space)) { |
| 3693 | #endif |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3694 | PyErr_SetString(PyExc_OSError, "ancillary data item too large"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3695 | goto finally; |
| 3696 | } |
| 3697 | controllen += space; |
| 3698 | if (controllen > SOCKLEN_T_LIMIT || controllen < controllen_last) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3699 | PyErr_SetString(PyExc_OSError, "too much ancillary data"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3700 | goto finally; |
| 3701 | } |
| 3702 | controllen_last = controllen; |
| 3703 | } |
| 3704 | |
| 3705 | /* Construct ancillary data block from control message info. */ |
| 3706 | if (ncmsgbufs > 0) { |
| 3707 | struct cmsghdr *cmsgh = NULL; |
| 3708 | |
| 3709 | if ((msg.msg_control = controlbuf = |
| 3710 | PyMem_Malloc(controllen)) == NULL) { |
| 3711 | PyErr_NoMemory(); |
| 3712 | goto finally; |
| 3713 | } |
| 3714 | msg.msg_controllen = controllen; |
| 3715 | |
| 3716 | /* Need to zero out the buffer as a workaround for glibc's |
| 3717 | CMSG_NXTHDR() implementation. After getting the pointer to |
| 3718 | the next header, it checks its (uninitialized) cmsg_len |
| 3719 | member to see if the "message" fits in the buffer, and |
| 3720 | returns NULL if it doesn't. Zero-filling the buffer |
Terry Jan Reedy | 0f84764 | 2013-03-11 18:34:00 -0400 | [diff] [blame] | 3721 | ensures that this doesn't happen. */ |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3722 | memset(controlbuf, 0, controllen); |
| 3723 | |
| 3724 | for (i = 0; i < ncmsgbufs; i++) { |
| 3725 | size_t msg_len, data_len = cmsgs[i].data.len; |
| 3726 | int enough_space = 0; |
| 3727 | |
| 3728 | cmsgh = (i == 0) ? CMSG_FIRSTHDR(&msg) : CMSG_NXTHDR(&msg, cmsgh); |
| 3729 | if (cmsgh == NULL) { |
| 3730 | PyErr_Format(PyExc_RuntimeError, |
| 3731 | "unexpected NULL result from %s()", |
| 3732 | (i == 0) ? "CMSG_FIRSTHDR" : "CMSG_NXTHDR"); |
| 3733 | goto finally; |
| 3734 | } |
| 3735 | if (!get_CMSG_LEN(data_len, &msg_len)) { |
| 3736 | PyErr_SetString(PyExc_RuntimeError, |
| 3737 | "item size out of range for CMSG_LEN()"); |
| 3738 | goto finally; |
| 3739 | } |
| 3740 | if (cmsg_min_space(&msg, cmsgh, msg_len)) { |
| 3741 | size_t space; |
| 3742 | |
| 3743 | cmsgh->cmsg_len = msg_len; |
| 3744 | if (get_cmsg_data_space(&msg, cmsgh, &space)) |
| 3745 | enough_space = (space >= data_len); |
| 3746 | } |
| 3747 | if (!enough_space) { |
| 3748 | PyErr_SetString(PyExc_RuntimeError, |
| 3749 | "ancillary data does not fit in calculated " |
| 3750 | "space"); |
| 3751 | goto finally; |
| 3752 | } |
| 3753 | cmsgh->cmsg_level = cmsgs[i].level; |
| 3754 | cmsgh->cmsg_type = cmsgs[i].type; |
| 3755 | memcpy(CMSG_DATA(cmsgh), cmsgs[i].data.buf, data_len); |
| 3756 | } |
| 3757 | } |
| 3758 | |
| 3759 | /* Make the system call. */ |
| 3760 | if (!IS_SELECTABLE(s)) { |
| 3761 | select_error(); |
| 3762 | goto finally; |
| 3763 | } |
| 3764 | |
| 3765 | BEGIN_SELECT_LOOP(s) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3766 | do { |
Victor Stinner | 391fa71 | 2015-03-31 13:15:31 +0200 | [diff] [blame^] | 3767 | timeout = internal_select(s, 1, interval); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3768 | |
| 3769 | if (!timeout) { |
| 3770 | Py_BEGIN_ALLOW_THREADS; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3771 | bytes_sent = sendmsg(s->sock_fd, &msg, flags); |
Victor Stinner | b7df314 | 2015-03-27 22:59:32 +0100 | [diff] [blame] | 3772 | Py_END_ALLOW_THREADS; |
| 3773 | } |
| 3774 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3775 | if (timeout == 1) { |
| 3776 | PyErr_SetString(socket_timeout, "timed out"); |
| 3777 | goto finally; |
| 3778 | } |
| 3779 | } while (bytes_sent < 0 && errno == EINTR && |
| 3780 | !(async_err = PyErr_CheckSignals())); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3781 | END_SELECT_LOOP(s) |
| 3782 | |
| 3783 | if (bytes_sent < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3784 | if (!async_err) |
| 3785 | s->errorhandler(); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3786 | goto finally; |
| 3787 | } |
| 3788 | retval = PyLong_FromSsize_t(bytes_sent); |
| 3789 | |
| 3790 | finally: |
| 3791 | PyMem_Free(controlbuf); |
| 3792 | for (i = 0; i < ncmsgbufs; i++) |
| 3793 | PyBuffer_Release(&cmsgs[i].data); |
| 3794 | PyMem_Free(cmsgs); |
| 3795 | Py_XDECREF(cmsg_fast); |
| 3796 | for (i = 0; i < ndatabufs; i++) |
| 3797 | PyBuffer_Release(&databufs[i]); |
| 3798 | PyMem_Free(databufs); |
| 3799 | PyMem_Free(iovs); |
| 3800 | Py_XDECREF(data_fast); |
| 3801 | return retval; |
| 3802 | } |
| 3803 | |
| 3804 | PyDoc_STRVAR(sendmsg_doc, |
| 3805 | "sendmsg(buffers[, ancdata[, flags[, address]]]) -> count\n\ |
| 3806 | \n\ |
| 3807 | Send normal and ancillary data to the socket, gathering the\n\ |
| 3808 | non-ancillary data from a series of buffers and concatenating it into\n\ |
| 3809 | a single message. The buffers argument specifies the non-ancillary\n\ |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 3810 | 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] | 3811 | The ancdata argument specifies the ancillary data (control messages)\n\ |
| 3812 | as an iterable of zero or more tuples (cmsg_level, cmsg_type,\n\ |
| 3813 | cmsg_data), where cmsg_level and cmsg_type are integers specifying the\n\ |
| 3814 | protocol level and protocol-specific type respectively, and cmsg_data\n\ |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 3815 | is a bytes-like object holding the associated data. The flags\n\ |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3816 | argument defaults to 0 and has the same meaning as for send(). If\n\ |
| 3817 | address is supplied and not None, it sets a destination address for\n\ |
| 3818 | the message. The return value is the number of bytes of non-ancillary\n\ |
| 3819 | data sent."); |
| 3820 | #endif /* CMSG_LEN */ |
| 3821 | |
| 3822 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3823 | /* s.shutdown(how) method */ |
| 3824 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3825 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3826 | sock_shutdown(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3827 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3828 | int how; |
| 3829 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 3830 | |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 3831 | how = _PyLong_AsInt(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3832 | if (how == -1 && PyErr_Occurred()) |
| 3833 | return NULL; |
| 3834 | Py_BEGIN_ALLOW_THREADS |
| 3835 | res = shutdown(s->sock_fd, how); |
| 3836 | Py_END_ALLOW_THREADS |
| 3837 | if (res < 0) |
| 3838 | return s->errorhandler(); |
| 3839 | Py_INCREF(Py_None); |
| 3840 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3843 | PyDoc_STRVAR(shutdown_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3844 | "shutdown(flag)\n\ |
| 3845 | \n\ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 3846 | Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\ |
| 3847 | 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] | 3848 | |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 3849 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3850 | static PyObject* |
| 3851 | sock_ioctl(PySocketSockObject *s, PyObject *arg) |
| 3852 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3853 | unsigned long cmd = SIO_RCVALL; |
| 3854 | PyObject *argO; |
| 3855 | DWORD recv; |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3856 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3857 | if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO)) |
| 3858 | return NULL; |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3859 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3860 | switch (cmd) { |
| 3861 | case SIO_RCVALL: { |
| 3862 | unsigned int option = RCVALL_ON; |
| 3863 | if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option)) |
| 3864 | return NULL; |
| 3865 | if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option), |
| 3866 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 3867 | return set_error(); |
| 3868 | } |
| 3869 | return PyLong_FromUnsignedLong(recv); } |
| 3870 | case SIO_KEEPALIVE_VALS: { |
| 3871 | struct tcp_keepalive ka; |
| 3872 | if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd, |
| 3873 | &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval)) |
| 3874 | return NULL; |
| 3875 | if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka), |
| 3876 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 3877 | return set_error(); |
| 3878 | } |
| 3879 | return PyLong_FromUnsignedLong(recv); } |
| 3880 | default: |
| 3881 | PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd); |
| 3882 | return NULL; |
| 3883 | } |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3884 | } |
| 3885 | PyDoc_STRVAR(sock_ioctl_doc, |
| 3886 | "ioctl(cmd, option) -> long\n\ |
| 3887 | \n\ |
Kristján Valur Jónsson | 847ec75 | 2009-09-27 21:10:38 +0000 | [diff] [blame] | 3888 | Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\ |
| 3889 | SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\ |
| 3890 | 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] | 3891 | #endif |
| 3892 | |
| 3893 | #if defined(MS_WINDOWS) |
| 3894 | static PyObject* |
| 3895 | sock_share(PySocketSockObject *s, PyObject *arg) |
| 3896 | { |
| 3897 | WSAPROTOCOL_INFO info; |
| 3898 | DWORD processId; |
| 3899 | int result; |
| 3900 | |
| 3901 | if (!PyArg_ParseTuple(arg, "I", &processId)) |
| 3902 | return NULL; |
| 3903 | |
| 3904 | Py_BEGIN_ALLOW_THREADS |
| 3905 | result = WSADuplicateSocket(s->sock_fd, processId, &info); |
| 3906 | Py_END_ALLOW_THREADS |
| 3907 | if (result == SOCKET_ERROR) |
| 3908 | return set_error(); |
| 3909 | return PyBytes_FromStringAndSize((const char*)&info, sizeof(info)); |
| 3910 | } |
| 3911 | PyDoc_STRVAR(sock_share_doc, |
| 3912 | "share(process_id) -> bytes\n\ |
| 3913 | \n\ |
| 3914 | Share the socket with another process. The target process id\n\ |
| 3915 | must be provided and the resulting bytes object passed to the target\n\ |
| 3916 | process. There the shared socket can be instantiated by calling\n\ |
| 3917 | socket.fromshare()."); |
| 3918 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3919 | |
| 3920 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3921 | |
| 3922 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3923 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3924 | static PyMethodDef sock_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3925 | {"_accept", (PyCFunction)sock_accept, METH_NOARGS, |
| 3926 | accept_doc}, |
| 3927 | {"bind", (PyCFunction)sock_bind, METH_O, |
| 3928 | bind_doc}, |
| 3929 | {"close", (PyCFunction)sock_close, METH_NOARGS, |
| 3930 | close_doc}, |
| 3931 | {"connect", (PyCFunction)sock_connect, METH_O, |
| 3932 | connect_doc}, |
| 3933 | {"connect_ex", (PyCFunction)sock_connect_ex, METH_O, |
| 3934 | connect_ex_doc}, |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 3935 | {"detach", (PyCFunction)sock_detach, METH_NOARGS, |
| 3936 | detach_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3937 | {"fileno", (PyCFunction)sock_fileno, METH_NOARGS, |
| 3938 | fileno_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3939 | #ifdef HAVE_GETPEERNAME |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3940 | {"getpeername", (PyCFunction)sock_getpeername, |
| 3941 | METH_NOARGS, getpeername_doc}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 3942 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3943 | {"getsockname", (PyCFunction)sock_getsockname, |
| 3944 | METH_NOARGS, getsockname_doc}, |
| 3945 | {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS, |
| 3946 | getsockopt_doc}, |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 3947 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3948 | {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS, |
| 3949 | sock_ioctl_doc}, |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3950 | #endif |
Kristján Valur Jónsson | 10f383a | 2012-04-07 11:23:31 +0000 | [diff] [blame] | 3951 | #if defined(MS_WINDOWS) |
| 3952 | {"share", (PyCFunction)sock_share, METH_VARARGS, |
| 3953 | sock_share_doc}, |
| 3954 | #endif |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 3955 | {"listen", (PyCFunction)sock_listen, METH_VARARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3956 | listen_doc}, |
| 3957 | {"recv", (PyCFunction)sock_recv, METH_VARARGS, |
| 3958 | recv_doc}, |
| 3959 | {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS, |
| 3960 | recv_into_doc}, |
| 3961 | {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS, |
| 3962 | recvfrom_doc}, |
| 3963 | {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS, |
| 3964 | recvfrom_into_doc}, |
| 3965 | {"send", (PyCFunction)sock_send, METH_VARARGS, |
| 3966 | send_doc}, |
| 3967 | {"sendall", (PyCFunction)sock_sendall, METH_VARARGS, |
| 3968 | sendall_doc}, |
| 3969 | {"sendto", (PyCFunction)sock_sendto, METH_VARARGS, |
| 3970 | sendto_doc}, |
| 3971 | {"setblocking", (PyCFunction)sock_setblocking, METH_O, |
| 3972 | setblocking_doc}, |
| 3973 | {"settimeout", (PyCFunction)sock_settimeout, METH_O, |
| 3974 | settimeout_doc}, |
| 3975 | {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS, |
| 3976 | gettimeout_doc}, |
| 3977 | {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS, |
| 3978 | setsockopt_doc}, |
| 3979 | {"shutdown", (PyCFunction)sock_shutdown, METH_O, |
| 3980 | shutdown_doc}, |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3981 | #ifdef CMSG_LEN |
| 3982 | {"recvmsg", (PyCFunction)sock_recvmsg, METH_VARARGS, |
| 3983 | recvmsg_doc}, |
| 3984 | {"recvmsg_into", (PyCFunction)sock_recvmsg_into, METH_VARARGS, |
| 3985 | recvmsg_into_doc,}, |
| 3986 | {"sendmsg", (PyCFunction)sock_sendmsg, METH_VARARGS, |
| 3987 | sendmsg_doc}, |
| 3988 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3989 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3990 | }; |
| 3991 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3992 | /* SockObject members */ |
| 3993 | static PyMemberDef sock_memberlist[] = { |
| 3994 | {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"}, |
| 3995 | {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"}, |
| 3996 | {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3997 | {0}, |
| 3998 | }; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3999 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 4000 | static PyGetSetDef sock_getsetlist[] = { |
| 4001 | {"timeout", (getter)sock_gettimeout, NULL, PyDoc_STR("the socket timeout")}, |
| 4002 | {NULL} /* sentinel */ |
| 4003 | }; |
| 4004 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4005 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4006 | First close the file description. */ |
| 4007 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4008 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4009 | sock_dealloc(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4010 | { |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 4011 | if (s->sock_fd != -1) { |
| 4012 | PyObject *exc, *val, *tb; |
| 4013 | Py_ssize_t old_refcount = Py_REFCNT(s); |
| 4014 | ++Py_REFCNT(s); |
| 4015 | PyErr_Fetch(&exc, &val, &tb); |
| 4016 | if (PyErr_WarnFormat(PyExc_ResourceWarning, 1, |
| 4017 | "unclosed %R", s)) |
| 4018 | /* Spurious errors can appear at shutdown */ |
| 4019 | if (PyErr_ExceptionMatches(PyExc_Warning)) |
| 4020 | PyErr_WriteUnraisable((PyObject *) s); |
| 4021 | PyErr_Restore(exc, val, tb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4022 | (void) SOCKETCLOSE(s->sock_fd); |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 4023 | Py_REFCNT(s) = old_refcount; |
| 4024 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4025 | Py_TYPE(s)->tp_free((PyObject *)s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4026 | } |
| 4027 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4028 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4029 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4030 | sock_repr(PySocketSockObject *s) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4031 | { |
Victor Stinner | e254e53 | 2014-07-26 14:36:55 +0200 | [diff] [blame] | 4032 | long sock_fd; |
| 4033 | /* On Windows, this test is needed because SOCKET_T is unsigned */ |
| 4034 | if (s->sock_fd == INVALID_SOCKET) { |
| 4035 | sock_fd = -1; |
| 4036 | } |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 4037 | #if SIZEOF_SOCKET_T > SIZEOF_LONG |
Victor Stinner | e254e53 | 2014-07-26 14:36:55 +0200 | [diff] [blame] | 4038 | else if (s->sock_fd > LONG_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4039 | /* this can occur on Win64, and actually there is a special |
| 4040 | ugly printf formatter for decimal pointer length integer |
| 4041 | printing, only bother if necessary*/ |
| 4042 | PyErr_SetString(PyExc_OverflowError, |
| 4043 | "no printf formatter to display " |
| 4044 | "the socket descriptor in decimal"); |
| 4045 | return NULL; |
| 4046 | } |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 4047 | #endif |
Victor Stinner | e254e53 | 2014-07-26 14:36:55 +0200 | [diff] [blame] | 4048 | else |
| 4049 | sock_fd = (long)s->sock_fd; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4050 | return PyUnicode_FromFormat( |
| 4051 | "<socket object, fd=%ld, family=%d, type=%d, proto=%d>", |
Victor Stinner | e254e53 | 2014-07-26 14:36:55 +0200 | [diff] [blame] | 4052 | sock_fd, s->sock_family, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4053 | s->sock_type, |
| 4054 | s->sock_proto); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4055 | } |
| 4056 | |
| 4057 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4058 | /* Create a new, uninitialized socket object. */ |
| 4059 | |
| 4060 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4061 | sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4062 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4063 | PyObject *new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4065 | new = type->tp_alloc(type, 0); |
| 4066 | if (new != NULL) { |
| 4067 | ((PySocketSockObject *)new)->sock_fd = -1; |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 4068 | ((PySocketSockObject *)new)->sock_timeout = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4069 | ((PySocketSockObject *)new)->errorhandler = &set_error; |
| 4070 | } |
| 4071 | return new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4072 | } |
| 4073 | |
| 4074 | |
| 4075 | /* Initialize a new socket object. */ |
| 4076 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4077 | #ifdef SOCK_CLOEXEC |
| 4078 | /* socket() and socketpair() fail with EINVAL on Linux kernel older |
| 4079 | * than 2.6.27 if SOCK_CLOEXEC flag is set in the socket type. */ |
| 4080 | static int sock_cloexec_works = -1; |
| 4081 | #endif |
| 4082 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4083 | /*ARGSUSED*/ |
| 4084 | static int |
Andrew MacIntyre | 7aec4a2 | 2002-06-13 11:53:52 +0000 | [diff] [blame] | 4085 | sock_initobj(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4086 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4087 | PySocketSockObject *s = (PySocketSockObject *)self; |
| 4088 | PyObject *fdobj = NULL; |
| 4089 | SOCKET_T fd = INVALID_SOCKET; |
| 4090 | int family = AF_INET, type = SOCK_STREAM, proto = 0; |
| 4091 | static char *keywords[] = {"family", "type", "proto", "fileno", 0}; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4092 | #ifndef MS_WINDOWS |
| 4093 | #ifdef SOCK_CLOEXEC |
| 4094 | int *atomic_flag_works = &sock_cloexec_works; |
| 4095 | #else |
| 4096 | int *atomic_flag_works = NULL; |
| 4097 | #endif |
| 4098 | #endif |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4099 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4100 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 4101 | "|iiiO:socket", keywords, |
| 4102 | &family, &type, &proto, &fdobj)) |
| 4103 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4104 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4105 | if (fdobj != NULL && fdobj != Py_None) { |
Kristján Valur Jónsson | 10f383a | 2012-04-07 11:23:31 +0000 | [diff] [blame] | 4106 | #ifdef MS_WINDOWS |
| 4107 | /* recreate a socket that was duplicated */ |
| 4108 | if (PyBytes_Check(fdobj)) { |
| 4109 | WSAPROTOCOL_INFO info; |
| 4110 | if (PyBytes_GET_SIZE(fdobj) != sizeof(info)) { |
| 4111 | PyErr_Format(PyExc_ValueError, |
| 4112 | "socket descriptor string has wrong size, " |
| 4113 | "should be %zu bytes.", sizeof(info)); |
| 4114 | return -1; |
| 4115 | } |
| 4116 | memcpy(&info, PyBytes_AS_STRING(fdobj), sizeof(info)); |
| 4117 | Py_BEGIN_ALLOW_THREADS |
| 4118 | fd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, |
| 4119 | FROM_PROTOCOL_INFO, &info, 0, WSA_FLAG_OVERLAPPED); |
| 4120 | Py_END_ALLOW_THREADS |
| 4121 | if (fd == INVALID_SOCKET) { |
| 4122 | set_error(); |
| 4123 | return -1; |
| 4124 | } |
| 4125 | family = info.iAddressFamily; |
| 4126 | type = info.iSocketType; |
| 4127 | proto = info.iProtocol; |
| 4128 | } |
| 4129 | else |
| 4130 | #endif |
| 4131 | { |
| 4132 | fd = PyLong_AsSocket_t(fdobj); |
| 4133 | if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
| 4134 | return -1; |
| 4135 | if (fd == INVALID_SOCKET) { |
| 4136 | PyErr_SetString(PyExc_ValueError, |
| 4137 | "can't use invalid socket value"); |
| 4138 | return -1; |
| 4139 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4140 | } |
| 4141 | } |
| 4142 | else { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4143 | #ifdef MS_WINDOWS |
| 4144 | /* Windows implementation */ |
| 4145 | #ifndef WSA_FLAG_NO_HANDLE_INHERIT |
| 4146 | #define WSA_FLAG_NO_HANDLE_INHERIT 0x80 |
| 4147 | #endif |
| 4148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4149 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4150 | if (support_wsa_no_inherit) { |
| 4151 | fd = WSASocket(family, type, proto, |
| 4152 | NULL, 0, |
| 4153 | WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT); |
| 4154 | if (fd == INVALID_SOCKET) { |
| 4155 | /* Windows 7 or Windows 2008 R2 without SP1 or the hotfix */ |
| 4156 | support_wsa_no_inherit = 0; |
| 4157 | fd = socket(family, type, proto); |
| 4158 | } |
| 4159 | } |
| 4160 | else { |
| 4161 | fd = socket(family, type, proto); |
| 4162 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4163 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4165 | if (fd == INVALID_SOCKET) { |
| 4166 | set_error(); |
| 4167 | return -1; |
| 4168 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4169 | |
| 4170 | if (!support_wsa_no_inherit) { |
| 4171 | if (!SetHandleInformation((HANDLE)fd, HANDLE_FLAG_INHERIT, 0)) { |
| 4172 | closesocket(fd); |
| 4173 | PyErr_SetFromWindowsErr(0); |
| 4174 | return -1; |
| 4175 | } |
| 4176 | } |
| 4177 | #else |
| 4178 | /* UNIX */ |
| 4179 | Py_BEGIN_ALLOW_THREADS |
| 4180 | #ifdef SOCK_CLOEXEC |
| 4181 | if (sock_cloexec_works != 0) { |
| 4182 | fd = socket(family, type | SOCK_CLOEXEC, proto); |
| 4183 | if (sock_cloexec_works == -1) { |
| 4184 | if (fd >= 0) { |
| 4185 | sock_cloexec_works = 1; |
| 4186 | } |
| 4187 | else if (errno == EINVAL) { |
| 4188 | /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */ |
| 4189 | sock_cloexec_works = 0; |
| 4190 | fd = socket(family, type, proto); |
| 4191 | } |
| 4192 | } |
| 4193 | } |
| 4194 | else |
| 4195 | #endif |
| 4196 | { |
| 4197 | fd = socket(family, type, proto); |
| 4198 | } |
| 4199 | Py_END_ALLOW_THREADS |
| 4200 | |
| 4201 | if (fd == INVALID_SOCKET) { |
| 4202 | set_error(); |
| 4203 | return -1; |
| 4204 | } |
| 4205 | |
| 4206 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 4207 | SOCKETCLOSE(fd); |
| 4208 | return -1; |
| 4209 | } |
| 4210 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4211 | } |
| 4212 | init_sockobject(s, fd, family, type, proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4214 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4215 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4216 | } |
| 4217 | |
| 4218 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4219 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4220 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4221 | static PyTypeObject sock_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4222 | PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */ |
| 4223 | "_socket.socket", /* tp_name */ |
| 4224 | sizeof(PySocketSockObject), /* tp_basicsize */ |
| 4225 | 0, /* tp_itemsize */ |
| 4226 | (destructor)sock_dealloc, /* tp_dealloc */ |
| 4227 | 0, /* tp_print */ |
| 4228 | 0, /* tp_getattr */ |
| 4229 | 0, /* tp_setattr */ |
| 4230 | 0, /* tp_reserved */ |
| 4231 | (reprfunc)sock_repr, /* tp_repr */ |
| 4232 | 0, /* tp_as_number */ |
| 4233 | 0, /* tp_as_sequence */ |
| 4234 | 0, /* tp_as_mapping */ |
| 4235 | 0, /* tp_hash */ |
| 4236 | 0, /* tp_call */ |
| 4237 | 0, /* tp_str */ |
| 4238 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4239 | 0, /* tp_setattro */ |
| 4240 | 0, /* tp_as_buffer */ |
| 4241 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 4242 | sock_doc, /* tp_doc */ |
| 4243 | 0, /* tp_traverse */ |
| 4244 | 0, /* tp_clear */ |
| 4245 | 0, /* tp_richcompare */ |
| 4246 | 0, /* tp_weaklistoffset */ |
| 4247 | 0, /* tp_iter */ |
| 4248 | 0, /* tp_iternext */ |
| 4249 | sock_methods, /* tp_methods */ |
| 4250 | sock_memberlist, /* tp_members */ |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 4251 | sock_getsetlist, /* tp_getset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4252 | 0, /* tp_base */ |
| 4253 | 0, /* tp_dict */ |
| 4254 | 0, /* tp_descr_get */ |
| 4255 | 0, /* tp_descr_set */ |
| 4256 | 0, /* tp_dictoffset */ |
| 4257 | sock_initobj, /* tp_init */ |
| 4258 | PyType_GenericAlloc, /* tp_alloc */ |
| 4259 | sock_new, /* tp_new */ |
| 4260 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4261 | }; |
| 4262 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4263 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 4264 | /* Python interface to gethostname(). */ |
| 4265 | |
| 4266 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4267 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4268 | socket_gethostname(PyObject *self, PyObject *unused) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 4269 | { |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4270 | #ifdef MS_WINDOWS |
| 4271 | /* Don't use winsock's gethostname, as this returns the ANSI |
| 4272 | version of the hostname, whereas we need a Unicode string. |
| 4273 | Otherwise, gethostname apparently also returns the DNS name. */ |
Hirokazu Yamamoto | 09fff7a | 2010-11-05 17:24:13 +0000 | [diff] [blame] | 4274 | wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 4275 | DWORD size = Py_ARRAY_LENGTH(buf); |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4276 | wchar_t *name; |
Hirokazu Yamamoto | 09fff7a | 2010-11-05 17:24:13 +0000 | [diff] [blame] | 4277 | PyObject *result; |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4278 | |
| 4279 | if (GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size)) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 4280 | return PyUnicode_FromWideChar(buf, size); |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4281 | |
| 4282 | if (GetLastError() != ERROR_MORE_DATA) |
| 4283 | return PyErr_SetFromWindowsErr(0); |
| 4284 | |
| 4285 | if (size == 0) |
| 4286 | return PyUnicode_New(0, 0); |
| 4287 | |
| 4288 | /* MSDN says ERROR_MORE_DATA may occur because DNS allows longer |
| 4289 | names */ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 4290 | name = PyMem_New(wchar_t, size); |
| 4291 | if (!name) { |
| 4292 | PyErr_NoMemory(); |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4293 | return NULL; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 4294 | } |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4295 | if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname, |
| 4296 | name, |
| 4297 | &size)) |
| 4298 | { |
| 4299 | PyMem_Free(name); |
| 4300 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4301 | } |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4302 | |
| 4303 | result = PyUnicode_FromWideChar(name, size); |
| 4304 | PyMem_Free(name); |
| 4305 | return result; |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4306 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4307 | char buf[1024]; |
| 4308 | int res; |
| 4309 | Py_BEGIN_ALLOW_THREADS |
| 4310 | res = gethostname(buf, (int) sizeof buf - 1); |
| 4311 | Py_END_ALLOW_THREADS |
| 4312 | if (res < 0) |
| 4313 | return set_error(); |
| 4314 | buf[sizeof buf - 1] = '\0'; |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4315 | return PyUnicode_DecodeFSDefault(buf); |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4316 | #endif |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 4317 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 4318 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4319 | PyDoc_STRVAR(gethostname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4320 | "gethostname() -> string\n\ |
| 4321 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4322 | Return the current host name."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4323 | |
Antoine Pitrou | 061cfb5 | 2011-02-28 22:25:22 +0000 | [diff] [blame] | 4324 | #ifdef HAVE_SETHOSTNAME |
| 4325 | PyDoc_STRVAR(sethostname_doc, |
| 4326 | "sethostname(name)\n\n\ |
| 4327 | Sets the hostname to name."); |
| 4328 | |
| 4329 | static PyObject * |
| 4330 | socket_sethostname(PyObject *self, PyObject *args) |
| 4331 | { |
| 4332 | PyObject *hnobj; |
| 4333 | Py_buffer buf; |
| 4334 | int res, flag = 0; |
| 4335 | |
Christian Heimes | d2774c7 | 2013-06-19 02:06:29 +0200 | [diff] [blame] | 4336 | #ifdef _AIX |
| 4337 | /* issue #18259, not declared in any useful header file */ |
| 4338 | extern int sethostname(const char *, size_t); |
| 4339 | #endif |
| 4340 | |
Antoine Pitrou | 061cfb5 | 2011-02-28 22:25:22 +0000 | [diff] [blame] | 4341 | if (!PyArg_ParseTuple(args, "S:sethostname", &hnobj)) { |
| 4342 | PyErr_Clear(); |
| 4343 | if (!PyArg_ParseTuple(args, "O&:sethostname", |
| 4344 | PyUnicode_FSConverter, &hnobj)) |
| 4345 | return NULL; |
| 4346 | flag = 1; |
| 4347 | } |
| 4348 | res = PyObject_GetBuffer(hnobj, &buf, PyBUF_SIMPLE); |
| 4349 | if (!res) { |
| 4350 | res = sethostname(buf.buf, buf.len); |
| 4351 | PyBuffer_Release(&buf); |
| 4352 | } |
| 4353 | if (flag) |
| 4354 | Py_DECREF(hnobj); |
| 4355 | if (res) |
| 4356 | return set_error(); |
| 4357 | Py_RETURN_NONE; |
| 4358 | } |
| 4359 | #endif |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 4360 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4361 | /* Python interface to gethostbyname(name). */ |
| 4362 | |
| 4363 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4364 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4365 | socket_gethostbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4366 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4367 | char *name; |
| 4368 | sock_addr_t addrbuf; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4369 | PyObject *ret = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4370 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4371 | if (!PyArg_ParseTuple(args, "et:gethostbyname", "idna", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4372 | return NULL; |
| 4373 | if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4374 | goto finally; |
| 4375 | ret = makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in)); |
| 4376 | finally: |
| 4377 | PyMem_Free(name); |
| 4378 | return ret; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4379 | } |
| 4380 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4381 | PyDoc_STRVAR(gethostbyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4382 | "gethostbyname(host) -> address\n\ |
| 4383 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4384 | 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] | 4385 | |
| 4386 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4387 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
| 4388 | |
| 4389 | static PyObject * |
Victor Stinner | 1a62a68 | 2014-08-17 19:33:28 +0200 | [diff] [blame] | 4390 | 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] | 4391 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4392 | char **pch; |
| 4393 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 4394 | PyObject *name_list = (PyObject *)NULL; |
| 4395 | PyObject *addr_list = (PyObject *)NULL; |
| 4396 | PyObject *tmp; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4398 | if (h == NULL) { |
| 4399 | /* Let's get real error message to return */ |
| 4400 | set_herror(h_errno); |
| 4401 | return NULL; |
| 4402 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4403 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4404 | if (h->h_addrtype != af) { |
| 4405 | /* Let's get real error message to return */ |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 4406 | errno = EAFNOSUPPORT; |
| 4407 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4408 | return NULL; |
| 4409 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4410 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4411 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4413 | case AF_INET: |
| 4414 | if (alen < sizeof(struct sockaddr_in)) |
| 4415 | return NULL; |
| 4416 | break; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4417 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4418 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4419 | case AF_INET6: |
| 4420 | if (alen < sizeof(struct sockaddr_in6)) |
| 4421 | return NULL; |
| 4422 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4423 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4424 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4425 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4426 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4427 | if ((name_list = PyList_New(0)) == NULL) |
| 4428 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4429 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4430 | if ((addr_list = PyList_New(0)) == NULL) |
| 4431 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4432 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4433 | /* SF #1511317: h_aliases can be NULL */ |
| 4434 | if (h->h_aliases) { |
| 4435 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 4436 | int status; |
| 4437 | tmp = PyUnicode_FromString(*pch); |
| 4438 | if (tmp == NULL) |
| 4439 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4441 | status = PyList_Append(name_list, tmp); |
| 4442 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4443 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4444 | if (status) |
| 4445 | goto err; |
| 4446 | } |
| 4447 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4448 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4449 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 4450 | int status; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4452 | switch (af) { |
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 | case AF_INET: |
| 4455 | { |
| 4456 | struct sockaddr_in sin; |
| 4457 | memset(&sin, 0, sizeof(sin)); |
| 4458 | sin.sin_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4459 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4460 | sin.sin_len = sizeof(sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4461 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4462 | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); |
| 4463 | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4464 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4465 | if (pch == h->h_addr_list && alen >= sizeof(sin)) |
| 4466 | memcpy((char *) addr, &sin, sizeof(sin)); |
| 4467 | break; |
| 4468 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4469 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4470 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4471 | case AF_INET6: |
| 4472 | { |
| 4473 | struct sockaddr_in6 sin6; |
| 4474 | memset(&sin6, 0, sizeof(sin6)); |
| 4475 | sin6.sin6_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4476 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4477 | sin6.sin6_len = sizeof(sin6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4478 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4479 | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); |
| 4480 | tmp = makeipaddr((struct sockaddr *)&sin6, |
| 4481 | sizeof(sin6)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4482 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4483 | if (pch == h->h_addr_list && alen >= sizeof(sin6)) |
| 4484 | memcpy((char *) addr, &sin6, sizeof(sin6)); |
| 4485 | break; |
| 4486 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4487 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4488 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4489 | default: /* can't happen */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4490 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4491 | "unsupported address family"); |
| 4492 | return NULL; |
| 4493 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4494 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4495 | if (tmp == NULL) |
| 4496 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4497 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4498 | status = PyList_Append(addr_list, tmp); |
| 4499 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4501 | if (status) |
| 4502 | goto err; |
| 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 | 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] | 4506 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4507 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4508 | Py_XDECREF(name_list); |
| 4509 | Py_XDECREF(addr_list); |
| 4510 | return rtn_tuple; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4511 | } |
| 4512 | |
| 4513 | |
| 4514 | /* Python interface to gethostbyname_ex(name). */ |
| 4515 | |
| 4516 | /*ARGSUSED*/ |
| 4517 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4518 | socket_gethostbyname_ex(PyObject *self, PyObject *args) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4519 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4520 | char *name; |
| 4521 | struct hostent *h; |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4522 | sock_addr_t addr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4523 | struct sockaddr *sa; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4524 | PyObject *ret = NULL; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4525 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4526 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4527 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4528 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4529 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4530 | char buf[16384]; |
| 4531 | int buf_len = (sizeof buf) - 1; |
| 4532 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4533 | #endif |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4534 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4535 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4536 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4537 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4538 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4539 | if (!PyArg_ParseTuple(args, "et:gethostbyname_ex", "idna", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4540 | return NULL; |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4541 | if (setipaddr(name, SAS2SA(&addr), sizeof(addr), AF_INET) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4542 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4543 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4544 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4545 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4546 | gethostbyname_r(name, &hp_allocated, buf, buf_len, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4547 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4548 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4549 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4550 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4551 | memset((void *) &data, '\0', sizeof(data)); |
| 4552 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 4553 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4554 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4555 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4556 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4557 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4558 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4559 | h = gethostbyname(name); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4560 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4561 | Py_END_ALLOW_THREADS |
| 4562 | /* Some C libraries would require addr.__ss_family instead of |
| 4563 | addr.ss_family. |
| 4564 | Therefore, we cast the sockaddr_storage into sockaddr to |
| 4565 | access sa_family. */ |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4566 | sa = SAS2SA(&addr); |
| 4567 | ret = gethost_common(h, SAS2SA(&addr), sizeof(addr), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4568 | sa->sa_family); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4569 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4570 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 4571 | #endif |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4572 | finally: |
| 4573 | PyMem_Free(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4574 | return ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4575 | } |
| 4576 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4577 | PyDoc_STRVAR(ghbn_ex_doc, |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4578 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
| 4579 | \n\ |
| 4580 | 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] | 4581 | 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] | 4582 | |
| 4583 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4584 | /* Python interface to gethostbyaddr(IP). */ |
| 4585 | |
| 4586 | /*ARGSUSED*/ |
| 4587 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4588 | socket_gethostbyaddr(PyObject *self, PyObject *args) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4589 | { |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4590 | sock_addr_t addr; |
| 4591 | struct sockaddr *sa = SAS2SA(&addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4592 | char *ip_num; |
| 4593 | struct hostent *h; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4594 | PyObject *ret = NULL; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4595 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4596 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4597 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4598 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4599 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4600 | /* glibcs up to 2.10 assume that the buf argument to |
| 4601 | gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc |
| 4602 | does not ensure. The attribute below instructs the compiler |
| 4603 | to maintain this alignment. */ |
| 4604 | char buf[16384] Py_ALIGNED(8); |
| 4605 | int buf_len = (sizeof buf) - 1; |
| 4606 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4607 | #endif |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4608 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4609 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4610 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4611 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4612 | char *ap; |
| 4613 | int al; |
| 4614 | int af; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4615 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4616 | if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4617 | return NULL; |
| 4618 | af = AF_UNSPEC; |
| 4619 | if (setipaddr(ip_num, sa, sizeof(addr), af) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4620 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4621 | af = sa->sa_family; |
| 4622 | ap = NULL; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 4623 | /* al = 0; */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4624 | switch (af) { |
| 4625 | case AF_INET: |
| 4626 | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr; |
| 4627 | al = sizeof(((struct sockaddr_in *)sa)->sin_addr); |
| 4628 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4629 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4630 | case AF_INET6: |
| 4631 | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr; |
| 4632 | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); |
| 4633 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4634 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4635 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4636 | PyErr_SetString(PyExc_OSError, "unsupported address family"); |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4637 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4638 | } |
| 4639 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4640 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4641 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4642 | gethostbyaddr_r(ap, al, af, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4643 | &hp_allocated, buf, buf_len, |
| 4644 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4645 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4646 | h = gethostbyaddr_r(ap, al, af, |
| 4647 | &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4648 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4649 | memset((void *) &data, '\0', sizeof(data)); |
| 4650 | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); |
| 4651 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4652 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4653 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4654 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4655 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4656 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4657 | h = gethostbyaddr(ap, al, af); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4658 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4659 | Py_END_ALLOW_THREADS |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4660 | ret = gethost_common(h, SAS2SA(&addr), sizeof(addr), af); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4661 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4662 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4663 | #endif |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4664 | finally: |
| 4665 | PyMem_Free(ip_num); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4666 | return ret; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4667 | } |
| 4668 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4669 | PyDoc_STRVAR(gethostbyaddr_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4670 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
| 4671 | \n\ |
| 4672 | 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] | 4673 | 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] | 4674 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4675 | |
| 4676 | /* Python interface to getservbyname(name). |
| 4677 | This only returns the port number, since the other info is already |
| 4678 | known or not useful (like the list of aliases). */ |
| 4679 | |
| 4680 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4681 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4682 | socket_getservbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4683 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4684 | char *name, *proto=NULL; |
| 4685 | struct servent *sp; |
| 4686 | if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto)) |
| 4687 | return NULL; |
| 4688 | Py_BEGIN_ALLOW_THREADS |
| 4689 | sp = getservbyname(name, proto); |
| 4690 | Py_END_ALLOW_THREADS |
| 4691 | if (sp == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4692 | PyErr_SetString(PyExc_OSError, "service/proto not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4693 | return NULL; |
| 4694 | } |
| 4695 | return PyLong_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4696 | } |
| 4697 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4698 | PyDoc_STRVAR(getservbyname_doc, |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4699 | "getservbyname(servicename[, protocolname]) -> integer\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4700 | \n\ |
| 4701 | Return a port number from a service name and protocol name.\n\ |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4702 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 4703 | otherwise any protocol will match."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4704 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4705 | |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4706 | /* Python interface to getservbyport(port). |
| 4707 | This only returns the service name, since the other info is already |
| 4708 | known or not useful (like the list of aliases). */ |
| 4709 | |
| 4710 | /*ARGSUSED*/ |
| 4711 | static PyObject * |
| 4712 | socket_getservbyport(PyObject *self, PyObject *args) |
| 4713 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4714 | int port; |
| 4715 | char *proto=NULL; |
| 4716 | struct servent *sp; |
| 4717 | if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto)) |
| 4718 | return NULL; |
| 4719 | if (port < 0 || port > 0xffff) { |
| 4720 | PyErr_SetString( |
| 4721 | PyExc_OverflowError, |
| 4722 | "getservbyport: port must be 0-65535."); |
| 4723 | return NULL; |
| 4724 | } |
| 4725 | Py_BEGIN_ALLOW_THREADS |
| 4726 | sp = getservbyport(htons((short)port), proto); |
| 4727 | Py_END_ALLOW_THREADS |
| 4728 | if (sp == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4729 | PyErr_SetString(PyExc_OSError, "port/proto not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4730 | return NULL; |
| 4731 | } |
| 4732 | return PyUnicode_FromString(sp->s_name); |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4733 | } |
| 4734 | |
| 4735 | PyDoc_STRVAR(getservbyport_doc, |
| 4736 | "getservbyport(port[, protocolname]) -> string\n\ |
| 4737 | \n\ |
| 4738 | Return the service name from a port number and protocol name.\n\ |
| 4739 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 4740 | otherwise any protocol will match."); |
| 4741 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4742 | /* Python interface to getprotobyname(name). |
| 4743 | This only returns the protocol number, since the other info is |
| 4744 | already known or not useful (like the list of aliases). */ |
| 4745 | |
| 4746 | /*ARGSUSED*/ |
| 4747 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4748 | socket_getprotobyname(PyObject *self, PyObject *args) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4749 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4750 | char *name; |
| 4751 | struct protoent *sp; |
| 4752 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) |
| 4753 | return NULL; |
| 4754 | Py_BEGIN_ALLOW_THREADS |
| 4755 | sp = getprotobyname(name); |
| 4756 | Py_END_ALLOW_THREADS |
| 4757 | if (sp == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4758 | PyErr_SetString(PyExc_OSError, "protocol not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4759 | return NULL; |
| 4760 | } |
| 4761 | return PyLong_FromLong((long) sp->p_proto); |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4762 | } |
| 4763 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4764 | PyDoc_STRVAR(getprotobyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4765 | "getprotobyname(name) -> integer\n\ |
| 4766 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4767 | Return the protocol number for the named protocol. (Rarely used.)"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4768 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4769 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4770 | #ifndef NO_DUP |
| 4771 | /* dup() function for socket fds */ |
| 4772 | |
| 4773 | static PyObject * |
| 4774 | socket_dup(PyObject *self, PyObject *fdobj) |
| 4775 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4776 | SOCKET_T fd, newfd; |
| 4777 | PyObject *newfdobj; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4778 | #ifdef MS_WINDOWS |
| 4779 | WSAPROTOCOL_INFO info; |
| 4780 | #endif |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4781 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4782 | fd = PyLong_AsSocket_t(fdobj); |
| 4783 | if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
| 4784 | return NULL; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4785 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4786 | #ifdef MS_WINDOWS |
| 4787 | if (WSADuplicateSocket(fd, GetCurrentProcessId(), &info)) |
| 4788 | return set_error(); |
| 4789 | |
| 4790 | newfd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, |
| 4791 | FROM_PROTOCOL_INFO, |
| 4792 | &info, 0, WSA_FLAG_OVERLAPPED); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4793 | if (newfd == INVALID_SOCKET) |
| 4794 | return set_error(); |
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 | if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) { |
| 4797 | closesocket(newfd); |
| 4798 | PyErr_SetFromWindowsErr(0); |
| 4799 | return NULL; |
| 4800 | } |
| 4801 | #else |
| 4802 | /* On UNIX, dup can be used to duplicate the file descriptor of a socket */ |
| 4803 | newfd = _Py_dup(fd); |
| 4804 | if (newfd == INVALID_SOCKET) |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 4805 | return NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4806 | #endif |
| 4807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4808 | newfdobj = PyLong_FromSocket_t(newfd); |
| 4809 | if (newfdobj == NULL) |
| 4810 | SOCKETCLOSE(newfd); |
| 4811 | return newfdobj; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4812 | } |
| 4813 | |
| 4814 | PyDoc_STRVAR(dup_doc, |
| 4815 | "dup(integer) -> integer\n\ |
| 4816 | \n\ |
| 4817 | Duplicate an integer socket file descriptor. This is like os.dup(), but for\n\ |
| 4818 | sockets; on some platforms os.dup() won't work for socket file descriptors."); |
| 4819 | #endif |
| 4820 | |
| 4821 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4822 | #ifdef HAVE_SOCKETPAIR |
| 4823 | /* Create a pair of sockets using the socketpair() function. |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 4824 | Arguments as for socket() except the default family is AF_UNIX if |
Dave Cole | e8bbfe4 | 2004-08-26 00:51:16 +0000 | [diff] [blame] | 4825 | defined on the platform; otherwise, the default is AF_INET. */ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4826 | |
| 4827 | /*ARGSUSED*/ |
| 4828 | static PyObject * |
| 4829 | socket_socketpair(PyObject *self, PyObject *args) |
| 4830 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4831 | PySocketSockObject *s0 = NULL, *s1 = NULL; |
| 4832 | SOCKET_T sv[2]; |
| 4833 | int family, type = SOCK_STREAM, proto = 0; |
| 4834 | PyObject *res = NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4835 | #ifdef SOCK_CLOEXEC |
| 4836 | int *atomic_flag_works = &sock_cloexec_works; |
| 4837 | #else |
| 4838 | int *atomic_flag_works = NULL; |
| 4839 | #endif |
| 4840 | int ret; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4841 | |
| 4842 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4843 | family = AF_UNIX; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4844 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4845 | family = AF_INET; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4846 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4847 | if (!PyArg_ParseTuple(args, "|iii:socketpair", |
| 4848 | &family, &type, &proto)) |
| 4849 | return NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4850 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4851 | /* Create a pair of socket fds */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4852 | Py_BEGIN_ALLOW_THREADS |
| 4853 | #ifdef SOCK_CLOEXEC |
| 4854 | if (sock_cloexec_works != 0) { |
| 4855 | ret = socketpair(family, type | SOCK_CLOEXEC, proto, sv); |
| 4856 | if (sock_cloexec_works == -1) { |
| 4857 | if (ret >= 0) { |
| 4858 | sock_cloexec_works = 1; |
| 4859 | } |
| 4860 | else if (errno == EINVAL) { |
| 4861 | /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */ |
| 4862 | sock_cloexec_works = 0; |
| 4863 | ret = socketpair(family, type, proto, sv); |
| 4864 | } |
| 4865 | } |
| 4866 | } |
| 4867 | else |
| 4868 | #endif |
| 4869 | { |
| 4870 | ret = socketpair(family, type, proto, sv); |
| 4871 | } |
| 4872 | Py_END_ALLOW_THREADS |
| 4873 | |
| 4874 | if (ret < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4875 | return set_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4876 | |
| 4877 | if (_Py_set_inheritable(sv[0], 0, atomic_flag_works) < 0) |
| 4878 | goto finally; |
| 4879 | if (_Py_set_inheritable(sv[1], 0, atomic_flag_works) < 0) |
| 4880 | goto finally; |
| 4881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4882 | s0 = new_sockobject(sv[0], family, type, proto); |
| 4883 | if (s0 == NULL) |
| 4884 | goto finally; |
| 4885 | s1 = new_sockobject(sv[1], family, type, proto); |
| 4886 | if (s1 == NULL) |
| 4887 | goto finally; |
| 4888 | res = PyTuple_Pack(2, s0, s1); |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4889 | |
| 4890 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4891 | if (res == NULL) { |
| 4892 | if (s0 == NULL) |
| 4893 | SOCKETCLOSE(sv[0]); |
| 4894 | if (s1 == NULL) |
| 4895 | SOCKETCLOSE(sv[1]); |
| 4896 | } |
| 4897 | Py_XDECREF(s0); |
| 4898 | Py_XDECREF(s1); |
| 4899 | return res; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4900 | } |
| 4901 | |
| 4902 | PyDoc_STRVAR(socketpair_doc, |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4903 | "socketpair([family[, type [, proto]]]) -> (socket object, socket object)\n\ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4904 | \n\ |
| 4905 | Create a pair of socket objects from the sockets returned by the platform\n\ |
| 4906 | socketpair() function.\n\ |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 4907 | 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] | 4908 | 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] | 4909 | |
| 4910 | #endif /* HAVE_SOCKETPAIR */ |
| 4911 | |
| 4912 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4913 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4914 | socket_ntohs(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4915 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4916 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4918 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { |
| 4919 | return NULL; |
| 4920 | } |
| 4921 | if (x1 < 0) { |
| 4922 | PyErr_SetString(PyExc_OverflowError, |
| 4923 | "can't convert negative number to unsigned long"); |
| 4924 | return NULL; |
| 4925 | } |
| 4926 | x2 = (unsigned int)ntohs((unsigned short)x1); |
| 4927 | return PyLong_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4928 | } |
| 4929 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4930 | PyDoc_STRVAR(ntohs_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4931 | "ntohs(integer) -> integer\n\ |
| 4932 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4933 | Convert a 16-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4934 | |
| 4935 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4936 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4937 | socket_ntohl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4938 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4939 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4940 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4941 | if (PyLong_Check(arg)) { |
| 4942 | x = PyLong_AsUnsignedLong(arg); |
| 4943 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 4944 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4945 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4946 | { |
| 4947 | unsigned long y; |
| 4948 | /* only want the trailing 32 bits */ |
| 4949 | y = x & 0xFFFFFFFFUL; |
| 4950 | if (y ^ x) |
| 4951 | return PyErr_Format(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4952 | "int larger than 32 bits"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4953 | x = y; |
| 4954 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4955 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4956 | } |
| 4957 | else |
| 4958 | return PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4959 | "expected int, %s found", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4960 | Py_TYPE(arg)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4961 | return PyLong_FromUnsignedLong(ntohl(x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4962 | } |
| 4963 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4964 | PyDoc_STRVAR(ntohl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4965 | "ntohl(integer) -> integer\n\ |
| 4966 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4967 | Convert a 32-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4968 | |
| 4969 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4970 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4971 | socket_htons(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4972 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4973 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4974 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4975 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) { |
| 4976 | return NULL; |
| 4977 | } |
| 4978 | if (x1 < 0) { |
| 4979 | PyErr_SetString(PyExc_OverflowError, |
| 4980 | "can't convert negative number to unsigned long"); |
| 4981 | return NULL; |
| 4982 | } |
| 4983 | x2 = (unsigned int)htons((unsigned short)x1); |
| 4984 | return PyLong_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4985 | } |
| 4986 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4987 | PyDoc_STRVAR(htons_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4988 | "htons(integer) -> integer\n\ |
| 4989 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4990 | Convert a 16-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4991 | |
| 4992 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4993 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4994 | socket_htonl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4995 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4996 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4997 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4998 | if (PyLong_Check(arg)) { |
| 4999 | x = PyLong_AsUnsignedLong(arg); |
| 5000 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 5001 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 5002 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5003 | { |
| 5004 | unsigned long y; |
| 5005 | /* only want the trailing 32 bits */ |
| 5006 | y = x & 0xFFFFFFFFUL; |
| 5007 | if (y ^ x) |
| 5008 | return PyErr_Format(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 5009 | "int larger than 32 bits"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5010 | x = y; |
| 5011 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 5012 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5013 | } |
| 5014 | else |
| 5015 | return PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 5016 | "expected int, %s found", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5017 | Py_TYPE(arg)->tp_name); |
| 5018 | return PyLong_FromUnsignedLong(htonl((unsigned long)x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 5019 | } |
| 5020 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5021 | PyDoc_STRVAR(htonl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5022 | "htonl(integer) -> integer\n\ |
| 5023 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5024 | Convert a 32-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5025 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5026 | /* socket.inet_aton() and socket.inet_ntoa() functions. */ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5027 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5028 | PyDoc_STRVAR(inet_aton_doc, |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 5029 | "inet_aton(string) -> bytes giving packed 32-bit IP representation\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5030 | \n\ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 5031 | 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] | 5032 | binary format used in low-level network functions."); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5033 | |
| 5034 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5035 | socket_inet_aton(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5036 | { |
Neal Norwitz | 88f115b | 2003-02-13 02:15:42 +0000 | [diff] [blame] | 5037 | #ifdef HAVE_INET_ATON |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5038 | struct in_addr buf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5039 | #endif |
| 5040 | |
| 5041 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 5042 | #if (SIZEOF_INT != 4) |
| 5043 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 5044 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5045 | /* Have to use inet_addr() instead */ |
| 5046 | unsigned int packed_addr; |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 5047 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5048 | char *ip_addr; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 5049 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5050 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) |
| 5051 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 5052 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 5053 | |
| 5054 | #ifdef HAVE_INET_ATON |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5055 | |
| 5056 | #ifdef USE_INET_ATON_WEAKLINK |
| 5057 | if (inet_aton != NULL) { |
| 5058 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5059 | if (inet_aton(ip_addr, &buf)) |
| 5060 | return PyBytes_FromStringAndSize((char *)(&buf), |
| 5061 | sizeof(buf)); |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 5062 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5063 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5064 | "illegal IP address string passed to inet_aton"); |
| 5065 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 5066 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5067 | #ifdef USE_INET_ATON_WEAKLINK |
| 5068 | } else { |
| 5069 | #endif |
| 5070 | |
| 5071 | #endif |
| 5072 | |
| 5073 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
| 5074 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5075 | /* special-case this address as inet_addr might return INADDR_NONE |
| 5076 | * for this */ |
| 5077 | if (strcmp(ip_addr, "255.255.255.255") == 0) { |
Charles-François Natali | 24aa041 | 2013-08-31 14:48:25 +0200 | [diff] [blame] | 5078 | packed_addr = INADDR_BROADCAST; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5079 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5080 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5081 | packed_addr = inet_addr(ip_addr); |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 5082 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5083 | if (packed_addr == INADDR_NONE) { /* invalid address */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5084 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5085 | "illegal IP address string passed to inet_aton"); |
| 5086 | return NULL; |
| 5087 | } |
| 5088 | } |
| 5089 | return PyBytes_FromStringAndSize((char *) &packed_addr, |
| 5090 | sizeof(packed_addr)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5091 | |
| 5092 | #ifdef USE_INET_ATON_WEAKLINK |
| 5093 | } |
| 5094 | #endif |
| 5095 | |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 5096 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5097 | } |
| 5098 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5099 | PyDoc_STRVAR(inet_ntoa_doc, |
Fred Drake | e066134 | 2000-03-07 14:05:16 +0000 | [diff] [blame] | 5100 | "inet_ntoa(packed_ip) -> ip_address_string\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5101 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5102 | 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] | 5103 | |
| 5104 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5105 | socket_inet_ntoa(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5106 | { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5107 | Py_buffer packed_ip; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5108 | struct in_addr packed_addr; |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5109 | |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5110 | if (!PyArg_ParseTuple(args, "y*:inet_ntoa", &packed_ip)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5111 | return NULL; |
| 5112 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 5113 | |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5114 | if (packed_ip.len != sizeof(packed_addr)) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5115 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5116 | "packed IP wrong length for inet_ntoa"); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5117 | PyBuffer_Release(&packed_ip); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5118 | return NULL; |
| 5119 | } |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5120 | |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5121 | memcpy(&packed_addr, packed_ip.buf, packed_ip.len); |
| 5122 | PyBuffer_Release(&packed_ip); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5124 | return PyUnicode_FromString(inet_ntoa(packed_addr)); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5125 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5126 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5127 | #if defined(HAVE_INET_PTON) || defined(MS_WINDOWS) |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5128 | |
| 5129 | PyDoc_STRVAR(inet_pton_doc, |
| 5130 | "inet_pton(af, ip) -> packed IP address string\n\ |
| 5131 | \n\ |
| 5132 | Convert an IP address from string format to a packed string suitable\n\ |
| 5133 | for use with low-level network functions."); |
| 5134 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5135 | #endif |
| 5136 | |
| 5137 | #ifdef HAVE_INET_PTON |
| 5138 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5139 | static PyObject * |
| 5140 | socket_inet_pton(PyObject *self, PyObject *args) |
| 5141 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5142 | int af; |
| 5143 | char* ip; |
| 5144 | int retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5145 | #ifdef ENABLE_IPV6 |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 5146 | 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] | 5147 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5148 | char packed[sizeof(struct in_addr)]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5149 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5150 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 5151 | return NULL; |
| 5152 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5153 | |
Martin v. Löwis | 04697e8 | 2004-06-02 12:35:29 +0000 | [diff] [blame] | 5154 | #if !defined(ENABLE_IPV6) && defined(AF_INET6) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5155 | if(af == AF_INET6) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5156 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5157 | "can't use AF_INET6, IPv6 is disabled"); |
| 5158 | return NULL; |
| 5159 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5160 | #endif |
Martin v. Löwis | 1064909 | 2003-08-05 06:25:06 +0000 | [diff] [blame] | 5161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5162 | retval = inet_pton(af, ip, packed); |
| 5163 | if (retval < 0) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5164 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5165 | return NULL; |
| 5166 | } else if (retval == 0) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5167 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5168 | "illegal IP address string passed to inet_pton"); |
| 5169 | return NULL; |
| 5170 | } else if (af == AF_INET) { |
| 5171 | return PyBytes_FromStringAndSize(packed, |
| 5172 | sizeof(struct in_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5173 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5174 | } else if (af == AF_INET6) { |
| 5175 | return PyBytes_FromStringAndSize(packed, |
| 5176 | sizeof(struct in6_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5177 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5178 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5179 | PyErr_SetString(PyExc_OSError, "unknown address family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5180 | return NULL; |
| 5181 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5182 | } |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5183 | #elif defined(MS_WINDOWS) |
| 5184 | |
| 5185 | static PyObject * |
| 5186 | socket_inet_pton(PyObject *self, PyObject *args) |
| 5187 | { |
| 5188 | int af; |
| 5189 | char* ip; |
| 5190 | struct sockaddr_in6 addr; |
| 5191 | INT ret, size; |
| 5192 | |
| 5193 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 5194 | return NULL; |
| 5195 | } |
| 5196 | |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5197 | size = sizeof(addr); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5198 | ret = WSAStringToAddressA(ip, af, NULL, (LPSOCKADDR)&addr, &size); |
| 5199 | |
| 5200 | if (ret) { |
| 5201 | PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError()); |
| 5202 | return NULL; |
| 5203 | } else if(af == AF_INET) { |
| 5204 | struct sockaddr_in *addr4 = (struct sockaddr_in*)&addr; |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5205 | return PyBytes_FromStringAndSize((const char *)&(addr4->sin_addr), |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5206 | sizeof(addr4->sin_addr)); |
| 5207 | } else if (af == AF_INET6) { |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5208 | return PyBytes_FromStringAndSize((const char *)&(addr.sin6_addr), |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5209 | sizeof(addr.sin6_addr)); |
| 5210 | } else { |
| 5211 | PyErr_SetString(PyExc_OSError, "unknown address family"); |
| 5212 | return NULL; |
| 5213 | } |
| 5214 | } |
| 5215 | |
| 5216 | #endif |
| 5217 | |
| 5218 | #if defined(HAVE_INET_PTON) || defined(MS_WINDOWS) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5219 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5220 | PyDoc_STRVAR(inet_ntop_doc, |
| 5221 | "inet_ntop(af, packed_ip) -> string formatted IP address\n\ |
| 5222 | \n\ |
| 5223 | Convert a packed IP address of the given family to string format."); |
| 5224 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5225 | #endif |
| 5226 | |
| 5227 | |
| 5228 | #ifdef HAVE_INET_PTON |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5229 | static PyObject * |
| 5230 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 5231 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5232 | int af; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5233 | Py_buffer packed_ip; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5234 | const char* retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5235 | #ifdef ENABLE_IPV6 |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 5236 | char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5237 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5238 | char ip[INET_ADDRSTRLEN + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5239 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5241 | /* Guarantee NUL-termination for PyUnicode_FromString() below */ |
| 5242 | memset((void *) &ip[0], '\0', sizeof(ip)); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5243 | |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5244 | if (!PyArg_ParseTuple(args, "iy*:inet_ntop", &af, &packed_ip)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5245 | return NULL; |
| 5246 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5247 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5248 | if (af == AF_INET) { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5249 | if (packed_ip.len != sizeof(struct in_addr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5250 | PyErr_SetString(PyExc_ValueError, |
| 5251 | "invalid length of packed IP address string"); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5252 | PyBuffer_Release(&packed_ip); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5253 | return NULL; |
| 5254 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5255 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5256 | } else if (af == AF_INET6) { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5257 | if (packed_ip.len != sizeof(struct in6_addr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5258 | PyErr_SetString(PyExc_ValueError, |
| 5259 | "invalid length of packed IP address string"); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5260 | PyBuffer_Release(&packed_ip); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5261 | return NULL; |
| 5262 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5263 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5264 | } else { |
| 5265 | PyErr_Format(PyExc_ValueError, |
| 5266 | "unknown address family %d", af); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5267 | PyBuffer_Release(&packed_ip); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5268 | return NULL; |
| 5269 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5270 | |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5271 | retval = inet_ntop(af, packed_ip.buf, ip, sizeof(ip)); |
| 5272 | PyBuffer_Release(&packed_ip); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5273 | if (!retval) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5274 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5275 | return NULL; |
| 5276 | } else { |
| 5277 | return PyUnicode_FromString(retval); |
| 5278 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5280 | /* NOTREACHED */ |
| 5281 | PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop"); |
| 5282 | return NULL; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5283 | } |
| 5284 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5285 | #elif defined(MS_WINDOWS) |
| 5286 | |
| 5287 | static PyObject * |
| 5288 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 5289 | { |
| 5290 | int af; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5291 | Py_buffer packed_ip; |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5292 | struct sockaddr_in6 addr; |
| 5293 | DWORD addrlen, ret, retlen; |
Jason R. Coombs | 8ec784c | 2013-11-10 13:43:22 -0500 | [diff] [blame] | 5294 | #ifdef ENABLE_IPV6 |
| 5295 | char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
| 5296 | #else |
| 5297 | char ip[INET_ADDRSTRLEN + 1]; |
| 5298 | #endif |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5299 | |
| 5300 | /* Guarantee NUL-termination for PyUnicode_FromString() below */ |
| 5301 | memset((void *) &ip[0], '\0', sizeof(ip)); |
| 5302 | |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5303 | if (!PyArg_ParseTuple(args, "iy*:inet_ntop", &af, &packed_ip)) { |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5304 | return NULL; |
| 5305 | } |
| 5306 | |
| 5307 | if (af == AF_INET) { |
| 5308 | struct sockaddr_in * addr4 = (struct sockaddr_in *)&addr; |
| 5309 | |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5310 | if (packed_ip.len != sizeof(struct in_addr)) { |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5311 | PyErr_SetString(PyExc_ValueError, |
| 5312 | "invalid length of packed IP address string"); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5313 | PyBuffer_Release(&packed_ip); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5314 | return NULL; |
| 5315 | } |
| 5316 | memset(addr4, 0, sizeof(struct sockaddr_in)); |
| 5317 | addr4->sin_family = AF_INET; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5318 | memcpy(&(addr4->sin_addr), packed_ip.buf, sizeof(addr4->sin_addr)); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5319 | addrlen = sizeof(struct sockaddr_in); |
| 5320 | } else if (af == AF_INET6) { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5321 | if (packed_ip.len != sizeof(struct in6_addr)) { |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5322 | PyErr_SetString(PyExc_ValueError, |
| 5323 | "invalid length of packed IP address string"); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5324 | PyBuffer_Release(&packed_ip); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5325 | return NULL; |
| 5326 | } |
| 5327 | |
| 5328 | memset(&addr, 0, sizeof(addr)); |
| 5329 | addr.sin6_family = AF_INET6; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5330 | memcpy(&(addr.sin6_addr), packed_ip.buf, sizeof(addr.sin6_addr)); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5331 | addrlen = sizeof(addr); |
| 5332 | } else { |
| 5333 | PyErr_Format(PyExc_ValueError, |
| 5334 | "unknown address family %d", af); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5335 | PyBuffer_Release(&packed_ip); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5336 | return NULL; |
| 5337 | } |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5338 | PyBuffer_Release(&packed_ip); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5339 | |
| 5340 | retlen = sizeof(ip); |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5341 | ret = WSAAddressToStringA((struct sockaddr*)&addr, addrlen, NULL, |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5342 | ip, &retlen); |
| 5343 | |
| 5344 | if (ret) { |
| 5345 | PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError()); |
| 5346 | return NULL; |
| 5347 | } else { |
| 5348 | return PyUnicode_FromString(ip); |
| 5349 | } |
| 5350 | } |
| 5351 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5352 | #endif /* HAVE_INET_PTON */ |
| 5353 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5354 | /* Python interface to getaddrinfo(host, port). */ |
| 5355 | |
| 5356 | /*ARGSUSED*/ |
| 5357 | static PyObject * |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5358 | socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5359 | { |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 5360 | static char* kwnames[] = {"host", "port", "family", "type", "proto", |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5361 | "flags", 0}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5362 | struct addrinfo hints, *res; |
| 5363 | struct addrinfo *res0 = NULL; |
| 5364 | PyObject *hobj = NULL; |
| 5365 | PyObject *pobj = (PyObject *)NULL; |
| 5366 | char pbuf[30]; |
| 5367 | char *hptr, *pptr; |
| 5368 | int family, socktype, protocol, flags; |
| 5369 | int error; |
| 5370 | PyObject *all = (PyObject *)NULL; |
| 5371 | PyObject *idna = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5372 | |
Georg Brandl | 6083a4b | 2013-10-14 06:51:46 +0200 | [diff] [blame] | 5373 | socktype = protocol = flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5374 | family = AF_UNSPEC; |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 5375 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iiii:getaddrinfo", |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5376 | kwnames, &hobj, &pobj, &family, &socktype, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5377 | &protocol, &flags)) { |
| 5378 | return NULL; |
| 5379 | } |
| 5380 | if (hobj == Py_None) { |
| 5381 | hptr = NULL; |
| 5382 | } else if (PyUnicode_Check(hobj)) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5383 | _Py_IDENTIFIER(encode); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 5384 | |
| 5385 | idna = _PyObject_CallMethodId(hobj, &PyId_encode, "s", "idna"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5386 | if (!idna) |
| 5387 | return NULL; |
| 5388 | assert(PyBytes_Check(idna)); |
| 5389 | hptr = PyBytes_AS_STRING(idna); |
| 5390 | } else if (PyBytes_Check(hobj)) { |
| 5391 | hptr = PyBytes_AsString(hobj); |
| 5392 | } else { |
| 5393 | PyErr_SetString(PyExc_TypeError, |
| 5394 | "getaddrinfo() argument 1 must be string or None"); |
| 5395 | return NULL; |
| 5396 | } |
| 5397 | if (PyLong_CheckExact(pobj)) { |
| 5398 | long value = PyLong_AsLong(pobj); |
| 5399 | if (value == -1 && PyErr_Occurred()) |
| 5400 | goto err; |
| 5401 | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value); |
| 5402 | pptr = pbuf; |
| 5403 | } else if (PyUnicode_Check(pobj)) { |
| 5404 | pptr = _PyUnicode_AsString(pobj); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 5405 | if (pptr == NULL) |
| 5406 | goto err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5407 | } else if (PyBytes_Check(pobj)) { |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 5408 | pptr = PyBytes_AS_STRING(pobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5409 | } else if (pobj == Py_None) { |
| 5410 | pptr = (char *)NULL; |
| 5411 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5412 | PyErr_SetString(PyExc_OSError, "Int or String expected"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5413 | goto err; |
| 5414 | } |
Ronald Oussoren | a822d366 | 2013-06-10 10:36:28 +0200 | [diff] [blame] | 5415 | #if defined(__APPLE__) && defined(AI_NUMERICSERV) |
Ronald Oussoren | 27a4ac5 | 2013-05-24 13:47:37 +0200 | [diff] [blame] | 5416 | if ((flags & AI_NUMERICSERV) && (pptr == NULL || (pptr[0] == '0' && pptr[1] == 0))) { |
| 5417 | /* On OSX upto at least OSX 10.8 getaddrinfo crashes |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 5418 | * if AI_NUMERICSERV is set and the servname is NULL or "0". |
| 5419 | * This workaround avoids a segfault in libsystem. |
| 5420 | */ |
Ronald Oussoren | 27a4ac5 | 2013-05-24 13:47:37 +0200 | [diff] [blame] | 5421 | pptr = "00"; |
| 5422 | } |
| 5423 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5424 | memset(&hints, 0, sizeof(hints)); |
| 5425 | hints.ai_family = family; |
| 5426 | hints.ai_socktype = socktype; |
| 5427 | hints.ai_protocol = protocol; |
| 5428 | hints.ai_flags = flags; |
| 5429 | Py_BEGIN_ALLOW_THREADS |
| 5430 | ACQUIRE_GETADDRINFO_LOCK |
| 5431 | error = getaddrinfo(hptr, pptr, &hints, &res0); |
| 5432 | Py_END_ALLOW_THREADS |
| 5433 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 5434 | if (error) { |
| 5435 | set_gaierror(error); |
| 5436 | goto err; |
| 5437 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5439 | if ((all = PyList_New(0)) == NULL) |
| 5440 | goto err; |
| 5441 | for (res = res0; res; res = res->ai_next) { |
| 5442 | PyObject *single; |
| 5443 | PyObject *addr = |
| 5444 | makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol); |
| 5445 | if (addr == NULL) |
| 5446 | goto err; |
| 5447 | single = Py_BuildValue("iiisO", res->ai_family, |
| 5448 | res->ai_socktype, res->ai_protocol, |
| 5449 | res->ai_canonname ? res->ai_canonname : "", |
| 5450 | addr); |
| 5451 | Py_DECREF(addr); |
| 5452 | if (single == NULL) |
| 5453 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5454 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5455 | if (PyList_Append(all, single)) |
| 5456 | goto err; |
| 5457 | Py_XDECREF(single); |
| 5458 | } |
| 5459 | Py_XDECREF(idna); |
| 5460 | if (res0) |
| 5461 | freeaddrinfo(res0); |
| 5462 | return all; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5463 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5464 | Py_XDECREF(all); |
| 5465 | Py_XDECREF(idna); |
| 5466 | if (res0) |
| 5467 | freeaddrinfo(res0); |
| 5468 | return (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5469 | } |
| 5470 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5471 | PyDoc_STRVAR(getaddrinfo_doc, |
Berker Peksag | a6ec5ee | 2014-06-30 11:28:40 +0300 | [diff] [blame] | 5472 | "getaddrinfo(host, port [, family, type, proto, flags])\n\ |
| 5473 | -> list of (family, type, proto, canonname, sockaddr)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5474 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5475 | Resolve host and port into addrinfo struct."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5476 | |
| 5477 | /* Python interface to getnameinfo(sa, flags). */ |
| 5478 | |
| 5479 | /*ARGSUSED*/ |
| 5480 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5481 | socket_getnameinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5482 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5483 | PyObject *sa = (PyObject *)NULL; |
| 5484 | int flags; |
| 5485 | char *hostp; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5486 | int port; |
| 5487 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5488 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; |
| 5489 | struct addrinfo hints, *res = NULL; |
| 5490 | int error; |
| 5491 | PyObject *ret = (PyObject *)NULL; |
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 | flags = flowinfo = scope_id = 0; |
| 5494 | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) |
| 5495 | return NULL; |
| 5496 | if (!PyTuple_Check(sa)) { |
| 5497 | PyErr_SetString(PyExc_TypeError, |
| 5498 | "getnameinfo() argument 1 must be a tuple"); |
| 5499 | return NULL; |
| 5500 | } |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5501 | if (!PyArg_ParseTuple(sa, "si|II", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5502 | &hostp, &port, &flowinfo, &scope_id)) |
| 5503 | return NULL; |
Charles-François Natali | 9b0c006 | 2012-06-23 10:17:05 +0200 | [diff] [blame] | 5504 | if (flowinfo > 0xfffff) { |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5505 | PyErr_SetString(PyExc_OverflowError, |
| 5506 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 5507 | return NULL; |
| 5508 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5509 | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); |
| 5510 | memset(&hints, 0, sizeof(hints)); |
| 5511 | hints.ai_family = AF_UNSPEC; |
| 5512 | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ |
Martin v. Löwis | 112c0f3 | 2010-08-25 07:38:15 +0000 | [diff] [blame] | 5513 | hints.ai_flags = AI_NUMERICHOST; /* don't do any name resolution */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5514 | Py_BEGIN_ALLOW_THREADS |
| 5515 | ACQUIRE_GETADDRINFO_LOCK |
| 5516 | error = getaddrinfo(hostp, pbuf, &hints, &res); |
| 5517 | Py_END_ALLOW_THREADS |
| 5518 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 5519 | if (error) { |
| 5520 | set_gaierror(error); |
| 5521 | goto fail; |
| 5522 | } |
| 5523 | if (res->ai_next) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5524 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5525 | "sockaddr resolved to multiple addresses"); |
| 5526 | goto fail; |
| 5527 | } |
| 5528 | switch (res->ai_family) { |
| 5529 | case AF_INET: |
| 5530 | { |
| 5531 | if (PyTuple_GET_SIZE(sa) != 2) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5532 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5533 | "IPv4 sockaddr must be 2 tuple"); |
| 5534 | goto fail; |
| 5535 | } |
| 5536 | break; |
| 5537 | } |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 5538 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5539 | case AF_INET6: |
| 5540 | { |
| 5541 | struct sockaddr_in6 *sin6; |
| 5542 | sin6 = (struct sockaddr_in6 *)res->ai_addr; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5543 | sin6->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5544 | sin6->sin6_scope_id = scope_id; |
| 5545 | break; |
| 5546 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5547 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5548 | } |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 5549 | error = getnameinfo(res->ai_addr, (socklen_t) res->ai_addrlen, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5550 | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); |
| 5551 | if (error) { |
| 5552 | set_gaierror(error); |
| 5553 | goto fail; |
| 5554 | } |
| 5555 | ret = Py_BuildValue("ss", hbuf, pbuf); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5556 | |
| 5557 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5558 | if (res) |
| 5559 | freeaddrinfo(res); |
| 5560 | return ret; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5561 | } |
| 5562 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5563 | PyDoc_STRVAR(getnameinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5564 | "getnameinfo(sockaddr, flags) --> (host, port)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5565 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5566 | Get host and port for a sockaddr."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5567 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5568 | |
| 5569 | /* Python API to getting and setting the default timeout value. */ |
| 5570 | |
| 5571 | static PyObject * |
| 5572 | socket_getdefaulttimeout(PyObject *self) |
| 5573 | { |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 5574 | if (defaulttimeout < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5575 | Py_INCREF(Py_None); |
| 5576 | return Py_None; |
| 5577 | } |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 5578 | else { |
| 5579 | double seconds = _PyTime_AsSecondsDouble(defaulttimeout); |
| 5580 | return PyFloat_FromDouble(seconds); |
| 5581 | } |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5582 | } |
| 5583 | |
| 5584 | PyDoc_STRVAR(getdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5585 | "getdefaulttimeout() -> timeout\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5586 | \n\ |
Ezio Melotti | 388c945 | 2011-08-14 08:28:57 +0300 | [diff] [blame] | 5587 | 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] | 5588 | A value of None indicates that new socket objects have no timeout.\n\ |
| 5589 | When the socket module is first imported, the default is None."); |
| 5590 | |
| 5591 | static PyObject * |
| 5592 | socket_setdefaulttimeout(PyObject *self, PyObject *arg) |
| 5593 | { |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 5594 | _PyTime_t timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5595 | |
Victor Stinner | 71694d5 | 2015-03-28 01:18:54 +0100 | [diff] [blame] | 5596 | if (socket_parse_timeout(&timeout, arg) < 0) |
| 5597 | return NULL; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5599 | defaulttimeout = timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5600 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5601 | Py_INCREF(Py_None); |
| 5602 | return Py_None; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5603 | } |
| 5604 | |
| 5605 | PyDoc_STRVAR(setdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5606 | "setdefaulttimeout(timeout)\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5607 | \n\ |
Ezio Melotti | 388c945 | 2011-08-14 08:28:57 +0300 | [diff] [blame] | 5608 | 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] | 5609 | A value of None indicates that new socket objects have no timeout.\n\ |
| 5610 | When the socket module is first imported, the default is None."); |
| 5611 | |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5612 | #ifdef HAVE_IF_NAMEINDEX |
| 5613 | /* Python API for getting interface indices and names */ |
| 5614 | |
| 5615 | static PyObject * |
| 5616 | socket_if_nameindex(PyObject *self, PyObject *arg) |
| 5617 | { |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5618 | PyObject *list; |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5619 | int i; |
| 5620 | struct if_nameindex *ni; |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 5621 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5622 | ni = if_nameindex(); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5623 | if (ni == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5624 | PyErr_SetFromErrno(PyExc_OSError); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5625 | return NULL; |
| 5626 | } |
| 5627 | |
| 5628 | list = PyList_New(0); |
| 5629 | if (list == NULL) { |
| 5630 | if_freenameindex(ni); |
| 5631 | return NULL; |
| 5632 | } |
| 5633 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5634 | for (i = 0; ni[i].if_index != 0 && i < INT_MAX; i++) { |
| 5635 | PyObject *ni_tuple = Py_BuildValue("IO&", |
| 5636 | ni[i].if_index, PyUnicode_DecodeFSDefault, ni[i].if_name); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5637 | |
| 5638 | if (ni_tuple == NULL || PyList_Append(list, ni_tuple) == -1) { |
| 5639 | Py_XDECREF(ni_tuple); |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5640 | Py_DECREF(list); |
| 5641 | if_freenameindex(ni); |
| 5642 | return NULL; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5643 | } |
| 5644 | Py_DECREF(ni_tuple); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5645 | } |
| 5646 | |
| 5647 | if_freenameindex(ni); |
| 5648 | return list; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5649 | } |
| 5650 | |
| 5651 | PyDoc_STRVAR(if_nameindex_doc, |
| 5652 | "if_nameindex()\n\ |
| 5653 | \n\ |
| 5654 | Returns a list of network interface information (index, name) tuples."); |
| 5655 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5656 | static PyObject * |
| 5657 | socket_if_nametoindex(PyObject *self, PyObject *args) |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5658 | { |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5659 | PyObject *oname; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5660 | unsigned long index; |
| 5661 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5662 | if (!PyArg_ParseTuple(args, "O&:if_nametoindex", |
| 5663 | PyUnicode_FSConverter, &oname)) |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5664 | return NULL; |
| 5665 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5666 | index = if_nametoindex(PyBytes_AS_STRING(oname)); |
| 5667 | Py_DECREF(oname); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5668 | if (index == 0) { |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5669 | /* if_nametoindex() doesn't set errno */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5670 | PyErr_SetString(PyExc_OSError, "no interface with this name"); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5671 | return NULL; |
| 5672 | } |
| 5673 | |
| 5674 | return PyLong_FromUnsignedLong(index); |
| 5675 | } |
| 5676 | |
| 5677 | PyDoc_STRVAR(if_nametoindex_doc, |
| 5678 | "if_nametoindex(if_name)\n\ |
| 5679 | \n\ |
| 5680 | Returns the interface index corresponding to the interface name if_name."); |
| 5681 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5682 | static PyObject * |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5683 | socket_if_indextoname(PyObject *self, PyObject *arg) |
| 5684 | { |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5685 | unsigned long index; |
| 5686 | char name[IF_NAMESIZE + 1]; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5687 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5688 | index = PyLong_AsUnsignedLong(arg); |
| 5689 | if (index == (unsigned long) -1) |
| 5690 | return NULL; |
| 5691 | |
| 5692 | if (if_indextoname(index, name) == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5693 | PyErr_SetFromErrno(PyExc_OSError); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5694 | return NULL; |
| 5695 | } |
| 5696 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5697 | return PyUnicode_DecodeFSDefault(name); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5698 | } |
| 5699 | |
| 5700 | PyDoc_STRVAR(if_indextoname_doc, |
| 5701 | "if_indextoname(if_index)\n\ |
| 5702 | \n\ |
| 5703 | Returns the interface name corresponding to the interface index if_index."); |
| 5704 | |
| 5705 | #endif /* HAVE_IF_NAMEINDEX */ |
| 5706 | |
| 5707 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 5708 | #ifdef CMSG_LEN |
| 5709 | /* Python interface to CMSG_LEN(length). */ |
| 5710 | |
| 5711 | static PyObject * |
| 5712 | socket_CMSG_LEN(PyObject *self, PyObject *args) |
| 5713 | { |
| 5714 | Py_ssize_t length; |
| 5715 | size_t result; |
| 5716 | |
| 5717 | if (!PyArg_ParseTuple(args, "n:CMSG_LEN", &length)) |
| 5718 | return NULL; |
| 5719 | if (length < 0 || !get_CMSG_LEN(length, &result)) { |
| 5720 | PyErr_Format(PyExc_OverflowError, "CMSG_LEN() argument out of range"); |
| 5721 | return NULL; |
| 5722 | } |
| 5723 | return PyLong_FromSize_t(result); |
| 5724 | } |
| 5725 | |
| 5726 | PyDoc_STRVAR(CMSG_LEN_doc, |
| 5727 | "CMSG_LEN(length) -> control message length\n\ |
| 5728 | \n\ |
| 5729 | Return the total length, without trailing padding, of an ancillary\n\ |
| 5730 | data item with associated data of the given length. This value can\n\ |
| 5731 | often be used as the buffer size for recvmsg() to receive a single\n\ |
| 5732 | item of ancillary data, but RFC 3542 requires portable applications to\n\ |
| 5733 | use CMSG_SPACE() and thus include space for padding, even when the\n\ |
| 5734 | item will be the last in the buffer. Raises OverflowError if length\n\ |
| 5735 | is outside the permissible range of values."); |
| 5736 | |
| 5737 | |
| 5738 | #ifdef CMSG_SPACE |
| 5739 | /* Python interface to CMSG_SPACE(length). */ |
| 5740 | |
| 5741 | static PyObject * |
| 5742 | socket_CMSG_SPACE(PyObject *self, PyObject *args) |
| 5743 | { |
| 5744 | Py_ssize_t length; |
| 5745 | size_t result; |
| 5746 | |
| 5747 | if (!PyArg_ParseTuple(args, "n:CMSG_SPACE", &length)) |
| 5748 | return NULL; |
| 5749 | if (length < 0 || !get_CMSG_SPACE(length, &result)) { |
| 5750 | PyErr_SetString(PyExc_OverflowError, |
| 5751 | "CMSG_SPACE() argument out of range"); |
| 5752 | return NULL; |
| 5753 | } |
| 5754 | return PyLong_FromSize_t(result); |
| 5755 | } |
| 5756 | |
| 5757 | PyDoc_STRVAR(CMSG_SPACE_doc, |
| 5758 | "CMSG_SPACE(length) -> buffer size\n\ |
| 5759 | \n\ |
| 5760 | Return the buffer size needed for recvmsg() to receive an ancillary\n\ |
| 5761 | data item with associated data of the given length, along with any\n\ |
| 5762 | trailing padding. The buffer space needed to receive multiple items\n\ |
| 5763 | is the sum of the CMSG_SPACE() values for their associated data\n\ |
| 5764 | lengths. Raises OverflowError if length is outside the permissible\n\ |
| 5765 | range of values."); |
| 5766 | #endif /* CMSG_SPACE */ |
| 5767 | #endif /* CMSG_LEN */ |
| 5768 | |
| 5769 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 5770 | /* List of functions exported by this module. */ |
| 5771 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5772 | static PyMethodDef socket_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5773 | {"gethostbyname", socket_gethostbyname, |
| 5774 | METH_VARARGS, gethostbyname_doc}, |
| 5775 | {"gethostbyname_ex", socket_gethostbyname_ex, |
| 5776 | METH_VARARGS, ghbn_ex_doc}, |
| 5777 | {"gethostbyaddr", socket_gethostbyaddr, |
| 5778 | METH_VARARGS, gethostbyaddr_doc}, |
| 5779 | {"gethostname", socket_gethostname, |
| 5780 | METH_NOARGS, gethostname_doc}, |
Antoine Pitrou | 061cfb5 | 2011-02-28 22:25:22 +0000 | [diff] [blame] | 5781 | #ifdef HAVE_SETHOSTNAME |
| 5782 | {"sethostname", socket_sethostname, |
| 5783 | METH_VARARGS, sethostname_doc}, |
| 5784 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5785 | {"getservbyname", socket_getservbyname, |
| 5786 | METH_VARARGS, getservbyname_doc}, |
| 5787 | {"getservbyport", socket_getservbyport, |
| 5788 | METH_VARARGS, getservbyport_doc}, |
| 5789 | {"getprotobyname", socket_getprotobyname, |
| 5790 | METH_VARARGS, getprotobyname_doc}, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 5791 | #ifndef NO_DUP |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5792 | {"dup", socket_dup, |
| 5793 | METH_O, dup_doc}, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 5794 | #endif |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 5795 | #ifdef HAVE_SOCKETPAIR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5796 | {"socketpair", socket_socketpair, |
| 5797 | METH_VARARGS, socketpair_doc}, |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 5798 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5799 | {"ntohs", socket_ntohs, |
| 5800 | METH_VARARGS, ntohs_doc}, |
| 5801 | {"ntohl", socket_ntohl, |
| 5802 | METH_O, ntohl_doc}, |
| 5803 | {"htons", socket_htons, |
| 5804 | METH_VARARGS, htons_doc}, |
| 5805 | {"htonl", socket_htonl, |
| 5806 | METH_O, htonl_doc}, |
| 5807 | {"inet_aton", socket_inet_aton, |
| 5808 | METH_VARARGS, inet_aton_doc}, |
| 5809 | {"inet_ntoa", socket_inet_ntoa, |
| 5810 | METH_VARARGS, inet_ntoa_doc}, |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5811 | #if defined(HAVE_INET_PTON) || defined(MS_WINDOWS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5812 | {"inet_pton", socket_inet_pton, |
| 5813 | METH_VARARGS, inet_pton_doc}, |
| 5814 | {"inet_ntop", socket_inet_ntop, |
| 5815 | METH_VARARGS, inet_ntop_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5816 | #endif |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5817 | {"getaddrinfo", (PyCFunction)socket_getaddrinfo, |
| 5818 | METH_VARARGS | METH_KEYWORDS, getaddrinfo_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5819 | {"getnameinfo", socket_getnameinfo, |
| 5820 | METH_VARARGS, getnameinfo_doc}, |
| 5821 | {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout, |
| 5822 | METH_NOARGS, getdefaulttimeout_doc}, |
| 5823 | {"setdefaulttimeout", socket_setdefaulttimeout, |
| 5824 | METH_O, setdefaulttimeout_doc}, |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5825 | #ifdef HAVE_IF_NAMEINDEX |
| 5826 | {"if_nameindex", socket_if_nameindex, |
| 5827 | METH_NOARGS, if_nameindex_doc}, |
| 5828 | {"if_nametoindex", socket_if_nametoindex, |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5829 | METH_VARARGS, if_nametoindex_doc}, |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5830 | {"if_indextoname", socket_if_indextoname, |
| 5831 | METH_O, if_indextoname_doc}, |
| 5832 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 5833 | #ifdef CMSG_LEN |
| 5834 | {"CMSG_LEN", socket_CMSG_LEN, |
| 5835 | METH_VARARGS, CMSG_LEN_doc}, |
| 5836 | #ifdef CMSG_SPACE |
| 5837 | {"CMSG_SPACE", socket_CMSG_SPACE, |
| 5838 | METH_VARARGS, CMSG_SPACE_doc}, |
| 5839 | #endif |
| 5840 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5841 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5842 | }; |
| 5843 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 5844 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5845 | #ifdef MS_WINDOWS |
| 5846 | #define OS_INIT_DEFINED |
| 5847 | |
| 5848 | /* Additional initialization and cleanup for Windows */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5849 | |
| 5850 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5851 | os_cleanup(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5852 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5853 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5854 | } |
| 5855 | |
| 5856 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5857 | os_init(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5858 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5859 | WSADATA WSAData; |
| 5860 | int ret; |
| 5861 | ret = WSAStartup(0x0101, &WSAData); |
| 5862 | switch (ret) { |
| 5863 | case 0: /* No error */ |
| 5864 | Py_AtExit(os_cleanup); |
| 5865 | return 1; /* Success */ |
| 5866 | case WSASYSNOTREADY: |
| 5867 | PyErr_SetString(PyExc_ImportError, |
| 5868 | "WSAStartup failed: network not ready"); |
| 5869 | break; |
| 5870 | case WSAVERNOTSUPPORTED: |
| 5871 | case WSAEINVAL: |
| 5872 | PyErr_SetString( |
| 5873 | PyExc_ImportError, |
| 5874 | "WSAStartup failed: requested version not supported"); |
| 5875 | break; |
| 5876 | default: |
| 5877 | PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret); |
| 5878 | break; |
| 5879 | } |
| 5880 | return 0; /* Failure */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5881 | } |
| 5882 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 5883 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5884 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5885 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5886 | |
| 5887 | #ifndef OS_INIT_DEFINED |
| 5888 | static int |
| 5889 | os_init(void) |
| 5890 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5891 | return 1; /* Success */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5892 | } |
| 5893 | #endif |
| 5894 | |
| 5895 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5896 | /* C API table - always add new things to the end for binary |
| 5897 | compatibility. */ |
| 5898 | static |
| 5899 | PySocketModule_APIObject PySocketModuleAPI = |
| 5900 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5901 | &sock_type, |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 5902 | NULL, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5903 | NULL |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5904 | }; |
| 5905 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5906 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5907 | /* Initialize the _socket module. |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5908 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5909 | This module is actually called "_socket", and there's a wrapper |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 5910 | "socket.py" which implements some additional functionality. |
| 5911 | The import of "_socket" may fail with an ImportError exception if |
| 5912 | os-specific initialization fails. On Windows, this does WINSOCK |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 5913 | initialization. When WINSOCK is initialized successfully, a call to |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 5914 | WSACleanup() is scheduled to be made at exit time. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5915 | */ |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5916 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5917 | PyDoc_STRVAR(socket_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5918 | "Implementation module for socket operations.\n\ |
| 5919 | \n\ |
| 5920 | See the socket module for documentation."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5921 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5922 | static struct PyModuleDef socketmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5923 | PyModuleDef_HEAD_INIT, |
| 5924 | PySocket_MODULE_NAME, |
| 5925 | socket_doc, |
| 5926 | -1, |
| 5927 | socket_methods, |
| 5928 | NULL, |
| 5929 | NULL, |
| 5930 | NULL, |
| 5931 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5932 | }; |
| 5933 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 5934 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5935 | PyInit__socket(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5936 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5937 | PyObject *m, *has_ipv6; |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5938 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5939 | if (!os_init()) |
| 5940 | return NULL; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5941 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5942 | #ifdef MS_WINDOWS |
| 5943 | if (support_wsa_no_inherit == -1) { |
Steve Dower | 65e4cb1 | 2014-11-22 12:54:57 -0800 | [diff] [blame] | 5944 | #if defined(_MSC_VER) && _MSC_VER >= 1800 |
| 5945 | support_wsa_no_inherit = IsWindows7SP1OrGreater(); |
| 5946 | #else |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5947 | DWORD version = GetVersion(); |
| 5948 | DWORD major = (DWORD)LOBYTE(LOWORD(version)); |
| 5949 | DWORD minor = (DWORD)HIBYTE(LOWORD(version)); |
| 5950 | /* need Windows 7 SP1, 2008 R2 SP1 or later */ |
Steve Dower | 65e4cb1 | 2014-11-22 12:54:57 -0800 | [diff] [blame] | 5951 | support_wsa_no_inherit = major > 6 || (major == 6 && minor >= 1); |
| 5952 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5953 | } |
| 5954 | #endif |
| 5955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5956 | Py_TYPE(&sock_type) = &PyType_Type; |
| 5957 | m = PyModule_Create(&socketmodule); |
| 5958 | if (m == NULL) |
| 5959 | return NULL; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5960 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5961 | Py_INCREF(PyExc_OSError); |
| 5962 | PySocketModuleAPI.error = PyExc_OSError; |
| 5963 | Py_INCREF(PyExc_OSError); |
| 5964 | PyModule_AddObject(m, "error", PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5965 | socket_herror = PyErr_NewException("socket.herror", |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5966 | PyExc_OSError, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5967 | if (socket_herror == NULL) |
| 5968 | return NULL; |
| 5969 | Py_INCREF(socket_herror); |
| 5970 | PyModule_AddObject(m, "herror", socket_herror); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5971 | socket_gaierror = PyErr_NewException("socket.gaierror", PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5972 | NULL); |
| 5973 | if (socket_gaierror == NULL) |
| 5974 | return NULL; |
| 5975 | Py_INCREF(socket_gaierror); |
| 5976 | PyModule_AddObject(m, "gaierror", socket_gaierror); |
| 5977 | socket_timeout = PyErr_NewException("socket.timeout", |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5978 | PyExc_OSError, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5979 | if (socket_timeout == NULL) |
| 5980 | return NULL; |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 5981 | PySocketModuleAPI.timeout_error = socket_timeout; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5982 | Py_INCREF(socket_timeout); |
| 5983 | PyModule_AddObject(m, "timeout", socket_timeout); |
| 5984 | Py_INCREF((PyObject *)&sock_type); |
| 5985 | if (PyModule_AddObject(m, "SocketType", |
| 5986 | (PyObject *)&sock_type) != 0) |
| 5987 | return NULL; |
| 5988 | Py_INCREF((PyObject *)&sock_type); |
| 5989 | if (PyModule_AddObject(m, "socket", |
| 5990 | (PyObject *)&sock_type) != 0) |
| 5991 | return NULL; |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5992 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5993 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5994 | has_ipv6 = Py_True; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5995 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5996 | has_ipv6 = Py_False; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5997 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5998 | Py_INCREF(has_ipv6); |
| 5999 | PyModule_AddObject(m, "has_ipv6", has_ipv6); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 6000 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6001 | /* Export C API */ |
| 6002 | if (PyModule_AddObject(m, PySocket_CAPI_NAME, |
| 6003 | PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL) |
| 6004 | ) != 0) |
| 6005 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6006 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6007 | /* Address families (we only support AF_INET and AF_UNIX) */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6008 | #ifdef AF_UNSPEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6009 | PyModule_AddIntMacro(m, AF_UNSPEC); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6010 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6011 | PyModule_AddIntMacro(m, AF_INET); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6012 | #ifdef AF_INET6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6013 | PyModule_AddIntMacro(m, AF_INET6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6014 | #endif /* AF_INET6 */ |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 6015 | #if defined(AF_UNIX) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6016 | PyModule_AddIntMacro(m, AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6017 | #endif /* AF_UNIX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6018 | #ifdef AF_AX25 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6019 | /* Amateur Radio AX.25 */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6020 | PyModule_AddIntMacro(m, AF_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6021 | #endif |
| 6022 | #ifdef AF_IPX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6023 | PyModule_AddIntMacro(m, AF_IPX); /* Novell IPX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6024 | #endif |
| 6025 | #ifdef AF_APPLETALK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6026 | /* Appletalk DDP */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6027 | PyModule_AddIntMacro(m, AF_APPLETALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6028 | #endif |
| 6029 | #ifdef AF_NETROM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6030 | /* Amateur radio NetROM */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6031 | PyModule_AddIntMacro(m, AF_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6032 | #endif |
| 6033 | #ifdef AF_BRIDGE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6034 | /* Multiprotocol bridge */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6035 | PyModule_AddIntMacro(m, AF_BRIDGE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6036 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6037 | #ifdef AF_ATMPVC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6038 | /* ATM PVCs */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6039 | PyModule_AddIntMacro(m, AF_ATMPVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6040 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6041 | #ifdef AF_AAL5 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6042 | /* Reserved for Werner's ATM */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6043 | PyModule_AddIntMacro(m, AF_AAL5); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6044 | #endif |
| 6045 | #ifdef AF_X25 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6046 | /* Reserved for X.25 project */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6047 | PyModule_AddIntMacro(m, AF_X25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6048 | #endif |
| 6049 | #ifdef AF_INET6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6050 | PyModule_AddIntMacro(m, AF_INET6); /* IP version 6 */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6051 | #endif |
| 6052 | #ifdef AF_ROSE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6053 | /* Amateur Radio X.25 PLP */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6054 | PyModule_AddIntMacro(m, AF_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6055 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6056 | #ifdef AF_DECnet |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6057 | /* Reserved for DECnet project */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6058 | PyModule_AddIntMacro(m, AF_DECnet); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6059 | #endif |
| 6060 | #ifdef AF_NETBEUI |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6061 | /* Reserved for 802.2LLC project */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6062 | PyModule_AddIntMacro(m, AF_NETBEUI); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6063 | #endif |
| 6064 | #ifdef AF_SECURITY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6065 | /* Security callback pseudo AF */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6066 | PyModule_AddIntMacro(m, AF_SECURITY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6067 | #endif |
| 6068 | #ifdef AF_KEY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6069 | /* PF_KEY key management API */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6070 | PyModule_AddIntMacro(m, AF_KEY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6071 | #endif |
| 6072 | #ifdef AF_NETLINK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6073 | /* */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6074 | PyModule_AddIntMacro(m, AF_NETLINK); |
| 6075 | PyModule_AddIntMacro(m, NETLINK_ROUTE); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6076 | #ifdef NETLINK_SKIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6077 | PyModule_AddIntMacro(m, NETLINK_SKIP); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6078 | #endif |
| 6079 | #ifdef NETLINK_W1 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6080 | PyModule_AddIntMacro(m, NETLINK_W1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6081 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6082 | PyModule_AddIntMacro(m, NETLINK_USERSOCK); |
| 6083 | PyModule_AddIntMacro(m, NETLINK_FIREWALL); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 6084 | #ifdef NETLINK_TCPDIAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6085 | PyModule_AddIntMacro(m, NETLINK_TCPDIAG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 6086 | #endif |
| 6087 | #ifdef NETLINK_NFLOG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6088 | PyModule_AddIntMacro(m, NETLINK_NFLOG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 6089 | #endif |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 6090 | #ifdef NETLINK_XFRM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6091 | PyModule_AddIntMacro(m, NETLINK_XFRM); |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 6092 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6093 | #ifdef NETLINK_ARPD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6094 | PyModule_AddIntMacro(m, NETLINK_ARPD); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6095 | #endif |
| 6096 | #ifdef NETLINK_ROUTE6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6097 | PyModule_AddIntMacro(m, NETLINK_ROUTE6); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6098 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6099 | PyModule_AddIntMacro(m, NETLINK_IP6_FW); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6100 | #ifdef NETLINK_DNRTMSG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6101 | PyModule_AddIntMacro(m, NETLINK_DNRTMSG); |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 6102 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6103 | #ifdef NETLINK_TAPBASE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6104 | PyModule_AddIntMacro(m, NETLINK_TAPBASE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6105 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6106 | #endif /* AF_NETLINK */ |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6107 | #ifdef AF_ROUTE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6108 | /* Alias to emulate 4.4BSD */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6109 | PyModule_AddIntMacro(m, AF_ROUTE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6110 | #endif |
Giampaolo Rodola' | 80e1c43 | 2013-05-21 21:02:04 +0200 | [diff] [blame] | 6111 | #ifdef AF_LINK |
| 6112 | PyModule_AddIntMacro(m, AF_LINK); |
| 6113 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6114 | #ifdef AF_ASH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6115 | /* Ash */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6116 | PyModule_AddIntMacro(m, AF_ASH); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6117 | #endif |
| 6118 | #ifdef AF_ECONET |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6119 | /* Acorn Econet */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6120 | PyModule_AddIntMacro(m, AF_ECONET); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6121 | #endif |
| 6122 | #ifdef AF_ATMSVC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6123 | /* ATM SVCs */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6124 | PyModule_AddIntMacro(m, AF_ATMSVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6125 | #endif |
| 6126 | #ifdef AF_SNA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6127 | /* Linux SNA Project (nutters!) */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6128 | PyModule_AddIntMacro(m, AF_SNA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6129 | #endif |
| 6130 | #ifdef AF_IRDA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6131 | /* IRDA sockets */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6132 | PyModule_AddIntMacro(m, AF_IRDA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6133 | #endif |
| 6134 | #ifdef AF_PPPOX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6135 | /* PPPoX sockets */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6136 | PyModule_AddIntMacro(m, AF_PPPOX); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6137 | #endif |
| 6138 | #ifdef AF_WANPIPE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6139 | /* Wanpipe API Sockets */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6140 | PyModule_AddIntMacro(m, AF_WANPIPE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6141 | #endif |
| 6142 | #ifdef AF_LLC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6143 | /* Linux LLC */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6144 | PyModule_AddIntMacro(m, AF_LLC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6145 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 6146 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 6147 | #ifdef USE_BLUETOOTH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6148 | PyModule_AddIntMacro(m, AF_BLUETOOTH); |
| 6149 | PyModule_AddIntMacro(m, BTPROTO_L2CAP); |
| 6150 | PyModule_AddIntMacro(m, BTPROTO_HCI); |
| 6151 | PyModule_AddIntMacro(m, SOL_HCI); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 6152 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6153 | PyModule_AddIntMacro(m, HCI_FILTER); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 6154 | #endif |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 6155 | #if !defined(__FreeBSD__) |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 6156 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6157 | PyModule_AddIntMacro(m, HCI_TIME_STAMP); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 6158 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6159 | PyModule_AddIntMacro(m, HCI_DATA_DIR); |
| 6160 | PyModule_AddIntMacro(m, BTPROTO_SCO); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 6161 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6162 | PyModule_AddIntMacro(m, BTPROTO_RFCOMM); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6163 | PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00"); |
| 6164 | 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] | 6165 | #endif |
| 6166 | |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6167 | #ifdef AF_CAN |
| 6168 | /* Controller Area Network */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6169 | PyModule_AddIntMacro(m, AF_CAN); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6170 | #endif |
| 6171 | #ifdef PF_CAN |
| 6172 | /* Controller Area Network */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6173 | PyModule_AddIntMacro(m, PF_CAN); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6174 | #endif |
| 6175 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6176 | /* Reliable Datagram Sockets */ |
| 6177 | #ifdef AF_RDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6178 | PyModule_AddIntMacro(m, AF_RDS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6179 | #endif |
| 6180 | #ifdef PF_RDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6181 | PyModule_AddIntMacro(m, PF_RDS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6182 | #endif |
| 6183 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6184 | /* Kernel event messages */ |
| 6185 | #ifdef PF_SYSTEM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6186 | PyModule_AddIntMacro(m, PF_SYSTEM); |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6187 | #endif |
| 6188 | #ifdef AF_SYSTEM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6189 | PyModule_AddIntMacro(m, AF_SYSTEM); |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6190 | #endif |
| 6191 | |
Antoine Pitrou | b156a46 | 2010-10-27 20:13:57 +0000 | [diff] [blame] | 6192 | #ifdef AF_PACKET |
| 6193 | PyModule_AddIntMacro(m, AF_PACKET); |
| 6194 | #endif |
| 6195 | #ifdef PF_PACKET |
| 6196 | PyModule_AddIntMacro(m, PF_PACKET); |
| 6197 | #endif |
| 6198 | #ifdef PACKET_HOST |
| 6199 | PyModule_AddIntMacro(m, PACKET_HOST); |
| 6200 | #endif |
| 6201 | #ifdef PACKET_BROADCAST |
| 6202 | PyModule_AddIntMacro(m, PACKET_BROADCAST); |
| 6203 | #endif |
| 6204 | #ifdef PACKET_MULTICAST |
| 6205 | PyModule_AddIntMacro(m, PACKET_MULTICAST); |
| 6206 | #endif |
| 6207 | #ifdef PACKET_OTHERHOST |
| 6208 | PyModule_AddIntMacro(m, PACKET_OTHERHOST); |
| 6209 | #endif |
| 6210 | #ifdef PACKET_OUTGOING |
| 6211 | PyModule_AddIntMacro(m, PACKET_OUTGOING); |
| 6212 | #endif |
| 6213 | #ifdef PACKET_LOOPBACK |
| 6214 | PyModule_AddIntMacro(m, PACKET_LOOPBACK); |
| 6215 | #endif |
| 6216 | #ifdef PACKET_FASTROUTE |
| 6217 | PyModule_AddIntMacro(m, PACKET_FASTROUTE); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 6218 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6219 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6220 | #ifdef HAVE_LINUX_TIPC_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6221 | PyModule_AddIntMacro(m, AF_TIPC); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6222 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6223 | /* for addresses */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6224 | PyModule_AddIntMacro(m, TIPC_ADDR_NAMESEQ); |
| 6225 | PyModule_AddIntMacro(m, TIPC_ADDR_NAME); |
| 6226 | PyModule_AddIntMacro(m, TIPC_ADDR_ID); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6227 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6228 | PyModule_AddIntMacro(m, TIPC_ZONE_SCOPE); |
| 6229 | PyModule_AddIntMacro(m, TIPC_CLUSTER_SCOPE); |
| 6230 | PyModule_AddIntMacro(m, TIPC_NODE_SCOPE); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6232 | /* for setsockopt() */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6233 | PyModule_AddIntMacro(m, SOL_TIPC); |
| 6234 | PyModule_AddIntMacro(m, TIPC_IMPORTANCE); |
| 6235 | PyModule_AddIntMacro(m, TIPC_SRC_DROPPABLE); |
| 6236 | PyModule_AddIntMacro(m, TIPC_DEST_DROPPABLE); |
| 6237 | PyModule_AddIntMacro(m, TIPC_CONN_TIMEOUT); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6238 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6239 | PyModule_AddIntMacro(m, TIPC_LOW_IMPORTANCE); |
| 6240 | PyModule_AddIntMacro(m, TIPC_MEDIUM_IMPORTANCE); |
| 6241 | PyModule_AddIntMacro(m, TIPC_HIGH_IMPORTANCE); |
| 6242 | PyModule_AddIntMacro(m, TIPC_CRITICAL_IMPORTANCE); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6243 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6244 | /* for subscriptions */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6245 | PyModule_AddIntMacro(m, TIPC_SUB_PORTS); |
| 6246 | PyModule_AddIntMacro(m, TIPC_SUB_SERVICE); |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 6247 | #ifdef TIPC_SUB_CANCEL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6248 | /* doesn't seem to be available everywhere */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6249 | PyModule_AddIntMacro(m, TIPC_SUB_CANCEL); |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 6250 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6251 | PyModule_AddIntMacro(m, TIPC_WAIT_FOREVER); |
| 6252 | PyModule_AddIntMacro(m, TIPC_PUBLISHED); |
| 6253 | PyModule_AddIntMacro(m, TIPC_WITHDRAWN); |
| 6254 | PyModule_AddIntMacro(m, TIPC_SUBSCR_TIMEOUT); |
| 6255 | PyModule_AddIntMacro(m, TIPC_CFG_SRV); |
| 6256 | PyModule_AddIntMacro(m, TIPC_TOP_SRV); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6257 | #endif |
| 6258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6259 | /* Socket types */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6260 | PyModule_AddIntMacro(m, SOCK_STREAM); |
| 6261 | PyModule_AddIntMacro(m, SOCK_DGRAM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 6262 | /* We have incomplete socket support. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6263 | PyModule_AddIntMacro(m, SOCK_RAW); |
| 6264 | PyModule_AddIntMacro(m, SOCK_SEQPACKET); |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 6265 | #if defined(SOCK_RDM) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6266 | PyModule_AddIntMacro(m, SOCK_RDM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 6267 | #endif |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 6268 | #ifdef SOCK_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6269 | PyModule_AddIntMacro(m, SOCK_CLOEXEC); |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 6270 | #endif |
| 6271 | #ifdef SOCK_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6272 | PyModule_AddIntMacro(m, SOCK_NONBLOCK); |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 6273 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6275 | #ifdef SO_DEBUG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6276 | PyModule_AddIntMacro(m, SO_DEBUG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6277 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6278 | #ifdef SO_ACCEPTCONN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6279 | PyModule_AddIntMacro(m, SO_ACCEPTCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6280 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6281 | #ifdef SO_REUSEADDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6282 | PyModule_AddIntMacro(m, SO_REUSEADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6283 | #endif |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 6284 | #ifdef SO_EXCLUSIVEADDRUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6285 | PyModule_AddIntMacro(m, SO_EXCLUSIVEADDRUSE); |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 6286 | #endif |
| 6287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6288 | #ifdef SO_KEEPALIVE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6289 | PyModule_AddIntMacro(m, SO_KEEPALIVE); |
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_DONTROUTE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6292 | PyModule_AddIntMacro(m, SO_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6293 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6294 | #ifdef SO_BROADCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6295 | PyModule_AddIntMacro(m, SO_BROADCAST); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6296 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6297 | #ifdef SO_USELOOPBACK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6298 | PyModule_AddIntMacro(m, SO_USELOOPBACK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6299 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6300 | #ifdef SO_LINGER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6301 | PyModule_AddIntMacro(m, SO_LINGER); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6302 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6303 | #ifdef SO_OOBINLINE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6304 | PyModule_AddIntMacro(m, SO_OOBINLINE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6305 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6306 | #ifdef SO_REUSEPORT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6307 | PyModule_AddIntMacro(m, SO_REUSEPORT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6308 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6309 | #ifdef SO_SNDBUF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6310 | PyModule_AddIntMacro(m, SO_SNDBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6311 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6312 | #ifdef SO_RCVBUF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6313 | PyModule_AddIntMacro(m, SO_RCVBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6314 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6315 | #ifdef SO_SNDLOWAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6316 | PyModule_AddIntMacro(m, SO_SNDLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6317 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6318 | #ifdef SO_RCVLOWAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6319 | PyModule_AddIntMacro(m, SO_RCVLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6320 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6321 | #ifdef SO_SNDTIMEO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6322 | PyModule_AddIntMacro(m, SO_SNDTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6323 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6324 | #ifdef SO_RCVTIMEO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6325 | PyModule_AddIntMacro(m, SO_RCVTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6326 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6327 | #ifdef SO_ERROR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6328 | PyModule_AddIntMacro(m, SO_ERROR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6329 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6330 | #ifdef SO_TYPE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6331 | PyModule_AddIntMacro(m, SO_TYPE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6332 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6333 | #ifdef SO_SETFIB |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6334 | PyModule_AddIntMacro(m, SO_SETFIB); |
Larry Hastings | f0f3795 | 2010-04-02 11:47:10 +0000 | [diff] [blame] | 6335 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6336 | #ifdef SO_PASSCRED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6337 | PyModule_AddIntMacro(m, SO_PASSCRED); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6338 | #endif |
| 6339 | #ifdef SO_PEERCRED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6340 | PyModule_AddIntMacro(m, SO_PEERCRED); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6341 | #endif |
| 6342 | #ifdef LOCAL_PEERCRED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6343 | PyModule_AddIntMacro(m, LOCAL_PEERCRED); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6344 | #endif |
Victor Stinner | cb98bed | 2011-10-25 13:45:48 +0200 | [diff] [blame] | 6345 | #ifdef SO_BINDTODEVICE |
| 6346 | PyModule_AddIntMacro(m, SO_BINDTODEVICE); |
| 6347 | #endif |
Benjamin Peterson | 1314ef7 | 2013-11-27 09:18:54 -0600 | [diff] [blame] | 6348 | #ifdef SO_PRIORITY |
| 6349 | PyModule_AddIntMacro(m, SO_PRIORITY); |
| 6350 | #endif |
Charles-François Natali | ea07eb9 | 2014-09-18 23:18:46 +0100 | [diff] [blame] | 6351 | #ifdef SO_MARK |
| 6352 | PyModule_AddIntMacro(m, SO_MARK); |
| 6353 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6355 | /* Maximum number of connections for "listen" */ |
| 6356 | #ifdef SOMAXCONN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6357 | PyModule_AddIntMacro(m, SOMAXCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6358 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6359 | PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6360 | #endif |
| 6361 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6362 | /* Ancilliary message types */ |
| 6363 | #ifdef SCM_RIGHTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6364 | PyModule_AddIntMacro(m, SCM_RIGHTS); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6365 | #endif |
| 6366 | #ifdef SCM_CREDENTIALS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6367 | PyModule_AddIntMacro(m, SCM_CREDENTIALS); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6368 | #endif |
| 6369 | #ifdef SCM_CREDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6370 | PyModule_AddIntMacro(m, SCM_CREDS); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6371 | #endif |
| 6372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6373 | /* Flags for send, recv */ |
| 6374 | #ifdef MSG_OOB |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6375 | PyModule_AddIntMacro(m, MSG_OOB); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6376 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6377 | #ifdef MSG_PEEK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6378 | PyModule_AddIntMacro(m, MSG_PEEK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6379 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6380 | #ifdef MSG_DONTROUTE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6381 | PyModule_AddIntMacro(m, MSG_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6382 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6383 | #ifdef MSG_DONTWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6384 | PyModule_AddIntMacro(m, MSG_DONTWAIT); |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 6385 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6386 | #ifdef MSG_EOR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6387 | PyModule_AddIntMacro(m, MSG_EOR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6388 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6389 | #ifdef MSG_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6390 | PyModule_AddIntMacro(m, MSG_TRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6391 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6392 | #ifdef MSG_CTRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6393 | PyModule_AddIntMacro(m, MSG_CTRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6394 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6395 | #ifdef MSG_WAITALL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6396 | PyModule_AddIntMacro(m, MSG_WAITALL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6397 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6398 | #ifdef MSG_BTAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6399 | PyModule_AddIntMacro(m, MSG_BTAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6400 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6401 | #ifdef MSG_ETAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6402 | PyModule_AddIntMacro(m, MSG_ETAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6403 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6404 | #ifdef MSG_NOSIGNAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6405 | PyModule_AddIntMacro(m, MSG_NOSIGNAL); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6406 | #endif |
| 6407 | #ifdef MSG_NOTIFICATION |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6408 | PyModule_AddIntMacro(m, MSG_NOTIFICATION); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6409 | #endif |
| 6410 | #ifdef MSG_CMSG_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6411 | PyModule_AddIntMacro(m, MSG_CMSG_CLOEXEC); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6412 | #endif |
| 6413 | #ifdef MSG_ERRQUEUE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6414 | PyModule_AddIntMacro(m, MSG_ERRQUEUE); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6415 | #endif |
| 6416 | #ifdef MSG_CONFIRM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6417 | PyModule_AddIntMacro(m, MSG_CONFIRM); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6418 | #endif |
| 6419 | #ifdef MSG_MORE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6420 | PyModule_AddIntMacro(m, MSG_MORE); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6421 | #endif |
| 6422 | #ifdef MSG_EOF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6423 | PyModule_AddIntMacro(m, MSG_EOF); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6424 | #endif |
| 6425 | #ifdef MSG_BCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6426 | PyModule_AddIntMacro(m, MSG_BCAST); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6427 | #endif |
| 6428 | #ifdef MSG_MCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6429 | PyModule_AddIntMacro(m, MSG_MCAST); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6430 | #endif |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 6431 | #ifdef MSG_FASTOPEN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6432 | PyModule_AddIntMacro(m, MSG_FASTOPEN); |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 6433 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6434 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6435 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 6436 | #ifdef SOL_SOCKET |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6437 | PyModule_AddIntMacro(m, SOL_SOCKET); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6438 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6439 | #ifdef SOL_IP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6440 | PyModule_AddIntMacro(m, SOL_IP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6441 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6442 | PyModule_AddIntConstant(m, "SOL_IP", 0); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6443 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6444 | #ifdef SOL_IPX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6445 | PyModule_AddIntMacro(m, SOL_IPX); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6446 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6447 | #ifdef SOL_AX25 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6448 | PyModule_AddIntMacro(m, SOL_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6449 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6450 | #ifdef SOL_ATALK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6451 | PyModule_AddIntMacro(m, SOL_ATALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6452 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6453 | #ifdef SOL_NETROM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6454 | PyModule_AddIntMacro(m, SOL_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6455 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6456 | #ifdef SOL_ROSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6457 | PyModule_AddIntMacro(m, SOL_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6458 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6459 | #ifdef SOL_TCP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6460 | PyModule_AddIntMacro(m, SOL_TCP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6461 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6462 | PyModule_AddIntConstant(m, "SOL_TCP", 6); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6463 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6464 | #ifdef SOL_UDP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6465 | PyModule_AddIntMacro(m, SOL_UDP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6466 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6467 | PyModule_AddIntConstant(m, "SOL_UDP", 17); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6468 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6469 | #ifdef SOL_CAN_BASE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6470 | PyModule_AddIntMacro(m, SOL_CAN_BASE); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6471 | #endif |
| 6472 | #ifdef SOL_CAN_RAW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6473 | PyModule_AddIntMacro(m, SOL_CAN_RAW); |
| 6474 | PyModule_AddIntMacro(m, CAN_RAW); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6475 | #endif |
| 6476 | #ifdef HAVE_LINUX_CAN_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6477 | PyModule_AddIntMacro(m, CAN_EFF_FLAG); |
| 6478 | PyModule_AddIntMacro(m, CAN_RTR_FLAG); |
| 6479 | PyModule_AddIntMacro(m, CAN_ERR_FLAG); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6480 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6481 | PyModule_AddIntMacro(m, CAN_SFF_MASK); |
| 6482 | PyModule_AddIntMacro(m, CAN_EFF_MASK); |
| 6483 | PyModule_AddIntMacro(m, CAN_ERR_MASK); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6484 | #endif |
| 6485 | #ifdef HAVE_LINUX_CAN_RAW_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6486 | PyModule_AddIntMacro(m, CAN_RAW_FILTER); |
| 6487 | PyModule_AddIntMacro(m, CAN_RAW_ERR_FILTER); |
| 6488 | PyModule_AddIntMacro(m, CAN_RAW_LOOPBACK); |
| 6489 | PyModule_AddIntMacro(m, CAN_RAW_RECV_OWN_MSGS); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6490 | #endif |
Charles-François Natali | 773e42d | 2013-02-05 19:42:01 +0100 | [diff] [blame] | 6491 | #ifdef HAVE_LINUX_CAN_BCM_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6492 | PyModule_AddIntMacro(m, CAN_BCM); |
Charles-François Natali | 773e42d | 2013-02-05 19:42:01 +0100 | [diff] [blame] | 6493 | PyModule_AddIntConstant(m, "CAN_BCM_TX_SETUP", TX_SETUP); |
| 6494 | PyModule_AddIntConstant(m, "CAN_BCM_TX_DELETE", TX_DELETE); |
| 6495 | PyModule_AddIntConstant(m, "CAN_BCM_TX_READ", TX_READ); |
| 6496 | PyModule_AddIntConstant(m, "CAN_BCM_TX_SEND", TX_SEND); |
| 6497 | PyModule_AddIntConstant(m, "CAN_BCM_RX_SETUP", RX_SETUP); |
| 6498 | PyModule_AddIntConstant(m, "CAN_BCM_RX_DELETE", RX_DELETE); |
| 6499 | PyModule_AddIntConstant(m, "CAN_BCM_RX_READ", RX_READ); |
| 6500 | PyModule_AddIntConstant(m, "CAN_BCM_TX_STATUS", TX_STATUS); |
| 6501 | PyModule_AddIntConstant(m, "CAN_BCM_TX_EXPIRED", TX_EXPIRED); |
| 6502 | PyModule_AddIntConstant(m, "CAN_BCM_RX_STATUS", RX_STATUS); |
| 6503 | PyModule_AddIntConstant(m, "CAN_BCM_RX_TIMEOUT", RX_TIMEOUT); |
| 6504 | PyModule_AddIntConstant(m, "CAN_BCM_RX_CHANGED", RX_CHANGED); |
| 6505 | #endif |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6506 | #ifdef SOL_RDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6507 | PyModule_AddIntMacro(m, SOL_RDS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6508 | #endif |
| 6509 | #ifdef RDS_CANCEL_SENT_TO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6510 | PyModule_AddIntMacro(m, RDS_CANCEL_SENT_TO); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6511 | #endif |
| 6512 | #ifdef RDS_GET_MR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6513 | PyModule_AddIntMacro(m, RDS_GET_MR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6514 | #endif |
| 6515 | #ifdef RDS_FREE_MR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6516 | PyModule_AddIntMacro(m, RDS_FREE_MR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6517 | #endif |
| 6518 | #ifdef RDS_RECVERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6519 | PyModule_AddIntMacro(m, RDS_RECVERR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6520 | #endif |
| 6521 | #ifdef RDS_CONG_MONITOR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6522 | PyModule_AddIntMacro(m, RDS_CONG_MONITOR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6523 | #endif |
| 6524 | #ifdef RDS_GET_MR_FOR_DEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6525 | PyModule_AddIntMacro(m, RDS_GET_MR_FOR_DEST); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6526 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6527 | #ifdef IPPROTO_IP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6528 | PyModule_AddIntMacro(m, IPPROTO_IP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6529 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6530 | PyModule_AddIntConstant(m, "IPPROTO_IP", 0); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6531 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6532 | #ifdef IPPROTO_HOPOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6533 | PyModule_AddIntMacro(m, IPPROTO_HOPOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6534 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6535 | #ifdef IPPROTO_ICMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6536 | PyModule_AddIntMacro(m, IPPROTO_ICMP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6537 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6538 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6539 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6540 | #ifdef IPPROTO_IGMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6541 | PyModule_AddIntMacro(m, IPPROTO_IGMP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6542 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6543 | #ifdef IPPROTO_GGP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6544 | PyModule_AddIntMacro(m, IPPROTO_GGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6545 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6546 | #ifdef IPPROTO_IPV4 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6547 | PyModule_AddIntMacro(m, IPPROTO_IPV4); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6548 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6549 | #ifdef IPPROTO_IPV6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6550 | PyModule_AddIntMacro(m, IPPROTO_IPV6); |
Martin v. Löwis | a0f1734 | 2003-10-03 13:56:20 +0000 | [diff] [blame] | 6551 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6552 | #ifdef IPPROTO_IPIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6553 | PyModule_AddIntMacro(m, IPPROTO_IPIP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6554 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6555 | #ifdef IPPROTO_TCP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6556 | PyModule_AddIntMacro(m, IPPROTO_TCP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6557 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6558 | PyModule_AddIntConstant(m, "IPPROTO_TCP", 6); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6559 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6560 | #ifdef IPPROTO_EGP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6561 | PyModule_AddIntMacro(m, IPPROTO_EGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6562 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6563 | #ifdef IPPROTO_PUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6564 | PyModule_AddIntMacro(m, IPPROTO_PUP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6565 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6566 | #ifdef IPPROTO_UDP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6567 | PyModule_AddIntMacro(m, IPPROTO_UDP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6568 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6569 | PyModule_AddIntConstant(m, "IPPROTO_UDP", 17); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6570 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6571 | #ifdef IPPROTO_IDP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6572 | PyModule_AddIntMacro(m, IPPROTO_IDP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6573 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6574 | #ifdef IPPROTO_HELLO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6575 | PyModule_AddIntMacro(m, IPPROTO_HELLO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6576 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6577 | #ifdef IPPROTO_ND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6578 | PyModule_AddIntMacro(m, IPPROTO_ND); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6579 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6580 | #ifdef IPPROTO_TP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6581 | PyModule_AddIntMacro(m, IPPROTO_TP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6582 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6583 | #ifdef IPPROTO_IPV6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6584 | PyModule_AddIntMacro(m, IPPROTO_IPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6585 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6586 | #ifdef IPPROTO_ROUTING |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6587 | PyModule_AddIntMacro(m, IPPROTO_ROUTING); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6588 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6589 | #ifdef IPPROTO_FRAGMENT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6590 | PyModule_AddIntMacro(m, IPPROTO_FRAGMENT); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6591 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6592 | #ifdef IPPROTO_RSVP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6593 | PyModule_AddIntMacro(m, IPPROTO_RSVP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6594 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6595 | #ifdef IPPROTO_GRE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6596 | PyModule_AddIntMacro(m, IPPROTO_GRE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6597 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6598 | #ifdef IPPROTO_ESP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6599 | PyModule_AddIntMacro(m, IPPROTO_ESP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6600 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6601 | #ifdef IPPROTO_AH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6602 | PyModule_AddIntMacro(m, IPPROTO_AH); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6603 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6604 | #ifdef IPPROTO_MOBILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6605 | PyModule_AddIntMacro(m, IPPROTO_MOBILE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6606 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6607 | #ifdef IPPROTO_ICMPV6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6608 | PyModule_AddIntMacro(m, IPPROTO_ICMPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6609 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6610 | #ifdef IPPROTO_NONE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6611 | PyModule_AddIntMacro(m, IPPROTO_NONE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6612 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6613 | #ifdef IPPROTO_DSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6614 | PyModule_AddIntMacro(m, IPPROTO_DSTOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6615 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6616 | #ifdef IPPROTO_XTP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6617 | PyModule_AddIntMacro(m, IPPROTO_XTP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6618 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6619 | #ifdef IPPROTO_EON |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6620 | PyModule_AddIntMacro(m, IPPROTO_EON); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6621 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6622 | #ifdef IPPROTO_PIM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6623 | PyModule_AddIntMacro(m, IPPROTO_PIM); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6624 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6625 | #ifdef IPPROTO_IPCOMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6626 | PyModule_AddIntMacro(m, IPPROTO_IPCOMP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6627 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6628 | #ifdef IPPROTO_VRRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6629 | PyModule_AddIntMacro(m, IPPROTO_VRRP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6630 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6631 | #ifdef IPPROTO_SCTP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6632 | PyModule_AddIntMacro(m, IPPROTO_SCTP); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6633 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6634 | #ifdef IPPROTO_BIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6635 | PyModule_AddIntMacro(m, IPPROTO_BIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6636 | #endif |
| 6637 | /**/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6638 | #ifdef IPPROTO_RAW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6639 | PyModule_AddIntMacro(m, IPPROTO_RAW); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6640 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6641 | PyModule_AddIntConstant(m, "IPPROTO_RAW", 255); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6642 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6643 | #ifdef IPPROTO_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6644 | PyModule_AddIntMacro(m, IPPROTO_MAX); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6645 | #endif |
| 6646 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6647 | #ifdef SYSPROTO_CONTROL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6648 | PyModule_AddIntMacro(m, SYSPROTO_CONTROL); |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6649 | #endif |
| 6650 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6651 | /* Some port configuration */ |
| 6652 | #ifdef IPPORT_RESERVED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6653 | PyModule_AddIntMacro(m, IPPORT_RESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6654 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6655 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6656 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6657 | #ifdef IPPORT_USERRESERVED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6658 | PyModule_AddIntMacro(m, IPPORT_USERRESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6659 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6660 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6661 | #endif |
| 6662 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6663 | /* Some reserved IP v.4 addresses */ |
| 6664 | #ifdef INADDR_ANY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6665 | PyModule_AddIntMacro(m, INADDR_ANY); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6666 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6667 | PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6668 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6669 | #ifdef INADDR_BROADCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6670 | PyModule_AddIntMacro(m, INADDR_BROADCAST); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6671 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6672 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6673 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6674 | #ifdef INADDR_LOOPBACK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6675 | PyModule_AddIntMacro(m, INADDR_LOOPBACK); |
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_LOOPBACK", 0x7F000001); |
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_UNSPEC_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6680 | PyModule_AddIntMacro(m, INADDR_UNSPEC_GROUP); |
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_UNSPEC_GROUP", 0xe0000000); |
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_ALLHOSTS_GROUP |
| 6685 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", |
| 6686 | INADDR_ALLHOSTS_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6687 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6688 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6689 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6690 | #ifdef INADDR_MAX_LOCAL_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6691 | PyModule_AddIntMacro(m, INADDR_MAX_LOCAL_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6692 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6693 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6694 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6695 | #ifdef INADDR_NONE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6696 | PyModule_AddIntMacro(m, INADDR_NONE); |
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_NONE", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6699 | #endif |
| 6700 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6701 | /* IPv4 [gs]etsockopt options */ |
| 6702 | #ifdef IP_OPTIONS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6703 | PyModule_AddIntMacro(m, IP_OPTIONS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6704 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6705 | #ifdef IP_HDRINCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6706 | PyModule_AddIntMacro(m, IP_HDRINCL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6707 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6708 | #ifdef IP_TOS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6709 | PyModule_AddIntMacro(m, IP_TOS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6710 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6711 | #ifdef IP_TTL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6712 | PyModule_AddIntMacro(m, IP_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6713 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6714 | #ifdef IP_RECVOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6715 | PyModule_AddIntMacro(m, IP_RECVOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6716 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6717 | #ifdef IP_RECVRETOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6718 | PyModule_AddIntMacro(m, IP_RECVRETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6719 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6720 | #ifdef IP_RECVDSTADDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6721 | PyModule_AddIntMacro(m, IP_RECVDSTADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6722 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6723 | #ifdef IP_RETOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6724 | PyModule_AddIntMacro(m, IP_RETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6725 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6726 | #ifdef IP_MULTICAST_IF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6727 | PyModule_AddIntMacro(m, IP_MULTICAST_IF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6728 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6729 | #ifdef IP_MULTICAST_TTL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6730 | PyModule_AddIntMacro(m, IP_MULTICAST_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6731 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6732 | #ifdef IP_MULTICAST_LOOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6733 | PyModule_AddIntMacro(m, IP_MULTICAST_LOOP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6734 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6735 | #ifdef IP_ADD_MEMBERSHIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6736 | PyModule_AddIntMacro(m, IP_ADD_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6737 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6738 | #ifdef IP_DROP_MEMBERSHIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6739 | PyModule_AddIntMacro(m, IP_DROP_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6740 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6741 | #ifdef IP_DEFAULT_MULTICAST_TTL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6742 | PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_TTL); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6743 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6744 | #ifdef IP_DEFAULT_MULTICAST_LOOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6745 | PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_LOOP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6746 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6747 | #ifdef IP_MAX_MEMBERSHIPS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6748 | PyModule_AddIntMacro(m, IP_MAX_MEMBERSHIPS); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6749 | #endif |
Charles-François Natali | 564a42c | 2011-12-17 14:59:56 +0100 | [diff] [blame] | 6750 | #ifdef IP_TRANSPARENT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6751 | PyModule_AddIntMacro(m, IP_TRANSPARENT); |
Charles-François Natali | 564a42c | 2011-12-17 14:59:56 +0100 | [diff] [blame] | 6752 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6753 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6754 | /* IPv6 [gs]etsockopt options, defined in RFC2553 */ |
| 6755 | #ifdef IPV6_JOIN_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6756 | PyModule_AddIntMacro(m, IPV6_JOIN_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6757 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6758 | #ifdef IPV6_LEAVE_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6759 | PyModule_AddIntMacro(m, IPV6_LEAVE_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6760 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6761 | #ifdef IPV6_MULTICAST_HOPS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6762 | PyModule_AddIntMacro(m, IPV6_MULTICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6763 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6764 | #ifdef IPV6_MULTICAST_IF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6765 | PyModule_AddIntMacro(m, IPV6_MULTICAST_IF); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6766 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6767 | #ifdef IPV6_MULTICAST_LOOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6768 | PyModule_AddIntMacro(m, IPV6_MULTICAST_LOOP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6769 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6770 | #ifdef IPV6_UNICAST_HOPS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6771 | PyModule_AddIntMacro(m, IPV6_UNICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6772 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6773 | /* Additional IPV6 socket options, defined in RFC 3493 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6774 | #ifdef IPV6_V6ONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6775 | PyModule_AddIntMacro(m, IPV6_V6ONLY); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6776 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6777 | /* Advanced IPV6 socket options, from RFC 3542 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6778 | #ifdef IPV6_CHECKSUM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6779 | PyModule_AddIntMacro(m, IPV6_CHECKSUM); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6780 | #endif |
| 6781 | #ifdef IPV6_DONTFRAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6782 | PyModule_AddIntMacro(m, IPV6_DONTFRAG); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6783 | #endif |
| 6784 | #ifdef IPV6_DSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6785 | PyModule_AddIntMacro(m, IPV6_DSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6786 | #endif |
| 6787 | #ifdef IPV6_HOPLIMIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6788 | PyModule_AddIntMacro(m, IPV6_HOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6789 | #endif |
| 6790 | #ifdef IPV6_HOPOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6791 | PyModule_AddIntMacro(m, IPV6_HOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6792 | #endif |
| 6793 | #ifdef IPV6_NEXTHOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6794 | PyModule_AddIntMacro(m, IPV6_NEXTHOP); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6795 | #endif |
| 6796 | #ifdef IPV6_PATHMTU |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6797 | PyModule_AddIntMacro(m, IPV6_PATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6798 | #endif |
| 6799 | #ifdef IPV6_PKTINFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6800 | PyModule_AddIntMacro(m, IPV6_PKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6801 | #endif |
| 6802 | #ifdef IPV6_RECVDSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6803 | PyModule_AddIntMacro(m, IPV6_RECVDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6804 | #endif |
| 6805 | #ifdef IPV6_RECVHOPLIMIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6806 | PyModule_AddIntMacro(m, IPV6_RECVHOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6807 | #endif |
| 6808 | #ifdef IPV6_RECVHOPOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6809 | PyModule_AddIntMacro(m, IPV6_RECVHOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6810 | #endif |
| 6811 | #ifdef IPV6_RECVPKTINFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6812 | PyModule_AddIntMacro(m, IPV6_RECVPKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6813 | #endif |
| 6814 | #ifdef IPV6_RECVRTHDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6815 | PyModule_AddIntMacro(m, IPV6_RECVRTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6816 | #endif |
| 6817 | #ifdef IPV6_RECVTCLASS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6818 | PyModule_AddIntMacro(m, IPV6_RECVTCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6819 | #endif |
| 6820 | #ifdef IPV6_RTHDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6821 | PyModule_AddIntMacro(m, IPV6_RTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6822 | #endif |
| 6823 | #ifdef IPV6_RTHDRDSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6824 | PyModule_AddIntMacro(m, IPV6_RTHDRDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6825 | #endif |
| 6826 | #ifdef IPV6_RTHDR_TYPE_0 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6827 | PyModule_AddIntMacro(m, IPV6_RTHDR_TYPE_0); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6828 | #endif |
| 6829 | #ifdef IPV6_RECVPATHMTU |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6830 | PyModule_AddIntMacro(m, IPV6_RECVPATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6831 | #endif |
| 6832 | #ifdef IPV6_TCLASS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6833 | PyModule_AddIntMacro(m, IPV6_TCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6834 | #endif |
| 6835 | #ifdef IPV6_USE_MIN_MTU |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6836 | PyModule_AddIntMacro(m, IPV6_USE_MIN_MTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6837 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6839 | /* TCP options */ |
| 6840 | #ifdef TCP_NODELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6841 | PyModule_AddIntMacro(m, TCP_NODELAY); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6842 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6843 | #ifdef TCP_MAXSEG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6844 | PyModule_AddIntMacro(m, TCP_MAXSEG); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6845 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6846 | #ifdef TCP_CORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6847 | PyModule_AddIntMacro(m, TCP_CORK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6848 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6849 | #ifdef TCP_KEEPIDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6850 | PyModule_AddIntMacro(m, TCP_KEEPIDLE); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6851 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6852 | #ifdef TCP_KEEPINTVL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6853 | PyModule_AddIntMacro(m, TCP_KEEPINTVL); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6854 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6855 | #ifdef TCP_KEEPCNT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6856 | PyModule_AddIntMacro(m, TCP_KEEPCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6857 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6858 | #ifdef TCP_SYNCNT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6859 | PyModule_AddIntMacro(m, TCP_SYNCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6860 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6861 | #ifdef TCP_LINGER2 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6862 | PyModule_AddIntMacro(m, TCP_LINGER2); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6863 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6864 | #ifdef TCP_DEFER_ACCEPT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6865 | PyModule_AddIntMacro(m, TCP_DEFER_ACCEPT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6866 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6867 | #ifdef TCP_WINDOW_CLAMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6868 | PyModule_AddIntMacro(m, TCP_WINDOW_CLAMP); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6869 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6870 | #ifdef TCP_INFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6871 | PyModule_AddIntMacro(m, TCP_INFO); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6872 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6873 | #ifdef TCP_QUICKACK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6874 | PyModule_AddIntMacro(m, TCP_QUICKACK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6875 | #endif |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 6876 | #ifdef TCP_FASTOPEN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6877 | PyModule_AddIntMacro(m, TCP_FASTOPEN); |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 6878 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6879 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6880 | /* IPX options */ |
| 6881 | #ifdef IPX_TYPE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6882 | PyModule_AddIntMacro(m, IPX_TYPE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6883 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 6884 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6885 | /* Reliable Datagram Sockets */ |
| 6886 | #ifdef RDS_CMSG_RDMA_ARGS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6887 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_ARGS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6888 | #endif |
| 6889 | #ifdef RDS_CMSG_RDMA_DEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6890 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_DEST); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6891 | #endif |
| 6892 | #ifdef RDS_CMSG_RDMA_MAP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6893 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_MAP); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6894 | #endif |
| 6895 | #ifdef RDS_CMSG_RDMA_STATUS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6896 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_STATUS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6897 | #endif |
| 6898 | #ifdef RDS_CMSG_RDMA_UPDATE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6899 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_UPDATE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6900 | #endif |
| 6901 | #ifdef RDS_RDMA_READWRITE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6902 | PyModule_AddIntMacro(m, RDS_RDMA_READWRITE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6903 | #endif |
| 6904 | #ifdef RDS_RDMA_FENCE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6905 | PyModule_AddIntMacro(m, RDS_RDMA_FENCE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6906 | #endif |
| 6907 | #ifdef RDS_RDMA_INVALIDATE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6908 | PyModule_AddIntMacro(m, RDS_RDMA_INVALIDATE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6909 | #endif |
| 6910 | #ifdef RDS_RDMA_USE_ONCE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6911 | PyModule_AddIntMacro(m, RDS_RDMA_USE_ONCE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6912 | #endif |
| 6913 | #ifdef RDS_RDMA_DONTWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6914 | PyModule_AddIntMacro(m, RDS_RDMA_DONTWAIT); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6915 | #endif |
| 6916 | #ifdef RDS_RDMA_NOTIFY_ME |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6917 | PyModule_AddIntMacro(m, RDS_RDMA_NOTIFY_ME); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6918 | #endif |
| 6919 | #ifdef RDS_RDMA_SILENT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6920 | PyModule_AddIntMacro(m, RDS_RDMA_SILENT); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6921 | #endif |
| 6922 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6923 | /* get{addr,name}info parameters */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6924 | #ifdef EAI_ADDRFAMILY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6925 | PyModule_AddIntMacro(m, EAI_ADDRFAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6926 | #endif |
| 6927 | #ifdef EAI_AGAIN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6928 | PyModule_AddIntMacro(m, EAI_AGAIN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6929 | #endif |
| 6930 | #ifdef EAI_BADFLAGS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6931 | PyModule_AddIntMacro(m, EAI_BADFLAGS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6932 | #endif |
| 6933 | #ifdef EAI_FAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6934 | PyModule_AddIntMacro(m, EAI_FAIL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6935 | #endif |
| 6936 | #ifdef EAI_FAMILY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6937 | PyModule_AddIntMacro(m, EAI_FAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6938 | #endif |
| 6939 | #ifdef EAI_MEMORY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6940 | PyModule_AddIntMacro(m, EAI_MEMORY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6941 | #endif |
| 6942 | #ifdef EAI_NODATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6943 | PyModule_AddIntMacro(m, EAI_NODATA); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6944 | #endif |
| 6945 | #ifdef EAI_NONAME |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6946 | PyModule_AddIntMacro(m, EAI_NONAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6947 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6948 | #ifdef EAI_OVERFLOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6949 | PyModule_AddIntMacro(m, EAI_OVERFLOW); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6950 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6951 | #ifdef EAI_SERVICE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6952 | PyModule_AddIntMacro(m, EAI_SERVICE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6953 | #endif |
| 6954 | #ifdef EAI_SOCKTYPE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6955 | PyModule_AddIntMacro(m, EAI_SOCKTYPE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6956 | #endif |
| 6957 | #ifdef EAI_SYSTEM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6958 | PyModule_AddIntMacro(m, EAI_SYSTEM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6959 | #endif |
| 6960 | #ifdef EAI_BADHINTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6961 | PyModule_AddIntMacro(m, EAI_BADHINTS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6962 | #endif |
| 6963 | #ifdef EAI_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6964 | PyModule_AddIntMacro(m, EAI_PROTOCOL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6965 | #endif |
| 6966 | #ifdef EAI_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6967 | PyModule_AddIntMacro(m, EAI_MAX); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6968 | #endif |
| 6969 | #ifdef AI_PASSIVE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6970 | PyModule_AddIntMacro(m, AI_PASSIVE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6971 | #endif |
| 6972 | #ifdef AI_CANONNAME |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6973 | PyModule_AddIntMacro(m, AI_CANONNAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6974 | #endif |
| 6975 | #ifdef AI_NUMERICHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6976 | PyModule_AddIntMacro(m, AI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6977 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6978 | #ifdef AI_NUMERICSERV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6979 | PyModule_AddIntMacro(m, AI_NUMERICSERV); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6980 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6981 | #ifdef AI_MASK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6982 | PyModule_AddIntMacro(m, AI_MASK); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6983 | #endif |
| 6984 | #ifdef AI_ALL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6985 | PyModule_AddIntMacro(m, AI_ALL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6986 | #endif |
| 6987 | #ifdef AI_V4MAPPED_CFG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6988 | PyModule_AddIntMacro(m, AI_V4MAPPED_CFG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6989 | #endif |
| 6990 | #ifdef AI_ADDRCONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6991 | PyModule_AddIntMacro(m, AI_ADDRCONFIG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6992 | #endif |
| 6993 | #ifdef AI_V4MAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6994 | PyModule_AddIntMacro(m, AI_V4MAPPED); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6995 | #endif |
| 6996 | #ifdef AI_DEFAULT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6997 | PyModule_AddIntMacro(m, AI_DEFAULT); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6998 | #endif |
| 6999 | #ifdef NI_MAXHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7000 | PyModule_AddIntMacro(m, NI_MAXHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7001 | #endif |
| 7002 | #ifdef NI_MAXSERV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7003 | PyModule_AddIntMacro(m, NI_MAXSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7004 | #endif |
| 7005 | #ifdef NI_NOFQDN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7006 | PyModule_AddIntMacro(m, NI_NOFQDN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7007 | #endif |
| 7008 | #ifdef NI_NUMERICHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7009 | PyModule_AddIntMacro(m, NI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7010 | #endif |
| 7011 | #ifdef NI_NAMEREQD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7012 | PyModule_AddIntMacro(m, NI_NAMEREQD); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7013 | #endif |
| 7014 | #ifdef NI_NUMERICSERV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7015 | PyModule_AddIntMacro(m, NI_NUMERICSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7016 | #endif |
| 7017 | #ifdef NI_DGRAM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7018 | PyModule_AddIntMacro(m, NI_DGRAM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 7019 | #endif |
| 7020 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7021 | /* shutdown() parameters */ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7022 | #ifdef SHUT_RD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7023 | PyModule_AddIntMacro(m, SHUT_RD); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7024 | #elif defined(SD_RECEIVE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7025 | PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7026 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7027 | PyModule_AddIntConstant(m, "SHUT_RD", 0); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7028 | #endif |
| 7029 | #ifdef SHUT_WR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7030 | PyModule_AddIntMacro(m, SHUT_WR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7031 | #elif defined(SD_SEND) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7032 | PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7033 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7034 | PyModule_AddIntConstant(m, "SHUT_WR", 1); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7035 | #endif |
| 7036 | #ifdef SHUT_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7037 | PyModule_AddIntMacro(m, SHUT_RDWR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7038 | #elif defined(SD_BOTH) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7039 | PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7040 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7041 | PyModule_AddIntConstant(m, "SHUT_RDWR", 2); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 7042 | #endif |
| 7043 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 7044 | #ifdef SIO_RCVALL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7045 | { |
| 7046 | DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS}; |
| 7047 | const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"}; |
| 7048 | int i; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 7049 | for(i = 0; i<Py_ARRAY_LENGTH(codes); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7050 | PyObject *tmp; |
| 7051 | tmp = PyLong_FromUnsignedLong(codes[i]); |
| 7052 | if (tmp == NULL) |
| 7053 | return NULL; |
| 7054 | PyModule_AddObject(m, names[i], tmp); |
| 7055 | } |
| 7056 | } |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7057 | PyModule_AddIntMacro(m, RCVALL_OFF); |
| 7058 | PyModule_AddIntMacro(m, RCVALL_ON); |
| 7059 | PyModule_AddIntMacro(m, RCVALL_SOCKETLEVELONLY); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 7060 | #ifdef RCVALL_IPLEVEL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7061 | PyModule_AddIntMacro(m, RCVALL_IPLEVEL); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 7062 | #endif |
| 7063 | #ifdef RCVALL_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 7064 | PyModule_AddIntMacro(m, RCVALL_MAX); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 7065 | #endif |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 7066 | #endif /* _MSTCPIP_ */ |
| 7067 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7068 | /* Initialize gethostbyname lock */ |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 7069 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7070 | netdb_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 7071 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7072 | return m; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 7073 | } |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7074 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 7075 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7076 | #ifndef HAVE_INET_PTON |
Christian Heimes | 96e7b3d | 2007-11-20 06:51:17 +0000 | [diff] [blame] | 7077 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 7078 | |
| 7079 | /* Simplistic emulation code for inet_pton that only works for IPv4 */ |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 7080 | /* These are not exposed because they do not set errno properly */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 7081 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 7082 | int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 7083 | inet_pton(int af, const char *src, void *dst) |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7084 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7085 | if (af == AF_INET) { |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 7086 | #if (SIZEOF_INT != 4) |
| 7087 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 7088 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7089 | unsigned int packed_addr; |
| 7090 | packed_addr = inet_addr(src); |
| 7091 | if (packed_addr == INADDR_NONE) |
| 7092 | return 0; |
| 7093 | memcpy(dst, &packed_addr, 4); |
| 7094 | return 1; |
| 7095 | } |
| 7096 | /* Should set errno to EAFNOSUPPORT */ |
| 7097 | return -1; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7098 | } |
| 7099 | |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 7100 | const char * |
| 7101 | 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] | 7102 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7103 | if (af == AF_INET) { |
| 7104 | struct in_addr packed_addr; |
| 7105 | if (size < 16) |
| 7106 | /* Should set errno to ENOSPC. */ |
| 7107 | return NULL; |
| 7108 | memcpy(&packed_addr, src, sizeof(packed_addr)); |
| 7109 | return strncpy(dst, inet_ntoa(packed_addr), size); |
| 7110 | } |
| 7111 | /* Should set errno to EAFNOSUPPORT */ |
| 7112 | return NULL; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7113 | } |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 7114 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7115 | #endif |
Christian Heimes | b615069 | 2007-11-15 23:37:07 +0000 | [diff] [blame] | 7116 | #endif |