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 | |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 287 | #endif |
| 288 | |
Skip Montanaro | 7befb99 | 2004-02-10 16:50:21 +0000 | [diff] [blame] | 289 | #include <stddef.h> |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 290 | |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 291 | #ifndef O_NONBLOCK |
| 292 | # define O_NONBLOCK O_NDELAY |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 293 | #endif |
| 294 | |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 295 | /* include Python's addrinfo.h unless it causes trouble */ |
| 296 | #if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE) |
| 297 | /* Do not include addinfo.h on some newer IRIX versions. |
| 298 | * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21, |
| 299 | * for example, but not by 6.5.10. |
| 300 | */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 301 | #elif defined(_MSC_VER) && _MSC_VER>1201 |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 302 | /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and |
| 303 | * EAI_* constants are defined in (the already included) ws2tcpip.h. |
| 304 | */ |
| 305 | #else |
| 306 | # include "addrinfo.h" |
| 307 | #endif |
Jason Tishler | c246cb7 | 2004-08-09 13:25:59 +0000 | [diff] [blame] | 308 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 309 | #ifndef HAVE_INET_PTON |
Christian Heimes | 96e7b3d | 2007-11-20 06:51:17 +0000 | [diff] [blame] | 310 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 311 | int inet_pton(int af, const char *src, void *dst); |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 312 | 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] | 313 | #endif |
Christian Heimes | b615069 | 2007-11-15 23:37:07 +0000 | [diff] [blame] | 314 | #endif |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 315 | |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 316 | #ifdef __APPLE__ |
| 317 | /* On OS X, getaddrinfo returns no error indication of lookup |
| 318 | failure, so we must use the emulation instead of the libinfo |
| 319 | implementation. Unfortunately, performing an autoconf test |
| 320 | for this bug would require DNS access for the machine performing |
| 321 | the configuration, which is not acceptable. Therefore, we |
| 322 | determine the bug just by checking for __APPLE__. If this bug |
| 323 | gets ever fixed, perhaps checking for sys/version.h would be |
| 324 | appropriate, which is 10/0 on the system with the bug. */ |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 325 | #ifndef HAVE_GETNAMEINFO |
| 326 | /* This bug seems to be fixed in Jaguar. Ths easiest way I could |
| 327 | Find to check for Jaguar is that it has getnameinfo(), which |
| 328 | older releases don't have */ |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 329 | #undef HAVE_GETADDRINFO |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 330 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 331 | |
| 332 | #ifdef HAVE_INET_ATON |
| 333 | #define USE_INET_ATON_WEAKLINK |
| 334 | #endif |
| 335 | |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 336 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 337 | |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 338 | /* 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] | 339 | #if !defined(HAVE_GETADDRINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 340 | /* avoid clashes with the C library definition of the symbol. */ |
| 341 | #define getaddrinfo fake_getaddrinfo |
| 342 | #define gai_strerror fake_gai_strerror |
| 343 | #define freeaddrinfo fake_freeaddrinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 344 | #include "getaddrinfo.c" |
| 345 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 346 | #if !defined(HAVE_GETNAMEINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 347 | #define getnameinfo fake_getnameinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 348 | #include "getnameinfo.c" |
| 349 | #endif |
| 350 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 351 | #ifdef MS_WINDOWS |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 352 | #define SOCKETCLOSE closesocket |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 353 | #endif |
| 354 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 355 | #ifdef MS_WIN32 |
Kristján Valur Jónsson | 310052c | 2012-03-31 13:35:00 +0000 | [diff] [blame] | 356 | #undef EAFNOSUPPORT |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 357 | #define EAFNOSUPPORT WSAEAFNOSUPPORT |
| 358 | #define snprintf _snprintf |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 359 | #endif |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 360 | |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 361 | #ifndef SOCKETCLOSE |
| 362 | #define SOCKETCLOSE close |
| 363 | #endif |
| 364 | |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 365 | #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] | 366 | #define USE_BLUETOOTH 1 |
| 367 | #if defined(__FreeBSD__) |
| 368 | #define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP |
| 369 | #define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 370 | #define BTPROTO_HCI BLUETOOTH_PROTO_HCI |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 371 | #define SOL_HCI SOL_HCI_RAW |
| 372 | #define HCI_FILTER SO_HCI_RAW_FILTER |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 373 | #define sockaddr_l2 sockaddr_l2cap |
| 374 | #define sockaddr_rc sockaddr_rfcomm |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 375 | #define hci_dev hci_node |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 376 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb) |
| 377 | #define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 378 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 379 | #elif defined(__NetBSD__) || defined(__DragonFly__) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 380 | #define sockaddr_l2 sockaddr_bt |
| 381 | #define sockaddr_rc sockaddr_bt |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 382 | #define sockaddr_hci sockaddr_bt |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 383 | #define sockaddr_sco sockaddr_bt |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 384 | #define SOL_HCI BTPROTO_HCI |
| 385 | #define HCI_DATA_DIR SO_HCI_DIRECTION |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 386 | #define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb) |
| 387 | #define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 388 | #define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 389 | #define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 390 | #else |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 391 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb) |
| 392 | #define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 393 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 394 | #define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb) |
| 395 | #endif |
| 396 | #endif |
| 397 | |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 398 | /* Convert "sock_addr_t *" to "struct sockaddr *". */ |
| 399 | #define SAS2SA(x) (&((x)->sa)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 400 | |
Martin v. Löwis | e941617 | 2003-05-03 10:12:45 +0000 | [diff] [blame] | 401 | /* |
| 402 | * Constants for getnameinfo() |
| 403 | */ |
| 404 | #if !defined(NI_MAXHOST) |
| 405 | #define NI_MAXHOST 1025 |
| 406 | #endif |
| 407 | #if !defined(NI_MAXSERV) |
| 408 | #define NI_MAXSERV 32 |
| 409 | #endif |
| 410 | |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 411 | #ifndef INVALID_SOCKET /* MS defines this */ |
| 412 | #define INVALID_SOCKET (-1) |
| 413 | #endif |
| 414 | |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 415 | #ifndef INADDR_NONE |
| 416 | #define INADDR_NONE (-1) |
| 417 | #endif |
| 418 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 419 | /* XXX There's a problem here: *static* functions are not supposed to have |
| 420 | a Py prefix (or use CapitalizedWords). Later... */ |
| 421 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 422 | /* Global variable holding the exception type for errors detected |
| 423 | by this module (but not argument type or memory errors, etc.). */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 424 | static PyObject *socket_herror; |
| 425 | static PyObject *socket_gaierror; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 426 | static PyObject *socket_timeout; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 427 | |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 428 | /* A forward reference to the socket type object. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 429 | The sock_type variable contains pointers to various functions, |
| 430 | some of which call new_sockobject(), which uses sock_type, so |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 431 | there has to be a circular reference. */ |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 432 | static PyTypeObject sock_type; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 433 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 434 | #if defined(HAVE_POLL_H) |
| 435 | #include <poll.h> |
| 436 | #elif defined(HAVE_SYS_POLL_H) |
| 437 | #include <sys/poll.h> |
| 438 | #endif |
| 439 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 440 | /* Largest value to try to store in a socklen_t (used when handling |
| 441 | ancillary data). POSIX requires socklen_t to hold at least |
| 442 | (2**31)-1 and recommends against storing larger values, but |
| 443 | socklen_t was originally int in the BSD interface, so to be on the |
| 444 | safe side we use the smaller of (2**31)-1 and INT_MAX. */ |
| 445 | #if INT_MAX > 0x7fffffff |
| 446 | #define SOCKLEN_T_LIMIT 0x7fffffff |
| 447 | #else |
| 448 | #define SOCKLEN_T_LIMIT INT_MAX |
| 449 | #endif |
| 450 | |
Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 451 | #ifdef HAVE_POLL |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 452 | /* Instead of select(), we'll use poll() since poll() works on any fd. */ |
| 453 | #define IS_SELECTABLE(s) 1 |
| 454 | /* 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] | 455 | #else |
Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 456 | /* If there's no timeout left, we don't have to call select, so it's a safe, |
| 457 | * little white lie. */ |
| 458 | #define IS_SELECTABLE(s) (_PyIsSelectable_fd((s)->sock_fd) || (s)->sock_timeout <= 0.0) |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 459 | #endif |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 460 | |
| 461 | static PyObject* |
| 462 | select_error(void) |
| 463 | { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 464 | PyErr_SetString(PyExc_OSError, "unable to select on socket"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 465 | return NULL; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 468 | #ifdef MS_WINDOWS |
Antoine Pitrou | c8f0196 | 2010-09-28 22:03:27 +0000 | [diff] [blame] | 469 | #ifndef WSAEAGAIN |
| 470 | #define WSAEAGAIN WSAEWOULDBLOCK |
| 471 | #endif |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 472 | #define CHECK_ERRNO(expected) \ |
| 473 | (WSAGetLastError() == WSA ## expected) |
| 474 | #else |
| 475 | #define CHECK_ERRNO(expected) \ |
| 476 | (errno == expected) |
| 477 | #endif |
| 478 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 479 | #ifdef MS_WINDOWS |
| 480 | /* Does WSASocket() support the WSA_FLAG_NO_HANDLE_INHERIT flag? */ |
| 481 | static int support_wsa_no_inherit = -1; |
| 482 | #endif |
| 483 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 484 | /* Convenience function to raise an error according to errno |
| 485 | and return a NULL pointer from a function. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 486 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 487 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 488 | set_error(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 489 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 490 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 491 | int err_no = WSAGetLastError(); |
| 492 | /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which |
| 493 | recognizes the error codes used by both GetLastError() and |
| 494 | WSAGetLastError */ |
| 495 | if (err_no) |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 496 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, err_no); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 497 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 498 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 499 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 502 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 503 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 504 | set_herror(int h_error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 505 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 506 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 507 | |
| 508 | #ifdef HAVE_HSTRERROR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 | v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 510 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 511 | v = Py_BuildValue("(is)", h_error, "host not found"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 512 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 513 | if (v != NULL) { |
| 514 | PyErr_SetObject(socket_herror, v); |
| 515 | Py_DECREF(v); |
| 516 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 517 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | |
| 522 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 523 | set_gaierror(int error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 524 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 525 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 526 | |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 527 | #ifdef EAI_SYSTEM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 528 | /* EAI_SYSTEM is not available on Windows XP. */ |
| 529 | if (error == EAI_SYSTEM) |
| 530 | return set_error(); |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 531 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 532 | |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 533 | #ifdef HAVE_GAI_STRERROR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 | v = Py_BuildValue("(is)", error, gai_strerror(error)); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 535 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 536 | v = Py_BuildValue("(is)", error, "getaddrinfo failed"); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 537 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 538 | if (v != NULL) { |
| 539 | PyErr_SetObject(socket_gaierror, v); |
| 540 | Py_DECREF(v); |
| 541 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 542 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 543 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 546 | /* Function to perform the setting of socket blocking mode |
| 547 | internally. block = (1 | 0). */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 548 | static int |
| 549 | internal_setblocking(PySocketSockObject *s, int block) |
| 550 | { |
Victor Stinner | b6dab6b | 2014-07-23 22:56:55 +0200 | [diff] [blame] | 551 | #ifdef MS_WINDOWS |
| 552 | u_long arg; |
| 553 | #endif |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 554 | #if !defined(MS_WINDOWS) \ |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 555 | && !((defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO))) |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 556 | int delay_flag, new_delay_flag; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 557 | #endif |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 558 | #ifdef SOCK_NONBLOCK |
| 559 | if (block) |
| 560 | s->sock_type &= (~SOCK_NONBLOCK); |
| 561 | else |
| 562 | s->sock_type |= SOCK_NONBLOCK; |
| 563 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 564 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 565 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 566 | #ifndef MS_WINDOWS |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 567 | #if (defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 568 | block = !block; |
| 569 | ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block); |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 570 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 571 | delay_flag = fcntl(s->sock_fd, F_GETFL, 0); |
| 572 | if (block) |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 573 | new_delay_flag = delay_flag & (~O_NONBLOCK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 574 | else |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 575 | new_delay_flag = delay_flag | O_NONBLOCK; |
| 576 | if (new_delay_flag != delay_flag) |
| 577 | fcntl(s->sock_fd, F_SETFL, new_delay_flag); |
| 578 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 579 | #else /* MS_WINDOWS */ |
Victor Stinner | b6dab6b | 2014-07-23 22:56:55 +0200 | [diff] [blame] | 580 | arg = !block; |
| 581 | ioctlsocket(s->sock_fd, FIONBIO, &arg); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 582 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 583 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 584 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 585 | /* Since these don't return anything */ |
| 586 | return 1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 589 | /* 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] | 590 | The argument writing indicates the direction. |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 591 | This does not raise an exception; we'll let our caller do that |
| 592 | after they've reacquired the interpreter lock. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 593 | Returns 1 on timeout, -1 on error, 0 otherwise. */ |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 594 | static int |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 595 | internal_select_ex(PySocketSockObject *s, int writing, double interval) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 596 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 597 | int n; |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 599 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
| 600 | if (s->sock_timeout <= 0.0) |
| 601 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 602 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 603 | /* Guard against closed socket */ |
| 604 | if (s->sock_fd < 0) |
| 605 | return 0; |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 606 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 607 | /* Handling this condition here simplifies the select loops */ |
| 608 | if (interval < 0.0) |
| 609 | return 1; |
| 610 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 611 | /* Prefer poll, if available, since you can poll() any fd |
| 612 | * which can't be done with select(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 613 | #ifdef HAVE_POLL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 614 | { |
| 615 | struct pollfd pollfd; |
| 616 | int timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 617 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 618 | pollfd.fd = s->sock_fd; |
| 619 | pollfd.events = writing ? POLLOUT : POLLIN; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 620 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 621 | /* s->sock_timeout is in seconds, timeout in ms */ |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 622 | timeout = (int)(interval * 1000 + 0.5); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | n = poll(&pollfd, 1, timeout); |
| 624 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 625 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 626 | { |
| 627 | /* Construct the arguments to select */ |
| 628 | fd_set fds; |
| 629 | struct timeval tv; |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 630 | tv.tv_sec = (int)interval; |
| 631 | tv.tv_usec = (int)((interval - tv.tv_sec) * 1e6); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | FD_ZERO(&fds); |
| 633 | FD_SET(s->sock_fd, &fds); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 634 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 635 | /* See if the socket is ready */ |
| 636 | if (writing) |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 637 | n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 638 | NULL, &fds, NULL, &tv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 639 | else |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 640 | n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 641 | &fds, NULL, NULL, &tv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 642 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 643 | #endif |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 645 | if (n < 0) |
| 646 | return -1; |
| 647 | if (n == 0) |
| 648 | return 1; |
| 649 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 652 | static int |
| 653 | internal_select(PySocketSockObject *s, int writing) |
| 654 | { |
| 655 | return internal_select_ex(s, writing, s->sock_timeout); |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | Two macros for automatic retry of select() in case of false positives |
| 660 | (for example, select() could indicate a socket is ready for reading |
| 661 | but the data then discarded by the OS because of a wrong checksum). |
| 662 | Here is an example of use: |
| 663 | |
| 664 | BEGIN_SELECT_LOOP(s) |
| 665 | Py_BEGIN_ALLOW_THREADS |
| 666 | timeout = internal_select_ex(s, 0, interval); |
| 667 | if (!timeout) |
| 668 | outlen = recv(s->sock_fd, cbuf, len, flags); |
| 669 | Py_END_ALLOW_THREADS |
| 670 | if (timeout == 1) { |
| 671 | PyErr_SetString(socket_timeout, "timed out"); |
| 672 | return -1; |
| 673 | } |
| 674 | END_SELECT_LOOP(s) |
| 675 | */ |
| 676 | |
| 677 | #define BEGIN_SELECT_LOOP(s) \ |
| 678 | { \ |
| 679 | _PyTime_timeval now, deadline = {0, 0}; \ |
| 680 | double interval = s->sock_timeout; \ |
| 681 | int has_timeout = s->sock_timeout > 0.0; \ |
| 682 | if (has_timeout) { \ |
Victor Stinner | ae58649 | 2014-09-02 23:18:25 +0200 | [diff] [blame] | 683 | _PyTime_monotonic(&now); \ |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 684 | deadline = now; \ |
| 685 | _PyTime_ADD_SECONDS(deadline, s->sock_timeout); \ |
| 686 | } \ |
| 687 | while (1) { \ |
| 688 | errno = 0; \ |
| 689 | |
| 690 | #define END_SELECT_LOOP(s) \ |
| 691 | if (!has_timeout || \ |
| 692 | (!CHECK_ERRNO(EWOULDBLOCK) && !CHECK_ERRNO(EAGAIN))) \ |
| 693 | break; \ |
Victor Stinner | ae58649 | 2014-09-02 23:18:25 +0200 | [diff] [blame] | 694 | _PyTime_monotonic(&now); \ |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 695 | interval = _PyTime_INTERVAL(now, deadline); \ |
| 696 | } \ |
| 697 | } \ |
| 698 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 699 | /* Initialize a new socket object. */ |
| 700 | |
Tim Peters | a12b4cf | 2002-07-18 22:38:44 +0000 | [diff] [blame] | 701 | static double defaulttimeout = -1.0; /* Default timeout for new sockets */ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 702 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 703 | static void |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 704 | init_sockobject(PySocketSockObject *s, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 | SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 706 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 707 | s->sock_fd = fd; |
| 708 | s->sock_family = family; |
| 709 | s->sock_type = type; |
| 710 | s->sock_proto = proto; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 711 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 712 | s->errorhandler = &set_error; |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 713 | #ifdef SOCK_NONBLOCK |
| 714 | if (type & SOCK_NONBLOCK) |
| 715 | s->sock_timeout = 0.0; |
| 716 | else |
| 717 | #endif |
| 718 | { |
| 719 | s->sock_timeout = defaulttimeout; |
| 720 | if (defaulttimeout >= 0.0) |
| 721 | internal_setblocking(s, 0); |
| 722 | } |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 723 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 727 | /* Create a new socket object. |
| 728 | This just creates the object and initializes it. |
| 729 | If the creation fails, return NULL and set an exception (implicit |
| 730 | in NEWOBJ()). */ |
| 731 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 732 | static PySocketSockObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 733 | new_sockobject(SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 734 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 735 | PySocketSockObject *s; |
| 736 | s = (PySocketSockObject *) |
| 737 | PyType_GenericNew(&sock_type, NULL, NULL); |
| 738 | if (s != NULL) |
| 739 | init_sockobject(s, fd, family, type, proto); |
| 740 | return s; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 743 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 744 | /* Lock to allow python interpreter to continue, but only allow one |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 745 | thread to be in gethostbyname or getaddrinfo */ |
| 746 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Matthias Klose | d182a6c | 2012-08-14 17:29:04 +0200 | [diff] [blame] | 747 | static PyThread_type_lock netdb_lock; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 748 | #endif |
| 749 | |
| 750 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 751 | /* Convert a string specifying a host name or one of a few symbolic |
| 752 | names to a numeric IP address. This usually calls gethostbyname() |
| 753 | to do the work; the names "" and "<broadcast>" are special. |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 754 | Return the length (IPv4 should be 4 bytes), or negative if |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 755 | an error occurred; then an exception is raised. */ |
| 756 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 757 | static int |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 758 | 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] | 759 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 760 | struct addrinfo hints, *res; |
| 761 | int error; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 762 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 763 | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
| 764 | if (name[0] == '\0') { |
| 765 | int siz; |
| 766 | memset(&hints, 0, sizeof(hints)); |
| 767 | hints.ai_family = af; |
| 768 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
| 769 | hints.ai_flags = AI_PASSIVE; |
| 770 | Py_BEGIN_ALLOW_THREADS |
| 771 | ACQUIRE_GETADDRINFO_LOCK |
| 772 | error = getaddrinfo(NULL, "0", &hints, &res); |
| 773 | Py_END_ALLOW_THREADS |
| 774 | /* We assume that those thread-unsafe getaddrinfo() versions |
| 775 | *are* safe regarding their return value, ie. that a |
| 776 | subsequent call to getaddrinfo() does not destroy the |
| 777 | outcome of the first call. */ |
| 778 | RELEASE_GETADDRINFO_LOCK |
| 779 | if (error) { |
| 780 | set_gaierror(error); |
| 781 | return -1; |
| 782 | } |
| 783 | switch (res->ai_family) { |
| 784 | case AF_INET: |
| 785 | siz = 4; |
| 786 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 787 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 788 | case AF_INET6: |
| 789 | siz = 16; |
| 790 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 791 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 792 | default: |
| 793 | freeaddrinfo(res); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 794 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 795 | "unsupported address family"); |
| 796 | return -1; |
| 797 | } |
| 798 | if (res->ai_next) { |
| 799 | freeaddrinfo(res); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 800 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 801 | "wildcard resolved to multiple address"); |
| 802 | return -1; |
| 803 | } |
| 804 | if (res->ai_addrlen < addr_ret_size) |
| 805 | addr_ret_size = res->ai_addrlen; |
| 806 | memcpy(addr_ret, res->ai_addr, addr_ret_size); |
| 807 | freeaddrinfo(res); |
| 808 | return siz; |
| 809 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 810 | /* special-case broadcast - inet_addr() below can return INADDR_NONE for |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 811 | * this */ |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 812 | if (strcmp(name, "255.255.255.255") == 0 || |
| 813 | strcmp(name, "<broadcast>") == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 814 | struct sockaddr_in *sin; |
| 815 | if (af != AF_INET && af != AF_UNSPEC) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 816 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 817 | "address family mismatched"); |
| 818 | return -1; |
| 819 | } |
| 820 | sin = (struct sockaddr_in *)addr_ret; |
| 821 | memset((void *) sin, '\0', sizeof(*sin)); |
| 822 | sin->sin_family = AF_INET; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 823 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 824 | sin->sin_len = sizeof(*sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 825 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 826 | sin->sin_addr.s_addr = INADDR_BROADCAST; |
| 827 | return sizeof(sin->sin_addr); |
| 828 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 829 | |
| 830 | /* avoid a name resolution in case of numeric address */ |
| 831 | #ifdef HAVE_INET_PTON |
| 832 | /* check for an IPv4 address */ |
| 833 | if (af == AF_UNSPEC || af == AF_INET) { |
| 834 | struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret; |
| 835 | memset(sin, 0, sizeof(*sin)); |
| 836 | if (inet_pton(AF_INET, name, &sin->sin_addr) > 0) { |
| 837 | sin->sin_family = AF_INET; |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 838 | #ifdef HAVE_SOCKADDR_SA_LEN |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 839 | sin->sin_len = sizeof(*sin); |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 840 | #endif |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 841 | return 4; |
| 842 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 843 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 844 | #ifdef ENABLE_IPV6 |
| 845 | /* check for an IPv6 address - if the address contains a scope ID, we |
| 846 | * fallback to getaddrinfo(), which can handle translation from interface |
| 847 | * name to interface index */ |
| 848 | if ((af == AF_UNSPEC || af == AF_INET6) && !strchr(name, '%')) { |
| 849 | struct sockaddr_in6 *sin = (struct sockaddr_in6 *)addr_ret; |
| 850 | memset(sin, 0, sizeof(*sin)); |
| 851 | if (inet_pton(AF_INET6, name, &sin->sin6_addr) > 0) { |
| 852 | sin->sin6_family = AF_INET6; |
| 853 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 854 | sin->sin6_len = sizeof(*sin); |
| 855 | #endif |
| 856 | return 16; |
| 857 | } |
| 858 | } |
| 859 | #endif /* ENABLE_IPV6 */ |
| 860 | #else /* HAVE_INET_PTON */ |
| 861 | /* check for an IPv4 address */ |
| 862 | if (af == AF_INET || af == AF_UNSPEC) { |
| 863 | struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret; |
| 864 | memset(sin, 0, sizeof(*sin)); |
| 865 | if ((sin->sin_addr.s_addr = inet_addr(name)) != INADDR_NONE) { |
| 866 | sin->sin_family = AF_INET; |
| 867 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 868 | sin->sin_len = sizeof(*sin); |
| 869 | #endif |
| 870 | return 4; |
| 871 | } |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 872 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 873 | #endif /* HAVE_INET_PTON */ |
| 874 | |
| 875 | /* perform a name resolution */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | memset(&hints, 0, sizeof(hints)); |
| 877 | hints.ai_family = af; |
| 878 | Py_BEGIN_ALLOW_THREADS |
| 879 | ACQUIRE_GETADDRINFO_LOCK |
| 880 | error = getaddrinfo(name, NULL, &hints, &res); |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 881 | #if defined(__digital__) && defined(__unix__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 882 | if (error == EAI_NONAME && af == AF_UNSPEC) { |
| 883 | /* On Tru64 V5.1, numeric-to-addr conversion fails |
| 884 | if no address family is given. Assume IPv4 for now.*/ |
| 885 | hints.ai_family = AF_INET; |
| 886 | error = getaddrinfo(name, NULL, &hints, &res); |
| 887 | } |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 888 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 889 | Py_END_ALLOW_THREADS |
| 890 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 891 | if (error) { |
| 892 | set_gaierror(error); |
| 893 | return -1; |
| 894 | } |
| 895 | if (res->ai_addrlen < addr_ret_size) |
| 896 | addr_ret_size = res->ai_addrlen; |
| 897 | memcpy((char *) addr_ret, res->ai_addr, addr_ret_size); |
| 898 | freeaddrinfo(res); |
| 899 | switch (addr_ret->sa_family) { |
| 900 | case AF_INET: |
| 901 | return 4; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 902 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 903 | case AF_INET6: |
| 904 | return 16; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 905 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 907 | PyErr_SetString(PyExc_OSError, "unknown address family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 908 | return -1; |
| 909 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 912 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 913 | /* Create a string object representing an IP address. |
| 914 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 915 | size numbers). */ |
| 916 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 917 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 918 | makeipaddr(struct sockaddr *addr, int addrlen) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 919 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 920 | char buf[NI_MAXHOST]; |
| 921 | int error; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 922 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 923 | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, |
| 924 | NI_NUMERICHOST); |
| 925 | if (error) { |
| 926 | set_gaierror(error); |
| 927 | return NULL; |
| 928 | } |
| 929 | return PyUnicode_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 933 | #ifdef USE_BLUETOOTH |
| 934 | /* Convert a string representation of a Bluetooth address into a numeric |
| 935 | address. Returns the length (6), or raises an exception and returns -1 if |
| 936 | an error occurred. */ |
| 937 | |
| 938 | static int |
| 939 | setbdaddr(char *name, bdaddr_t *bdaddr) |
| 940 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 941 | unsigned int b0, b1, b2, b3, b4, b5; |
| 942 | char ch; |
| 943 | int n; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 944 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 945 | n = sscanf(name, "%X:%X:%X:%X:%X:%X%c", |
| 946 | &b5, &b4, &b3, &b2, &b1, &b0, &ch); |
| 947 | if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) { |
| 948 | bdaddr->b[0] = b0; |
| 949 | bdaddr->b[1] = b1; |
| 950 | bdaddr->b[2] = b2; |
| 951 | bdaddr->b[3] = b3; |
| 952 | bdaddr->b[4] = b4; |
| 953 | bdaddr->b[5] = b5; |
| 954 | return 6; |
| 955 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 956 | PyErr_SetString(PyExc_OSError, "bad bluetooth address"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 957 | return -1; |
| 958 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | /* Create a string representation of the Bluetooth address. This is always a |
| 962 | string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal |
| 963 | value (zero padded if necessary). */ |
| 964 | |
| 965 | static PyObject * |
| 966 | makebdaddr(bdaddr_t *bdaddr) |
| 967 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 968 | char buf[(6 * 2) + 5 + 1]; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 969 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 970 | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 971 | bdaddr->b[5], bdaddr->b[4], bdaddr->b[3], |
| 972 | bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]); |
| 973 | return PyUnicode_FromString(buf); |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 974 | } |
| 975 | #endif |
| 976 | |
| 977 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 978 | /* Create an object representing the given socket address, |
| 979 | suitable for passing it back to bind(), connect() etc. |
| 980 | The family field of the sockaddr structure is inspected |
| 981 | to determine what kind of address it really is. */ |
| 982 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 983 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 984 | static PyObject * |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 985 | 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] | 986 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 987 | if (addrlen == 0) { |
| 988 | /* No address -- may be recvfrom() from known socket */ |
| 989 | Py_INCREF(Py_None); |
| 990 | return Py_None; |
| 991 | } |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 992 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 993 | switch (addr->sa_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 994 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 995 | case AF_INET: |
| 996 | { |
| 997 | struct sockaddr_in *a; |
| 998 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 999 | PyObject *ret = NULL; |
| 1000 | if (addrobj) { |
| 1001 | a = (struct sockaddr_in *)addr; |
| 1002 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 1003 | Py_DECREF(addrobj); |
| 1004 | } |
| 1005 | return ret; |
| 1006 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1007 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1008 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1009 | case AF_UNIX: |
| 1010 | { |
| 1011 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1012 | #ifdef linux |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1013 | if (a->sun_path[0] == 0) { /* Linux abstract namespace */ |
| 1014 | addrlen -= offsetof(struct sockaddr_un, sun_path); |
Charles-François Natali | b10c71d | 2013-05-21 10:45:46 +0200 | [diff] [blame] | 1015 | return PyBytes_FromStringAndSize(a->sun_path, addrlen); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 | } |
| 1017 | else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1018 | #endif /* linux */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1019 | { |
| 1020 | /* regular NULL-terminated string */ |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1021 | return PyUnicode_DecodeFSDefault(a->sun_path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1022 | } |
| 1023 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1024 | #endif /* AF_UNIX */ |
| 1025 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1026 | #if defined(AF_NETLINK) |
| 1027 | case AF_NETLINK: |
| 1028 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1029 | struct sockaddr_nl *a = (struct sockaddr_nl *) addr; |
| 1030 | return Py_BuildValue("II", a->nl_pid, a->nl_groups); |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1031 | } |
| 1032 | #endif /* AF_NETLINK */ |
| 1033 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1034 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | case AF_INET6: |
| 1036 | { |
| 1037 | struct sockaddr_in6 *a; |
| 1038 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1039 | PyObject *ret = NULL; |
| 1040 | if (addrobj) { |
| 1041 | a = (struct sockaddr_in6 *)addr; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1042 | ret = Py_BuildValue("OiII", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1043 | addrobj, |
| 1044 | ntohs(a->sin6_port), |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1045 | ntohl(a->sin6_flowinfo), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1046 | a->sin6_scope_id); |
| 1047 | Py_DECREF(addrobj); |
| 1048 | } |
| 1049 | return ret; |
| 1050 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1051 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1052 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1053 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1054 | case AF_BLUETOOTH: |
| 1055 | switch (proto) { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1056 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1057 | case BTPROTO_L2CAP: |
| 1058 | { |
| 1059 | struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr; |
| 1060 | PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr)); |
| 1061 | PyObject *ret = NULL; |
| 1062 | if (addrobj) { |
| 1063 | ret = Py_BuildValue("Oi", |
| 1064 | addrobj, |
| 1065 | _BT_L2_MEMB(a, psm)); |
| 1066 | Py_DECREF(addrobj); |
| 1067 | } |
| 1068 | return ret; |
| 1069 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1070 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1071 | case BTPROTO_RFCOMM: |
| 1072 | { |
| 1073 | struct sockaddr_rc *a = (struct sockaddr_rc *) addr; |
| 1074 | PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr)); |
| 1075 | PyObject *ret = NULL; |
| 1076 | if (addrobj) { |
| 1077 | ret = Py_BuildValue("Oi", |
| 1078 | addrobj, |
| 1079 | _BT_RC_MEMB(a, channel)); |
| 1080 | Py_DECREF(addrobj); |
| 1081 | } |
| 1082 | return ret; |
| 1083 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1084 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1085 | case BTPROTO_HCI: |
| 1086 | { |
| 1087 | struct sockaddr_hci *a = (struct sockaddr_hci *) addr; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1088 | #if defined(__NetBSD__) || defined(__DragonFly__) |
| 1089 | return makebdaddr(&_BT_HCI_MEMB(a, bdaddr)); |
| 1090 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1091 | PyObject *ret = NULL; |
| 1092 | ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev)); |
| 1093 | return ret; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1094 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1095 | } |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1096 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1097 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1098 | case BTPROTO_SCO: |
| 1099 | { |
| 1100 | struct sockaddr_sco *a = (struct sockaddr_sco *) addr; |
| 1101 | return makebdaddr(&_BT_SCO_MEMB(a, bdaddr)); |
| 1102 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1103 | #endif |
| 1104 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1105 | default: |
| 1106 | PyErr_SetString(PyExc_ValueError, |
| 1107 | "Unknown Bluetooth protocol"); |
| 1108 | return NULL; |
| 1109 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1110 | #endif |
| 1111 | |
Antoine Pitrou | b156a46 | 2010-10-27 20:13:57 +0000 | [diff] [blame] | 1112 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1113 | case AF_PACKET: |
| 1114 | { |
| 1115 | struct sockaddr_ll *a = (struct sockaddr_ll *)addr; |
| 1116 | char *ifname = ""; |
| 1117 | struct ifreq ifr; |
| 1118 | /* need to look up interface name give index */ |
| 1119 | if (a->sll_ifindex) { |
| 1120 | ifr.ifr_ifindex = a->sll_ifindex; |
| 1121 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
| 1122 | ifname = ifr.ifr_name; |
| 1123 | } |
| 1124 | return Py_BuildValue("shbhy#", |
| 1125 | ifname, |
| 1126 | ntohs(a->sll_protocol), |
| 1127 | a->sll_pkttype, |
| 1128 | a->sll_hatype, |
| 1129 | a->sll_addr, |
| 1130 | a->sll_halen); |
| 1131 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1132 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1133 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1134 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1135 | case AF_TIPC: |
| 1136 | { |
| 1137 | struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr; |
| 1138 | if (a->addrtype == TIPC_ADDR_NAMESEQ) { |
| 1139 | return Py_BuildValue("IIIII", |
| 1140 | a->addrtype, |
| 1141 | a->addr.nameseq.type, |
| 1142 | a->addr.nameseq.lower, |
| 1143 | a->addr.nameseq.upper, |
| 1144 | a->scope); |
| 1145 | } else if (a->addrtype == TIPC_ADDR_NAME) { |
| 1146 | return Py_BuildValue("IIIII", |
| 1147 | a->addrtype, |
| 1148 | a->addr.name.name.type, |
| 1149 | a->addr.name.name.instance, |
| 1150 | a->addr.name.name.instance, |
| 1151 | a->scope); |
| 1152 | } else if (a->addrtype == TIPC_ADDR_ID) { |
| 1153 | return Py_BuildValue("IIIII", |
| 1154 | a->addrtype, |
| 1155 | a->addr.id.node, |
| 1156 | a->addr.id.ref, |
| 1157 | 0, |
| 1158 | a->scope); |
| 1159 | } else { |
| 1160 | PyErr_SetString(PyExc_ValueError, |
| 1161 | "Invalid address type"); |
| 1162 | return NULL; |
| 1163 | } |
| 1164 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1165 | #endif |
| 1166 | |
Vinay Sajip | ecfc98c | 2014-03-20 12:42:42 +0000 | [diff] [blame] | 1167 | #ifdef AF_CAN |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1168 | case AF_CAN: |
| 1169 | { |
| 1170 | struct sockaddr_can *a = (struct sockaddr_can *)addr; |
| 1171 | char *ifname = ""; |
| 1172 | struct ifreq ifr; |
| 1173 | /* need to look up interface name given index */ |
| 1174 | if (a->can_ifindex) { |
| 1175 | ifr.ifr_ifindex = a->can_ifindex; |
| 1176 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
| 1177 | ifname = ifr.ifr_name; |
| 1178 | } |
| 1179 | |
| 1180 | return Py_BuildValue("O&h", PyUnicode_DecodeFSDefault, |
| 1181 | ifname, |
| 1182 | a->can_family); |
| 1183 | } |
| 1184 | #endif |
| 1185 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1186 | #ifdef PF_SYSTEM |
| 1187 | case PF_SYSTEM: |
| 1188 | switch(proto) { |
| 1189 | #ifdef SYSPROTO_CONTROL |
| 1190 | case SYSPROTO_CONTROL: |
| 1191 | { |
| 1192 | struct sockaddr_ctl *a = (struct sockaddr_ctl *)addr; |
| 1193 | return Py_BuildValue("(II)", a->sc_id, a->sc_unit); |
| 1194 | } |
| 1195 | #endif |
| 1196 | default: |
| 1197 | PyErr_SetString(PyExc_ValueError, |
| 1198 | "Invalid address type"); |
| 1199 | return 0; |
| 1200 | } |
| 1201 | #endif |
| 1202 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1203 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1204 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1205 | default: |
| 1206 | /* If we don't know the address family, don't raise an |
| 1207 | exception -- return it as an (int, bytes) tuple. */ |
| 1208 | return Py_BuildValue("iy#", |
| 1209 | addr->sa_family, |
| 1210 | addr->sa_data, |
| 1211 | sizeof(addr->sa_data)); |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1213 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1216 | /* Helper for getsockaddrarg: bypass IDNA for ASCII-only host names |
| 1217 | (in particular, numeric IP addresses). */ |
| 1218 | struct maybe_idna { |
| 1219 | PyObject *obj; |
| 1220 | char *buf; |
| 1221 | }; |
| 1222 | |
| 1223 | static void |
| 1224 | idna_cleanup(struct maybe_idna *data) |
| 1225 | { |
| 1226 | Py_CLEAR(data->obj); |
| 1227 | } |
| 1228 | |
| 1229 | static int |
| 1230 | idna_converter(PyObject *obj, struct maybe_idna *data) |
| 1231 | { |
| 1232 | size_t len; |
| 1233 | PyObject *obj2, *obj3; |
| 1234 | if (obj == NULL) { |
| 1235 | idna_cleanup(data); |
| 1236 | return 1; |
| 1237 | } |
| 1238 | data->obj = NULL; |
| 1239 | len = -1; |
| 1240 | if (PyBytes_Check(obj)) { |
| 1241 | data->buf = PyBytes_AsString(obj); |
| 1242 | len = PyBytes_Size(obj); |
| 1243 | } |
| 1244 | else if (PyByteArray_Check(obj)) { |
| 1245 | data->buf = PyByteArray_AsString(obj); |
| 1246 | len = PyByteArray_Size(obj); |
| 1247 | } |
| 1248 | else if (PyUnicode_Check(obj) && PyUnicode_READY(obj) == 0 && PyUnicode_IS_COMPACT_ASCII(obj)) { |
| 1249 | data->buf = PyUnicode_DATA(obj); |
| 1250 | len = PyUnicode_GET_LENGTH(obj); |
| 1251 | } |
| 1252 | else { |
| 1253 | obj2 = PyUnicode_FromObject(obj); |
| 1254 | if (!obj2) { |
| 1255 | PyErr_Format(PyExc_TypeError, "string or unicode text buffer expected, not %s", |
| 1256 | obj->ob_type->tp_name); |
| 1257 | return 0; |
| 1258 | } |
| 1259 | obj3 = PyUnicode_AsEncodedString(obj2, "idna", NULL); |
| 1260 | Py_DECREF(obj2); |
| 1261 | if (!obj3) { |
| 1262 | PyErr_SetString(PyExc_TypeError, "encoding of hostname failed"); |
| 1263 | return 0; |
| 1264 | } |
| 1265 | if (!PyBytes_Check(obj3)) { |
Martin v. Löwis | 17fd1e1 | 2014-08-05 16:13:50 +0200 | [diff] [blame] | 1266 | Py_DECREF(obj3); |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1267 | PyErr_SetString(PyExc_TypeError, "encoding of hostname failed to return bytes"); |
| 1268 | return 0; |
| 1269 | } |
| 1270 | data->obj = obj3; |
| 1271 | data->buf = PyBytes_AS_STRING(obj3); |
| 1272 | len = PyBytes_GET_SIZE(obj3); |
| 1273 | } |
| 1274 | if (strlen(data->buf) != len) { |
| 1275 | Py_CLEAR(data->obj); |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1276 | 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] | 1277 | return 0; |
| 1278 | } |
| 1279 | return Py_CLEANUP_SUPPORTED; |
Victor Stinner | 1a62a68 | 2014-08-17 19:33:28 +0200 | [diff] [blame] | 1280 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1281 | |
| 1282 | /* Parse a socket address argument according to the socket object's |
| 1283 | address family. Return 1 if the address was in the proper format, |
| 1284 | 0 of not. The address is returned through addr_ret, its length |
| 1285 | through len_ret. */ |
| 1286 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1287 | static int |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1288 | getsockaddrarg(PySocketSockObject *s, PyObject *args, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1289 | struct sockaddr *addr_ret, int *len_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1290 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1291 | switch (s->sock_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1292 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1293 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1294 | case AF_UNIX: |
| 1295 | { |
| 1296 | struct sockaddr_un* addr; |
| 1297 | char *path; |
| 1298 | int len; |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1299 | int retval = 0; |
| 1300 | |
| 1301 | /* PEP 383. Not using PyUnicode_FSConverter since we need to |
| 1302 | allow embedded nulls on Linux. */ |
| 1303 | if (PyUnicode_Check(args)) { |
| 1304 | if ((args = PyUnicode_EncodeFSDefault(args)) == NULL) |
| 1305 | return 0; |
| 1306 | } |
| 1307 | else |
| 1308 | Py_INCREF(args); |
| 1309 | if (!PyArg_Parse(args, "y#", &path, &len)) |
| 1310 | goto unix_out; |
Victor Stinner | 1a62a68 | 2014-08-17 19:33:28 +0200 | [diff] [blame] | 1311 | assert(len >= 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1313 | addr = (struct sockaddr_un*)addr_ret; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1314 | #ifdef linux |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1315 | if (len > 0 && path[0] == 0) { |
| 1316 | /* Linux abstract namespace extension */ |
Victor Stinner | 1a62a68 | 2014-08-17 19:33:28 +0200 | [diff] [blame] | 1317 | if ((size_t)len > sizeof addr->sun_path) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1318 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1319 | "AF_UNIX path too long"); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1320 | goto unix_out; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1321 | } |
| 1322 | } |
| 1323 | else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1324 | #endif /* linux */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1325 | { |
| 1326 | /* regular NULL-terminated string */ |
Victor Stinner | 1a62a68 | 2014-08-17 19:33:28 +0200 | [diff] [blame] | 1327 | if ((size_t)len >= sizeof addr->sun_path) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1328 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1329 | "AF_UNIX path too long"); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1330 | goto unix_out; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1331 | } |
| 1332 | addr->sun_path[len] = 0; |
| 1333 | } |
| 1334 | addr->sun_family = s->sock_family; |
| 1335 | memcpy(addr->sun_path, path, len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1336 | *len_ret = len + offsetof(struct sockaddr_un, sun_path); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1337 | retval = 1; |
| 1338 | unix_out: |
| 1339 | Py_DECREF(args); |
| 1340 | return retval; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1341 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1342 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1343 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1344 | #if defined(AF_NETLINK) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1345 | case AF_NETLINK: |
| 1346 | { |
| 1347 | struct sockaddr_nl* addr; |
| 1348 | int pid, groups; |
| 1349 | addr = (struct sockaddr_nl *)addr_ret; |
| 1350 | if (!PyTuple_Check(args)) { |
| 1351 | PyErr_Format( |
| 1352 | PyExc_TypeError, |
| 1353 | "getsockaddrarg: " |
| 1354 | "AF_NETLINK address must be tuple, not %.500s", |
| 1355 | Py_TYPE(args)->tp_name); |
| 1356 | return 0; |
| 1357 | } |
| 1358 | if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups)) |
| 1359 | return 0; |
| 1360 | addr->nl_family = AF_NETLINK; |
| 1361 | addr->nl_pid = pid; |
| 1362 | addr->nl_groups = groups; |
| 1363 | *len_ret = sizeof(*addr); |
| 1364 | return 1; |
| 1365 | } |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1366 | #endif |
| 1367 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 1368 | #ifdef AF_RDS |
| 1369 | case AF_RDS: |
| 1370 | /* RDS sockets use sockaddr_in: fall-through */ |
| 1371 | #endif |
| 1372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1373 | case AF_INET: |
| 1374 | { |
| 1375 | struct sockaddr_in* addr; |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1376 | struct maybe_idna host = {NULL, NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1377 | int port, result; |
| 1378 | if (!PyTuple_Check(args)) { |
| 1379 | PyErr_Format( |
| 1380 | PyExc_TypeError, |
| 1381 | "getsockaddrarg: " |
| 1382 | "AF_INET address must be tuple, not %.500s", |
| 1383 | Py_TYPE(args)->tp_name); |
| 1384 | return 0; |
| 1385 | } |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1386 | if (!PyArg_ParseTuple(args, "O&i:getsockaddrarg", |
| 1387 | idna_converter, &host, &port)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1388 | return 0; |
| 1389 | addr=(struct sockaddr_in*)addr_ret; |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1390 | result = setipaddr(host.buf, (struct sockaddr *)addr, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1391 | sizeof(*addr), AF_INET); |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1392 | idna_cleanup(&host); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1393 | if (result < 0) |
| 1394 | return 0; |
| 1395 | if (port < 0 || port > 0xffff) { |
| 1396 | PyErr_SetString( |
| 1397 | PyExc_OverflowError, |
| 1398 | "getsockaddrarg: port must be 0-65535."); |
| 1399 | return 0; |
| 1400 | } |
| 1401 | addr->sin_family = AF_INET; |
| 1402 | addr->sin_port = htons((short)port); |
| 1403 | *len_ret = sizeof *addr; |
| 1404 | return 1; |
| 1405 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1406 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1407 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1408 | case AF_INET6: |
| 1409 | { |
| 1410 | struct sockaddr_in6* addr; |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1411 | struct maybe_idna host = {NULL, NULL}; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1412 | int port, result; |
| 1413 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1414 | flowinfo = scope_id = 0; |
| 1415 | if (!PyTuple_Check(args)) { |
| 1416 | PyErr_Format( |
| 1417 | PyExc_TypeError, |
| 1418 | "getsockaddrarg: " |
| 1419 | "AF_INET6 address must be tuple, not %.500s", |
| 1420 | Py_TYPE(args)->tp_name); |
| 1421 | return 0; |
| 1422 | } |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1423 | if (!PyArg_ParseTuple(args, "O&i|II", |
| 1424 | idna_converter, &host, &port, &flowinfo, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1425 | &scope_id)) { |
| 1426 | return 0; |
| 1427 | } |
| 1428 | addr = (struct sockaddr_in6*)addr_ret; |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1429 | result = setipaddr(host.buf, (struct sockaddr *)addr, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1430 | sizeof(*addr), AF_INET6); |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1431 | idna_cleanup(&host); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1432 | if (result < 0) |
| 1433 | return 0; |
| 1434 | if (port < 0 || port > 0xffff) { |
| 1435 | PyErr_SetString( |
| 1436 | PyExc_OverflowError, |
| 1437 | "getsockaddrarg: port must be 0-65535."); |
| 1438 | return 0; |
| 1439 | } |
Charles-François Natali | 9b0c006 | 2012-06-23 10:17:05 +0200 | [diff] [blame] | 1440 | if (flowinfo > 0xfffff) { |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1441 | PyErr_SetString( |
| 1442 | PyExc_OverflowError, |
| 1443 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 1444 | return 0; |
| 1445 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1446 | addr->sin6_family = s->sock_family; |
| 1447 | addr->sin6_port = htons((short)port); |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1448 | addr->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1449 | addr->sin6_scope_id = scope_id; |
| 1450 | *len_ret = sizeof *addr; |
| 1451 | return 1; |
| 1452 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1453 | #endif |
| 1454 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1455 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1456 | case AF_BLUETOOTH: |
| 1457 | { |
| 1458 | switch (s->sock_proto) { |
| 1459 | case BTPROTO_L2CAP: |
| 1460 | { |
| 1461 | struct sockaddr_l2 *addr; |
| 1462 | char *straddr; |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1463 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1464 | addr = (struct sockaddr_l2 *)addr_ret; |
| 1465 | memset(addr, 0, sizeof(struct sockaddr_l2)); |
| 1466 | _BT_L2_MEMB(addr, family) = AF_BLUETOOTH; |
| 1467 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1468 | &_BT_L2_MEMB(addr, psm))) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1469 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1470 | "wrong format"); |
| 1471 | return 0; |
| 1472 | } |
| 1473 | if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0) |
| 1474 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1476 | *len_ret = sizeof *addr; |
| 1477 | return 1; |
| 1478 | } |
| 1479 | case BTPROTO_RFCOMM: |
| 1480 | { |
| 1481 | struct sockaddr_rc *addr; |
| 1482 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1483 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1484 | addr = (struct sockaddr_rc *)addr_ret; |
| 1485 | _BT_RC_MEMB(addr, family) = AF_BLUETOOTH; |
| 1486 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1487 | &_BT_RC_MEMB(addr, channel))) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1488 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1489 | "wrong format"); |
| 1490 | return 0; |
| 1491 | } |
| 1492 | if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0) |
| 1493 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1494 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1495 | *len_ret = sizeof *addr; |
| 1496 | return 1; |
| 1497 | } |
| 1498 | case BTPROTO_HCI: |
| 1499 | { |
| 1500 | struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1501 | #if defined(__NetBSD__) || defined(__DragonFly__) |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1502 | char *straddr = PyBytes_AS_STRING(args); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1503 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1504 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1505 | if (straddr == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1506 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1507 | "wrong format"); |
| 1508 | return 0; |
| 1509 | } |
| 1510 | if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0) |
| 1511 | return 0; |
| 1512 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1513 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
| 1514 | if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1515 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1516 | "wrong format"); |
| 1517 | return 0; |
| 1518 | } |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1519 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1520 | *len_ret = sizeof *addr; |
| 1521 | return 1; |
| 1522 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1523 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1524 | case BTPROTO_SCO: |
| 1525 | { |
| 1526 | struct sockaddr_sco *addr; |
| 1527 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1528 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1529 | addr = (struct sockaddr_sco *)addr_ret; |
| 1530 | _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; |
| 1531 | if (!PyBytes_Check(args)) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1532 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1533 | "wrong format"); |
| 1534 | return 0; |
| 1535 | } |
| 1536 | straddr = PyBytes_AS_STRING(args); |
| 1537 | if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0) |
| 1538 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1539 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1540 | *len_ret = sizeof *addr; |
| 1541 | return 1; |
| 1542 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1543 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1544 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1545 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: unknown Bluetooth protocol"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1546 | return 0; |
| 1547 | } |
| 1548 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1549 | #endif |
| 1550 | |
Antoine Pitrou | b156a46 | 2010-10-27 20:13:57 +0000 | [diff] [blame] | 1551 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1552 | case AF_PACKET: |
| 1553 | { |
| 1554 | struct sockaddr_ll* addr; |
| 1555 | struct ifreq ifr; |
| 1556 | char *interfaceName; |
| 1557 | int protoNumber; |
| 1558 | int hatype = 0; |
| 1559 | int pkttype = 0; |
| 1560 | char *haddr = NULL; |
| 1561 | unsigned int halen = 0; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1562 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1563 | if (!PyTuple_Check(args)) { |
| 1564 | PyErr_Format( |
| 1565 | PyExc_TypeError, |
| 1566 | "getsockaddrarg: " |
| 1567 | "AF_PACKET address must be tuple, not %.500s", |
| 1568 | Py_TYPE(args)->tp_name); |
| 1569 | return 0; |
| 1570 | } |
| 1571 | if (!PyArg_ParseTuple(args, "si|iiy#", &interfaceName, |
| 1572 | &protoNumber, &pkttype, &hatype, |
| 1573 | &haddr, &halen)) |
| 1574 | return 0; |
| 1575 | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name)); |
| 1576 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
| 1577 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
| 1578 | s->errorhandler(); |
| 1579 | return 0; |
| 1580 | } |
| 1581 | if (halen > 8) { |
| 1582 | PyErr_SetString(PyExc_ValueError, |
| 1583 | "Hardware address must be 8 bytes or less"); |
| 1584 | return 0; |
| 1585 | } |
| 1586 | if (protoNumber < 0 || protoNumber > 0xffff) { |
| 1587 | PyErr_SetString( |
| 1588 | PyExc_OverflowError, |
| 1589 | "getsockaddrarg: protoNumber must be 0-65535."); |
| 1590 | return 0; |
| 1591 | } |
| 1592 | addr = (struct sockaddr_ll*)addr_ret; |
| 1593 | addr->sll_family = AF_PACKET; |
| 1594 | addr->sll_protocol = htons((short)protoNumber); |
| 1595 | addr->sll_ifindex = ifr.ifr_ifindex; |
| 1596 | addr->sll_pkttype = pkttype; |
| 1597 | addr->sll_hatype = hatype; |
| 1598 | if (halen != 0) { |
| 1599 | memcpy(&addr->sll_addr, haddr, halen); |
| 1600 | } |
| 1601 | addr->sll_halen = halen; |
| 1602 | *len_ret = sizeof *addr; |
| 1603 | return 1; |
| 1604 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1605 | #endif |
| 1606 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1607 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1608 | case AF_TIPC: |
| 1609 | { |
| 1610 | unsigned int atype, v1, v2, v3; |
| 1611 | unsigned int scope = TIPC_CLUSTER_SCOPE; |
| 1612 | struct sockaddr_tipc *addr; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1614 | if (!PyTuple_Check(args)) { |
| 1615 | PyErr_Format( |
| 1616 | PyExc_TypeError, |
| 1617 | "getsockaddrarg: " |
| 1618 | "AF_TIPC address must be tuple, not %.500s", |
| 1619 | Py_TYPE(args)->tp_name); |
| 1620 | return 0; |
| 1621 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1622 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1623 | if (!PyArg_ParseTuple(args, |
| 1624 | "IIII|I;Invalid TIPC address format", |
| 1625 | &atype, &v1, &v2, &v3, &scope)) |
| 1626 | return 0; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1627 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1628 | addr = (struct sockaddr_tipc *) addr_ret; |
| 1629 | memset(addr, 0, sizeof(struct sockaddr_tipc)); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1630 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1631 | addr->family = AF_TIPC; |
| 1632 | addr->scope = scope; |
| 1633 | addr->addrtype = atype; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1634 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1635 | if (atype == TIPC_ADDR_NAMESEQ) { |
| 1636 | addr->addr.nameseq.type = v1; |
| 1637 | addr->addr.nameseq.lower = v2; |
| 1638 | addr->addr.nameseq.upper = v3; |
| 1639 | } else if (atype == TIPC_ADDR_NAME) { |
| 1640 | addr->addr.name.name.type = v1; |
| 1641 | addr->addr.name.name.instance = v2; |
| 1642 | } else if (atype == TIPC_ADDR_ID) { |
| 1643 | addr->addr.id.node = v1; |
| 1644 | addr->addr.id.ref = v2; |
| 1645 | } else { |
| 1646 | /* Shouldn't happen */ |
| 1647 | PyErr_SetString(PyExc_TypeError, "Invalid address type"); |
| 1648 | return 0; |
| 1649 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1650 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1651 | *len_ret = sizeof(*addr); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1653 | return 1; |
| 1654 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1655 | #endif |
| 1656 | |
Vinay Sajip | ed6783f | 2014-03-21 11:44:32 +0000 | [diff] [blame] | 1657 | #if defined(AF_CAN) && defined(CAN_RAW) && defined(CAN_BCM) |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1658 | case AF_CAN: |
| 1659 | switch (s->sock_proto) { |
| 1660 | case CAN_RAW: |
Charles-François Natali | 773e42d | 2013-02-05 19:42:01 +0100 | [diff] [blame] | 1661 | /* fall-through */ |
| 1662 | case CAN_BCM: |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1663 | { |
| 1664 | struct sockaddr_can *addr; |
| 1665 | PyObject *interfaceName; |
| 1666 | struct ifreq ifr; |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1667 | Py_ssize_t len; |
| 1668 | |
Benjamin Peterson | 18b7191 | 2013-05-16 15:29:44 -0500 | [diff] [blame] | 1669 | addr = (struct sockaddr_can *)addr_ret; |
| 1670 | |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1671 | if (!PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter, |
| 1672 | &interfaceName)) |
| 1673 | return 0; |
| 1674 | |
| 1675 | len = PyBytes_GET_SIZE(interfaceName); |
| 1676 | |
| 1677 | if (len == 0) { |
| 1678 | ifr.ifr_ifindex = 0; |
Victor Stinner | 1a62a68 | 2014-08-17 19:33:28 +0200 | [diff] [blame] | 1679 | } else if ((size_t)len < sizeof(ifr.ifr_name)) { |
Christian Heimes | 15b6885 | 2012-09-10 01:25:50 +0200 | [diff] [blame] | 1680 | strncpy(ifr.ifr_name, PyBytes_AS_STRING(interfaceName), sizeof(ifr.ifr_name)); |
| 1681 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1682 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
| 1683 | s->errorhandler(); |
| 1684 | Py_DECREF(interfaceName); |
| 1685 | return 0; |
| 1686 | } |
| 1687 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1688 | PyErr_SetString(PyExc_OSError, |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1689 | "AF_CAN interface name too long"); |
| 1690 | Py_DECREF(interfaceName); |
| 1691 | return 0; |
| 1692 | } |
| 1693 | |
| 1694 | addr->can_family = AF_CAN; |
| 1695 | addr->can_ifindex = ifr.ifr_ifindex; |
| 1696 | |
| 1697 | *len_ret = sizeof(*addr); |
| 1698 | Py_DECREF(interfaceName); |
| 1699 | return 1; |
| 1700 | } |
| 1701 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1702 | PyErr_SetString(PyExc_OSError, |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1703 | "getsockaddrarg: unsupported CAN protocol"); |
| 1704 | return 0; |
| 1705 | } |
| 1706 | #endif |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1707 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1708 | #ifdef PF_SYSTEM |
| 1709 | case PF_SYSTEM: |
| 1710 | switch (s->sock_proto) { |
| 1711 | #ifdef SYSPROTO_CONTROL |
| 1712 | case SYSPROTO_CONTROL: |
| 1713 | { |
| 1714 | struct sockaddr_ctl *addr; |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1715 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1716 | addr = (struct sockaddr_ctl *)addr_ret; |
| 1717 | addr->sc_family = AF_SYSTEM; |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1718 | addr->ss_sysaddr = AF_SYS_CONTROL; |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1719 | |
| 1720 | if (PyUnicode_Check(args)) { |
| 1721 | struct ctl_info info; |
| 1722 | PyObject *ctl_name; |
| 1723 | |
| 1724 | if (!PyArg_Parse(args, "O&", |
| 1725 | PyUnicode_FSConverter, &ctl_name)) { |
| 1726 | return 0; |
| 1727 | } |
| 1728 | |
| 1729 | if (PyBytes_GET_SIZE(ctl_name) > sizeof(info.ctl_name)) { |
| 1730 | PyErr_SetString(PyExc_ValueError, |
| 1731 | "provided string is too long"); |
| 1732 | Py_DECREF(ctl_name); |
| 1733 | return 0; |
| 1734 | } |
| 1735 | strncpy(info.ctl_name, PyBytes_AS_STRING(ctl_name), |
| 1736 | sizeof(info.ctl_name)); |
| 1737 | Py_DECREF(ctl_name); |
| 1738 | |
| 1739 | if (ioctl(s->sock_fd, CTLIOCGINFO, &info)) { |
| 1740 | PyErr_SetString(PyExc_OSError, |
| 1741 | "cannot find kernel control with provided name"); |
| 1742 | return 0; |
| 1743 | } |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1744 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1745 | addr->sc_id = info.ctl_id; |
| 1746 | addr->sc_unit = 0; |
| 1747 | } else if (!PyArg_ParseTuple(args, "II", |
| 1748 | &(addr->sc_id), &(addr->sc_unit))) { |
| 1749 | PyErr_SetString(PyExc_TypeError, "getsockaddrarg: " |
| 1750 | "expected str or tuple of two ints"); |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1751 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1752 | return 0; |
| 1753 | } |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1754 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1755 | *len_ret = sizeof(*addr); |
| 1756 | return 1; |
| 1757 | } |
| 1758 | #endif |
| 1759 | default: |
| 1760 | PyErr_SetString(PyExc_OSError, |
| 1761 | "getsockaddrarg: unsupported PF_SYSTEM protocol"); |
| 1762 | return 0; |
| 1763 | } |
| 1764 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1766 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1767 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1768 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1769 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: bad family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1770 | return 0; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1771 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1772 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1775 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1776 | /* 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] | 1777 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 1778 | through len_ret. */ |
| 1779 | |
| 1780 | static int |
Peter Schneider-Kamp | 618e25d | 2000-07-11 23:00:12 +0000 | [diff] [blame] | 1781 | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1782 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1783 | switch (s->sock_family) { |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1784 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1785 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1786 | case AF_UNIX: |
| 1787 | { |
| 1788 | *len_ret = sizeof (struct sockaddr_un); |
| 1789 | return 1; |
| 1790 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1791 | #endif /* AF_UNIX */ |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1792 | #if defined(AF_NETLINK) |
| 1793 | case AF_NETLINK: |
| 1794 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1795 | *len_ret = sizeof (struct sockaddr_nl); |
| 1796 | return 1; |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1797 | } |
| 1798 | #endif |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1799 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 1800 | #ifdef AF_RDS |
| 1801 | case AF_RDS: |
| 1802 | /* RDS sockets use sockaddr_in: fall-through */ |
| 1803 | #endif |
| 1804 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1805 | case AF_INET: |
| 1806 | { |
| 1807 | *len_ret = sizeof (struct sockaddr_in); |
| 1808 | return 1; |
| 1809 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1810 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1811 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1812 | case AF_INET6: |
| 1813 | { |
| 1814 | *len_ret = sizeof (struct sockaddr_in6); |
| 1815 | return 1; |
| 1816 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1817 | #endif |
| 1818 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1819 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1820 | case AF_BLUETOOTH: |
| 1821 | { |
| 1822 | switch(s->sock_proto) |
| 1823 | { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1824 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1825 | case BTPROTO_L2CAP: |
| 1826 | *len_ret = sizeof (struct sockaddr_l2); |
| 1827 | return 1; |
| 1828 | case BTPROTO_RFCOMM: |
| 1829 | *len_ret = sizeof (struct sockaddr_rc); |
| 1830 | return 1; |
| 1831 | case BTPROTO_HCI: |
| 1832 | *len_ret = sizeof (struct sockaddr_hci); |
| 1833 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1834 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1835 | case BTPROTO_SCO: |
| 1836 | *len_ret = sizeof (struct sockaddr_sco); |
| 1837 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1838 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1839 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1840 | PyErr_SetString(PyExc_OSError, "getsockaddrlen: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1841 | "unknown BT protocol"); |
| 1842 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1843 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1844 | } |
| 1845 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1846 | #endif |
| 1847 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1848 | #ifdef HAVE_NETPACKET_PACKET_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1849 | case AF_PACKET: |
| 1850 | { |
| 1851 | *len_ret = sizeof (struct sockaddr_ll); |
| 1852 | return 1; |
| 1853 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1854 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1855 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1856 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1857 | case AF_TIPC: |
| 1858 | { |
| 1859 | *len_ret = sizeof (struct sockaddr_tipc); |
| 1860 | return 1; |
| 1861 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1862 | #endif |
| 1863 | |
Vinay Sajip | ecfc98c | 2014-03-20 12:42:42 +0000 | [diff] [blame] | 1864 | #ifdef AF_CAN |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1865 | case AF_CAN: |
| 1866 | { |
| 1867 | *len_ret = sizeof (struct sockaddr_can); |
| 1868 | return 1; |
| 1869 | } |
| 1870 | #endif |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1871 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1872 | #ifdef PF_SYSTEM |
| 1873 | case PF_SYSTEM: |
| 1874 | switch(s->sock_proto) { |
| 1875 | #ifdef SYSPROTO_CONTROL |
| 1876 | case SYSPROTO_CONTROL: |
| 1877 | *len_ret = sizeof (struct sockaddr_ctl); |
| 1878 | return 1; |
| 1879 | #endif |
| 1880 | default: |
| 1881 | PyErr_SetString(PyExc_OSError, "getsockaddrlen: " |
| 1882 | "unknown PF_SYSTEM protocol"); |
| 1883 | return 0; |
| 1884 | } |
| 1885 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1886 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1887 | /* More cases here... */ |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1889 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1890 | PyErr_SetString(PyExc_OSError, "getsockaddrlen: bad family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1891 | return 0; |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1892 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1893 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
| 1896 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 1897 | /* Support functions for the sendmsg() and recvmsg[_into]() methods. |
| 1898 | Currently, these methods are only compiled if the RFC 2292/3542 |
| 1899 | CMSG_LEN() macro is available. Older systems seem to have used |
| 1900 | sizeof(struct cmsghdr) + (length) where CMSG_LEN() is used now, so |
| 1901 | it may be possible to define CMSG_LEN() that way if it's not |
| 1902 | provided. Some architectures might need extra padding after the |
| 1903 | cmsghdr, however, and CMSG_LEN() would have to take account of |
| 1904 | this. */ |
| 1905 | #ifdef CMSG_LEN |
| 1906 | /* If length is in range, set *result to CMSG_LEN(length) and return |
| 1907 | true; otherwise, return false. */ |
| 1908 | static int |
| 1909 | get_CMSG_LEN(size_t length, size_t *result) |
| 1910 | { |
| 1911 | size_t tmp; |
| 1912 | |
| 1913 | if (length > (SOCKLEN_T_LIMIT - CMSG_LEN(0))) |
| 1914 | return 0; |
| 1915 | tmp = CMSG_LEN(length); |
| 1916 | if (tmp > SOCKLEN_T_LIMIT || tmp < length) |
| 1917 | return 0; |
| 1918 | *result = tmp; |
| 1919 | return 1; |
| 1920 | } |
| 1921 | |
| 1922 | #ifdef CMSG_SPACE |
| 1923 | /* If length is in range, set *result to CMSG_SPACE(length) and return |
| 1924 | true; otherwise, return false. */ |
| 1925 | static int |
| 1926 | get_CMSG_SPACE(size_t length, size_t *result) |
| 1927 | { |
| 1928 | size_t tmp; |
| 1929 | |
| 1930 | /* Use CMSG_SPACE(1) here in order to take account of the padding |
| 1931 | necessary before *and* after the data. */ |
| 1932 | if (length > (SOCKLEN_T_LIMIT - CMSG_SPACE(1))) |
| 1933 | return 0; |
| 1934 | tmp = CMSG_SPACE(length); |
| 1935 | if (tmp > SOCKLEN_T_LIMIT || tmp < length) |
| 1936 | return 0; |
| 1937 | *result = tmp; |
| 1938 | return 1; |
| 1939 | } |
| 1940 | #endif |
| 1941 | |
| 1942 | /* Return true iff msg->msg_controllen is valid, cmsgh is a valid |
| 1943 | pointer in msg->msg_control with at least "space" bytes after it, |
| 1944 | and its cmsg_len member inside the buffer. */ |
| 1945 | static int |
| 1946 | cmsg_min_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t space) |
| 1947 | { |
| 1948 | size_t cmsg_offset; |
| 1949 | static const size_t cmsg_len_end = (offsetof(struct cmsghdr, cmsg_len) + |
| 1950 | sizeof(cmsgh->cmsg_len)); |
| 1951 | |
Charles-François Natali | 466517d | 2011-08-28 18:23:43 +0200 | [diff] [blame] | 1952 | /* Note that POSIX allows msg_controllen to be of signed type. */ |
Brett Cannon | b05cbe6 | 2014-01-07 17:01:01 -0500 | [diff] [blame] | 1953 | if (cmsgh == NULL || msg->msg_control == NULL) |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 1954 | return 0; |
Brett Cannon | b05cbe6 | 2014-01-07 17:01:01 -0500 | [diff] [blame] | 1955 | /* Note that POSIX allows msg_controllen to be of a signed type. This is |
| 1956 | annoying under OS X as it's unsigned there and so it triggers a |
| 1957 | tautological comparison warning under Clang when compared against 0. |
| 1958 | Since the check is valid on other platforms, silence the warning under |
| 1959 | Clang. */ |
| 1960 | #ifdef __clang__ |
| 1961 | #pragma clang diagnostic push |
| 1962 | #pragma clang diagnostic ignored "-Wtautological-compare" |
| 1963 | #endif |
| 1964 | if (msg->msg_controllen < 0) |
| 1965 | return 0; |
| 1966 | #ifdef __clang__ |
| 1967 | #pragma clang diagnostic pop |
| 1968 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 1969 | if (space < cmsg_len_end) |
| 1970 | space = cmsg_len_end; |
| 1971 | cmsg_offset = (char *)cmsgh - (char *)msg->msg_control; |
| 1972 | return (cmsg_offset <= (size_t)-1 - space && |
| 1973 | cmsg_offset + space <= msg->msg_controllen); |
| 1974 | } |
| 1975 | |
| 1976 | /* If pointer CMSG_DATA(cmsgh) is in buffer msg->msg_control, set |
| 1977 | *space to number of bytes following it in the buffer and return |
| 1978 | true; otherwise, return false. Assumes cmsgh, msg->msg_control and |
| 1979 | msg->msg_controllen are valid. */ |
| 1980 | static int |
| 1981 | get_cmsg_data_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *space) |
| 1982 | { |
| 1983 | size_t data_offset; |
| 1984 | char *data_ptr; |
| 1985 | |
| 1986 | if ((data_ptr = (char *)CMSG_DATA(cmsgh)) == NULL) |
| 1987 | return 0; |
| 1988 | data_offset = data_ptr - (char *)msg->msg_control; |
| 1989 | if (data_offset > msg->msg_controllen) |
| 1990 | return 0; |
| 1991 | *space = msg->msg_controllen - data_offset; |
| 1992 | return 1; |
| 1993 | } |
| 1994 | |
| 1995 | /* If cmsgh is invalid or not contained in the buffer pointed to by |
| 1996 | msg->msg_control, return -1. If cmsgh is valid and its associated |
| 1997 | data is entirely contained in the buffer, set *data_len to the |
| 1998 | length of the associated data and return 0. If only part of the |
| 1999 | associated data is contained in the buffer but cmsgh is otherwise |
| 2000 | valid, set *data_len to the length contained in the buffer and |
| 2001 | return 1. */ |
| 2002 | static int |
| 2003 | get_cmsg_data_len(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *data_len) |
| 2004 | { |
| 2005 | size_t space, cmsg_data_len; |
| 2006 | |
| 2007 | if (!cmsg_min_space(msg, cmsgh, CMSG_LEN(0)) || |
| 2008 | cmsgh->cmsg_len < CMSG_LEN(0)) |
| 2009 | return -1; |
| 2010 | cmsg_data_len = cmsgh->cmsg_len - CMSG_LEN(0); |
| 2011 | if (!get_cmsg_data_space(msg, cmsgh, &space)) |
| 2012 | return -1; |
| 2013 | if (space >= cmsg_data_len) { |
| 2014 | *data_len = cmsg_data_len; |
| 2015 | return 0; |
| 2016 | } |
| 2017 | *data_len = space; |
| 2018 | return 1; |
| 2019 | } |
| 2020 | #endif /* CMSG_LEN */ |
| 2021 | |
| 2022 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2023 | /* s._accept() -> (fd, address) */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2024 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2025 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2026 | sock_accept(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2027 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2028 | sock_addr_t addrbuf; |
| 2029 | SOCKET_T newfd = INVALID_SOCKET; |
| 2030 | socklen_t addrlen; |
| 2031 | PyObject *sock = NULL; |
| 2032 | PyObject *addr = NULL; |
| 2033 | PyObject *res = NULL; |
| 2034 | int timeout; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2035 | #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) |
| 2036 | /* accept4() is available on Linux 2.6.28+ and glibc 2.10 */ |
| 2037 | static int accept4_works = -1; |
| 2038 | #endif |
| 2039 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2040 | if (!getsockaddrlen(s, &addrlen)) |
| 2041 | return NULL; |
| 2042 | memset(&addrbuf, 0, addrlen); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2043 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2044 | if (!IS_SELECTABLE(s)) |
| 2045 | return select_error(); |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2046 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2047 | BEGIN_SELECT_LOOP(s) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2048 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2049 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2050 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 2051 | if (!timeout) { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2052 | #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) |
| 2053 | if (accept4_works != 0) { |
| 2054 | newfd = accept4(s->sock_fd, SAS2SA(&addrbuf), &addrlen, |
| 2055 | SOCK_CLOEXEC); |
| 2056 | if (newfd == INVALID_SOCKET && accept4_works == -1) { |
| 2057 | /* On Linux older than 2.6.28, accept4() fails with ENOSYS */ |
| 2058 | accept4_works = (errno != ENOSYS); |
| 2059 | } |
| 2060 | } |
| 2061 | if (accept4_works == 0) |
| 2062 | newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2063 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2064 | newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2065 | #endif |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 2066 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2067 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2068 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2069 | if (timeout == 1) { |
| 2070 | PyErr_SetString(socket_timeout, "timed out"); |
| 2071 | return NULL; |
| 2072 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2073 | END_SELECT_LOOP(s) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2074 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2075 | if (newfd == INVALID_SOCKET) |
| 2076 | return s->errorhandler(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2077 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2078 | #ifdef MS_WINDOWS |
| 2079 | if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) { |
| 2080 | PyErr_SetFromWindowsErr(0); |
| 2081 | SOCKETCLOSE(newfd); |
| 2082 | goto finally; |
| 2083 | } |
| 2084 | #else |
| 2085 | |
| 2086 | #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) |
| 2087 | if (!accept4_works) |
| 2088 | #endif |
| 2089 | { |
| 2090 | if (_Py_set_inheritable(newfd, 0, NULL) < 0) { |
| 2091 | SOCKETCLOSE(newfd); |
| 2092 | goto finally; |
| 2093 | } |
| 2094 | } |
| 2095 | #endif |
| 2096 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2097 | sock = PyLong_FromSocket_t(newfd); |
| 2098 | if (sock == NULL) { |
| 2099 | SOCKETCLOSE(newfd); |
| 2100 | goto finally; |
| 2101 | } |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2103 | addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 2104 | addrlen, s->sock_proto); |
| 2105 | if (addr == NULL) |
| 2106 | goto finally; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2107 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2108 | res = PyTuple_Pack(2, sock, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2109 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2110 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2111 | Py_XDECREF(sock); |
| 2112 | Py_XDECREF(addr); |
| 2113 | return res; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2116 | PyDoc_STRVAR(accept_doc, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2117 | "_accept() -> (integer, address info)\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2118 | \n\ |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2119 | Wait for an incoming connection. Return a new socket file descriptor\n\ |
| 2120 | representing the connection, and the address of the client.\n\ |
| 2121 | For IP sockets, the address info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2122 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2123 | /* s.setblocking(flag) method. Argument: |
| 2124 | False -- non-blocking mode; same as settimeout(0) |
| 2125 | True -- blocking mode; same as settimeout(None) |
| 2126 | */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2127 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2128 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2129 | sock_setblocking(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2130 | { |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 2131 | long block; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2132 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2133 | block = PyLong_AsLong(arg); |
| 2134 | if (block == -1 && PyErr_Occurred()) |
| 2135 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2137 | s->sock_timeout = block ? -1.0 : 0.0; |
| 2138 | internal_setblocking(s, block); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2139 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2140 | Py_INCREF(Py_None); |
| 2141 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2142 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2143 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2144 | PyDoc_STRVAR(setblocking_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2145 | "setblocking(flag)\n\ |
| 2146 | \n\ |
| 2147 | 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] | 2148 | setblocking(True) is equivalent to settimeout(None);\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2149 | setblocking(False) is equivalent to settimeout(0.0)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2150 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2151 | /* s.settimeout(timeout) method. Argument: |
| 2152 | None -- no timeout, blocking mode; same as setblocking(True) |
| 2153 | 0.0 -- non-blocking mode; same as setblocking(False) |
| 2154 | > 0 -- timeout mode; operations time out after timeout seconds |
| 2155 | < 0 -- illegal; raises an exception |
| 2156 | */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2157 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2158 | sock_settimeout(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2159 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2160 | double timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2162 | if (arg == Py_None) |
| 2163 | timeout = -1.0; |
| 2164 | else { |
| 2165 | timeout = PyFloat_AsDouble(arg); |
| 2166 | if (timeout < 0.0) { |
| 2167 | if (!PyErr_Occurred()) |
| 2168 | PyErr_SetString(PyExc_ValueError, |
| 2169 | "Timeout value out of range"); |
| 2170 | return NULL; |
| 2171 | } |
| 2172 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2174 | s->sock_timeout = timeout; |
| 2175 | internal_setblocking(s, timeout < 0.0); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2177 | Py_INCREF(Py_None); |
| 2178 | return Py_None; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2181 | PyDoc_STRVAR(settimeout_doc, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2182 | "settimeout(timeout)\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2183 | \n\ |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2184 | Set a timeout on socket operations. 'timeout' can be a float,\n\ |
| 2185 | giving in seconds, or None. Setting a timeout of None disables\n\ |
| 2186 | the timeout feature and is equivalent to setblocking(1).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2187 | Setting a timeout of zero is the same as setblocking(0)."); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2188 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2189 | /* s.gettimeout() method. |
| 2190 | Returns the timeout associated with a socket. */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2191 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2192 | sock_gettimeout(PySocketSockObject *s) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2193 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2194 | if (s->sock_timeout < 0.0) { |
| 2195 | Py_INCREF(Py_None); |
| 2196 | return Py_None; |
| 2197 | } |
| 2198 | else |
| 2199 | return PyFloat_FromDouble(s->sock_timeout); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2202 | PyDoc_STRVAR(gettimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2203 | "gettimeout() -> timeout\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2204 | \n\ |
Ezio Melotti | 388c945 | 2011-08-14 08:28:57 +0300 | [diff] [blame] | 2205 | Returns the timeout in seconds (float) associated with socket \n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2206 | 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] | 2207 | operations are disabled."); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2208 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 2209 | /* s.setsockopt() method. |
| 2210 | With an integer third argument, sets an integer option. |
| 2211 | With a string third argument, sets an option from a buffer; |
| 2212 | use optional built-in module 'struct' to encode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2213 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2214 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2215 | sock_setsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2216 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2217 | int level; |
| 2218 | int optname; |
| 2219 | int res; |
| 2220 | char *buf; |
| 2221 | int buflen; |
| 2222 | int flag; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2223 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2224 | if (PyArg_ParseTuple(args, "iii:setsockopt", |
| 2225 | &level, &optname, &flag)) { |
| 2226 | buf = (char *) &flag; |
| 2227 | buflen = sizeof flag; |
| 2228 | } |
| 2229 | else { |
| 2230 | PyErr_Clear(); |
| 2231 | if (!PyArg_ParseTuple(args, "iiy#:setsockopt", |
| 2232 | &level, &optname, &buf, &buflen)) |
| 2233 | return NULL; |
| 2234 | } |
| 2235 | res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen); |
| 2236 | if (res < 0) |
| 2237 | return s->errorhandler(); |
| 2238 | Py_INCREF(Py_None); |
| 2239 | return Py_None; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2240 | } |
| 2241 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2242 | PyDoc_STRVAR(setsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2243 | "setsockopt(level, option, value)\n\ |
| 2244 | \n\ |
| 2245 | 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] | 2246 | The value argument can either be an integer or a string."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2247 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2248 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 2249 | /* s.getsockopt() method. |
| 2250 | With two arguments, retrieves an integer option. |
| 2251 | With a third integer argument, retrieves a string buffer of that size; |
| 2252 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2253 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2254 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2255 | sock_getsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2256 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2257 | int level; |
| 2258 | int optname; |
| 2259 | int res; |
| 2260 | PyObject *buf; |
| 2261 | socklen_t buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2263 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt", |
| 2264 | &level, &optname, &buflen)) |
| 2265 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2267 | if (buflen == 0) { |
| 2268 | int flag = 0; |
| 2269 | socklen_t flagsize = sizeof flag; |
| 2270 | res = getsockopt(s->sock_fd, level, optname, |
| 2271 | (void *)&flag, &flagsize); |
| 2272 | if (res < 0) |
| 2273 | return s->errorhandler(); |
| 2274 | return PyLong_FromLong(flag); |
| 2275 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2276 | if (buflen <= 0 || buflen > 1024) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2277 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2278 | "getsockopt buflen out of range"); |
| 2279 | return NULL; |
| 2280 | } |
| 2281 | buf = PyBytes_FromStringAndSize((char *)NULL, buflen); |
| 2282 | if (buf == NULL) |
| 2283 | return NULL; |
| 2284 | res = getsockopt(s->sock_fd, level, optname, |
| 2285 | (void *)PyBytes_AS_STRING(buf), &buflen); |
| 2286 | if (res < 0) { |
| 2287 | Py_DECREF(buf); |
| 2288 | return s->errorhandler(); |
| 2289 | } |
| 2290 | _PyBytes_Resize(&buf, buflen); |
| 2291 | return buf; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2294 | PyDoc_STRVAR(getsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2295 | "getsockopt(level, option[, buffersize]) -> value\n\ |
| 2296 | \n\ |
| 2297 | Get a socket option. See the Unix manual for level and option.\n\ |
| 2298 | 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] | 2299 | string of that length; otherwise it is an integer."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2300 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2301 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2302 | /* s.bind(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2303 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2304 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2305 | sock_bind(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2306 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2307 | sock_addr_t addrbuf; |
| 2308 | int addrlen; |
| 2309 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2311 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2312 | return NULL; |
| 2313 | Py_BEGIN_ALLOW_THREADS |
| 2314 | res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen); |
| 2315 | Py_END_ALLOW_THREADS |
| 2316 | if (res < 0) |
| 2317 | return s->errorhandler(); |
| 2318 | Py_INCREF(Py_None); |
| 2319 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2322 | PyDoc_STRVAR(bind_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2323 | "bind(address)\n\ |
| 2324 | \n\ |
| 2325 | 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] | 2326 | 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] | 2327 | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2328 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2329 | |
| 2330 | /* s.close() method. |
| 2331 | Set the file descriptor to -1 so operations tried subsequently |
| 2332 | will surely fail. */ |
| 2333 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2334 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2335 | sock_close(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2336 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2337 | SOCKET_T fd; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2339 | if ((fd = s->sock_fd) != -1) { |
| 2340 | s->sock_fd = -1; |
| 2341 | Py_BEGIN_ALLOW_THREADS |
| 2342 | (void) SOCKETCLOSE(fd); |
| 2343 | Py_END_ALLOW_THREADS |
| 2344 | } |
| 2345 | Py_INCREF(Py_None); |
| 2346 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2349 | PyDoc_STRVAR(close_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2350 | "close()\n\ |
| 2351 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2352 | Close the socket. It cannot be used after this call."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2353 | |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2354 | static PyObject * |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2355 | sock_detach(PySocketSockObject *s) |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2356 | { |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2357 | SOCKET_T fd = s->sock_fd; |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2358 | s->sock_fd = -1; |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2359 | return PyLong_FromSocket_t(fd); |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2360 | } |
| 2361 | |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2362 | PyDoc_STRVAR(detach_doc, |
| 2363 | "detach()\n\ |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2364 | \n\ |
Guido van Rossum | 638aebd | 2013-12-04 11:50:09 -0800 | [diff] [blame] | 2365 | Close the socket object without closing the underlying file descriptor.\n\ |
| 2366 | 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] | 2367 | can be reused for other purposes. The file descriptor is returned."); |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2368 | |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2369 | static int |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2370 | internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2371 | int *timeoutp) |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2372 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2373 | int res, timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2375 | timeout = 0; |
| 2376 | res = connect(s->sock_fd, addr, addrlen); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2377 | |
| 2378 | #ifdef MS_WINDOWS |
| 2379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2380 | if (s->sock_timeout > 0.0) { |
| 2381 | if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK && |
| 2382 | IS_SELECTABLE(s)) { |
| 2383 | /* This is a mess. Best solution: trust select */ |
| 2384 | fd_set fds; |
| 2385 | fd_set fds_exc; |
| 2386 | struct timeval tv; |
| 2387 | tv.tv_sec = (int)s->sock_timeout; |
| 2388 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 2389 | FD_ZERO(&fds); |
| 2390 | FD_SET(s->sock_fd, &fds); |
| 2391 | FD_ZERO(&fds_exc); |
| 2392 | FD_SET(s->sock_fd, &fds_exc); |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2393 | res = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 2394 | NULL, &fds, &fds_exc, &tv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2395 | if (res == 0) { |
| 2396 | res = WSAEWOULDBLOCK; |
| 2397 | timeout = 1; |
| 2398 | } else if (res > 0) { |
| 2399 | if (FD_ISSET(s->sock_fd, &fds)) |
| 2400 | /* The socket is in the writable set - this |
| 2401 | means connected */ |
| 2402 | res = 0; |
| 2403 | else { |
| 2404 | /* As per MS docs, we need to call getsockopt() |
| 2405 | to get the underlying error */ |
| 2406 | int res_size = sizeof res; |
| 2407 | /* It must be in the exception set */ |
| 2408 | assert(FD_ISSET(s->sock_fd, &fds_exc)); |
| 2409 | if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR, |
| 2410 | (char *)&res, &res_size)) |
| 2411 | /* getsockopt also clears WSAGetLastError, |
| 2412 | so reset it back. */ |
| 2413 | WSASetLastError(res); |
| 2414 | else |
| 2415 | res = WSAGetLastError(); |
| 2416 | } |
| 2417 | } |
| 2418 | /* else if (res < 0) an error occurred */ |
| 2419 | } |
| 2420 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2421 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2422 | if (res < 0) |
| 2423 | res = WSAGetLastError(); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2424 | |
| 2425 | #else |
| 2426 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2427 | if (s->sock_timeout > 0.0) { |
| 2428 | if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) { |
| 2429 | timeout = internal_select(s, 1); |
| 2430 | if (timeout == 0) { |
| 2431 | /* Bug #1019808: in case of an EINPROGRESS, |
| 2432 | use getsockopt(SO_ERROR) to get the real |
| 2433 | error. */ |
| 2434 | socklen_t res_size = sizeof res; |
| 2435 | (void)getsockopt(s->sock_fd, SOL_SOCKET, |
| 2436 | SO_ERROR, &res, &res_size); |
| 2437 | if (res == EISCONN) |
| 2438 | res = 0; |
| 2439 | errno = res; |
| 2440 | } |
| 2441 | else if (timeout == -1) { |
| 2442 | res = errno; /* had error */ |
| 2443 | } |
| 2444 | else |
| 2445 | res = EWOULDBLOCK; /* timed out */ |
| 2446 | } |
| 2447 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2448 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2449 | if (res < 0) |
| 2450 | res = errno; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2451 | |
| 2452 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2453 | *timeoutp = timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2454 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2455 | return res; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2456 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2457 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2458 | /* s.connect(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2459 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2460 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2461 | sock_connect(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2462 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2463 | sock_addr_t addrbuf; |
| 2464 | int addrlen; |
| 2465 | int res; |
| 2466 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2467 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2468 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2469 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2470 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2471 | Py_BEGIN_ALLOW_THREADS |
| 2472 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 2473 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2474 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2475 | if (timeout == 1) { |
| 2476 | PyErr_SetString(socket_timeout, "timed out"); |
| 2477 | return NULL; |
| 2478 | } |
| 2479 | if (res != 0) |
| 2480 | return s->errorhandler(); |
| 2481 | Py_INCREF(Py_None); |
| 2482 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2485 | PyDoc_STRVAR(connect_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2486 | "connect(address)\n\ |
| 2487 | \n\ |
| 2488 | 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] | 2489 | is a pair (host, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2490 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2491 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2492 | /* s.connect_ex(sockaddr) method */ |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2493 | |
| 2494 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2495 | sock_connect_ex(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2496 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2497 | sock_addr_t addrbuf; |
| 2498 | int addrlen; |
| 2499 | int res; |
| 2500 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2501 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2502 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2503 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2505 | Py_BEGIN_ALLOW_THREADS |
| 2506 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 2507 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2508 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2509 | /* Signals are not errors (though they may raise exceptions). Adapted |
| 2510 | from PyErr_SetFromErrnoWithFilenameObject(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2511 | #ifdef EINTR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2512 | if (res == EINTR && PyErr_CheckSignals()) |
| 2513 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2514 | #endif |
| 2515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2516 | return PyLong_FromLong((long) res); |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2517 | } |
| 2518 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2519 | PyDoc_STRVAR(connect_ex_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2520 | "connect_ex(address) -> errno\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2521 | \n\ |
| 2522 | 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] | 2523 | instead of raising an exception when an error occurs."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2524 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2525 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2526 | /* s.fileno() method */ |
| 2527 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2528 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2529 | sock_fileno(PySocketSockObject *s) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2530 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2531 | return PyLong_FromSocket_t(s->sock_fd); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2534 | PyDoc_STRVAR(fileno_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2535 | "fileno() -> integer\n\ |
| 2536 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2537 | Return the integer file descriptor of the socket."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2538 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2539 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2540 | /* s.getsockname() method */ |
| 2541 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2542 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2543 | sock_getsockname(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2544 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2545 | sock_addr_t addrbuf; |
| 2546 | int res; |
| 2547 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2549 | if (!getsockaddrlen(s, &addrlen)) |
| 2550 | return NULL; |
| 2551 | memset(&addrbuf, 0, addrlen); |
| 2552 | Py_BEGIN_ALLOW_THREADS |
| 2553 | res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2554 | Py_END_ALLOW_THREADS |
| 2555 | if (res < 0) |
| 2556 | return s->errorhandler(); |
| 2557 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2558 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2559 | } |
| 2560 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2561 | PyDoc_STRVAR(getsockname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2562 | "getsockname() -> address info\n\ |
| 2563 | \n\ |
| 2564 | 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] | 2565 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2566 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2568 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2569 | /* s.getpeername() method */ |
| 2570 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2571 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2572 | sock_getpeername(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2573 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2574 | sock_addr_t addrbuf; |
| 2575 | int res; |
| 2576 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2578 | if (!getsockaddrlen(s, &addrlen)) |
| 2579 | return NULL; |
| 2580 | memset(&addrbuf, 0, addrlen); |
| 2581 | Py_BEGIN_ALLOW_THREADS |
| 2582 | res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2583 | Py_END_ALLOW_THREADS |
| 2584 | if (res < 0) |
| 2585 | return s->errorhandler(); |
| 2586 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2587 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2588 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2589 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2590 | PyDoc_STRVAR(getpeername_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2591 | "getpeername() -> address info\n\ |
| 2592 | \n\ |
| 2593 | 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] | 2594 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2595 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2596 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2597 | |
| 2598 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2599 | /* s.listen(n) method */ |
| 2600 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2601 | static PyObject * |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2602 | sock_listen(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2603 | { |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2604 | /* We try to choose a default backlog high enough to avoid connection drops |
| 2605 | * for common workloads, yet not too high to limit resource usage. */ |
| 2606 | int backlog = Py_MIN(SOMAXCONN, 128); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2607 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2608 | |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2609 | if (!PyArg_ParseTuple(args, "|i:listen", &backlog)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2610 | return NULL; |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2611 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2612 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 1be815a | 2011-05-10 19:16:29 +0200 | [diff] [blame] | 2613 | /* To avoid problems on systems that don't allow a negative backlog |
| 2614 | * (which doesn't make sense anyway) we force a minimum value of 0. */ |
| 2615 | if (backlog < 0) |
| 2616 | backlog = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2617 | res = listen(s->sock_fd, backlog); |
| 2618 | Py_END_ALLOW_THREADS |
| 2619 | if (res < 0) |
| 2620 | return s->errorhandler(); |
| 2621 | Py_INCREF(Py_None); |
| 2622 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2623 | } |
| 2624 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2625 | PyDoc_STRVAR(listen_doc, |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2626 | "listen([backlog])\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2627 | \n\ |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2628 | Enable a server to accept connections. If backlog is specified, it must be\n\ |
| 2629 | 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] | 2630 | 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] | 2631 | connections. If not specified, a default reasonable value is chosen."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2632 | |
| 2633 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2634 | /* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2635 | * 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] | 2636 | * 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] | 2637 | * 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] | 2638 | * 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] | 2639 | * also possible that we return a number of bytes smaller than the request |
| 2640 | * bytes. |
| 2641 | */ |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2642 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2643 | static Py_ssize_t |
| 2644 | 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] | 2645 | { |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2646 | Py_ssize_t outlen = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2647 | int timeout; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2649 | if (!IS_SELECTABLE(s)) { |
| 2650 | select_error(); |
| 2651 | return -1; |
| 2652 | } |
| 2653 | if (len == 0) { |
| 2654 | /* If 0 bytes were requested, do nothing. */ |
| 2655 | return 0; |
| 2656 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2657 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2658 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2659 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2660 | timeout = internal_select_ex(s, 0, interval); |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 2661 | if (!timeout) { |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 2662 | #ifdef MS_WINDOWS |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 2663 | if (len > INT_MAX) |
| 2664 | len = INT_MAX; |
| 2665 | outlen = recv(s->sock_fd, cbuf, (int)len, flags); |
| 2666 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2667 | outlen = recv(s->sock_fd, cbuf, len, flags); |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 2668 | #endif |
| 2669 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2670 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2671 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2672 | if (timeout == 1) { |
| 2673 | PyErr_SetString(socket_timeout, "timed out"); |
| 2674 | return -1; |
| 2675 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2676 | END_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2677 | if (outlen < 0) { |
| 2678 | /* Note: the call to errorhandler() ALWAYS indirectly returned |
| 2679 | NULL, so ignore its return value */ |
| 2680 | s->errorhandler(); |
| 2681 | return -1; |
| 2682 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2683 | return outlen; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2686 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2687 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2688 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2689 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2690 | sock_recv(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2691 | { |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2692 | Py_ssize_t recvlen, outlen; |
| 2693 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2694 | PyObject *buf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2695 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2696 | if (!PyArg_ParseTuple(args, "n|i:recv", &recvlen, &flags)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2697 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2698 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2699 | if (recvlen < 0) { |
| 2700 | PyErr_SetString(PyExc_ValueError, |
| 2701 | "negative buffersize in recv"); |
| 2702 | return NULL; |
| 2703 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2704 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2705 | /* Allocate a new string. */ |
| 2706 | buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
| 2707 | if (buf == NULL) |
| 2708 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2709 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2710 | /* Call the guts */ |
| 2711 | outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags); |
| 2712 | if (outlen < 0) { |
| 2713 | /* An error occurred, release the string and return an |
| 2714 | error. */ |
| 2715 | Py_DECREF(buf); |
| 2716 | return NULL; |
| 2717 | } |
| 2718 | if (outlen != recvlen) { |
| 2719 | /* We did not read as many bytes as we anticipated, resize the |
| 2720 | string if possible and be successful. */ |
| 2721 | _PyBytes_Resize(&buf, outlen); |
| 2722 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2723 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2724 | return buf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2725 | } |
| 2726 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2727 | PyDoc_STRVAR(recv_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2728 | "recv(buffersize[, flags]) -> data\n\ |
| 2729 | \n\ |
| 2730 | Receive up to buffersize bytes from the socket. For the optional flags\n\ |
| 2731 | argument, see the Unix manual. When no data is available, block until\n\ |
| 2732 | 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] | 2733 | 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] | 2734 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2735 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2736 | /* s.recv_into(buffer, [nbytes [,flags]]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2737 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2738 | static PyObject* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2739 | sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2740 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2741 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2742 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2743 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2744 | Py_buffer pbuf; |
| 2745 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2746 | Py_ssize_t buflen, readlen, recvlen = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2747 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2748 | /* Get the buffer's memory */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2749 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recv_into", kwlist, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2750 | &pbuf, &recvlen, &flags)) |
| 2751 | return NULL; |
| 2752 | buf = pbuf.buf; |
| 2753 | buflen = pbuf.len; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2754 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2755 | if (recvlen < 0) { |
| 2756 | PyBuffer_Release(&pbuf); |
| 2757 | PyErr_SetString(PyExc_ValueError, |
| 2758 | "negative buffersize in recv_into"); |
| 2759 | return NULL; |
| 2760 | } |
| 2761 | if (recvlen == 0) { |
| 2762 | /* If nbytes was not specified, use the buffer's length */ |
| 2763 | recvlen = buflen; |
| 2764 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2766 | /* Check if the buffer is large enough */ |
| 2767 | if (buflen < recvlen) { |
| 2768 | PyBuffer_Release(&pbuf); |
| 2769 | PyErr_SetString(PyExc_ValueError, |
| 2770 | "buffer too small for requested bytes"); |
| 2771 | return NULL; |
| 2772 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2773 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2774 | /* Call the guts */ |
| 2775 | readlen = sock_recv_guts(s, buf, recvlen, flags); |
| 2776 | if (readlen < 0) { |
| 2777 | /* Return an error. */ |
| 2778 | PyBuffer_Release(&pbuf); |
| 2779 | return NULL; |
| 2780 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2781 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2782 | PyBuffer_Release(&pbuf); |
| 2783 | /* Return the number of bytes read. Note that we do not do anything |
| 2784 | special here in the case that readlen < recvlen. */ |
| 2785 | return PyLong_FromSsize_t(readlen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2788 | PyDoc_STRVAR(recv_into_doc, |
| 2789 | "recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2790 | \n\ |
| 2791 | A version of recv() that stores its data into a buffer rather than creating \n\ |
| 2792 | a new string. Receive up to buffersize bytes from the socket. If buffersize \n\ |
| 2793 | is not specified (or 0), receive up to the size available in the given buffer.\n\ |
| 2794 | \n\ |
| 2795 | See recv() for documentation about the flags."); |
| 2796 | |
| 2797 | |
| 2798 | /* |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 2799 | * This is the guts of the recvfrom() and recvfrom_into() methods, which reads |
| 2800 | * into a char buffer. If you have any inc/def ref to do to the objects that |
| 2801 | * 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] | 2802 | * 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] | 2803 | * that it is also possible that we return a number of bytes smaller than the |
| 2804 | * request bytes. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2805 | * |
| 2806 | * 'addr' is a return value for the address object. Note that you must decref |
| 2807 | * it yourself. |
| 2808 | */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2809 | static Py_ssize_t |
| 2810 | 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] | 2811 | PyObject** addr) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2812 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2813 | sock_addr_t addrbuf; |
| 2814 | int timeout; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2815 | Py_ssize_t n = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2816 | socklen_t addrlen; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2817 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2818 | *addr = NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2819 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2820 | if (!getsockaddrlen(s, &addrlen)) |
| 2821 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2822 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2823 | if (!IS_SELECTABLE(s)) { |
| 2824 | select_error(); |
| 2825 | return -1; |
| 2826 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2827 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2828 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2829 | Py_BEGIN_ALLOW_THREADS |
| 2830 | memset(&addrbuf, 0, addrlen); |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2831 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2832 | if (!timeout) { |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 2833 | #ifdef MS_WINDOWS |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 2834 | if (len > INT_MAX) |
| 2835 | len = INT_MAX; |
| 2836 | n = recvfrom(s->sock_fd, cbuf, (int)len, flags, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2837 | (void *) &addrbuf, &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2838 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2839 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2840 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2841 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2842 | } |
| 2843 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2844 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2845 | if (timeout == 1) { |
| 2846 | PyErr_SetString(socket_timeout, "timed out"); |
| 2847 | return -1; |
| 2848 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2849 | END_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2850 | if (n < 0) { |
| 2851 | s->errorhandler(); |
| 2852 | return -1; |
| 2853 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2854 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2855 | if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 2856 | addrlen, s->sock_proto))) |
| 2857 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2858 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2859 | return n; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
| 2862 | /* s.recvfrom(nbytes [,flags]) method */ |
| 2863 | |
| 2864 | static PyObject * |
| 2865 | sock_recvfrom(PySocketSockObject *s, PyObject *args) |
| 2866 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2867 | PyObject *buf = NULL; |
| 2868 | PyObject *addr = NULL; |
| 2869 | PyObject *ret = NULL; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2870 | int flags = 0; |
| 2871 | Py_ssize_t recvlen, outlen; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2872 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2873 | if (!PyArg_ParseTuple(args, "n|i:recvfrom", &recvlen, &flags)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2874 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2876 | if (recvlen < 0) { |
| 2877 | PyErr_SetString(PyExc_ValueError, |
| 2878 | "negative buffersize in recvfrom"); |
| 2879 | return NULL; |
| 2880 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2882 | buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
| 2883 | if (buf == NULL) |
| 2884 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2885 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2886 | outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf), |
| 2887 | recvlen, flags, &addr); |
| 2888 | if (outlen < 0) { |
| 2889 | goto finally; |
| 2890 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2891 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2892 | if (outlen != recvlen) { |
| 2893 | /* We did not read as many bytes as we anticipated, resize the |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 2894 | string if possible and be successful. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2895 | if (_PyBytes_Resize(&buf, outlen) < 0) |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 2896 | /* Oopsy, not so successful after all. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2897 | goto finally; |
| 2898 | } |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2899 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2900 | ret = PyTuple_Pack(2, buf, addr); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2901 | |
| 2902 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2903 | Py_XDECREF(buf); |
| 2904 | Py_XDECREF(addr); |
| 2905 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2906 | } |
| 2907 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2908 | PyDoc_STRVAR(recvfrom_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2909 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
| 2910 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2911 | 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] | 2912 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2913 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2914 | /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2915 | |
| 2916 | static PyObject * |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2917 | sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2918 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2919 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2920 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2921 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2922 | Py_buffer pbuf; |
| 2923 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2924 | Py_ssize_t readlen, buflen, recvlen = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2925 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2926 | PyObject *addr = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2927 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2928 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recvfrom_into", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2929 | kwlist, &pbuf, |
| 2930 | &recvlen, &flags)) |
| 2931 | return NULL; |
| 2932 | buf = pbuf.buf; |
| 2933 | buflen = pbuf.len; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2934 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2935 | if (recvlen < 0) { |
| 2936 | PyBuffer_Release(&pbuf); |
| 2937 | PyErr_SetString(PyExc_ValueError, |
| 2938 | "negative buffersize in recvfrom_into"); |
| 2939 | return NULL; |
| 2940 | } |
| 2941 | if (recvlen == 0) { |
| 2942 | /* If nbytes was not specified, use the buffer's length */ |
| 2943 | recvlen = buflen; |
Benjamin Peterson | c6b37e2 | 2014-01-13 23:14:42 -0500 | [diff] [blame] | 2944 | } else if (recvlen > buflen) { |
| 2945 | PyBuffer_Release(&pbuf); |
| 2946 | PyErr_SetString(PyExc_ValueError, |
| 2947 | "nbytes is greater than the length of the buffer"); |
| 2948 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2949 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2950 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2951 | readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr); |
| 2952 | if (readlen < 0) { |
| 2953 | PyBuffer_Release(&pbuf); |
| 2954 | /* Return an error */ |
| 2955 | Py_XDECREF(addr); |
| 2956 | return NULL; |
| 2957 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2958 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2959 | PyBuffer_Release(&pbuf); |
| 2960 | /* Return the number of bytes read and the address. Note that we do |
| 2961 | not do anything special here in the case that readlen < recvlen. */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2962 | return Py_BuildValue("nN", readlen, addr); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2965 | PyDoc_STRVAR(recvfrom_into_doc, |
| 2966 | "recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2967 | \n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2968 | 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] | 2969 | |
| 2970 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2971 | /* The sendmsg() and recvmsg[_into]() methods require a working |
| 2972 | CMSG_LEN(). See the comment near get_CMSG_LEN(). */ |
| 2973 | #ifdef CMSG_LEN |
| 2974 | /* |
| 2975 | * Call recvmsg() with the supplied iovec structures, flags, and |
| 2976 | * ancillary data buffer size (controllen). Returns the tuple return |
| 2977 | * value for recvmsg() or recvmsg_into(), with the first item provided |
| 2978 | * by the supplied makeval() function. makeval() will be called with |
| 2979 | * the length read and makeval_data as arguments, and must return a |
| 2980 | * new reference (which will be decrefed if there is a subsequent |
| 2981 | * error). On error, closes any file descriptors received via |
| 2982 | * SCM_RIGHTS. |
| 2983 | */ |
| 2984 | static PyObject * |
| 2985 | sock_recvmsg_guts(PySocketSockObject *s, struct iovec *iov, int iovlen, |
| 2986 | int flags, Py_ssize_t controllen, |
| 2987 | PyObject *(*makeval)(ssize_t, void *), void *makeval_data) |
| 2988 | { |
| 2989 | ssize_t bytes_received = -1; |
| 2990 | int timeout; |
| 2991 | sock_addr_t addrbuf; |
| 2992 | socklen_t addrbuflen; |
Charles-François Natali | e9e95ae | 2011-08-24 21:40:53 +0200 | [diff] [blame] | 2993 | struct msghdr msg = {0}; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2994 | PyObject *cmsg_list = NULL, *retval = NULL; |
| 2995 | void *controlbuf = NULL; |
| 2996 | struct cmsghdr *cmsgh; |
| 2997 | size_t cmsgdatalen = 0; |
| 2998 | int cmsg_status; |
| 2999 | |
| 3000 | /* XXX: POSIX says that msg_name and msg_namelen "shall be |
| 3001 | ignored" when the socket is connected (Linux fills them in |
| 3002 | anyway for AF_UNIX sockets at least). Normally msg_namelen |
| 3003 | seems to be set to 0 if there's no address, but try to |
| 3004 | initialize msg_name to something that won't be mistaken for a |
| 3005 | real address if that doesn't happen. */ |
| 3006 | if (!getsockaddrlen(s, &addrbuflen)) |
| 3007 | return NULL; |
| 3008 | memset(&addrbuf, 0, addrbuflen); |
| 3009 | SAS2SA(&addrbuf)->sa_family = AF_UNSPEC; |
| 3010 | |
| 3011 | if (controllen < 0 || controllen > SOCKLEN_T_LIMIT) { |
| 3012 | PyErr_SetString(PyExc_ValueError, |
| 3013 | "invalid ancillary data buffer length"); |
| 3014 | return NULL; |
| 3015 | } |
| 3016 | if (controllen > 0 && (controlbuf = PyMem_Malloc(controllen)) == NULL) |
| 3017 | return PyErr_NoMemory(); |
| 3018 | |
| 3019 | /* Make the system call. */ |
| 3020 | if (!IS_SELECTABLE(s)) { |
| 3021 | select_error(); |
| 3022 | goto finally; |
| 3023 | } |
| 3024 | |
| 3025 | BEGIN_SELECT_LOOP(s) |
| 3026 | Py_BEGIN_ALLOW_THREADS; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3027 | msg.msg_name = SAS2SA(&addrbuf); |
| 3028 | msg.msg_namelen = addrbuflen; |
| 3029 | msg.msg_iov = iov; |
| 3030 | msg.msg_iovlen = iovlen; |
| 3031 | msg.msg_control = controlbuf; |
| 3032 | msg.msg_controllen = controllen; |
| 3033 | timeout = internal_select_ex(s, 0, interval); |
| 3034 | if (!timeout) |
| 3035 | bytes_received = recvmsg(s->sock_fd, &msg, flags); |
| 3036 | Py_END_ALLOW_THREADS; |
| 3037 | if (timeout == 1) { |
| 3038 | PyErr_SetString(socket_timeout, "timed out"); |
| 3039 | goto finally; |
| 3040 | } |
| 3041 | END_SELECT_LOOP(s) |
| 3042 | |
| 3043 | if (bytes_received < 0) { |
| 3044 | s->errorhandler(); |
| 3045 | goto finally; |
| 3046 | } |
| 3047 | |
| 3048 | /* Make list of (level, type, data) tuples from control messages. */ |
| 3049 | if ((cmsg_list = PyList_New(0)) == NULL) |
| 3050 | goto err_closefds; |
| 3051 | /* Check for empty ancillary data as old CMSG_FIRSTHDR() |
| 3052 | implementations didn't do so. */ |
| 3053 | for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL); |
| 3054 | cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) { |
| 3055 | PyObject *bytes, *tuple; |
| 3056 | int tmp; |
| 3057 | |
| 3058 | cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen); |
| 3059 | if (cmsg_status != 0) { |
| 3060 | if (PyErr_WarnEx(PyExc_RuntimeWarning, |
| 3061 | "received malformed or improperly-truncated " |
| 3062 | "ancillary data", 1) == -1) |
| 3063 | goto err_closefds; |
| 3064 | } |
| 3065 | if (cmsg_status < 0) |
| 3066 | break; |
| 3067 | if (cmsgdatalen > PY_SSIZE_T_MAX) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3068 | PyErr_SetString(PyExc_OSError, "control message too long"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3069 | goto err_closefds; |
| 3070 | } |
| 3071 | |
| 3072 | bytes = PyBytes_FromStringAndSize((char *)CMSG_DATA(cmsgh), |
| 3073 | cmsgdatalen); |
| 3074 | tuple = Py_BuildValue("iiN", (int)cmsgh->cmsg_level, |
| 3075 | (int)cmsgh->cmsg_type, bytes); |
| 3076 | if (tuple == NULL) |
| 3077 | goto err_closefds; |
| 3078 | tmp = PyList_Append(cmsg_list, tuple); |
| 3079 | Py_DECREF(tuple); |
| 3080 | if (tmp != 0) |
| 3081 | goto err_closefds; |
| 3082 | |
| 3083 | if (cmsg_status != 0) |
| 3084 | break; |
| 3085 | } |
| 3086 | |
| 3087 | retval = Py_BuildValue("NOiN", |
| 3088 | (*makeval)(bytes_received, makeval_data), |
| 3089 | cmsg_list, |
| 3090 | (int)msg.msg_flags, |
| 3091 | makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 3092 | ((msg.msg_namelen > addrbuflen) ? |
| 3093 | addrbuflen : msg.msg_namelen), |
| 3094 | s->sock_proto)); |
| 3095 | if (retval == NULL) |
| 3096 | goto err_closefds; |
| 3097 | |
| 3098 | finally: |
| 3099 | Py_XDECREF(cmsg_list); |
| 3100 | PyMem_Free(controlbuf); |
| 3101 | return retval; |
| 3102 | |
| 3103 | err_closefds: |
| 3104 | #ifdef SCM_RIGHTS |
| 3105 | /* Close all descriptors coming from SCM_RIGHTS, so they don't leak. */ |
| 3106 | for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL); |
| 3107 | cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) { |
| 3108 | cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen); |
| 3109 | if (cmsg_status < 0) |
| 3110 | break; |
| 3111 | if (cmsgh->cmsg_level == SOL_SOCKET && |
| 3112 | cmsgh->cmsg_type == SCM_RIGHTS) { |
| 3113 | size_t numfds; |
| 3114 | int *fdp; |
| 3115 | |
| 3116 | numfds = cmsgdatalen / sizeof(int); |
| 3117 | fdp = (int *)CMSG_DATA(cmsgh); |
| 3118 | while (numfds-- > 0) |
| 3119 | close(*fdp++); |
| 3120 | } |
| 3121 | if (cmsg_status != 0) |
| 3122 | break; |
| 3123 | } |
| 3124 | #endif /* SCM_RIGHTS */ |
| 3125 | goto finally; |
| 3126 | } |
| 3127 | |
| 3128 | |
| 3129 | static PyObject * |
| 3130 | makeval_recvmsg(ssize_t received, void *data) |
| 3131 | { |
| 3132 | PyObject **buf = data; |
| 3133 | |
| 3134 | if (received < PyBytes_GET_SIZE(*buf)) |
| 3135 | _PyBytes_Resize(buf, received); |
| 3136 | Py_XINCREF(*buf); |
| 3137 | return *buf; |
| 3138 | } |
| 3139 | |
| 3140 | /* s.recvmsg(bufsize[, ancbufsize[, flags]]) method */ |
| 3141 | |
| 3142 | static PyObject * |
| 3143 | sock_recvmsg(PySocketSockObject *s, PyObject *args) |
| 3144 | { |
| 3145 | Py_ssize_t bufsize, ancbufsize = 0; |
| 3146 | int flags = 0; |
| 3147 | struct iovec iov; |
| 3148 | PyObject *buf = NULL, *retval = NULL; |
| 3149 | |
| 3150 | if (!PyArg_ParseTuple(args, "n|ni:recvmsg", &bufsize, &ancbufsize, &flags)) |
| 3151 | return NULL; |
| 3152 | |
| 3153 | if (bufsize < 0) { |
| 3154 | PyErr_SetString(PyExc_ValueError, "negative buffer size in recvmsg()"); |
| 3155 | return NULL; |
| 3156 | } |
| 3157 | if ((buf = PyBytes_FromStringAndSize(NULL, bufsize)) == NULL) |
| 3158 | return NULL; |
| 3159 | iov.iov_base = PyBytes_AS_STRING(buf); |
| 3160 | iov.iov_len = bufsize; |
| 3161 | |
| 3162 | /* Note that we're passing a pointer to *our pointer* to the bytes |
| 3163 | object here (&buf); makeval_recvmsg() may incref the object, or |
| 3164 | deallocate it and set our pointer to NULL. */ |
| 3165 | retval = sock_recvmsg_guts(s, &iov, 1, flags, ancbufsize, |
| 3166 | &makeval_recvmsg, &buf); |
| 3167 | Py_XDECREF(buf); |
| 3168 | return retval; |
| 3169 | } |
| 3170 | |
| 3171 | PyDoc_STRVAR(recvmsg_doc, |
| 3172 | "recvmsg(bufsize[, ancbufsize[, flags]]) -> (data, ancdata, msg_flags, address)\n\ |
| 3173 | \n\ |
| 3174 | Receive normal data (up to bufsize bytes) and ancillary data from the\n\ |
| 3175 | socket. The ancbufsize argument sets the size in bytes of the\n\ |
| 3176 | internal buffer used to receive the ancillary data; it defaults to 0,\n\ |
| 3177 | meaning that no ancillary data will be received. Appropriate buffer\n\ |
| 3178 | sizes for ancillary data can be calculated using CMSG_SPACE() or\n\ |
| 3179 | CMSG_LEN(), and items which do not fit into the buffer might be\n\ |
| 3180 | truncated or discarded. The flags argument defaults to 0 and has the\n\ |
| 3181 | same meaning as for recv().\n\ |
| 3182 | \n\ |
| 3183 | The return value is a 4-tuple: (data, ancdata, msg_flags, address).\n\ |
| 3184 | The data item is a bytes object holding the non-ancillary data\n\ |
| 3185 | received. The ancdata item is a list of zero or more tuples\n\ |
| 3186 | (cmsg_level, cmsg_type, cmsg_data) representing the ancillary data\n\ |
| 3187 | (control messages) received: cmsg_level and cmsg_type are integers\n\ |
| 3188 | specifying the protocol level and protocol-specific type respectively,\n\ |
| 3189 | and cmsg_data is a bytes object holding the associated data. The\n\ |
| 3190 | msg_flags item is the bitwise OR of various flags indicating\n\ |
| 3191 | conditions on the received message; see your system documentation for\n\ |
| 3192 | details. If the receiving socket is unconnected, address is the\n\ |
| 3193 | address of the sending socket, if available; otherwise, its value is\n\ |
| 3194 | unspecified.\n\ |
| 3195 | \n\ |
| 3196 | If recvmsg() raises an exception after the system call returns, it\n\ |
| 3197 | will first attempt to close any file descriptors received via the\n\ |
| 3198 | SCM_RIGHTS mechanism."); |
| 3199 | |
| 3200 | |
| 3201 | static PyObject * |
| 3202 | makeval_recvmsg_into(ssize_t received, void *data) |
| 3203 | { |
| 3204 | return PyLong_FromSsize_t(received); |
| 3205 | } |
| 3206 | |
| 3207 | /* s.recvmsg_into(buffers[, ancbufsize[, flags]]) method */ |
| 3208 | |
| 3209 | static PyObject * |
| 3210 | sock_recvmsg_into(PySocketSockObject *s, PyObject *args) |
| 3211 | { |
| 3212 | Py_ssize_t ancbufsize = 0; |
| 3213 | int flags = 0; |
| 3214 | struct iovec *iovs = NULL; |
| 3215 | Py_ssize_t i, nitems, nbufs = 0; |
| 3216 | Py_buffer *bufs = NULL; |
| 3217 | PyObject *buffers_arg, *fast, *retval = NULL; |
| 3218 | |
| 3219 | if (!PyArg_ParseTuple(args, "O|ni:recvmsg_into", |
| 3220 | &buffers_arg, &ancbufsize, &flags)) |
| 3221 | return NULL; |
| 3222 | |
| 3223 | if ((fast = PySequence_Fast(buffers_arg, |
| 3224 | "recvmsg_into() argument 1 must be an " |
| 3225 | "iterable")) == NULL) |
| 3226 | return NULL; |
| 3227 | nitems = PySequence_Fast_GET_SIZE(fast); |
| 3228 | if (nitems > INT_MAX) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3229 | PyErr_SetString(PyExc_OSError, "recvmsg_into() argument 1 is too long"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3230 | goto finally; |
| 3231 | } |
| 3232 | |
| 3233 | /* Fill in an iovec for each item, and save the Py_buffer |
| 3234 | structs to release afterwards. */ |
| 3235 | if (nitems > 0 && ((iovs = PyMem_New(struct iovec, nitems)) == NULL || |
| 3236 | (bufs = PyMem_New(Py_buffer, nitems)) == NULL)) { |
| 3237 | PyErr_NoMemory(); |
| 3238 | goto finally; |
| 3239 | } |
| 3240 | for (; nbufs < nitems; nbufs++) { |
| 3241 | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(fast, nbufs), |
| 3242 | "w*;recvmsg_into() argument 1 must be an iterable " |
| 3243 | "of single-segment read-write buffers", |
| 3244 | &bufs[nbufs])) |
| 3245 | goto finally; |
| 3246 | iovs[nbufs].iov_base = bufs[nbufs].buf; |
| 3247 | iovs[nbufs].iov_len = bufs[nbufs].len; |
| 3248 | } |
| 3249 | |
| 3250 | retval = sock_recvmsg_guts(s, iovs, nitems, flags, ancbufsize, |
| 3251 | &makeval_recvmsg_into, NULL); |
| 3252 | finally: |
| 3253 | for (i = 0; i < nbufs; i++) |
| 3254 | PyBuffer_Release(&bufs[i]); |
| 3255 | PyMem_Free(bufs); |
| 3256 | PyMem_Free(iovs); |
| 3257 | Py_DECREF(fast); |
| 3258 | return retval; |
| 3259 | } |
| 3260 | |
| 3261 | PyDoc_STRVAR(recvmsg_into_doc, |
| 3262 | "recvmsg_into(buffers[, ancbufsize[, flags]]) -> (nbytes, ancdata, msg_flags, address)\n\ |
| 3263 | \n\ |
| 3264 | Receive normal data and ancillary data from the socket, scattering the\n\ |
| 3265 | non-ancillary data into a series of buffers. The buffers argument\n\ |
| 3266 | must be an iterable of objects that export writable buffers\n\ |
| 3267 | (e.g. bytearray objects); these will be filled with successive chunks\n\ |
| 3268 | of the non-ancillary data until it has all been written or there are\n\ |
| 3269 | no more buffers. The ancbufsize argument sets the size in bytes of\n\ |
| 3270 | the internal buffer used to receive the ancillary data; it defaults to\n\ |
| 3271 | 0, meaning that no ancillary data will be received. Appropriate\n\ |
| 3272 | buffer sizes for ancillary data can be calculated using CMSG_SPACE()\n\ |
| 3273 | or CMSG_LEN(), and items which do not fit into the buffer might be\n\ |
| 3274 | truncated or discarded. The flags argument defaults to 0 and has the\n\ |
| 3275 | same meaning as for recv().\n\ |
| 3276 | \n\ |
| 3277 | The return value is a 4-tuple: (nbytes, ancdata, msg_flags, address).\n\ |
| 3278 | The nbytes item is the total number of bytes of non-ancillary data\n\ |
| 3279 | written into the buffers. The ancdata item is a list of zero or more\n\ |
| 3280 | tuples (cmsg_level, cmsg_type, cmsg_data) representing the ancillary\n\ |
| 3281 | data (control messages) received: cmsg_level and cmsg_type are\n\ |
| 3282 | integers specifying the protocol level and protocol-specific type\n\ |
| 3283 | respectively, and cmsg_data is a bytes object holding the associated\n\ |
| 3284 | data. The msg_flags item is the bitwise OR of various flags\n\ |
| 3285 | indicating conditions on the received message; see your system\n\ |
| 3286 | documentation for details. If the receiving socket is unconnected,\n\ |
| 3287 | address is the address of the sending socket, if available; otherwise,\n\ |
| 3288 | its value is unspecified.\n\ |
| 3289 | \n\ |
| 3290 | If recvmsg_into() raises an exception after the system call returns,\n\ |
| 3291 | it will first attempt to close any file descriptors received via the\n\ |
| 3292 | SCM_RIGHTS mechanism."); |
| 3293 | #endif /* CMSG_LEN */ |
| 3294 | |
| 3295 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 3296 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3297 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3298 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3299 | sock_send(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3300 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3301 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3302 | Py_ssize_t len, n = -1; |
| 3303 | int flags = 0, timeout; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3304 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3306 | if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags)) |
| 3307 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3309 | if (!IS_SELECTABLE(s)) { |
| 3310 | PyBuffer_Release(&pbuf); |
| 3311 | return select_error(); |
| 3312 | } |
| 3313 | buf = pbuf.buf; |
| 3314 | len = pbuf.len; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 3315 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3316 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3317 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3318 | timeout = internal_select_ex(s, 1, interval); |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 3319 | if (!timeout) { |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 3320 | #ifdef MS_WINDOWS |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 3321 | if (len > INT_MAX) |
| 3322 | len = INT_MAX; |
| 3323 | n = send(s->sock_fd, buf, (int)len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3324 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3325 | n = send(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3326 | #endif |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 3327 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3328 | Py_END_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3329 | if (timeout == 1) { |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3330 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3331 | PyErr_SetString(socket_timeout, "timed out"); |
| 3332 | return NULL; |
| 3333 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3334 | END_SELECT_LOOP(s) |
| 3335 | |
| 3336 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3337 | if (n < 0) |
| 3338 | return s->errorhandler(); |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3339 | return PyLong_FromSsize_t(n); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3342 | PyDoc_STRVAR(send_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3343 | "send(data[, flags]) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3344 | \n\ |
| 3345 | 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] | 3346 | 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] | 3347 | 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] | 3348 | |
| 3349 | |
| 3350 | /* s.sendall(data [,flags]) method */ |
| 3351 | |
| 3352 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3353 | sock_sendall(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3354 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3355 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3356 | Py_ssize_t len, n = -1; |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3357 | int flags = 0, timeout, saved_errno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3358 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3360 | if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags)) |
| 3361 | return NULL; |
| 3362 | buf = pbuf.buf; |
| 3363 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3364 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3365 | if (!IS_SELECTABLE(s)) { |
| 3366 | PyBuffer_Release(&pbuf); |
| 3367 | return select_error(); |
| 3368 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 3369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3370 | do { |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3371 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3372 | timeout = internal_select(s, 1); |
| 3373 | n = -1; |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3374 | if (!timeout) { |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 3375 | #ifdef MS_WINDOWS |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 3376 | if (len > INT_MAX) |
| 3377 | len = INT_MAX; |
| 3378 | n = send(s->sock_fd, buf, (int)len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3379 | #else |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3380 | n = send(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3381 | #endif |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3382 | } |
| 3383 | Py_END_ALLOW_THREADS |
| 3384 | if (timeout == 1) { |
| 3385 | PyBuffer_Release(&pbuf); |
| 3386 | PyErr_SetString(socket_timeout, "timed out"); |
| 3387 | return NULL; |
| 3388 | } |
| 3389 | /* PyErr_CheckSignals() might change errno */ |
| 3390 | saved_errno = errno; |
| 3391 | /* We must run our signal handlers before looping again. |
| 3392 | send() can return a successful partial write when it is |
| 3393 | interrupted, so we can't restrict ourselves to EINTR. */ |
| 3394 | if (PyErr_CheckSignals()) { |
| 3395 | PyBuffer_Release(&pbuf); |
| 3396 | return NULL; |
| 3397 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3398 | if (n < 0) { |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3399 | /* If interrupted, try again */ |
| 3400 | if (saved_errno == EINTR) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3401 | continue; |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3402 | else |
| 3403 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3404 | } |
| 3405 | buf += n; |
| 3406 | len -= n; |
| 3407 | } while (len > 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3408 | PyBuffer_Release(&pbuf); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3409 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3410 | if (n < 0) |
| 3411 | return s->errorhandler(); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3413 | Py_INCREF(Py_None); |
| 3414 | return Py_None; |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3415 | } |
| 3416 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3417 | PyDoc_STRVAR(sendall_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3418 | "sendall(data[, flags])\n\ |
| 3419 | \n\ |
| 3420 | Send a data string to the socket. For the optional flags\n\ |
| 3421 | argument, see the Unix manual. This calls send() repeatedly\n\ |
| 3422 | 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] | 3423 | to tell how much data has been sent."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3424 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3425 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 3426 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3427 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3428 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3429 | sock_sendto(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3430 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3431 | Py_buffer pbuf; |
| 3432 | PyObject *addro; |
| 3433 | char *buf; |
Antoine Pitrou | 5e98141 | 2011-03-17 22:38:37 +0100 | [diff] [blame] | 3434 | Py_ssize_t len, arglen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3435 | sock_addr_t addrbuf; |
| 3436 | int addrlen, n = -1, flags, timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3437 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3438 | flags = 0; |
Antoine Pitrou | 5e98141 | 2011-03-17 22:38:37 +0100 | [diff] [blame] | 3439 | arglen = PyTuple_Size(args); |
| 3440 | switch (arglen) { |
| 3441 | case 2: |
| 3442 | PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro); |
| 3443 | break; |
| 3444 | case 3: |
| 3445 | PyArg_ParseTuple(args, "y*iO:sendto", |
| 3446 | &pbuf, &flags, &addro); |
| 3447 | break; |
| 3448 | default: |
| 3449 | PyErr_Format(PyExc_TypeError, |
| 3450 | "sendto() takes 2 or 3 arguments (%d given)", |
| 3451 | arglen); |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 3452 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3453 | } |
Antoine Pitrou | 5e98141 | 2011-03-17 22:38:37 +0100 | [diff] [blame] | 3454 | if (PyErr_Occurred()) |
| 3455 | return NULL; |
| 3456 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3457 | buf = pbuf.buf; |
| 3458 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3459 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3460 | if (!IS_SELECTABLE(s)) { |
| 3461 | PyBuffer_Release(&pbuf); |
| 3462 | return select_error(); |
| 3463 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 3464 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3465 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) { |
| 3466 | PyBuffer_Release(&pbuf); |
| 3467 | return NULL; |
| 3468 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3469 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3470 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3471 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3472 | timeout = internal_select_ex(s, 1, interval); |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 3473 | if (!timeout) { |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 3474 | #ifdef MS_WINDOWS |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 3475 | if (len > INT_MAX) |
| 3476 | len = INT_MAX; |
| 3477 | n = sendto(s->sock_fd, buf, (int)len, flags, |
| 3478 | SAS2SA(&addrbuf), addrlen); |
| 3479 | #else |
| 3480 | n = sendto(s->sock_fd, buf, len, flags, |
| 3481 | SAS2SA(&addrbuf), addrlen); |
| 3482 | #endif |
| 3483 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3484 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3485 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3486 | if (timeout == 1) { |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3487 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3488 | PyErr_SetString(socket_timeout, "timed out"); |
| 3489 | return NULL; |
| 3490 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3491 | END_SELECT_LOOP(s) |
| 3492 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3493 | if (n < 0) |
| 3494 | return s->errorhandler(); |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3495 | return PyLong_FromSsize_t(n); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3496 | } |
| 3497 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3498 | PyDoc_STRVAR(sendto_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3499 | "sendto(data[, flags], address) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3500 | \n\ |
| 3501 | 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] | 3502 | For IP sockets, the address is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3503 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3504 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3505 | /* The sendmsg() and recvmsg[_into]() methods require a working |
| 3506 | CMSG_LEN(). See the comment near get_CMSG_LEN(). */ |
| 3507 | #ifdef CMSG_LEN |
| 3508 | /* s.sendmsg(buffers[, ancdata[, flags[, address]]]) method */ |
| 3509 | |
| 3510 | static PyObject * |
| 3511 | sock_sendmsg(PySocketSockObject *s, PyObject *args) |
| 3512 | { |
| 3513 | Py_ssize_t i, ndataparts, ndatabufs = 0, ncmsgs, ncmsgbufs = 0; |
| 3514 | Py_buffer *databufs = NULL; |
| 3515 | struct iovec *iovs = NULL; |
| 3516 | sock_addr_t addrbuf; |
Charles-François Natali | e9e95ae | 2011-08-24 21:40:53 +0200 | [diff] [blame] | 3517 | struct msghdr msg = {0}; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3518 | struct cmsginfo { |
| 3519 | int level; |
| 3520 | int type; |
| 3521 | Py_buffer data; |
| 3522 | } *cmsgs = NULL; |
| 3523 | void *controlbuf = NULL; |
| 3524 | size_t controllen, controllen_last; |
| 3525 | ssize_t bytes_sent = -1; |
| 3526 | int addrlen, timeout, flags = 0; |
| 3527 | PyObject *data_arg, *cmsg_arg = NULL, *addr_arg = NULL, *data_fast = NULL, |
| 3528 | *cmsg_fast = NULL, *retval = NULL; |
| 3529 | |
| 3530 | if (!PyArg_ParseTuple(args, "O|OiO:sendmsg", |
| 3531 | &data_arg, &cmsg_arg, &flags, &addr_arg)) |
| 3532 | return NULL; |
| 3533 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3534 | /* Parse destination address. */ |
| 3535 | if (addr_arg != NULL && addr_arg != Py_None) { |
| 3536 | if (!getsockaddrarg(s, addr_arg, SAS2SA(&addrbuf), &addrlen)) |
| 3537 | goto finally; |
| 3538 | msg.msg_name = &addrbuf; |
| 3539 | msg.msg_namelen = addrlen; |
| 3540 | } |
| 3541 | |
| 3542 | /* Fill in an iovec for each message part, and save the Py_buffer |
| 3543 | structs to release afterwards. */ |
| 3544 | if ((data_fast = PySequence_Fast(data_arg, |
| 3545 | "sendmsg() argument 1 must be an " |
| 3546 | "iterable")) == NULL) |
| 3547 | goto finally; |
| 3548 | ndataparts = PySequence_Fast_GET_SIZE(data_fast); |
| 3549 | if (ndataparts > INT_MAX) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3550 | PyErr_SetString(PyExc_OSError, "sendmsg() argument 1 is too long"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3551 | goto finally; |
| 3552 | } |
| 3553 | msg.msg_iovlen = ndataparts; |
| 3554 | if (ndataparts > 0 && |
| 3555 | ((msg.msg_iov = iovs = PyMem_New(struct iovec, ndataparts)) == NULL || |
| 3556 | (databufs = PyMem_New(Py_buffer, ndataparts)) == NULL)) { |
| 3557 | PyErr_NoMemory(); |
| 3558 | goto finally; |
| 3559 | } |
| 3560 | for (; ndatabufs < ndataparts; ndatabufs++) { |
| 3561 | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(data_fast, ndatabufs), |
| 3562 | "y*;sendmsg() argument 1 must be an iterable of " |
| 3563 | "buffer-compatible objects", |
| 3564 | &databufs[ndatabufs])) |
| 3565 | goto finally; |
| 3566 | iovs[ndatabufs].iov_base = databufs[ndatabufs].buf; |
| 3567 | iovs[ndatabufs].iov_len = databufs[ndatabufs].len; |
| 3568 | } |
| 3569 | |
| 3570 | if (cmsg_arg == NULL) |
| 3571 | ncmsgs = 0; |
| 3572 | else { |
| 3573 | if ((cmsg_fast = PySequence_Fast(cmsg_arg, |
| 3574 | "sendmsg() argument 2 must be an " |
| 3575 | "iterable")) == NULL) |
| 3576 | goto finally; |
| 3577 | ncmsgs = PySequence_Fast_GET_SIZE(cmsg_fast); |
| 3578 | } |
| 3579 | |
| 3580 | #ifndef CMSG_SPACE |
| 3581 | if (ncmsgs > 1) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3582 | PyErr_SetString(PyExc_OSError, |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3583 | "sending multiple control messages is not supported " |
| 3584 | "on this system"); |
| 3585 | goto finally; |
| 3586 | } |
| 3587 | #endif |
| 3588 | /* Save level, type and Py_buffer for each control message, |
| 3589 | and calculate total size. */ |
| 3590 | if (ncmsgs > 0 && (cmsgs = PyMem_New(struct cmsginfo, ncmsgs)) == NULL) { |
| 3591 | PyErr_NoMemory(); |
| 3592 | goto finally; |
| 3593 | } |
| 3594 | controllen = controllen_last = 0; |
| 3595 | while (ncmsgbufs < ncmsgs) { |
| 3596 | size_t bufsize, space; |
| 3597 | |
| 3598 | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(cmsg_fast, ncmsgbufs), |
| 3599 | "(iiy*):[sendmsg() ancillary data items]", |
| 3600 | &cmsgs[ncmsgbufs].level, |
| 3601 | &cmsgs[ncmsgbufs].type, |
| 3602 | &cmsgs[ncmsgbufs].data)) |
| 3603 | goto finally; |
| 3604 | bufsize = cmsgs[ncmsgbufs++].data.len; |
| 3605 | |
| 3606 | #ifdef CMSG_SPACE |
| 3607 | if (!get_CMSG_SPACE(bufsize, &space)) { |
| 3608 | #else |
| 3609 | if (!get_CMSG_LEN(bufsize, &space)) { |
| 3610 | #endif |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3611 | PyErr_SetString(PyExc_OSError, "ancillary data item too large"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3612 | goto finally; |
| 3613 | } |
| 3614 | controllen += space; |
| 3615 | if (controllen > SOCKLEN_T_LIMIT || controllen < controllen_last) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3616 | PyErr_SetString(PyExc_OSError, "too much ancillary data"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3617 | goto finally; |
| 3618 | } |
| 3619 | controllen_last = controllen; |
| 3620 | } |
| 3621 | |
| 3622 | /* Construct ancillary data block from control message info. */ |
| 3623 | if (ncmsgbufs > 0) { |
| 3624 | struct cmsghdr *cmsgh = NULL; |
| 3625 | |
| 3626 | if ((msg.msg_control = controlbuf = |
| 3627 | PyMem_Malloc(controllen)) == NULL) { |
| 3628 | PyErr_NoMemory(); |
| 3629 | goto finally; |
| 3630 | } |
| 3631 | msg.msg_controllen = controllen; |
| 3632 | |
| 3633 | /* Need to zero out the buffer as a workaround for glibc's |
| 3634 | CMSG_NXTHDR() implementation. After getting the pointer to |
| 3635 | the next header, it checks its (uninitialized) cmsg_len |
| 3636 | member to see if the "message" fits in the buffer, and |
| 3637 | returns NULL if it doesn't. Zero-filling the buffer |
Terry Jan Reedy | 0f84764 | 2013-03-11 18:34:00 -0400 | [diff] [blame] | 3638 | ensures that this doesn't happen. */ |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3639 | memset(controlbuf, 0, controllen); |
| 3640 | |
| 3641 | for (i = 0; i < ncmsgbufs; i++) { |
| 3642 | size_t msg_len, data_len = cmsgs[i].data.len; |
| 3643 | int enough_space = 0; |
| 3644 | |
| 3645 | cmsgh = (i == 0) ? CMSG_FIRSTHDR(&msg) : CMSG_NXTHDR(&msg, cmsgh); |
| 3646 | if (cmsgh == NULL) { |
| 3647 | PyErr_Format(PyExc_RuntimeError, |
| 3648 | "unexpected NULL result from %s()", |
| 3649 | (i == 0) ? "CMSG_FIRSTHDR" : "CMSG_NXTHDR"); |
| 3650 | goto finally; |
| 3651 | } |
| 3652 | if (!get_CMSG_LEN(data_len, &msg_len)) { |
| 3653 | PyErr_SetString(PyExc_RuntimeError, |
| 3654 | "item size out of range for CMSG_LEN()"); |
| 3655 | goto finally; |
| 3656 | } |
| 3657 | if (cmsg_min_space(&msg, cmsgh, msg_len)) { |
| 3658 | size_t space; |
| 3659 | |
| 3660 | cmsgh->cmsg_len = msg_len; |
| 3661 | if (get_cmsg_data_space(&msg, cmsgh, &space)) |
| 3662 | enough_space = (space >= data_len); |
| 3663 | } |
| 3664 | if (!enough_space) { |
| 3665 | PyErr_SetString(PyExc_RuntimeError, |
| 3666 | "ancillary data does not fit in calculated " |
| 3667 | "space"); |
| 3668 | goto finally; |
| 3669 | } |
| 3670 | cmsgh->cmsg_level = cmsgs[i].level; |
| 3671 | cmsgh->cmsg_type = cmsgs[i].type; |
| 3672 | memcpy(CMSG_DATA(cmsgh), cmsgs[i].data.buf, data_len); |
| 3673 | } |
| 3674 | } |
| 3675 | |
| 3676 | /* Make the system call. */ |
| 3677 | if (!IS_SELECTABLE(s)) { |
| 3678 | select_error(); |
| 3679 | goto finally; |
| 3680 | } |
| 3681 | |
| 3682 | BEGIN_SELECT_LOOP(s) |
| 3683 | Py_BEGIN_ALLOW_THREADS; |
| 3684 | timeout = internal_select_ex(s, 1, interval); |
| 3685 | if (!timeout) |
| 3686 | bytes_sent = sendmsg(s->sock_fd, &msg, flags); |
| 3687 | Py_END_ALLOW_THREADS; |
| 3688 | if (timeout == 1) { |
| 3689 | PyErr_SetString(socket_timeout, "timed out"); |
| 3690 | goto finally; |
| 3691 | } |
| 3692 | END_SELECT_LOOP(s) |
| 3693 | |
| 3694 | if (bytes_sent < 0) { |
| 3695 | s->errorhandler(); |
| 3696 | goto finally; |
| 3697 | } |
| 3698 | retval = PyLong_FromSsize_t(bytes_sent); |
| 3699 | |
| 3700 | finally: |
| 3701 | PyMem_Free(controlbuf); |
| 3702 | for (i = 0; i < ncmsgbufs; i++) |
| 3703 | PyBuffer_Release(&cmsgs[i].data); |
| 3704 | PyMem_Free(cmsgs); |
| 3705 | Py_XDECREF(cmsg_fast); |
| 3706 | for (i = 0; i < ndatabufs; i++) |
| 3707 | PyBuffer_Release(&databufs[i]); |
| 3708 | PyMem_Free(databufs); |
| 3709 | PyMem_Free(iovs); |
| 3710 | Py_XDECREF(data_fast); |
| 3711 | return retval; |
| 3712 | } |
| 3713 | |
| 3714 | PyDoc_STRVAR(sendmsg_doc, |
| 3715 | "sendmsg(buffers[, ancdata[, flags[, address]]]) -> count\n\ |
| 3716 | \n\ |
| 3717 | Send normal and ancillary data to the socket, gathering the\n\ |
| 3718 | non-ancillary data from a series of buffers and concatenating it into\n\ |
| 3719 | a single message. The buffers argument specifies the non-ancillary\n\ |
| 3720 | data as an iterable of buffer-compatible objects (e.g. bytes objects).\n\ |
| 3721 | The ancdata argument specifies the ancillary data (control messages)\n\ |
| 3722 | as an iterable of zero or more tuples (cmsg_level, cmsg_type,\n\ |
| 3723 | cmsg_data), where cmsg_level and cmsg_type are integers specifying the\n\ |
| 3724 | protocol level and protocol-specific type respectively, and cmsg_data\n\ |
| 3725 | is a buffer-compatible object holding the associated data. The flags\n\ |
| 3726 | argument defaults to 0 and has the same meaning as for send(). If\n\ |
| 3727 | address is supplied and not None, it sets a destination address for\n\ |
| 3728 | the message. The return value is the number of bytes of non-ancillary\n\ |
| 3729 | data sent."); |
| 3730 | #endif /* CMSG_LEN */ |
| 3731 | |
| 3732 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3733 | /* s.shutdown(how) method */ |
| 3734 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3735 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3736 | sock_shutdown(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3737 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3738 | int how; |
| 3739 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 3740 | |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 3741 | how = _PyLong_AsInt(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3742 | if (how == -1 && PyErr_Occurred()) |
| 3743 | return NULL; |
| 3744 | Py_BEGIN_ALLOW_THREADS |
| 3745 | res = shutdown(s->sock_fd, how); |
| 3746 | Py_END_ALLOW_THREADS |
| 3747 | if (res < 0) |
| 3748 | return s->errorhandler(); |
| 3749 | Py_INCREF(Py_None); |
| 3750 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3751 | } |
| 3752 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3753 | PyDoc_STRVAR(shutdown_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3754 | "shutdown(flag)\n\ |
| 3755 | \n\ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 3756 | Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\ |
| 3757 | 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] | 3758 | |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 3759 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3760 | static PyObject* |
| 3761 | sock_ioctl(PySocketSockObject *s, PyObject *arg) |
| 3762 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3763 | unsigned long cmd = SIO_RCVALL; |
| 3764 | PyObject *argO; |
| 3765 | DWORD recv; |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3766 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3767 | if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO)) |
| 3768 | return NULL; |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3769 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3770 | switch (cmd) { |
| 3771 | case SIO_RCVALL: { |
| 3772 | unsigned int option = RCVALL_ON; |
| 3773 | if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option)) |
| 3774 | return NULL; |
| 3775 | if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option), |
| 3776 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 3777 | return set_error(); |
| 3778 | } |
| 3779 | return PyLong_FromUnsignedLong(recv); } |
| 3780 | case SIO_KEEPALIVE_VALS: { |
| 3781 | struct tcp_keepalive ka; |
| 3782 | if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd, |
| 3783 | &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval)) |
| 3784 | return NULL; |
| 3785 | if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka), |
| 3786 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 3787 | return set_error(); |
| 3788 | } |
| 3789 | return PyLong_FromUnsignedLong(recv); } |
| 3790 | default: |
| 3791 | PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd); |
| 3792 | return NULL; |
| 3793 | } |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3794 | } |
| 3795 | PyDoc_STRVAR(sock_ioctl_doc, |
| 3796 | "ioctl(cmd, option) -> long\n\ |
| 3797 | \n\ |
Kristján Valur Jónsson | 847ec75 | 2009-09-27 21:10:38 +0000 | [diff] [blame] | 3798 | Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\ |
| 3799 | SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\ |
| 3800 | 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] | 3801 | #endif |
| 3802 | |
| 3803 | #if defined(MS_WINDOWS) |
| 3804 | static PyObject* |
| 3805 | sock_share(PySocketSockObject *s, PyObject *arg) |
| 3806 | { |
| 3807 | WSAPROTOCOL_INFO info; |
| 3808 | DWORD processId; |
| 3809 | int result; |
| 3810 | |
| 3811 | if (!PyArg_ParseTuple(arg, "I", &processId)) |
| 3812 | return NULL; |
| 3813 | |
| 3814 | Py_BEGIN_ALLOW_THREADS |
| 3815 | result = WSADuplicateSocket(s->sock_fd, processId, &info); |
| 3816 | Py_END_ALLOW_THREADS |
| 3817 | if (result == SOCKET_ERROR) |
| 3818 | return set_error(); |
| 3819 | return PyBytes_FromStringAndSize((const char*)&info, sizeof(info)); |
| 3820 | } |
| 3821 | PyDoc_STRVAR(sock_share_doc, |
| 3822 | "share(process_id) -> bytes\n\ |
| 3823 | \n\ |
| 3824 | Share the socket with another process. The target process id\n\ |
| 3825 | must be provided and the resulting bytes object passed to the target\n\ |
| 3826 | process. There the shared socket can be instantiated by calling\n\ |
| 3827 | socket.fromshare()."); |
| 3828 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3829 | |
| 3830 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3831 | |
| 3832 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3833 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3834 | static PyMethodDef sock_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3835 | {"_accept", (PyCFunction)sock_accept, METH_NOARGS, |
| 3836 | accept_doc}, |
| 3837 | {"bind", (PyCFunction)sock_bind, METH_O, |
| 3838 | bind_doc}, |
| 3839 | {"close", (PyCFunction)sock_close, METH_NOARGS, |
| 3840 | close_doc}, |
| 3841 | {"connect", (PyCFunction)sock_connect, METH_O, |
| 3842 | connect_doc}, |
| 3843 | {"connect_ex", (PyCFunction)sock_connect_ex, METH_O, |
| 3844 | connect_ex_doc}, |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 3845 | {"detach", (PyCFunction)sock_detach, METH_NOARGS, |
| 3846 | detach_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3847 | {"fileno", (PyCFunction)sock_fileno, METH_NOARGS, |
| 3848 | fileno_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3849 | #ifdef HAVE_GETPEERNAME |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3850 | {"getpeername", (PyCFunction)sock_getpeername, |
| 3851 | METH_NOARGS, getpeername_doc}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 3852 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3853 | {"getsockname", (PyCFunction)sock_getsockname, |
| 3854 | METH_NOARGS, getsockname_doc}, |
| 3855 | {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS, |
| 3856 | getsockopt_doc}, |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 3857 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3858 | {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS, |
| 3859 | sock_ioctl_doc}, |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3860 | #endif |
Kristján Valur Jónsson | 10f383a | 2012-04-07 11:23:31 +0000 | [diff] [blame] | 3861 | #if defined(MS_WINDOWS) |
| 3862 | {"share", (PyCFunction)sock_share, METH_VARARGS, |
| 3863 | sock_share_doc}, |
| 3864 | #endif |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 3865 | {"listen", (PyCFunction)sock_listen, METH_VARARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3866 | listen_doc}, |
| 3867 | {"recv", (PyCFunction)sock_recv, METH_VARARGS, |
| 3868 | recv_doc}, |
| 3869 | {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS, |
| 3870 | recv_into_doc}, |
| 3871 | {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS, |
| 3872 | recvfrom_doc}, |
| 3873 | {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS, |
| 3874 | recvfrom_into_doc}, |
| 3875 | {"send", (PyCFunction)sock_send, METH_VARARGS, |
| 3876 | send_doc}, |
| 3877 | {"sendall", (PyCFunction)sock_sendall, METH_VARARGS, |
| 3878 | sendall_doc}, |
| 3879 | {"sendto", (PyCFunction)sock_sendto, METH_VARARGS, |
| 3880 | sendto_doc}, |
| 3881 | {"setblocking", (PyCFunction)sock_setblocking, METH_O, |
| 3882 | setblocking_doc}, |
| 3883 | {"settimeout", (PyCFunction)sock_settimeout, METH_O, |
| 3884 | settimeout_doc}, |
| 3885 | {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS, |
| 3886 | gettimeout_doc}, |
| 3887 | {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS, |
| 3888 | setsockopt_doc}, |
| 3889 | {"shutdown", (PyCFunction)sock_shutdown, METH_O, |
| 3890 | shutdown_doc}, |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3891 | #ifdef CMSG_LEN |
| 3892 | {"recvmsg", (PyCFunction)sock_recvmsg, METH_VARARGS, |
| 3893 | recvmsg_doc}, |
| 3894 | {"recvmsg_into", (PyCFunction)sock_recvmsg_into, METH_VARARGS, |
| 3895 | recvmsg_into_doc,}, |
| 3896 | {"sendmsg", (PyCFunction)sock_sendmsg, METH_VARARGS, |
| 3897 | sendmsg_doc}, |
| 3898 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3899 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3900 | }; |
| 3901 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3902 | /* SockObject members */ |
| 3903 | static PyMemberDef sock_memberlist[] = { |
| 3904 | {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"}, |
| 3905 | {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"}, |
| 3906 | {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"}, |
| 3907 | {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"}, |
| 3908 | {0}, |
| 3909 | }; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3910 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3911 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3912 | First close the file description. */ |
| 3913 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3914 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3915 | sock_dealloc(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3916 | { |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 3917 | if (s->sock_fd != -1) { |
| 3918 | PyObject *exc, *val, *tb; |
| 3919 | Py_ssize_t old_refcount = Py_REFCNT(s); |
| 3920 | ++Py_REFCNT(s); |
| 3921 | PyErr_Fetch(&exc, &val, &tb); |
| 3922 | if (PyErr_WarnFormat(PyExc_ResourceWarning, 1, |
| 3923 | "unclosed %R", s)) |
| 3924 | /* Spurious errors can appear at shutdown */ |
| 3925 | if (PyErr_ExceptionMatches(PyExc_Warning)) |
| 3926 | PyErr_WriteUnraisable((PyObject *) s); |
| 3927 | PyErr_Restore(exc, val, tb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3928 | (void) SOCKETCLOSE(s->sock_fd); |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 3929 | Py_REFCNT(s) = old_refcount; |
| 3930 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3931 | Py_TYPE(s)->tp_free((PyObject *)s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3932 | } |
| 3933 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3934 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3935 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3936 | sock_repr(PySocketSockObject *s) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3937 | { |
Victor Stinner | e254e53 | 2014-07-26 14:36:55 +0200 | [diff] [blame] | 3938 | long sock_fd; |
| 3939 | /* On Windows, this test is needed because SOCKET_T is unsigned */ |
| 3940 | if (s->sock_fd == INVALID_SOCKET) { |
| 3941 | sock_fd = -1; |
| 3942 | } |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 3943 | #if SIZEOF_SOCKET_T > SIZEOF_LONG |
Victor Stinner | e254e53 | 2014-07-26 14:36:55 +0200 | [diff] [blame] | 3944 | else if (s->sock_fd > LONG_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3945 | /* this can occur on Win64, and actually there is a special |
| 3946 | ugly printf formatter for decimal pointer length integer |
| 3947 | printing, only bother if necessary*/ |
| 3948 | PyErr_SetString(PyExc_OverflowError, |
| 3949 | "no printf formatter to display " |
| 3950 | "the socket descriptor in decimal"); |
| 3951 | return NULL; |
| 3952 | } |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 3953 | #endif |
Victor Stinner | e254e53 | 2014-07-26 14:36:55 +0200 | [diff] [blame] | 3954 | else |
| 3955 | sock_fd = (long)s->sock_fd; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3956 | return PyUnicode_FromFormat( |
| 3957 | "<socket object, fd=%ld, family=%d, type=%d, proto=%d>", |
Victor Stinner | e254e53 | 2014-07-26 14:36:55 +0200 | [diff] [blame] | 3958 | sock_fd, s->sock_family, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3959 | s->sock_type, |
| 3960 | s->sock_proto); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3964 | /* Create a new, uninitialized socket object. */ |
| 3965 | |
| 3966 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3967 | sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3968 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3969 | PyObject *new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3970 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3971 | new = type->tp_alloc(type, 0); |
| 3972 | if (new != NULL) { |
| 3973 | ((PySocketSockObject *)new)->sock_fd = -1; |
| 3974 | ((PySocketSockObject *)new)->sock_timeout = -1.0; |
| 3975 | ((PySocketSockObject *)new)->errorhandler = &set_error; |
| 3976 | } |
| 3977 | return new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3978 | } |
| 3979 | |
| 3980 | |
| 3981 | /* Initialize a new socket object. */ |
| 3982 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3983 | #ifdef SOCK_CLOEXEC |
| 3984 | /* socket() and socketpair() fail with EINVAL on Linux kernel older |
| 3985 | * than 2.6.27 if SOCK_CLOEXEC flag is set in the socket type. */ |
| 3986 | static int sock_cloexec_works = -1; |
| 3987 | #endif |
| 3988 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3989 | /*ARGSUSED*/ |
| 3990 | static int |
Andrew MacIntyre | 7aec4a2 | 2002-06-13 11:53:52 +0000 | [diff] [blame] | 3991 | sock_initobj(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3992 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3993 | PySocketSockObject *s = (PySocketSockObject *)self; |
| 3994 | PyObject *fdobj = NULL; |
| 3995 | SOCKET_T fd = INVALID_SOCKET; |
| 3996 | int family = AF_INET, type = SOCK_STREAM, proto = 0; |
| 3997 | static char *keywords[] = {"family", "type", "proto", "fileno", 0}; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3998 | #ifndef MS_WINDOWS |
| 3999 | #ifdef SOCK_CLOEXEC |
| 4000 | int *atomic_flag_works = &sock_cloexec_works; |
| 4001 | #else |
| 4002 | int *atomic_flag_works = NULL; |
| 4003 | #endif |
| 4004 | #endif |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4005 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4006 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 4007 | "|iiiO:socket", keywords, |
| 4008 | &family, &type, &proto, &fdobj)) |
| 4009 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4010 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4011 | if (fdobj != NULL && fdobj != Py_None) { |
Kristján Valur Jónsson | 10f383a | 2012-04-07 11:23:31 +0000 | [diff] [blame] | 4012 | #ifdef MS_WINDOWS |
| 4013 | /* recreate a socket that was duplicated */ |
| 4014 | if (PyBytes_Check(fdobj)) { |
| 4015 | WSAPROTOCOL_INFO info; |
| 4016 | if (PyBytes_GET_SIZE(fdobj) != sizeof(info)) { |
| 4017 | PyErr_Format(PyExc_ValueError, |
| 4018 | "socket descriptor string has wrong size, " |
| 4019 | "should be %zu bytes.", sizeof(info)); |
| 4020 | return -1; |
| 4021 | } |
| 4022 | memcpy(&info, PyBytes_AS_STRING(fdobj), sizeof(info)); |
| 4023 | Py_BEGIN_ALLOW_THREADS |
| 4024 | fd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, |
| 4025 | FROM_PROTOCOL_INFO, &info, 0, WSA_FLAG_OVERLAPPED); |
| 4026 | Py_END_ALLOW_THREADS |
| 4027 | if (fd == INVALID_SOCKET) { |
| 4028 | set_error(); |
| 4029 | return -1; |
| 4030 | } |
| 4031 | family = info.iAddressFamily; |
| 4032 | type = info.iSocketType; |
| 4033 | proto = info.iProtocol; |
| 4034 | } |
| 4035 | else |
| 4036 | #endif |
| 4037 | { |
| 4038 | fd = PyLong_AsSocket_t(fdobj); |
| 4039 | if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
| 4040 | return -1; |
| 4041 | if (fd == INVALID_SOCKET) { |
| 4042 | PyErr_SetString(PyExc_ValueError, |
| 4043 | "can't use invalid socket value"); |
| 4044 | return -1; |
| 4045 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4046 | } |
| 4047 | } |
| 4048 | else { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4049 | #ifdef MS_WINDOWS |
| 4050 | /* Windows implementation */ |
| 4051 | #ifndef WSA_FLAG_NO_HANDLE_INHERIT |
| 4052 | #define WSA_FLAG_NO_HANDLE_INHERIT 0x80 |
| 4053 | #endif |
| 4054 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4055 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4056 | if (support_wsa_no_inherit) { |
| 4057 | fd = WSASocket(family, type, proto, |
| 4058 | NULL, 0, |
| 4059 | WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT); |
| 4060 | if (fd == INVALID_SOCKET) { |
| 4061 | /* Windows 7 or Windows 2008 R2 without SP1 or the hotfix */ |
| 4062 | support_wsa_no_inherit = 0; |
| 4063 | fd = socket(family, type, proto); |
| 4064 | } |
| 4065 | } |
| 4066 | else { |
| 4067 | fd = socket(family, type, proto); |
| 4068 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4069 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4070 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4071 | if (fd == INVALID_SOCKET) { |
| 4072 | set_error(); |
| 4073 | return -1; |
| 4074 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4075 | |
| 4076 | if (!support_wsa_no_inherit) { |
| 4077 | if (!SetHandleInformation((HANDLE)fd, HANDLE_FLAG_INHERIT, 0)) { |
| 4078 | closesocket(fd); |
| 4079 | PyErr_SetFromWindowsErr(0); |
| 4080 | return -1; |
| 4081 | } |
| 4082 | } |
| 4083 | #else |
| 4084 | /* UNIX */ |
| 4085 | Py_BEGIN_ALLOW_THREADS |
| 4086 | #ifdef SOCK_CLOEXEC |
| 4087 | if (sock_cloexec_works != 0) { |
| 4088 | fd = socket(family, type | SOCK_CLOEXEC, proto); |
| 4089 | if (sock_cloexec_works == -1) { |
| 4090 | if (fd >= 0) { |
| 4091 | sock_cloexec_works = 1; |
| 4092 | } |
| 4093 | else if (errno == EINVAL) { |
| 4094 | /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */ |
| 4095 | sock_cloexec_works = 0; |
| 4096 | fd = socket(family, type, proto); |
| 4097 | } |
| 4098 | } |
| 4099 | } |
| 4100 | else |
| 4101 | #endif |
| 4102 | { |
| 4103 | fd = socket(family, type, proto); |
| 4104 | } |
| 4105 | Py_END_ALLOW_THREADS |
| 4106 | |
| 4107 | if (fd == INVALID_SOCKET) { |
| 4108 | set_error(); |
| 4109 | return -1; |
| 4110 | } |
| 4111 | |
| 4112 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 4113 | SOCKETCLOSE(fd); |
| 4114 | return -1; |
| 4115 | } |
| 4116 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4117 | } |
| 4118 | init_sockobject(s, fd, family, type, proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4119 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4120 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4121 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4122 | } |
| 4123 | |
| 4124 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4125 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4126 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4127 | static PyTypeObject sock_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4128 | PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */ |
| 4129 | "_socket.socket", /* tp_name */ |
| 4130 | sizeof(PySocketSockObject), /* tp_basicsize */ |
| 4131 | 0, /* tp_itemsize */ |
| 4132 | (destructor)sock_dealloc, /* tp_dealloc */ |
| 4133 | 0, /* tp_print */ |
| 4134 | 0, /* tp_getattr */ |
| 4135 | 0, /* tp_setattr */ |
| 4136 | 0, /* tp_reserved */ |
| 4137 | (reprfunc)sock_repr, /* tp_repr */ |
| 4138 | 0, /* tp_as_number */ |
| 4139 | 0, /* tp_as_sequence */ |
| 4140 | 0, /* tp_as_mapping */ |
| 4141 | 0, /* tp_hash */ |
| 4142 | 0, /* tp_call */ |
| 4143 | 0, /* tp_str */ |
| 4144 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4145 | 0, /* tp_setattro */ |
| 4146 | 0, /* tp_as_buffer */ |
| 4147 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 4148 | sock_doc, /* tp_doc */ |
| 4149 | 0, /* tp_traverse */ |
| 4150 | 0, /* tp_clear */ |
| 4151 | 0, /* tp_richcompare */ |
| 4152 | 0, /* tp_weaklistoffset */ |
| 4153 | 0, /* tp_iter */ |
| 4154 | 0, /* tp_iternext */ |
| 4155 | sock_methods, /* tp_methods */ |
| 4156 | sock_memberlist, /* tp_members */ |
| 4157 | 0, /* tp_getset */ |
| 4158 | 0, /* tp_base */ |
| 4159 | 0, /* tp_dict */ |
| 4160 | 0, /* tp_descr_get */ |
| 4161 | 0, /* tp_descr_set */ |
| 4162 | 0, /* tp_dictoffset */ |
| 4163 | sock_initobj, /* tp_init */ |
| 4164 | PyType_GenericAlloc, /* tp_alloc */ |
| 4165 | sock_new, /* tp_new */ |
| 4166 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4167 | }; |
| 4168 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4169 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 4170 | /* Python interface to gethostname(). */ |
| 4171 | |
| 4172 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4173 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4174 | socket_gethostname(PyObject *self, PyObject *unused) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 4175 | { |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4176 | #ifdef MS_WINDOWS |
| 4177 | /* Don't use winsock's gethostname, as this returns the ANSI |
| 4178 | version of the hostname, whereas we need a Unicode string. |
| 4179 | Otherwise, gethostname apparently also returns the DNS name. */ |
Hirokazu Yamamoto | 09fff7a | 2010-11-05 17:24:13 +0000 | [diff] [blame] | 4180 | wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 4181 | DWORD size = Py_ARRAY_LENGTH(buf); |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4182 | wchar_t *name; |
Hirokazu Yamamoto | 09fff7a | 2010-11-05 17:24:13 +0000 | [diff] [blame] | 4183 | PyObject *result; |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4184 | |
| 4185 | if (GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size)) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 4186 | return PyUnicode_FromWideChar(buf, size); |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4187 | |
| 4188 | if (GetLastError() != ERROR_MORE_DATA) |
| 4189 | return PyErr_SetFromWindowsErr(0); |
| 4190 | |
| 4191 | if (size == 0) |
| 4192 | return PyUnicode_New(0, 0); |
| 4193 | |
| 4194 | /* MSDN says ERROR_MORE_DATA may occur because DNS allows longer |
| 4195 | names */ |
| 4196 | name = PyMem_Malloc(size * sizeof(wchar_t)); |
| 4197 | if (!name) |
| 4198 | return NULL; |
| 4199 | if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname, |
| 4200 | name, |
| 4201 | &size)) |
| 4202 | { |
| 4203 | PyMem_Free(name); |
| 4204 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4205 | } |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4206 | |
| 4207 | result = PyUnicode_FromWideChar(name, size); |
| 4208 | PyMem_Free(name); |
| 4209 | return result; |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4210 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4211 | char buf[1024]; |
| 4212 | int res; |
| 4213 | Py_BEGIN_ALLOW_THREADS |
| 4214 | res = gethostname(buf, (int) sizeof buf - 1); |
| 4215 | Py_END_ALLOW_THREADS |
| 4216 | if (res < 0) |
| 4217 | return set_error(); |
| 4218 | buf[sizeof buf - 1] = '\0'; |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4219 | return PyUnicode_DecodeFSDefault(buf); |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4220 | #endif |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 4221 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 4222 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4223 | PyDoc_STRVAR(gethostname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4224 | "gethostname() -> string\n\ |
| 4225 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4226 | Return the current host name."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4227 | |
Antoine Pitrou | 061cfb5 | 2011-02-28 22:25:22 +0000 | [diff] [blame] | 4228 | #ifdef HAVE_SETHOSTNAME |
| 4229 | PyDoc_STRVAR(sethostname_doc, |
| 4230 | "sethostname(name)\n\n\ |
| 4231 | Sets the hostname to name."); |
| 4232 | |
| 4233 | static PyObject * |
| 4234 | socket_sethostname(PyObject *self, PyObject *args) |
| 4235 | { |
| 4236 | PyObject *hnobj; |
| 4237 | Py_buffer buf; |
| 4238 | int res, flag = 0; |
| 4239 | |
Christian Heimes | d2774c7 | 2013-06-19 02:06:29 +0200 | [diff] [blame] | 4240 | #ifdef _AIX |
| 4241 | /* issue #18259, not declared in any useful header file */ |
| 4242 | extern int sethostname(const char *, size_t); |
| 4243 | #endif |
| 4244 | |
Antoine Pitrou | 061cfb5 | 2011-02-28 22:25:22 +0000 | [diff] [blame] | 4245 | if (!PyArg_ParseTuple(args, "S:sethostname", &hnobj)) { |
| 4246 | PyErr_Clear(); |
| 4247 | if (!PyArg_ParseTuple(args, "O&:sethostname", |
| 4248 | PyUnicode_FSConverter, &hnobj)) |
| 4249 | return NULL; |
| 4250 | flag = 1; |
| 4251 | } |
| 4252 | res = PyObject_GetBuffer(hnobj, &buf, PyBUF_SIMPLE); |
| 4253 | if (!res) { |
| 4254 | res = sethostname(buf.buf, buf.len); |
| 4255 | PyBuffer_Release(&buf); |
| 4256 | } |
| 4257 | if (flag) |
| 4258 | Py_DECREF(hnobj); |
| 4259 | if (res) |
| 4260 | return set_error(); |
| 4261 | Py_RETURN_NONE; |
| 4262 | } |
| 4263 | #endif |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 4264 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4265 | /* Python interface to gethostbyname(name). */ |
| 4266 | |
| 4267 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4268 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4269 | socket_gethostbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4270 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4271 | char *name; |
| 4272 | sock_addr_t addrbuf; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4273 | PyObject *ret = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4274 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4275 | if (!PyArg_ParseTuple(args, "et:gethostbyname", "idna", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4276 | return NULL; |
| 4277 | if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4278 | goto finally; |
| 4279 | ret = makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in)); |
| 4280 | finally: |
| 4281 | PyMem_Free(name); |
| 4282 | return ret; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4283 | } |
| 4284 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4285 | PyDoc_STRVAR(gethostbyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4286 | "gethostbyname(host) -> address\n\ |
| 4287 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4288 | 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] | 4289 | |
| 4290 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4291 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
| 4292 | |
| 4293 | static PyObject * |
Victor Stinner | 1a62a68 | 2014-08-17 19:33:28 +0200 | [diff] [blame] | 4294 | 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] | 4295 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4296 | char **pch; |
| 4297 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 4298 | PyObject *name_list = (PyObject *)NULL; |
| 4299 | PyObject *addr_list = (PyObject *)NULL; |
| 4300 | PyObject *tmp; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4302 | if (h == NULL) { |
| 4303 | /* Let's get real error message to return */ |
| 4304 | set_herror(h_errno); |
| 4305 | return NULL; |
| 4306 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4308 | if (h->h_addrtype != af) { |
| 4309 | /* Let's get real error message to return */ |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 4310 | errno = EAFNOSUPPORT; |
| 4311 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4312 | return NULL; |
| 4313 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4315 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4316 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4317 | case AF_INET: |
| 4318 | if (alen < sizeof(struct sockaddr_in)) |
| 4319 | return NULL; |
| 4320 | break; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4321 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4322 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4323 | case AF_INET6: |
| 4324 | if (alen < sizeof(struct sockaddr_in6)) |
| 4325 | return NULL; |
| 4326 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4327 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4329 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4330 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4331 | if ((name_list = PyList_New(0)) == NULL) |
| 4332 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4334 | if ((addr_list = PyList_New(0)) == NULL) |
| 4335 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4336 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4337 | /* SF #1511317: h_aliases can be NULL */ |
| 4338 | if (h->h_aliases) { |
| 4339 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 4340 | int status; |
| 4341 | tmp = PyUnicode_FromString(*pch); |
| 4342 | if (tmp == NULL) |
| 4343 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4344 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4345 | status = PyList_Append(name_list, tmp); |
| 4346 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4348 | if (status) |
| 4349 | goto err; |
| 4350 | } |
| 4351 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4353 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 4354 | int status; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4356 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4357 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4358 | case AF_INET: |
| 4359 | { |
| 4360 | struct sockaddr_in sin; |
| 4361 | memset(&sin, 0, sizeof(sin)); |
| 4362 | sin.sin_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4363 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4364 | sin.sin_len = sizeof(sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4365 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4366 | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); |
| 4367 | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4368 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4369 | if (pch == h->h_addr_list && alen >= sizeof(sin)) |
| 4370 | memcpy((char *) addr, &sin, sizeof(sin)); |
| 4371 | break; |
| 4372 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4373 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4374 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4375 | case AF_INET6: |
| 4376 | { |
| 4377 | struct sockaddr_in6 sin6; |
| 4378 | memset(&sin6, 0, sizeof(sin6)); |
| 4379 | sin6.sin6_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4380 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4381 | sin6.sin6_len = sizeof(sin6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4382 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4383 | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); |
| 4384 | tmp = makeipaddr((struct sockaddr *)&sin6, |
| 4385 | sizeof(sin6)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4386 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4387 | if (pch == h->h_addr_list && alen >= sizeof(sin6)) |
| 4388 | memcpy((char *) addr, &sin6, sizeof(sin6)); |
| 4389 | break; |
| 4390 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4391 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4393 | default: /* can't happen */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4394 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4395 | "unsupported address family"); |
| 4396 | return NULL; |
| 4397 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4399 | if (tmp == NULL) |
| 4400 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4402 | status = PyList_Append(addr_list, tmp); |
| 4403 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4405 | if (status) |
| 4406 | goto err; |
| 4407 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4408 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4409 | 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] | 4410 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4411 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4412 | Py_XDECREF(name_list); |
| 4413 | Py_XDECREF(addr_list); |
| 4414 | return rtn_tuple; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4415 | } |
| 4416 | |
| 4417 | |
| 4418 | /* Python interface to gethostbyname_ex(name). */ |
| 4419 | |
| 4420 | /*ARGSUSED*/ |
| 4421 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4422 | socket_gethostbyname_ex(PyObject *self, PyObject *args) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4423 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4424 | char *name; |
| 4425 | struct hostent *h; |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4426 | sock_addr_t addr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4427 | struct sockaddr *sa; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4428 | PyObject *ret = NULL; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4429 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4430 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4431 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4432 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4433 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4434 | char buf[16384]; |
| 4435 | int buf_len = (sizeof buf) - 1; |
| 4436 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4437 | #endif |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4438 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4439 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4440 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4441 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4442 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4443 | if (!PyArg_ParseTuple(args, "et:gethostbyname_ex", "idna", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4444 | return NULL; |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4445 | if (setipaddr(name, SAS2SA(&addr), sizeof(addr), AF_INET) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4446 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4447 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4448 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4449 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4450 | gethostbyname_r(name, &hp_allocated, buf, buf_len, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4451 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4452 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4453 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4454 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4455 | memset((void *) &data, '\0', sizeof(data)); |
| 4456 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 4457 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4458 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4459 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4460 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4461 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4462 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4463 | h = gethostbyname(name); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4464 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4465 | Py_END_ALLOW_THREADS |
| 4466 | /* Some C libraries would require addr.__ss_family instead of |
| 4467 | addr.ss_family. |
| 4468 | Therefore, we cast the sockaddr_storage into sockaddr to |
| 4469 | access sa_family. */ |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4470 | sa = SAS2SA(&addr); |
| 4471 | ret = gethost_common(h, SAS2SA(&addr), sizeof(addr), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4472 | sa->sa_family); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4473 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4474 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 4475 | #endif |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4476 | finally: |
| 4477 | PyMem_Free(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4478 | return ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4481 | PyDoc_STRVAR(ghbn_ex_doc, |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4482 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
| 4483 | \n\ |
| 4484 | 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] | 4485 | 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] | 4486 | |
| 4487 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4488 | /* Python interface to gethostbyaddr(IP). */ |
| 4489 | |
| 4490 | /*ARGSUSED*/ |
| 4491 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4492 | socket_gethostbyaddr(PyObject *self, PyObject *args) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4493 | { |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4494 | sock_addr_t addr; |
| 4495 | struct sockaddr *sa = SAS2SA(&addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4496 | char *ip_num; |
| 4497 | struct hostent *h; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4498 | PyObject *ret = NULL; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4499 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4500 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4501 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4502 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4503 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4504 | /* glibcs up to 2.10 assume that the buf argument to |
| 4505 | gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc |
| 4506 | does not ensure. The attribute below instructs the compiler |
| 4507 | to maintain this alignment. */ |
| 4508 | char buf[16384] Py_ALIGNED(8); |
| 4509 | int buf_len = (sizeof buf) - 1; |
| 4510 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4511 | #endif |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4512 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4513 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4514 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4515 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4516 | char *ap; |
| 4517 | int al; |
| 4518 | int af; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4519 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4520 | if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4521 | return NULL; |
| 4522 | af = AF_UNSPEC; |
| 4523 | if (setipaddr(ip_num, sa, sizeof(addr), af) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4524 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4525 | af = sa->sa_family; |
| 4526 | ap = NULL; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 4527 | /* al = 0; */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4528 | switch (af) { |
| 4529 | case AF_INET: |
| 4530 | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr; |
| 4531 | al = sizeof(((struct sockaddr_in *)sa)->sin_addr); |
| 4532 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4533 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4534 | case AF_INET6: |
| 4535 | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr; |
| 4536 | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); |
| 4537 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4538 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4539 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4540 | PyErr_SetString(PyExc_OSError, "unsupported address family"); |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4541 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4542 | } |
| 4543 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +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 | gethostbyaddr_r(ap, al, af, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4547 | &hp_allocated, buf, buf_len, |
| 4548 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4549 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4550 | h = gethostbyaddr_r(ap, al, af, |
| 4551 | &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4552 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4553 | memset((void *) &data, '\0', sizeof(data)); |
| 4554 | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); |
| 4555 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4556 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4557 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4558 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4559 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4560 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4561 | h = gethostbyaddr(ap, al, af); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4562 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4563 | Py_END_ALLOW_THREADS |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4564 | ret = gethost_common(h, SAS2SA(&addr), sizeof(addr), af); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4565 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4566 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4567 | #endif |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4568 | finally: |
| 4569 | PyMem_Free(ip_num); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4570 | return ret; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4571 | } |
| 4572 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4573 | PyDoc_STRVAR(gethostbyaddr_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4574 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
| 4575 | \n\ |
| 4576 | 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] | 4577 | 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] | 4578 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4579 | |
| 4580 | /* Python interface to getservbyname(name). |
| 4581 | This only returns the port number, since the other info is already |
| 4582 | known or not useful (like the list of aliases). */ |
| 4583 | |
| 4584 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4585 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4586 | socket_getservbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4587 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4588 | char *name, *proto=NULL; |
| 4589 | struct servent *sp; |
| 4590 | if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto)) |
| 4591 | return NULL; |
| 4592 | Py_BEGIN_ALLOW_THREADS |
| 4593 | sp = getservbyname(name, proto); |
| 4594 | Py_END_ALLOW_THREADS |
| 4595 | if (sp == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4596 | PyErr_SetString(PyExc_OSError, "service/proto not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4597 | return NULL; |
| 4598 | } |
| 4599 | return PyLong_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4600 | } |
| 4601 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4602 | PyDoc_STRVAR(getservbyname_doc, |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4603 | "getservbyname(servicename[, protocolname]) -> integer\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4604 | \n\ |
| 4605 | Return a port number from a service name and protocol name.\n\ |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4606 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 4607 | otherwise any protocol will match."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4608 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4609 | |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4610 | /* Python interface to getservbyport(port). |
| 4611 | This only returns the service name, since the other info is already |
| 4612 | known or not useful (like the list of aliases). */ |
| 4613 | |
| 4614 | /*ARGSUSED*/ |
| 4615 | static PyObject * |
| 4616 | socket_getservbyport(PyObject *self, PyObject *args) |
| 4617 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4618 | int port; |
| 4619 | char *proto=NULL; |
| 4620 | struct servent *sp; |
| 4621 | if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto)) |
| 4622 | return NULL; |
| 4623 | if (port < 0 || port > 0xffff) { |
| 4624 | PyErr_SetString( |
| 4625 | PyExc_OverflowError, |
| 4626 | "getservbyport: port must be 0-65535."); |
| 4627 | return NULL; |
| 4628 | } |
| 4629 | Py_BEGIN_ALLOW_THREADS |
| 4630 | sp = getservbyport(htons((short)port), proto); |
| 4631 | Py_END_ALLOW_THREADS |
| 4632 | if (sp == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4633 | PyErr_SetString(PyExc_OSError, "port/proto not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4634 | return NULL; |
| 4635 | } |
| 4636 | return PyUnicode_FromString(sp->s_name); |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4637 | } |
| 4638 | |
| 4639 | PyDoc_STRVAR(getservbyport_doc, |
| 4640 | "getservbyport(port[, protocolname]) -> string\n\ |
| 4641 | \n\ |
| 4642 | Return the service name from a port number and protocol name.\n\ |
| 4643 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 4644 | otherwise any protocol will match."); |
| 4645 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4646 | /* Python interface to getprotobyname(name). |
| 4647 | This only returns the protocol number, since the other info is |
| 4648 | already known or not useful (like the list of aliases). */ |
| 4649 | |
| 4650 | /*ARGSUSED*/ |
| 4651 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4652 | socket_getprotobyname(PyObject *self, PyObject *args) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4653 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4654 | char *name; |
| 4655 | struct protoent *sp; |
| 4656 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) |
| 4657 | return NULL; |
| 4658 | Py_BEGIN_ALLOW_THREADS |
| 4659 | sp = getprotobyname(name); |
| 4660 | Py_END_ALLOW_THREADS |
| 4661 | if (sp == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4662 | PyErr_SetString(PyExc_OSError, "protocol not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4663 | return NULL; |
| 4664 | } |
| 4665 | return PyLong_FromLong((long) sp->p_proto); |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4666 | } |
| 4667 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4668 | PyDoc_STRVAR(getprotobyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4669 | "getprotobyname(name) -> integer\n\ |
| 4670 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4671 | Return the protocol number for the named protocol. (Rarely used.)"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4672 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4673 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4674 | #ifndef NO_DUP |
| 4675 | /* dup() function for socket fds */ |
| 4676 | |
| 4677 | static PyObject * |
| 4678 | socket_dup(PyObject *self, PyObject *fdobj) |
| 4679 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4680 | SOCKET_T fd, newfd; |
| 4681 | PyObject *newfdobj; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4682 | #ifdef MS_WINDOWS |
| 4683 | WSAPROTOCOL_INFO info; |
| 4684 | #endif |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4685 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4686 | fd = PyLong_AsSocket_t(fdobj); |
| 4687 | if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
| 4688 | return NULL; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4689 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4690 | #ifdef MS_WINDOWS |
| 4691 | if (WSADuplicateSocket(fd, GetCurrentProcessId(), &info)) |
| 4692 | return set_error(); |
| 4693 | |
| 4694 | newfd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, |
| 4695 | FROM_PROTOCOL_INFO, |
| 4696 | &info, 0, WSA_FLAG_OVERLAPPED); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4697 | if (newfd == INVALID_SOCKET) |
| 4698 | return set_error(); |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4699 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4700 | if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) { |
| 4701 | closesocket(newfd); |
| 4702 | PyErr_SetFromWindowsErr(0); |
| 4703 | return NULL; |
| 4704 | } |
| 4705 | #else |
| 4706 | /* On UNIX, dup can be used to duplicate the file descriptor of a socket */ |
| 4707 | newfd = _Py_dup(fd); |
| 4708 | if (newfd == INVALID_SOCKET) |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 4709 | return NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4710 | #endif |
| 4711 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4712 | newfdobj = PyLong_FromSocket_t(newfd); |
| 4713 | if (newfdobj == NULL) |
| 4714 | SOCKETCLOSE(newfd); |
| 4715 | return newfdobj; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4716 | } |
| 4717 | |
| 4718 | PyDoc_STRVAR(dup_doc, |
| 4719 | "dup(integer) -> integer\n\ |
| 4720 | \n\ |
| 4721 | Duplicate an integer socket file descriptor. This is like os.dup(), but for\n\ |
| 4722 | sockets; on some platforms os.dup() won't work for socket file descriptors."); |
| 4723 | #endif |
| 4724 | |
| 4725 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4726 | #ifdef HAVE_SOCKETPAIR |
| 4727 | /* Create a pair of sockets using the socketpair() function. |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 4728 | Arguments as for socket() except the default family is AF_UNIX if |
Dave Cole | e8bbfe4 | 2004-08-26 00:51:16 +0000 | [diff] [blame] | 4729 | defined on the platform; otherwise, the default is AF_INET. */ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4730 | |
| 4731 | /*ARGSUSED*/ |
| 4732 | static PyObject * |
| 4733 | socket_socketpair(PyObject *self, PyObject *args) |
| 4734 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4735 | PySocketSockObject *s0 = NULL, *s1 = NULL; |
| 4736 | SOCKET_T sv[2]; |
| 4737 | int family, type = SOCK_STREAM, proto = 0; |
| 4738 | PyObject *res = NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4739 | #ifdef SOCK_CLOEXEC |
| 4740 | int *atomic_flag_works = &sock_cloexec_works; |
| 4741 | #else |
| 4742 | int *atomic_flag_works = NULL; |
| 4743 | #endif |
| 4744 | int ret; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4745 | |
| 4746 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4747 | family = AF_UNIX; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4748 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4749 | family = AF_INET; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4750 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4751 | if (!PyArg_ParseTuple(args, "|iii:socketpair", |
| 4752 | &family, &type, &proto)) |
| 4753 | return NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4754 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4755 | /* Create a pair of socket fds */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4756 | Py_BEGIN_ALLOW_THREADS |
| 4757 | #ifdef SOCK_CLOEXEC |
| 4758 | if (sock_cloexec_works != 0) { |
| 4759 | ret = socketpair(family, type | SOCK_CLOEXEC, proto, sv); |
| 4760 | if (sock_cloexec_works == -1) { |
| 4761 | if (ret >= 0) { |
| 4762 | sock_cloexec_works = 1; |
| 4763 | } |
| 4764 | else if (errno == EINVAL) { |
| 4765 | /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */ |
| 4766 | sock_cloexec_works = 0; |
| 4767 | ret = socketpair(family, type, proto, sv); |
| 4768 | } |
| 4769 | } |
| 4770 | } |
| 4771 | else |
| 4772 | #endif |
| 4773 | { |
| 4774 | ret = socketpair(family, type, proto, sv); |
| 4775 | } |
| 4776 | Py_END_ALLOW_THREADS |
| 4777 | |
| 4778 | if (ret < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4779 | return set_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4780 | |
| 4781 | if (_Py_set_inheritable(sv[0], 0, atomic_flag_works) < 0) |
| 4782 | goto finally; |
| 4783 | if (_Py_set_inheritable(sv[1], 0, atomic_flag_works) < 0) |
| 4784 | goto finally; |
| 4785 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4786 | s0 = new_sockobject(sv[0], family, type, proto); |
| 4787 | if (s0 == NULL) |
| 4788 | goto finally; |
| 4789 | s1 = new_sockobject(sv[1], family, type, proto); |
| 4790 | if (s1 == NULL) |
| 4791 | goto finally; |
| 4792 | res = PyTuple_Pack(2, s0, s1); |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4793 | |
| 4794 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4795 | if (res == NULL) { |
| 4796 | if (s0 == NULL) |
| 4797 | SOCKETCLOSE(sv[0]); |
| 4798 | if (s1 == NULL) |
| 4799 | SOCKETCLOSE(sv[1]); |
| 4800 | } |
| 4801 | Py_XDECREF(s0); |
| 4802 | Py_XDECREF(s1); |
| 4803 | return res; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4804 | } |
| 4805 | |
| 4806 | PyDoc_STRVAR(socketpair_doc, |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4807 | "socketpair([family[, type [, proto]]]) -> (socket object, socket object)\n\ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4808 | \n\ |
| 4809 | Create a pair of socket objects from the sockets returned by the platform\n\ |
| 4810 | socketpair() function.\n\ |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 4811 | 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] | 4812 | 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] | 4813 | |
| 4814 | #endif /* HAVE_SOCKETPAIR */ |
| 4815 | |
| 4816 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4817 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4818 | socket_ntohs(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4819 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4820 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4821 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4822 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { |
| 4823 | return NULL; |
| 4824 | } |
| 4825 | if (x1 < 0) { |
| 4826 | PyErr_SetString(PyExc_OverflowError, |
| 4827 | "can't convert negative number to unsigned long"); |
| 4828 | return NULL; |
| 4829 | } |
| 4830 | x2 = (unsigned int)ntohs((unsigned short)x1); |
| 4831 | return PyLong_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4832 | } |
| 4833 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4834 | PyDoc_STRVAR(ntohs_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4835 | "ntohs(integer) -> integer\n\ |
| 4836 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4837 | Convert a 16-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4838 | |
| 4839 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4840 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4841 | socket_ntohl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4842 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4843 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4844 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4845 | if (PyLong_Check(arg)) { |
| 4846 | x = PyLong_AsUnsignedLong(arg); |
| 4847 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 4848 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4849 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4850 | { |
| 4851 | unsigned long y; |
| 4852 | /* only want the trailing 32 bits */ |
| 4853 | y = x & 0xFFFFFFFFUL; |
| 4854 | if (y ^ x) |
| 4855 | return PyErr_Format(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4856 | "int larger than 32 bits"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4857 | x = y; |
| 4858 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4859 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4860 | } |
| 4861 | else |
| 4862 | return PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4863 | "expected int, %s found", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4864 | Py_TYPE(arg)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4865 | return PyLong_FromUnsignedLong(ntohl(x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4866 | } |
| 4867 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4868 | PyDoc_STRVAR(ntohl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4869 | "ntohl(integer) -> integer\n\ |
| 4870 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4871 | Convert a 32-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4872 | |
| 4873 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4874 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4875 | socket_htons(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4876 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4877 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4879 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) { |
| 4880 | return NULL; |
| 4881 | } |
| 4882 | if (x1 < 0) { |
| 4883 | PyErr_SetString(PyExc_OverflowError, |
| 4884 | "can't convert negative number to unsigned long"); |
| 4885 | return NULL; |
| 4886 | } |
| 4887 | x2 = (unsigned int)htons((unsigned short)x1); |
| 4888 | return PyLong_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4889 | } |
| 4890 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4891 | PyDoc_STRVAR(htons_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4892 | "htons(integer) -> integer\n\ |
| 4893 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4894 | Convert a 16-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4895 | |
| 4896 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4897 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4898 | socket_htonl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4899 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4900 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4901 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4902 | if (PyLong_Check(arg)) { |
| 4903 | x = PyLong_AsUnsignedLong(arg); |
| 4904 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 4905 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4906 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4907 | { |
| 4908 | unsigned long y; |
| 4909 | /* only want the trailing 32 bits */ |
| 4910 | y = x & 0xFFFFFFFFUL; |
| 4911 | if (y ^ x) |
| 4912 | return PyErr_Format(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4913 | "int larger than 32 bits"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4914 | x = y; |
| 4915 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4916 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4917 | } |
| 4918 | else |
| 4919 | return PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4920 | "expected int, %s found", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4921 | Py_TYPE(arg)->tp_name); |
| 4922 | return PyLong_FromUnsignedLong(htonl((unsigned long)x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4923 | } |
| 4924 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4925 | PyDoc_STRVAR(htonl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4926 | "htonl(integer) -> integer\n\ |
| 4927 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4928 | Convert a 32-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4929 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4930 | /* socket.inet_aton() and socket.inet_ntoa() functions. */ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4931 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4932 | PyDoc_STRVAR(inet_aton_doc, |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 4933 | "inet_aton(string) -> bytes giving packed 32-bit IP representation\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4934 | \n\ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 4935 | 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] | 4936 | binary format used in low-level network functions."); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4937 | |
| 4938 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4939 | socket_inet_aton(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4940 | { |
Neal Norwitz | 88f115b | 2003-02-13 02:15:42 +0000 | [diff] [blame] | 4941 | #ifdef HAVE_INET_ATON |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4942 | struct in_addr buf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4943 | #endif |
| 4944 | |
| 4945 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 4946 | #if (SIZEOF_INT != 4) |
| 4947 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 4948 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4949 | /* Have to use inet_addr() instead */ |
| 4950 | unsigned int packed_addr; |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 4951 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4952 | char *ip_addr; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 4953 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4954 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) |
| 4955 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 4956 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 4957 | |
| 4958 | #ifdef HAVE_INET_ATON |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4959 | |
| 4960 | #ifdef USE_INET_ATON_WEAKLINK |
| 4961 | if (inet_aton != NULL) { |
| 4962 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4963 | if (inet_aton(ip_addr, &buf)) |
| 4964 | return PyBytes_FromStringAndSize((char *)(&buf), |
| 4965 | sizeof(buf)); |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 4966 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4967 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4968 | "illegal IP address string passed to inet_aton"); |
| 4969 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 4970 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4971 | #ifdef USE_INET_ATON_WEAKLINK |
| 4972 | } else { |
| 4973 | #endif |
| 4974 | |
| 4975 | #endif |
| 4976 | |
| 4977 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
| 4978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4979 | /* special-case this address as inet_addr might return INADDR_NONE |
| 4980 | * for this */ |
| 4981 | if (strcmp(ip_addr, "255.255.255.255") == 0) { |
Charles-François Natali | 24aa041 | 2013-08-31 14:48:25 +0200 | [diff] [blame] | 4982 | packed_addr = INADDR_BROADCAST; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4983 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4984 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4985 | packed_addr = inet_addr(ip_addr); |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 4986 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4987 | if (packed_addr == INADDR_NONE) { /* invalid address */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4988 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4989 | "illegal IP address string passed to inet_aton"); |
| 4990 | return NULL; |
| 4991 | } |
| 4992 | } |
| 4993 | return PyBytes_FromStringAndSize((char *) &packed_addr, |
| 4994 | sizeof(packed_addr)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4995 | |
| 4996 | #ifdef USE_INET_ATON_WEAKLINK |
| 4997 | } |
| 4998 | #endif |
| 4999 | |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 5000 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5001 | } |
| 5002 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5003 | PyDoc_STRVAR(inet_ntoa_doc, |
Fred Drake | e066134 | 2000-03-07 14:05:16 +0000 | [diff] [blame] | 5004 | "inet_ntoa(packed_ip) -> ip_address_string\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5005 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5006 | 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] | 5007 | |
| 5008 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5009 | socket_inet_ntoa(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5010 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5011 | char *packed_str; |
| 5012 | int addr_len; |
| 5013 | struct in_addr packed_addr; |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5014 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5015 | if (!PyArg_ParseTuple(args, "y#:inet_ntoa", &packed_str, &addr_len)) { |
| 5016 | return NULL; |
| 5017 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 5018 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5019 | if (addr_len != sizeof(packed_addr)) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5020 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5021 | "packed IP wrong length for inet_ntoa"); |
| 5022 | return NULL; |
| 5023 | } |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5024 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5025 | memcpy(&packed_addr, packed_str, addr_len); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5026 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5027 | return PyUnicode_FromString(inet_ntoa(packed_addr)); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5028 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5029 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5030 | #if defined(HAVE_INET_PTON) || defined(MS_WINDOWS) |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5031 | |
| 5032 | PyDoc_STRVAR(inet_pton_doc, |
| 5033 | "inet_pton(af, ip) -> packed IP address string\n\ |
| 5034 | \n\ |
| 5035 | Convert an IP address from string format to a packed string suitable\n\ |
| 5036 | for use with low-level network functions."); |
| 5037 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5038 | #endif |
| 5039 | |
| 5040 | #ifdef HAVE_INET_PTON |
| 5041 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5042 | static PyObject * |
| 5043 | socket_inet_pton(PyObject *self, PyObject *args) |
| 5044 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5045 | int af; |
| 5046 | char* ip; |
| 5047 | int retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5048 | #ifdef ENABLE_IPV6 |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 5049 | 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] | 5050 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5051 | char packed[sizeof(struct in_addr)]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5052 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5053 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 5054 | return NULL; |
| 5055 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5056 | |
Martin v. Löwis | 04697e8 | 2004-06-02 12:35:29 +0000 | [diff] [blame] | 5057 | #if !defined(ENABLE_IPV6) && defined(AF_INET6) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5058 | if(af == AF_INET6) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5059 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5060 | "can't use AF_INET6, IPv6 is disabled"); |
| 5061 | return NULL; |
| 5062 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5063 | #endif |
Martin v. Löwis | 1064909 | 2003-08-05 06:25:06 +0000 | [diff] [blame] | 5064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5065 | retval = inet_pton(af, ip, packed); |
| 5066 | if (retval < 0) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5067 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5068 | return NULL; |
| 5069 | } else if (retval == 0) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5070 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5071 | "illegal IP address string passed to inet_pton"); |
| 5072 | return NULL; |
| 5073 | } else if (af == AF_INET) { |
| 5074 | return PyBytes_FromStringAndSize(packed, |
| 5075 | sizeof(struct in_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5076 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5077 | } else if (af == AF_INET6) { |
| 5078 | return PyBytes_FromStringAndSize(packed, |
| 5079 | sizeof(struct in6_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5080 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5081 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5082 | PyErr_SetString(PyExc_OSError, "unknown address family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5083 | return NULL; |
| 5084 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5085 | } |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5086 | #elif defined(MS_WINDOWS) |
| 5087 | |
| 5088 | static PyObject * |
| 5089 | socket_inet_pton(PyObject *self, PyObject *args) |
| 5090 | { |
| 5091 | int af; |
| 5092 | char* ip; |
| 5093 | struct sockaddr_in6 addr; |
| 5094 | INT ret, size; |
| 5095 | |
| 5096 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 5097 | return NULL; |
| 5098 | } |
| 5099 | |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5100 | size = sizeof(addr); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5101 | ret = WSAStringToAddressA(ip, af, NULL, (LPSOCKADDR)&addr, &size); |
| 5102 | |
| 5103 | if (ret) { |
| 5104 | PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError()); |
| 5105 | return NULL; |
| 5106 | } else if(af == AF_INET) { |
| 5107 | struct sockaddr_in *addr4 = (struct sockaddr_in*)&addr; |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5108 | return PyBytes_FromStringAndSize((const char *)&(addr4->sin_addr), |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5109 | sizeof(addr4->sin_addr)); |
| 5110 | } else if (af == AF_INET6) { |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5111 | return PyBytes_FromStringAndSize((const char *)&(addr.sin6_addr), |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5112 | sizeof(addr.sin6_addr)); |
| 5113 | } else { |
| 5114 | PyErr_SetString(PyExc_OSError, "unknown address family"); |
| 5115 | return NULL; |
| 5116 | } |
| 5117 | } |
| 5118 | |
| 5119 | #endif |
| 5120 | |
| 5121 | #if defined(HAVE_INET_PTON) || defined(MS_WINDOWS) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5122 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5123 | PyDoc_STRVAR(inet_ntop_doc, |
| 5124 | "inet_ntop(af, packed_ip) -> string formatted IP address\n\ |
| 5125 | \n\ |
| 5126 | Convert a packed IP address of the given family to string format."); |
| 5127 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5128 | #endif |
| 5129 | |
| 5130 | |
| 5131 | #ifdef HAVE_INET_PTON |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5132 | static PyObject * |
| 5133 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 5134 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5135 | int af; |
| 5136 | char* packed; |
| 5137 | int len; |
| 5138 | const char* retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5139 | #ifdef ENABLE_IPV6 |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 5140 | char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5141 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5142 | char ip[INET_ADDRSTRLEN + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5143 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5145 | /* Guarantee NUL-termination for PyUnicode_FromString() below */ |
| 5146 | memset((void *) &ip[0], '\0', sizeof(ip)); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5148 | if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) { |
| 5149 | return NULL; |
| 5150 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5152 | if (af == AF_INET) { |
| 5153 | if (len != sizeof(struct in_addr)) { |
| 5154 | PyErr_SetString(PyExc_ValueError, |
| 5155 | "invalid length of packed IP address string"); |
| 5156 | return NULL; |
| 5157 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5158 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5159 | } else if (af == AF_INET6) { |
| 5160 | if (len != sizeof(struct in6_addr)) { |
| 5161 | PyErr_SetString(PyExc_ValueError, |
| 5162 | "invalid length of packed IP address string"); |
| 5163 | return NULL; |
| 5164 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5165 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5166 | } else { |
| 5167 | PyErr_Format(PyExc_ValueError, |
| 5168 | "unknown address family %d", af); |
| 5169 | return NULL; |
| 5170 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5172 | retval = inet_ntop(af, packed, ip, sizeof(ip)); |
| 5173 | if (!retval) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5174 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5175 | return NULL; |
| 5176 | } else { |
| 5177 | return PyUnicode_FromString(retval); |
| 5178 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5179 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5180 | /* NOTREACHED */ |
| 5181 | PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop"); |
| 5182 | return NULL; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5183 | } |
| 5184 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5185 | #elif defined(MS_WINDOWS) |
| 5186 | |
| 5187 | static PyObject * |
| 5188 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 5189 | { |
| 5190 | int af; |
| 5191 | char* packed; |
| 5192 | int len; |
| 5193 | struct sockaddr_in6 addr; |
| 5194 | DWORD addrlen, ret, retlen; |
Jason R. Coombs | 8ec784c | 2013-11-10 13:43:22 -0500 | [diff] [blame] | 5195 | #ifdef ENABLE_IPV6 |
| 5196 | char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
| 5197 | #else |
| 5198 | char ip[INET_ADDRSTRLEN + 1]; |
| 5199 | #endif |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5200 | |
| 5201 | /* Guarantee NUL-termination for PyUnicode_FromString() below */ |
| 5202 | memset((void *) &ip[0], '\0', sizeof(ip)); |
| 5203 | |
| 5204 | if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) { |
| 5205 | return NULL; |
| 5206 | } |
| 5207 | |
| 5208 | if (af == AF_INET) { |
| 5209 | struct sockaddr_in * addr4 = (struct sockaddr_in *)&addr; |
| 5210 | |
| 5211 | if (len != sizeof(struct in_addr)) { |
| 5212 | PyErr_SetString(PyExc_ValueError, |
| 5213 | "invalid length of packed IP address string"); |
| 5214 | return NULL; |
| 5215 | } |
| 5216 | memset(addr4, 0, sizeof(struct sockaddr_in)); |
| 5217 | addr4->sin_family = AF_INET; |
| 5218 | memcpy(&(addr4->sin_addr), packed, sizeof(addr4->sin_addr)); |
| 5219 | addrlen = sizeof(struct sockaddr_in); |
| 5220 | } else if (af == AF_INET6) { |
| 5221 | if (len != sizeof(struct in6_addr)) { |
| 5222 | PyErr_SetString(PyExc_ValueError, |
| 5223 | "invalid length of packed IP address string"); |
| 5224 | return NULL; |
| 5225 | } |
| 5226 | |
| 5227 | memset(&addr, 0, sizeof(addr)); |
| 5228 | addr.sin6_family = AF_INET6; |
| 5229 | memcpy(&(addr.sin6_addr), packed, sizeof(addr.sin6_addr)); |
| 5230 | addrlen = sizeof(addr); |
| 5231 | } else { |
| 5232 | PyErr_Format(PyExc_ValueError, |
| 5233 | "unknown address family %d", af); |
| 5234 | return NULL; |
| 5235 | } |
| 5236 | |
| 5237 | retlen = sizeof(ip); |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5238 | ret = WSAAddressToStringA((struct sockaddr*)&addr, addrlen, NULL, |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5239 | ip, &retlen); |
| 5240 | |
| 5241 | if (ret) { |
| 5242 | PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError()); |
| 5243 | return NULL; |
| 5244 | } else { |
| 5245 | return PyUnicode_FromString(ip); |
| 5246 | } |
| 5247 | } |
| 5248 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5249 | #endif /* HAVE_INET_PTON */ |
| 5250 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5251 | /* Python interface to getaddrinfo(host, port). */ |
| 5252 | |
| 5253 | /*ARGSUSED*/ |
| 5254 | static PyObject * |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5255 | socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5256 | { |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 5257 | static char* kwnames[] = {"host", "port", "family", "type", "proto", |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5258 | "flags", 0}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5259 | struct addrinfo hints, *res; |
| 5260 | struct addrinfo *res0 = NULL; |
| 5261 | PyObject *hobj = NULL; |
| 5262 | PyObject *pobj = (PyObject *)NULL; |
| 5263 | char pbuf[30]; |
| 5264 | char *hptr, *pptr; |
| 5265 | int family, socktype, protocol, flags; |
| 5266 | int error; |
| 5267 | PyObject *all = (PyObject *)NULL; |
| 5268 | PyObject *idna = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5269 | |
Georg Brandl | 6083a4b | 2013-10-14 06:51:46 +0200 | [diff] [blame] | 5270 | socktype = protocol = flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5271 | family = AF_UNSPEC; |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 5272 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iiii:getaddrinfo", |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5273 | kwnames, &hobj, &pobj, &family, &socktype, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5274 | &protocol, &flags)) { |
| 5275 | return NULL; |
| 5276 | } |
| 5277 | if (hobj == Py_None) { |
| 5278 | hptr = NULL; |
| 5279 | } else if (PyUnicode_Check(hobj)) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5280 | _Py_IDENTIFIER(encode); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 5281 | |
| 5282 | idna = _PyObject_CallMethodId(hobj, &PyId_encode, "s", "idna"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5283 | if (!idna) |
| 5284 | return NULL; |
| 5285 | assert(PyBytes_Check(idna)); |
| 5286 | hptr = PyBytes_AS_STRING(idna); |
| 5287 | } else if (PyBytes_Check(hobj)) { |
| 5288 | hptr = PyBytes_AsString(hobj); |
| 5289 | } else { |
| 5290 | PyErr_SetString(PyExc_TypeError, |
| 5291 | "getaddrinfo() argument 1 must be string or None"); |
| 5292 | return NULL; |
| 5293 | } |
| 5294 | if (PyLong_CheckExact(pobj)) { |
| 5295 | long value = PyLong_AsLong(pobj); |
| 5296 | if (value == -1 && PyErr_Occurred()) |
| 5297 | goto err; |
| 5298 | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value); |
| 5299 | pptr = pbuf; |
| 5300 | } else if (PyUnicode_Check(pobj)) { |
| 5301 | pptr = _PyUnicode_AsString(pobj); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 5302 | if (pptr == NULL) |
| 5303 | goto err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5304 | } else if (PyBytes_Check(pobj)) { |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 5305 | pptr = PyBytes_AS_STRING(pobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5306 | } else if (pobj == Py_None) { |
| 5307 | pptr = (char *)NULL; |
| 5308 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5309 | PyErr_SetString(PyExc_OSError, "Int or String expected"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5310 | goto err; |
| 5311 | } |
Ronald Oussoren | a822d366 | 2013-06-10 10:36:28 +0200 | [diff] [blame] | 5312 | #if defined(__APPLE__) && defined(AI_NUMERICSERV) |
Ronald Oussoren | 27a4ac5 | 2013-05-24 13:47:37 +0200 | [diff] [blame] | 5313 | if ((flags & AI_NUMERICSERV) && (pptr == NULL || (pptr[0] == '0' && pptr[1] == 0))) { |
| 5314 | /* On OSX upto at least OSX 10.8 getaddrinfo crashes |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 5315 | * if AI_NUMERICSERV is set and the servname is NULL or "0". |
| 5316 | * This workaround avoids a segfault in libsystem. |
| 5317 | */ |
Ronald Oussoren | 27a4ac5 | 2013-05-24 13:47:37 +0200 | [diff] [blame] | 5318 | pptr = "00"; |
| 5319 | } |
| 5320 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5321 | memset(&hints, 0, sizeof(hints)); |
| 5322 | hints.ai_family = family; |
| 5323 | hints.ai_socktype = socktype; |
| 5324 | hints.ai_protocol = protocol; |
| 5325 | hints.ai_flags = flags; |
| 5326 | Py_BEGIN_ALLOW_THREADS |
| 5327 | ACQUIRE_GETADDRINFO_LOCK |
| 5328 | error = getaddrinfo(hptr, pptr, &hints, &res0); |
| 5329 | Py_END_ALLOW_THREADS |
| 5330 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 5331 | if (error) { |
| 5332 | set_gaierror(error); |
| 5333 | goto err; |
| 5334 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5336 | if ((all = PyList_New(0)) == NULL) |
| 5337 | goto err; |
| 5338 | for (res = res0; res; res = res->ai_next) { |
| 5339 | PyObject *single; |
| 5340 | PyObject *addr = |
| 5341 | makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol); |
| 5342 | if (addr == NULL) |
| 5343 | goto err; |
| 5344 | single = Py_BuildValue("iiisO", res->ai_family, |
| 5345 | res->ai_socktype, res->ai_protocol, |
| 5346 | res->ai_canonname ? res->ai_canonname : "", |
| 5347 | addr); |
| 5348 | Py_DECREF(addr); |
| 5349 | if (single == NULL) |
| 5350 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5351 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5352 | if (PyList_Append(all, single)) |
| 5353 | goto err; |
| 5354 | Py_XDECREF(single); |
| 5355 | } |
| 5356 | Py_XDECREF(idna); |
| 5357 | if (res0) |
| 5358 | freeaddrinfo(res0); |
| 5359 | return all; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5360 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5361 | Py_XDECREF(all); |
| 5362 | Py_XDECREF(idna); |
| 5363 | if (res0) |
| 5364 | freeaddrinfo(res0); |
| 5365 | return (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5366 | } |
| 5367 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5368 | PyDoc_STRVAR(getaddrinfo_doc, |
Berker Peksag | a6ec5ee | 2014-06-30 11:28:40 +0300 | [diff] [blame] | 5369 | "getaddrinfo(host, port [, family, type, proto, flags])\n\ |
| 5370 | -> list of (family, type, proto, canonname, sockaddr)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5371 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5372 | Resolve host and port into addrinfo struct."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5373 | |
| 5374 | /* Python interface to getnameinfo(sa, flags). */ |
| 5375 | |
| 5376 | /*ARGSUSED*/ |
| 5377 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5378 | socket_getnameinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5379 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5380 | PyObject *sa = (PyObject *)NULL; |
| 5381 | int flags; |
| 5382 | char *hostp; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5383 | int port; |
| 5384 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5385 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; |
| 5386 | struct addrinfo hints, *res = NULL; |
| 5387 | int error; |
| 5388 | PyObject *ret = (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5390 | flags = flowinfo = scope_id = 0; |
| 5391 | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) |
| 5392 | return NULL; |
| 5393 | if (!PyTuple_Check(sa)) { |
| 5394 | PyErr_SetString(PyExc_TypeError, |
| 5395 | "getnameinfo() argument 1 must be a tuple"); |
| 5396 | return NULL; |
| 5397 | } |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5398 | if (!PyArg_ParseTuple(sa, "si|II", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5399 | &hostp, &port, &flowinfo, &scope_id)) |
| 5400 | return NULL; |
Charles-François Natali | 9b0c006 | 2012-06-23 10:17:05 +0200 | [diff] [blame] | 5401 | if (flowinfo > 0xfffff) { |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5402 | PyErr_SetString(PyExc_OverflowError, |
| 5403 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 5404 | return NULL; |
| 5405 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5406 | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); |
| 5407 | memset(&hints, 0, sizeof(hints)); |
| 5408 | hints.ai_family = AF_UNSPEC; |
| 5409 | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ |
Martin v. Löwis | 112c0f3 | 2010-08-25 07:38:15 +0000 | [diff] [blame] | 5410 | hints.ai_flags = AI_NUMERICHOST; /* don't do any name resolution */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5411 | Py_BEGIN_ALLOW_THREADS |
| 5412 | ACQUIRE_GETADDRINFO_LOCK |
| 5413 | error = getaddrinfo(hostp, pbuf, &hints, &res); |
| 5414 | Py_END_ALLOW_THREADS |
| 5415 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 5416 | if (error) { |
| 5417 | set_gaierror(error); |
| 5418 | goto fail; |
| 5419 | } |
| 5420 | if (res->ai_next) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5421 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5422 | "sockaddr resolved to multiple addresses"); |
| 5423 | goto fail; |
| 5424 | } |
| 5425 | switch (res->ai_family) { |
| 5426 | case AF_INET: |
| 5427 | { |
| 5428 | if (PyTuple_GET_SIZE(sa) != 2) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5429 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5430 | "IPv4 sockaddr must be 2 tuple"); |
| 5431 | goto fail; |
| 5432 | } |
| 5433 | break; |
| 5434 | } |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 5435 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5436 | case AF_INET6: |
| 5437 | { |
| 5438 | struct sockaddr_in6 *sin6; |
| 5439 | sin6 = (struct sockaddr_in6 *)res->ai_addr; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5440 | sin6->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5441 | sin6->sin6_scope_id = scope_id; |
| 5442 | break; |
| 5443 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5444 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5445 | } |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 5446 | error = getnameinfo(res->ai_addr, (socklen_t) res->ai_addrlen, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5447 | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); |
| 5448 | if (error) { |
| 5449 | set_gaierror(error); |
| 5450 | goto fail; |
| 5451 | } |
| 5452 | ret = Py_BuildValue("ss", hbuf, pbuf); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5453 | |
| 5454 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5455 | if (res) |
| 5456 | freeaddrinfo(res); |
| 5457 | return ret; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5458 | } |
| 5459 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5460 | PyDoc_STRVAR(getnameinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5461 | "getnameinfo(sockaddr, flags) --> (host, port)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5462 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5463 | Get host and port for a sockaddr."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5464 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5465 | |
| 5466 | /* Python API to getting and setting the default timeout value. */ |
| 5467 | |
| 5468 | static PyObject * |
| 5469 | socket_getdefaulttimeout(PyObject *self) |
| 5470 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5471 | if (defaulttimeout < 0.0) { |
| 5472 | Py_INCREF(Py_None); |
| 5473 | return Py_None; |
| 5474 | } |
| 5475 | else |
| 5476 | return PyFloat_FromDouble(defaulttimeout); |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5477 | } |
| 5478 | |
| 5479 | PyDoc_STRVAR(getdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5480 | "getdefaulttimeout() -> timeout\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5481 | \n\ |
Ezio Melotti | 388c945 | 2011-08-14 08:28:57 +0300 | [diff] [blame] | 5482 | 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] | 5483 | A value of None indicates that new socket objects have no timeout.\n\ |
| 5484 | When the socket module is first imported, the default is None."); |
| 5485 | |
| 5486 | static PyObject * |
| 5487 | socket_setdefaulttimeout(PyObject *self, PyObject *arg) |
| 5488 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5489 | double timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5491 | if (arg == Py_None) |
| 5492 | timeout = -1.0; |
| 5493 | else { |
| 5494 | timeout = PyFloat_AsDouble(arg); |
| 5495 | if (timeout < 0.0) { |
| 5496 | if (!PyErr_Occurred()) |
| 5497 | PyErr_SetString(PyExc_ValueError, |
| 5498 | "Timeout value out of range"); |
| 5499 | return NULL; |
| 5500 | } |
| 5501 | } |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5503 | defaulttimeout = timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5505 | Py_INCREF(Py_None); |
| 5506 | return Py_None; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5507 | } |
| 5508 | |
| 5509 | PyDoc_STRVAR(setdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5510 | "setdefaulttimeout(timeout)\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5511 | \n\ |
Ezio Melotti | 388c945 | 2011-08-14 08:28:57 +0300 | [diff] [blame] | 5512 | 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] | 5513 | A value of None indicates that new socket objects have no timeout.\n\ |
| 5514 | When the socket module is first imported, the default is None."); |
| 5515 | |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5516 | #ifdef HAVE_IF_NAMEINDEX |
| 5517 | /* Python API for getting interface indices and names */ |
| 5518 | |
| 5519 | static PyObject * |
| 5520 | socket_if_nameindex(PyObject *self, PyObject *arg) |
| 5521 | { |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5522 | PyObject *list; |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5523 | int i; |
| 5524 | struct if_nameindex *ni; |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 5525 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5526 | ni = if_nameindex(); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5527 | if (ni == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5528 | PyErr_SetFromErrno(PyExc_OSError); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5529 | return NULL; |
| 5530 | } |
| 5531 | |
| 5532 | list = PyList_New(0); |
| 5533 | if (list == NULL) { |
| 5534 | if_freenameindex(ni); |
| 5535 | return NULL; |
| 5536 | } |
| 5537 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5538 | for (i = 0; ni[i].if_index != 0 && i < INT_MAX; i++) { |
| 5539 | PyObject *ni_tuple = Py_BuildValue("IO&", |
| 5540 | ni[i].if_index, PyUnicode_DecodeFSDefault, ni[i].if_name); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5541 | |
| 5542 | if (ni_tuple == NULL || PyList_Append(list, ni_tuple) == -1) { |
| 5543 | Py_XDECREF(ni_tuple); |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5544 | Py_DECREF(list); |
| 5545 | if_freenameindex(ni); |
| 5546 | return NULL; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5547 | } |
| 5548 | Py_DECREF(ni_tuple); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5549 | } |
| 5550 | |
| 5551 | if_freenameindex(ni); |
| 5552 | return list; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5553 | } |
| 5554 | |
| 5555 | PyDoc_STRVAR(if_nameindex_doc, |
| 5556 | "if_nameindex()\n\ |
| 5557 | \n\ |
| 5558 | Returns a list of network interface information (index, name) tuples."); |
| 5559 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5560 | static PyObject * |
| 5561 | socket_if_nametoindex(PyObject *self, PyObject *args) |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5562 | { |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5563 | PyObject *oname; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5564 | unsigned long index; |
| 5565 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5566 | if (!PyArg_ParseTuple(args, "O&:if_nametoindex", |
| 5567 | PyUnicode_FSConverter, &oname)) |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5568 | return NULL; |
| 5569 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5570 | index = if_nametoindex(PyBytes_AS_STRING(oname)); |
| 5571 | Py_DECREF(oname); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5572 | if (index == 0) { |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5573 | /* if_nametoindex() doesn't set errno */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5574 | PyErr_SetString(PyExc_OSError, "no interface with this name"); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5575 | return NULL; |
| 5576 | } |
| 5577 | |
| 5578 | return PyLong_FromUnsignedLong(index); |
| 5579 | } |
| 5580 | |
| 5581 | PyDoc_STRVAR(if_nametoindex_doc, |
| 5582 | "if_nametoindex(if_name)\n\ |
| 5583 | \n\ |
| 5584 | Returns the interface index corresponding to the interface name if_name."); |
| 5585 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5586 | static PyObject * |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5587 | socket_if_indextoname(PyObject *self, PyObject *arg) |
| 5588 | { |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5589 | unsigned long index; |
| 5590 | char name[IF_NAMESIZE + 1]; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5591 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5592 | index = PyLong_AsUnsignedLong(arg); |
| 5593 | if (index == (unsigned long) -1) |
| 5594 | return NULL; |
| 5595 | |
| 5596 | if (if_indextoname(index, name) == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5597 | PyErr_SetFromErrno(PyExc_OSError); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5598 | return NULL; |
| 5599 | } |
| 5600 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5601 | return PyUnicode_DecodeFSDefault(name); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5602 | } |
| 5603 | |
| 5604 | PyDoc_STRVAR(if_indextoname_doc, |
| 5605 | "if_indextoname(if_index)\n\ |
| 5606 | \n\ |
| 5607 | Returns the interface name corresponding to the interface index if_index."); |
| 5608 | |
| 5609 | #endif /* HAVE_IF_NAMEINDEX */ |
| 5610 | |
| 5611 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 5612 | #ifdef CMSG_LEN |
| 5613 | /* Python interface to CMSG_LEN(length). */ |
| 5614 | |
| 5615 | static PyObject * |
| 5616 | socket_CMSG_LEN(PyObject *self, PyObject *args) |
| 5617 | { |
| 5618 | Py_ssize_t length; |
| 5619 | size_t result; |
| 5620 | |
| 5621 | if (!PyArg_ParseTuple(args, "n:CMSG_LEN", &length)) |
| 5622 | return NULL; |
| 5623 | if (length < 0 || !get_CMSG_LEN(length, &result)) { |
| 5624 | PyErr_Format(PyExc_OverflowError, "CMSG_LEN() argument out of range"); |
| 5625 | return NULL; |
| 5626 | } |
| 5627 | return PyLong_FromSize_t(result); |
| 5628 | } |
| 5629 | |
| 5630 | PyDoc_STRVAR(CMSG_LEN_doc, |
| 5631 | "CMSG_LEN(length) -> control message length\n\ |
| 5632 | \n\ |
| 5633 | Return the total length, without trailing padding, of an ancillary\n\ |
| 5634 | data item with associated data of the given length. This value can\n\ |
| 5635 | often be used as the buffer size for recvmsg() to receive a single\n\ |
| 5636 | item of ancillary data, but RFC 3542 requires portable applications to\n\ |
| 5637 | use CMSG_SPACE() and thus include space for padding, even when the\n\ |
| 5638 | item will be the last in the buffer. Raises OverflowError if length\n\ |
| 5639 | is outside the permissible range of values."); |
| 5640 | |
| 5641 | |
| 5642 | #ifdef CMSG_SPACE |
| 5643 | /* Python interface to CMSG_SPACE(length). */ |
| 5644 | |
| 5645 | static PyObject * |
| 5646 | socket_CMSG_SPACE(PyObject *self, PyObject *args) |
| 5647 | { |
| 5648 | Py_ssize_t length; |
| 5649 | size_t result; |
| 5650 | |
| 5651 | if (!PyArg_ParseTuple(args, "n:CMSG_SPACE", &length)) |
| 5652 | return NULL; |
| 5653 | if (length < 0 || !get_CMSG_SPACE(length, &result)) { |
| 5654 | PyErr_SetString(PyExc_OverflowError, |
| 5655 | "CMSG_SPACE() argument out of range"); |
| 5656 | return NULL; |
| 5657 | } |
| 5658 | return PyLong_FromSize_t(result); |
| 5659 | } |
| 5660 | |
| 5661 | PyDoc_STRVAR(CMSG_SPACE_doc, |
| 5662 | "CMSG_SPACE(length) -> buffer size\n\ |
| 5663 | \n\ |
| 5664 | Return the buffer size needed for recvmsg() to receive an ancillary\n\ |
| 5665 | data item with associated data of the given length, along with any\n\ |
| 5666 | trailing padding. The buffer space needed to receive multiple items\n\ |
| 5667 | is the sum of the CMSG_SPACE() values for their associated data\n\ |
| 5668 | lengths. Raises OverflowError if length is outside the permissible\n\ |
| 5669 | range of values."); |
| 5670 | #endif /* CMSG_SPACE */ |
| 5671 | #endif /* CMSG_LEN */ |
| 5672 | |
| 5673 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 5674 | /* List of functions exported by this module. */ |
| 5675 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5676 | static PyMethodDef socket_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5677 | {"gethostbyname", socket_gethostbyname, |
| 5678 | METH_VARARGS, gethostbyname_doc}, |
| 5679 | {"gethostbyname_ex", socket_gethostbyname_ex, |
| 5680 | METH_VARARGS, ghbn_ex_doc}, |
| 5681 | {"gethostbyaddr", socket_gethostbyaddr, |
| 5682 | METH_VARARGS, gethostbyaddr_doc}, |
| 5683 | {"gethostname", socket_gethostname, |
| 5684 | METH_NOARGS, gethostname_doc}, |
Antoine Pitrou | 061cfb5 | 2011-02-28 22:25:22 +0000 | [diff] [blame] | 5685 | #ifdef HAVE_SETHOSTNAME |
| 5686 | {"sethostname", socket_sethostname, |
| 5687 | METH_VARARGS, sethostname_doc}, |
| 5688 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5689 | {"getservbyname", socket_getservbyname, |
| 5690 | METH_VARARGS, getservbyname_doc}, |
| 5691 | {"getservbyport", socket_getservbyport, |
| 5692 | METH_VARARGS, getservbyport_doc}, |
| 5693 | {"getprotobyname", socket_getprotobyname, |
| 5694 | METH_VARARGS, getprotobyname_doc}, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 5695 | #ifndef NO_DUP |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5696 | {"dup", socket_dup, |
| 5697 | METH_O, dup_doc}, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 5698 | #endif |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 5699 | #ifdef HAVE_SOCKETPAIR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5700 | {"socketpair", socket_socketpair, |
| 5701 | METH_VARARGS, socketpair_doc}, |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 5702 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5703 | {"ntohs", socket_ntohs, |
| 5704 | METH_VARARGS, ntohs_doc}, |
| 5705 | {"ntohl", socket_ntohl, |
| 5706 | METH_O, ntohl_doc}, |
| 5707 | {"htons", socket_htons, |
| 5708 | METH_VARARGS, htons_doc}, |
| 5709 | {"htonl", socket_htonl, |
| 5710 | METH_O, htonl_doc}, |
| 5711 | {"inet_aton", socket_inet_aton, |
| 5712 | METH_VARARGS, inet_aton_doc}, |
| 5713 | {"inet_ntoa", socket_inet_ntoa, |
| 5714 | METH_VARARGS, inet_ntoa_doc}, |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5715 | #if defined(HAVE_INET_PTON) || defined(MS_WINDOWS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5716 | {"inet_pton", socket_inet_pton, |
| 5717 | METH_VARARGS, inet_pton_doc}, |
| 5718 | {"inet_ntop", socket_inet_ntop, |
| 5719 | METH_VARARGS, inet_ntop_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5720 | #endif |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5721 | {"getaddrinfo", (PyCFunction)socket_getaddrinfo, |
| 5722 | METH_VARARGS | METH_KEYWORDS, getaddrinfo_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5723 | {"getnameinfo", socket_getnameinfo, |
| 5724 | METH_VARARGS, getnameinfo_doc}, |
| 5725 | {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout, |
| 5726 | METH_NOARGS, getdefaulttimeout_doc}, |
| 5727 | {"setdefaulttimeout", socket_setdefaulttimeout, |
| 5728 | METH_O, setdefaulttimeout_doc}, |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5729 | #ifdef HAVE_IF_NAMEINDEX |
| 5730 | {"if_nameindex", socket_if_nameindex, |
| 5731 | METH_NOARGS, if_nameindex_doc}, |
| 5732 | {"if_nametoindex", socket_if_nametoindex, |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5733 | METH_VARARGS, if_nametoindex_doc}, |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5734 | {"if_indextoname", socket_if_indextoname, |
| 5735 | METH_O, if_indextoname_doc}, |
| 5736 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 5737 | #ifdef CMSG_LEN |
| 5738 | {"CMSG_LEN", socket_CMSG_LEN, |
| 5739 | METH_VARARGS, CMSG_LEN_doc}, |
| 5740 | #ifdef CMSG_SPACE |
| 5741 | {"CMSG_SPACE", socket_CMSG_SPACE, |
| 5742 | METH_VARARGS, CMSG_SPACE_doc}, |
| 5743 | #endif |
| 5744 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5745 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5746 | }; |
| 5747 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 5748 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5749 | #ifdef MS_WINDOWS |
| 5750 | #define OS_INIT_DEFINED |
| 5751 | |
| 5752 | /* Additional initialization and cleanup for Windows */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5753 | |
| 5754 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5755 | os_cleanup(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5756 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5757 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5758 | } |
| 5759 | |
| 5760 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5761 | os_init(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5762 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5763 | WSADATA WSAData; |
| 5764 | int ret; |
| 5765 | ret = WSAStartup(0x0101, &WSAData); |
| 5766 | switch (ret) { |
| 5767 | case 0: /* No error */ |
| 5768 | Py_AtExit(os_cleanup); |
| 5769 | return 1; /* Success */ |
| 5770 | case WSASYSNOTREADY: |
| 5771 | PyErr_SetString(PyExc_ImportError, |
| 5772 | "WSAStartup failed: network not ready"); |
| 5773 | break; |
| 5774 | case WSAVERNOTSUPPORTED: |
| 5775 | case WSAEINVAL: |
| 5776 | PyErr_SetString( |
| 5777 | PyExc_ImportError, |
| 5778 | "WSAStartup failed: requested version not supported"); |
| 5779 | break; |
| 5780 | default: |
| 5781 | PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret); |
| 5782 | break; |
| 5783 | } |
| 5784 | return 0; /* Failure */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5785 | } |
| 5786 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 5787 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5788 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5789 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5790 | |
| 5791 | #ifndef OS_INIT_DEFINED |
| 5792 | static int |
| 5793 | os_init(void) |
| 5794 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5795 | return 1; /* Success */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5796 | } |
| 5797 | #endif |
| 5798 | |
| 5799 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5800 | /* C API table - always add new things to the end for binary |
| 5801 | compatibility. */ |
| 5802 | static |
| 5803 | PySocketModule_APIObject PySocketModuleAPI = |
| 5804 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5805 | &sock_type, |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 5806 | NULL, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5807 | NULL |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5808 | }; |
| 5809 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5810 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5811 | /* Initialize the _socket module. |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5812 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5813 | This module is actually called "_socket", and there's a wrapper |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 5814 | "socket.py" which implements some additional functionality. |
| 5815 | The import of "_socket" may fail with an ImportError exception if |
| 5816 | os-specific initialization fails. On Windows, this does WINSOCK |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 5817 | initialization. When WINSOCK is initialized successfully, a call to |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 5818 | WSACleanup() is scheduled to be made at exit time. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5819 | */ |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5820 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5821 | PyDoc_STRVAR(socket_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5822 | "Implementation module for socket operations.\n\ |
| 5823 | \n\ |
| 5824 | See the socket module for documentation."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5825 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5826 | static struct PyModuleDef socketmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5827 | PyModuleDef_HEAD_INIT, |
| 5828 | PySocket_MODULE_NAME, |
| 5829 | socket_doc, |
| 5830 | -1, |
| 5831 | socket_methods, |
| 5832 | NULL, |
| 5833 | NULL, |
| 5834 | NULL, |
| 5835 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5836 | }; |
| 5837 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 5838 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5839 | PyInit__socket(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5840 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5841 | PyObject *m, *has_ipv6; |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5842 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5843 | if (!os_init()) |
| 5844 | return NULL; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5845 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5846 | #ifdef MS_WINDOWS |
| 5847 | if (support_wsa_no_inherit == -1) { |
| 5848 | DWORD version = GetVersion(); |
| 5849 | DWORD major = (DWORD)LOBYTE(LOWORD(version)); |
| 5850 | DWORD minor = (DWORD)HIBYTE(LOWORD(version)); |
| 5851 | /* need Windows 7 SP1, 2008 R2 SP1 or later */ |
| 5852 | support_wsa_no_inherit = (major >= 6 && minor >= 1); |
| 5853 | } |
| 5854 | #endif |
| 5855 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5856 | Py_TYPE(&sock_type) = &PyType_Type; |
| 5857 | m = PyModule_Create(&socketmodule); |
| 5858 | if (m == NULL) |
| 5859 | return NULL; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5860 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5861 | Py_INCREF(PyExc_OSError); |
| 5862 | PySocketModuleAPI.error = PyExc_OSError; |
| 5863 | Py_INCREF(PyExc_OSError); |
| 5864 | PyModule_AddObject(m, "error", PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5865 | socket_herror = PyErr_NewException("socket.herror", |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5866 | PyExc_OSError, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5867 | if (socket_herror == NULL) |
| 5868 | return NULL; |
| 5869 | Py_INCREF(socket_herror); |
| 5870 | PyModule_AddObject(m, "herror", socket_herror); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5871 | socket_gaierror = PyErr_NewException("socket.gaierror", PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5872 | NULL); |
| 5873 | if (socket_gaierror == NULL) |
| 5874 | return NULL; |
| 5875 | Py_INCREF(socket_gaierror); |
| 5876 | PyModule_AddObject(m, "gaierror", socket_gaierror); |
| 5877 | socket_timeout = PyErr_NewException("socket.timeout", |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5878 | PyExc_OSError, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5879 | if (socket_timeout == NULL) |
| 5880 | return NULL; |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 5881 | PySocketModuleAPI.timeout_error = socket_timeout; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5882 | Py_INCREF(socket_timeout); |
| 5883 | PyModule_AddObject(m, "timeout", socket_timeout); |
| 5884 | Py_INCREF((PyObject *)&sock_type); |
| 5885 | if (PyModule_AddObject(m, "SocketType", |
| 5886 | (PyObject *)&sock_type) != 0) |
| 5887 | return NULL; |
| 5888 | Py_INCREF((PyObject *)&sock_type); |
| 5889 | if (PyModule_AddObject(m, "socket", |
| 5890 | (PyObject *)&sock_type) != 0) |
| 5891 | return NULL; |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5892 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5893 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5894 | has_ipv6 = Py_True; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5895 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5896 | has_ipv6 = Py_False; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5897 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5898 | Py_INCREF(has_ipv6); |
| 5899 | PyModule_AddObject(m, "has_ipv6", has_ipv6); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5900 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5901 | /* Export C API */ |
| 5902 | if (PyModule_AddObject(m, PySocket_CAPI_NAME, |
| 5903 | PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL) |
| 5904 | ) != 0) |
| 5905 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5906 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5907 | /* Address families (we only support AF_INET and AF_UNIX) */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5908 | #ifdef AF_UNSPEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5909 | PyModule_AddIntMacro(m, AF_UNSPEC); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5910 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5911 | PyModule_AddIntMacro(m, AF_INET); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5912 | #ifdef AF_INET6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5913 | PyModule_AddIntMacro(m, AF_INET6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5914 | #endif /* AF_INET6 */ |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 5915 | #if defined(AF_UNIX) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5916 | PyModule_AddIntMacro(m, AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5917 | #endif /* AF_UNIX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5918 | #ifdef AF_AX25 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5919 | /* Amateur Radio AX.25 */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5920 | PyModule_AddIntMacro(m, AF_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5921 | #endif |
| 5922 | #ifdef AF_IPX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5923 | PyModule_AddIntMacro(m, AF_IPX); /* Novell IPX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5924 | #endif |
| 5925 | #ifdef AF_APPLETALK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5926 | /* Appletalk DDP */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5927 | PyModule_AddIntMacro(m, AF_APPLETALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5928 | #endif |
| 5929 | #ifdef AF_NETROM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5930 | /* Amateur radio NetROM */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5931 | PyModule_AddIntMacro(m, AF_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5932 | #endif |
| 5933 | #ifdef AF_BRIDGE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5934 | /* Multiprotocol bridge */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5935 | PyModule_AddIntMacro(m, AF_BRIDGE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5936 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5937 | #ifdef AF_ATMPVC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5938 | /* ATM PVCs */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5939 | PyModule_AddIntMacro(m, AF_ATMPVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5940 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5941 | #ifdef AF_AAL5 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5942 | /* Reserved for Werner's ATM */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5943 | PyModule_AddIntMacro(m, AF_AAL5); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5944 | #endif |
| 5945 | #ifdef AF_X25 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5946 | /* Reserved for X.25 project */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5947 | PyModule_AddIntMacro(m, AF_X25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5948 | #endif |
| 5949 | #ifdef AF_INET6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5950 | PyModule_AddIntMacro(m, AF_INET6); /* IP version 6 */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5951 | #endif |
| 5952 | #ifdef AF_ROSE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5953 | /* Amateur Radio X.25 PLP */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5954 | PyModule_AddIntMacro(m, AF_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5955 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5956 | #ifdef AF_DECnet |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5957 | /* Reserved for DECnet project */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5958 | PyModule_AddIntMacro(m, AF_DECnet); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5959 | #endif |
| 5960 | #ifdef AF_NETBEUI |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5961 | /* Reserved for 802.2LLC project */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5962 | PyModule_AddIntMacro(m, AF_NETBEUI); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5963 | #endif |
| 5964 | #ifdef AF_SECURITY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5965 | /* Security callback pseudo AF */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5966 | PyModule_AddIntMacro(m, AF_SECURITY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5967 | #endif |
| 5968 | #ifdef AF_KEY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5969 | /* PF_KEY key management API */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5970 | PyModule_AddIntMacro(m, AF_KEY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5971 | #endif |
| 5972 | #ifdef AF_NETLINK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5973 | /* */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5974 | PyModule_AddIntMacro(m, AF_NETLINK); |
| 5975 | PyModule_AddIntMacro(m, NETLINK_ROUTE); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5976 | #ifdef NETLINK_SKIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5977 | PyModule_AddIntMacro(m, NETLINK_SKIP); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5978 | #endif |
| 5979 | #ifdef NETLINK_W1 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5980 | PyModule_AddIntMacro(m, NETLINK_W1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5981 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5982 | PyModule_AddIntMacro(m, NETLINK_USERSOCK); |
| 5983 | PyModule_AddIntMacro(m, NETLINK_FIREWALL); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 5984 | #ifdef NETLINK_TCPDIAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5985 | PyModule_AddIntMacro(m, NETLINK_TCPDIAG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 5986 | #endif |
| 5987 | #ifdef NETLINK_NFLOG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5988 | PyModule_AddIntMacro(m, NETLINK_NFLOG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 5989 | #endif |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 5990 | #ifdef NETLINK_XFRM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5991 | PyModule_AddIntMacro(m, NETLINK_XFRM); |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 5992 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5993 | #ifdef NETLINK_ARPD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5994 | PyModule_AddIntMacro(m, NETLINK_ARPD); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5995 | #endif |
| 5996 | #ifdef NETLINK_ROUTE6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5997 | PyModule_AddIntMacro(m, NETLINK_ROUTE6); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5998 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5999 | PyModule_AddIntMacro(m, NETLINK_IP6_FW); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6000 | #ifdef NETLINK_DNRTMSG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6001 | PyModule_AddIntMacro(m, NETLINK_DNRTMSG); |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 6002 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6003 | #ifdef NETLINK_TAPBASE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6004 | PyModule_AddIntMacro(m, NETLINK_TAPBASE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6005 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6006 | #endif /* AF_NETLINK */ |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6007 | #ifdef AF_ROUTE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6008 | /* Alias to emulate 4.4BSD */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6009 | PyModule_AddIntMacro(m, AF_ROUTE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6010 | #endif |
Giampaolo Rodola' | 80e1c43 | 2013-05-21 21:02:04 +0200 | [diff] [blame] | 6011 | #ifdef AF_LINK |
| 6012 | PyModule_AddIntMacro(m, AF_LINK); |
| 6013 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6014 | #ifdef AF_ASH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6015 | /* Ash */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6016 | PyModule_AddIntMacro(m, AF_ASH); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6017 | #endif |
| 6018 | #ifdef AF_ECONET |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6019 | /* Acorn Econet */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6020 | PyModule_AddIntMacro(m, AF_ECONET); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6021 | #endif |
| 6022 | #ifdef AF_ATMSVC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6023 | /* ATM SVCs */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6024 | PyModule_AddIntMacro(m, AF_ATMSVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6025 | #endif |
| 6026 | #ifdef AF_SNA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6027 | /* Linux SNA Project (nutters!) */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6028 | PyModule_AddIntMacro(m, AF_SNA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6029 | #endif |
| 6030 | #ifdef AF_IRDA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6031 | /* IRDA sockets */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6032 | PyModule_AddIntMacro(m, AF_IRDA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6033 | #endif |
| 6034 | #ifdef AF_PPPOX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6035 | /* PPPoX sockets */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6036 | PyModule_AddIntMacro(m, AF_PPPOX); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6037 | #endif |
| 6038 | #ifdef AF_WANPIPE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6039 | /* Wanpipe API Sockets */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6040 | PyModule_AddIntMacro(m, AF_WANPIPE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6041 | #endif |
| 6042 | #ifdef AF_LLC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6043 | /* Linux LLC */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6044 | PyModule_AddIntMacro(m, AF_LLC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6045 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 6046 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 6047 | #ifdef USE_BLUETOOTH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6048 | PyModule_AddIntMacro(m, AF_BLUETOOTH); |
| 6049 | PyModule_AddIntMacro(m, BTPROTO_L2CAP); |
| 6050 | PyModule_AddIntMacro(m, BTPROTO_HCI); |
| 6051 | PyModule_AddIntMacro(m, SOL_HCI); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 6052 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6053 | PyModule_AddIntMacro(m, HCI_FILTER); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 6054 | #endif |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 6055 | #if !defined(__FreeBSD__) |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 6056 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6057 | PyModule_AddIntMacro(m, HCI_TIME_STAMP); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 6058 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6059 | PyModule_AddIntMacro(m, HCI_DATA_DIR); |
| 6060 | PyModule_AddIntMacro(m, BTPROTO_SCO); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 6061 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6062 | PyModule_AddIntMacro(m, BTPROTO_RFCOMM); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6063 | PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00"); |
| 6064 | 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] | 6065 | #endif |
| 6066 | |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6067 | #ifdef AF_CAN |
| 6068 | /* Controller Area Network */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6069 | PyModule_AddIntMacro(m, AF_CAN); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6070 | #endif |
| 6071 | #ifdef PF_CAN |
| 6072 | /* Controller Area Network */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6073 | PyModule_AddIntMacro(m, PF_CAN); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6074 | #endif |
| 6075 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6076 | /* Reliable Datagram Sockets */ |
| 6077 | #ifdef AF_RDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6078 | PyModule_AddIntMacro(m, AF_RDS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6079 | #endif |
| 6080 | #ifdef PF_RDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6081 | PyModule_AddIntMacro(m, PF_RDS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6082 | #endif |
| 6083 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6084 | /* Kernel event messages */ |
| 6085 | #ifdef PF_SYSTEM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6086 | PyModule_AddIntMacro(m, PF_SYSTEM); |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6087 | #endif |
| 6088 | #ifdef AF_SYSTEM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6089 | PyModule_AddIntMacro(m, AF_SYSTEM); |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6090 | #endif |
| 6091 | |
Antoine Pitrou | b156a46 | 2010-10-27 20:13:57 +0000 | [diff] [blame] | 6092 | #ifdef AF_PACKET |
| 6093 | PyModule_AddIntMacro(m, AF_PACKET); |
| 6094 | #endif |
| 6095 | #ifdef PF_PACKET |
| 6096 | PyModule_AddIntMacro(m, PF_PACKET); |
| 6097 | #endif |
| 6098 | #ifdef PACKET_HOST |
| 6099 | PyModule_AddIntMacro(m, PACKET_HOST); |
| 6100 | #endif |
| 6101 | #ifdef PACKET_BROADCAST |
| 6102 | PyModule_AddIntMacro(m, PACKET_BROADCAST); |
| 6103 | #endif |
| 6104 | #ifdef PACKET_MULTICAST |
| 6105 | PyModule_AddIntMacro(m, PACKET_MULTICAST); |
| 6106 | #endif |
| 6107 | #ifdef PACKET_OTHERHOST |
| 6108 | PyModule_AddIntMacro(m, PACKET_OTHERHOST); |
| 6109 | #endif |
| 6110 | #ifdef PACKET_OUTGOING |
| 6111 | PyModule_AddIntMacro(m, PACKET_OUTGOING); |
| 6112 | #endif |
| 6113 | #ifdef PACKET_LOOPBACK |
| 6114 | PyModule_AddIntMacro(m, PACKET_LOOPBACK); |
| 6115 | #endif |
| 6116 | #ifdef PACKET_FASTROUTE |
| 6117 | PyModule_AddIntMacro(m, PACKET_FASTROUTE); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 6118 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6119 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6120 | #ifdef HAVE_LINUX_TIPC_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6121 | PyModule_AddIntMacro(m, AF_TIPC); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6123 | /* for addresses */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6124 | PyModule_AddIntMacro(m, TIPC_ADDR_NAMESEQ); |
| 6125 | PyModule_AddIntMacro(m, TIPC_ADDR_NAME); |
| 6126 | PyModule_AddIntMacro(m, TIPC_ADDR_ID); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6127 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6128 | PyModule_AddIntMacro(m, TIPC_ZONE_SCOPE); |
| 6129 | PyModule_AddIntMacro(m, TIPC_CLUSTER_SCOPE); |
| 6130 | PyModule_AddIntMacro(m, TIPC_NODE_SCOPE); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6132 | /* for setsockopt() */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6133 | PyModule_AddIntMacro(m, SOL_TIPC); |
| 6134 | PyModule_AddIntMacro(m, TIPC_IMPORTANCE); |
| 6135 | PyModule_AddIntMacro(m, TIPC_SRC_DROPPABLE); |
| 6136 | PyModule_AddIntMacro(m, TIPC_DEST_DROPPABLE); |
| 6137 | PyModule_AddIntMacro(m, TIPC_CONN_TIMEOUT); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6138 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6139 | PyModule_AddIntMacro(m, TIPC_LOW_IMPORTANCE); |
| 6140 | PyModule_AddIntMacro(m, TIPC_MEDIUM_IMPORTANCE); |
| 6141 | PyModule_AddIntMacro(m, TIPC_HIGH_IMPORTANCE); |
| 6142 | PyModule_AddIntMacro(m, TIPC_CRITICAL_IMPORTANCE); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6143 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6144 | /* for subscriptions */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6145 | PyModule_AddIntMacro(m, TIPC_SUB_PORTS); |
| 6146 | PyModule_AddIntMacro(m, TIPC_SUB_SERVICE); |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 6147 | #ifdef TIPC_SUB_CANCEL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6148 | /* doesn't seem to be available everywhere */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6149 | PyModule_AddIntMacro(m, TIPC_SUB_CANCEL); |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 6150 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6151 | PyModule_AddIntMacro(m, TIPC_WAIT_FOREVER); |
| 6152 | PyModule_AddIntMacro(m, TIPC_PUBLISHED); |
| 6153 | PyModule_AddIntMacro(m, TIPC_WITHDRAWN); |
| 6154 | PyModule_AddIntMacro(m, TIPC_SUBSCR_TIMEOUT); |
| 6155 | PyModule_AddIntMacro(m, TIPC_CFG_SRV); |
| 6156 | PyModule_AddIntMacro(m, TIPC_TOP_SRV); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6157 | #endif |
| 6158 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6159 | /* Socket types */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6160 | PyModule_AddIntMacro(m, SOCK_STREAM); |
| 6161 | PyModule_AddIntMacro(m, SOCK_DGRAM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 6162 | /* We have incomplete socket support. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6163 | PyModule_AddIntMacro(m, SOCK_RAW); |
| 6164 | PyModule_AddIntMacro(m, SOCK_SEQPACKET); |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 6165 | #if defined(SOCK_RDM) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6166 | PyModule_AddIntMacro(m, SOCK_RDM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 6167 | #endif |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 6168 | #ifdef SOCK_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6169 | PyModule_AddIntMacro(m, SOCK_CLOEXEC); |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 6170 | #endif |
| 6171 | #ifdef SOCK_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6172 | PyModule_AddIntMacro(m, SOCK_NONBLOCK); |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 6173 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6175 | #ifdef SO_DEBUG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6176 | PyModule_AddIntMacro(m, SO_DEBUG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6177 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6178 | #ifdef SO_ACCEPTCONN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6179 | PyModule_AddIntMacro(m, SO_ACCEPTCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6180 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6181 | #ifdef SO_REUSEADDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6182 | PyModule_AddIntMacro(m, SO_REUSEADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6183 | #endif |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 6184 | #ifdef SO_EXCLUSIVEADDRUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6185 | PyModule_AddIntMacro(m, SO_EXCLUSIVEADDRUSE); |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 6186 | #endif |
| 6187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6188 | #ifdef SO_KEEPALIVE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6189 | PyModule_AddIntMacro(m, SO_KEEPALIVE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6190 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6191 | #ifdef SO_DONTROUTE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6192 | PyModule_AddIntMacro(m, SO_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6193 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6194 | #ifdef SO_BROADCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6195 | PyModule_AddIntMacro(m, SO_BROADCAST); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6196 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6197 | #ifdef SO_USELOOPBACK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6198 | PyModule_AddIntMacro(m, SO_USELOOPBACK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6199 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6200 | #ifdef SO_LINGER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6201 | PyModule_AddIntMacro(m, SO_LINGER); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6202 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6203 | #ifdef SO_OOBINLINE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6204 | PyModule_AddIntMacro(m, SO_OOBINLINE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6205 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6206 | #ifdef SO_REUSEPORT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6207 | PyModule_AddIntMacro(m, SO_REUSEPORT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6208 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6209 | #ifdef SO_SNDBUF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6210 | PyModule_AddIntMacro(m, SO_SNDBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6211 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6212 | #ifdef SO_RCVBUF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6213 | PyModule_AddIntMacro(m, SO_RCVBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6214 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6215 | #ifdef SO_SNDLOWAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6216 | PyModule_AddIntMacro(m, SO_SNDLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6217 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6218 | #ifdef SO_RCVLOWAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6219 | PyModule_AddIntMacro(m, SO_RCVLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6220 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6221 | #ifdef SO_SNDTIMEO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6222 | PyModule_AddIntMacro(m, SO_SNDTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6223 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6224 | #ifdef SO_RCVTIMEO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6225 | PyModule_AddIntMacro(m, SO_RCVTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6226 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6227 | #ifdef SO_ERROR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6228 | PyModule_AddIntMacro(m, SO_ERROR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6229 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6230 | #ifdef SO_TYPE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6231 | PyModule_AddIntMacro(m, SO_TYPE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6232 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6233 | #ifdef SO_SETFIB |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6234 | PyModule_AddIntMacro(m, SO_SETFIB); |
Larry Hastings | f0f3795 | 2010-04-02 11:47:10 +0000 | [diff] [blame] | 6235 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6236 | #ifdef SO_PASSCRED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6237 | PyModule_AddIntMacro(m, SO_PASSCRED); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6238 | #endif |
| 6239 | #ifdef SO_PEERCRED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6240 | PyModule_AddIntMacro(m, SO_PEERCRED); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6241 | #endif |
| 6242 | #ifdef LOCAL_PEERCRED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6243 | PyModule_AddIntMacro(m, LOCAL_PEERCRED); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6244 | #endif |
Victor Stinner | cb98bed | 2011-10-25 13:45:48 +0200 | [diff] [blame] | 6245 | #ifdef SO_BINDTODEVICE |
| 6246 | PyModule_AddIntMacro(m, SO_BINDTODEVICE); |
| 6247 | #endif |
Benjamin Peterson | 1314ef7 | 2013-11-27 09:18:54 -0600 | [diff] [blame] | 6248 | #ifdef SO_PRIORITY |
| 6249 | PyModule_AddIntMacro(m, SO_PRIORITY); |
| 6250 | #endif |
Charles-François Natali | ea07eb9 | 2014-09-18 23:18:46 +0100 | [diff] [blame] | 6251 | #ifdef SO_MARK |
| 6252 | PyModule_AddIntMacro(m, SO_MARK); |
| 6253 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6255 | /* Maximum number of connections for "listen" */ |
| 6256 | #ifdef SOMAXCONN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6257 | PyModule_AddIntMacro(m, SOMAXCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6258 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6259 | PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6260 | #endif |
| 6261 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6262 | /* Ancilliary message types */ |
| 6263 | #ifdef SCM_RIGHTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6264 | PyModule_AddIntMacro(m, SCM_RIGHTS); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6265 | #endif |
| 6266 | #ifdef SCM_CREDENTIALS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6267 | PyModule_AddIntMacro(m, SCM_CREDENTIALS); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6268 | #endif |
| 6269 | #ifdef SCM_CREDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6270 | PyModule_AddIntMacro(m, SCM_CREDS); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6271 | #endif |
| 6272 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6273 | /* Flags for send, recv */ |
| 6274 | #ifdef MSG_OOB |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6275 | PyModule_AddIntMacro(m, MSG_OOB); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6276 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6277 | #ifdef MSG_PEEK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6278 | PyModule_AddIntMacro(m, MSG_PEEK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6279 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6280 | #ifdef MSG_DONTROUTE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6281 | PyModule_AddIntMacro(m, MSG_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6282 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6283 | #ifdef MSG_DONTWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6284 | PyModule_AddIntMacro(m, MSG_DONTWAIT); |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 6285 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6286 | #ifdef MSG_EOR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6287 | PyModule_AddIntMacro(m, MSG_EOR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6288 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6289 | #ifdef MSG_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6290 | PyModule_AddIntMacro(m, MSG_TRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6291 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6292 | #ifdef MSG_CTRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6293 | PyModule_AddIntMacro(m, MSG_CTRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6294 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6295 | #ifdef MSG_WAITALL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6296 | PyModule_AddIntMacro(m, MSG_WAITALL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6297 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6298 | #ifdef MSG_BTAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6299 | PyModule_AddIntMacro(m, MSG_BTAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6300 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6301 | #ifdef MSG_ETAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6302 | PyModule_AddIntMacro(m, MSG_ETAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6303 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6304 | #ifdef MSG_NOSIGNAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6305 | PyModule_AddIntMacro(m, MSG_NOSIGNAL); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6306 | #endif |
| 6307 | #ifdef MSG_NOTIFICATION |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6308 | PyModule_AddIntMacro(m, MSG_NOTIFICATION); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6309 | #endif |
| 6310 | #ifdef MSG_CMSG_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6311 | PyModule_AddIntMacro(m, MSG_CMSG_CLOEXEC); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6312 | #endif |
| 6313 | #ifdef MSG_ERRQUEUE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6314 | PyModule_AddIntMacro(m, MSG_ERRQUEUE); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6315 | #endif |
| 6316 | #ifdef MSG_CONFIRM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6317 | PyModule_AddIntMacro(m, MSG_CONFIRM); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6318 | #endif |
| 6319 | #ifdef MSG_MORE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6320 | PyModule_AddIntMacro(m, MSG_MORE); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6321 | #endif |
| 6322 | #ifdef MSG_EOF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6323 | PyModule_AddIntMacro(m, MSG_EOF); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6324 | #endif |
| 6325 | #ifdef MSG_BCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6326 | PyModule_AddIntMacro(m, MSG_BCAST); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6327 | #endif |
| 6328 | #ifdef MSG_MCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6329 | PyModule_AddIntMacro(m, MSG_MCAST); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6330 | #endif |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 6331 | #ifdef MSG_FASTOPEN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6332 | PyModule_AddIntMacro(m, MSG_FASTOPEN); |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 6333 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6335 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 6336 | #ifdef SOL_SOCKET |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6337 | PyModule_AddIntMacro(m, SOL_SOCKET); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6338 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6339 | #ifdef SOL_IP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6340 | PyModule_AddIntMacro(m, SOL_IP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6341 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6342 | PyModule_AddIntConstant(m, "SOL_IP", 0); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6343 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6344 | #ifdef SOL_IPX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6345 | PyModule_AddIntMacro(m, SOL_IPX); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6346 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6347 | #ifdef SOL_AX25 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6348 | PyModule_AddIntMacro(m, SOL_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6349 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6350 | #ifdef SOL_ATALK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6351 | PyModule_AddIntMacro(m, SOL_ATALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6352 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6353 | #ifdef SOL_NETROM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6354 | PyModule_AddIntMacro(m, SOL_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6355 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6356 | #ifdef SOL_ROSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6357 | PyModule_AddIntMacro(m, SOL_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6358 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6359 | #ifdef SOL_TCP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6360 | PyModule_AddIntMacro(m, SOL_TCP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6361 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6362 | PyModule_AddIntConstant(m, "SOL_TCP", 6); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6363 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6364 | #ifdef SOL_UDP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6365 | PyModule_AddIntMacro(m, SOL_UDP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6366 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6367 | PyModule_AddIntConstant(m, "SOL_UDP", 17); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6368 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6369 | #ifdef SOL_CAN_BASE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6370 | PyModule_AddIntMacro(m, SOL_CAN_BASE); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6371 | #endif |
| 6372 | #ifdef SOL_CAN_RAW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6373 | PyModule_AddIntMacro(m, SOL_CAN_RAW); |
| 6374 | PyModule_AddIntMacro(m, CAN_RAW); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6375 | #endif |
| 6376 | #ifdef HAVE_LINUX_CAN_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6377 | PyModule_AddIntMacro(m, CAN_EFF_FLAG); |
| 6378 | PyModule_AddIntMacro(m, CAN_RTR_FLAG); |
| 6379 | PyModule_AddIntMacro(m, CAN_ERR_FLAG); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6380 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6381 | PyModule_AddIntMacro(m, CAN_SFF_MASK); |
| 6382 | PyModule_AddIntMacro(m, CAN_EFF_MASK); |
| 6383 | PyModule_AddIntMacro(m, CAN_ERR_MASK); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6384 | #endif |
| 6385 | #ifdef HAVE_LINUX_CAN_RAW_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6386 | PyModule_AddIntMacro(m, CAN_RAW_FILTER); |
| 6387 | PyModule_AddIntMacro(m, CAN_RAW_ERR_FILTER); |
| 6388 | PyModule_AddIntMacro(m, CAN_RAW_LOOPBACK); |
| 6389 | PyModule_AddIntMacro(m, CAN_RAW_RECV_OWN_MSGS); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6390 | #endif |
Charles-François Natali | 773e42d | 2013-02-05 19:42:01 +0100 | [diff] [blame] | 6391 | #ifdef HAVE_LINUX_CAN_BCM_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6392 | PyModule_AddIntMacro(m, CAN_BCM); |
Charles-François Natali | 773e42d | 2013-02-05 19:42:01 +0100 | [diff] [blame] | 6393 | PyModule_AddIntConstant(m, "CAN_BCM_TX_SETUP", TX_SETUP); |
| 6394 | PyModule_AddIntConstant(m, "CAN_BCM_TX_DELETE", TX_DELETE); |
| 6395 | PyModule_AddIntConstant(m, "CAN_BCM_TX_READ", TX_READ); |
| 6396 | PyModule_AddIntConstant(m, "CAN_BCM_TX_SEND", TX_SEND); |
| 6397 | PyModule_AddIntConstant(m, "CAN_BCM_RX_SETUP", RX_SETUP); |
| 6398 | PyModule_AddIntConstant(m, "CAN_BCM_RX_DELETE", RX_DELETE); |
| 6399 | PyModule_AddIntConstant(m, "CAN_BCM_RX_READ", RX_READ); |
| 6400 | PyModule_AddIntConstant(m, "CAN_BCM_TX_STATUS", TX_STATUS); |
| 6401 | PyModule_AddIntConstant(m, "CAN_BCM_TX_EXPIRED", TX_EXPIRED); |
| 6402 | PyModule_AddIntConstant(m, "CAN_BCM_RX_STATUS", RX_STATUS); |
| 6403 | PyModule_AddIntConstant(m, "CAN_BCM_RX_TIMEOUT", RX_TIMEOUT); |
| 6404 | PyModule_AddIntConstant(m, "CAN_BCM_RX_CHANGED", RX_CHANGED); |
| 6405 | #endif |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6406 | #ifdef SOL_RDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6407 | PyModule_AddIntMacro(m, SOL_RDS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6408 | #endif |
| 6409 | #ifdef RDS_CANCEL_SENT_TO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6410 | PyModule_AddIntMacro(m, RDS_CANCEL_SENT_TO); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6411 | #endif |
| 6412 | #ifdef RDS_GET_MR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6413 | PyModule_AddIntMacro(m, RDS_GET_MR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6414 | #endif |
| 6415 | #ifdef RDS_FREE_MR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6416 | PyModule_AddIntMacro(m, RDS_FREE_MR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6417 | #endif |
| 6418 | #ifdef RDS_RECVERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6419 | PyModule_AddIntMacro(m, RDS_RECVERR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6420 | #endif |
| 6421 | #ifdef RDS_CONG_MONITOR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6422 | PyModule_AddIntMacro(m, RDS_CONG_MONITOR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6423 | #endif |
| 6424 | #ifdef RDS_GET_MR_FOR_DEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6425 | PyModule_AddIntMacro(m, RDS_GET_MR_FOR_DEST); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6426 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6427 | #ifdef IPPROTO_IP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6428 | PyModule_AddIntMacro(m, IPPROTO_IP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6429 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6430 | PyModule_AddIntConstant(m, "IPPROTO_IP", 0); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6431 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6432 | #ifdef IPPROTO_HOPOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6433 | PyModule_AddIntMacro(m, IPPROTO_HOPOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6434 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6435 | #ifdef IPPROTO_ICMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6436 | PyModule_AddIntMacro(m, IPPROTO_ICMP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6437 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6438 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6439 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6440 | #ifdef IPPROTO_IGMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6441 | PyModule_AddIntMacro(m, IPPROTO_IGMP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6442 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6443 | #ifdef IPPROTO_GGP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6444 | PyModule_AddIntMacro(m, IPPROTO_GGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6445 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6446 | #ifdef IPPROTO_IPV4 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6447 | PyModule_AddIntMacro(m, IPPROTO_IPV4); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6448 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6449 | #ifdef IPPROTO_IPV6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6450 | PyModule_AddIntMacro(m, IPPROTO_IPV6); |
Martin v. Löwis | a0f1734 | 2003-10-03 13:56:20 +0000 | [diff] [blame] | 6451 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6452 | #ifdef IPPROTO_IPIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6453 | PyModule_AddIntMacro(m, IPPROTO_IPIP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6454 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6455 | #ifdef IPPROTO_TCP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6456 | PyModule_AddIntMacro(m, IPPROTO_TCP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6457 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6458 | PyModule_AddIntConstant(m, "IPPROTO_TCP", 6); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6459 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6460 | #ifdef IPPROTO_EGP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6461 | PyModule_AddIntMacro(m, IPPROTO_EGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6462 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6463 | #ifdef IPPROTO_PUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6464 | PyModule_AddIntMacro(m, IPPROTO_PUP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6465 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6466 | #ifdef IPPROTO_UDP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6467 | PyModule_AddIntMacro(m, IPPROTO_UDP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6468 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6469 | PyModule_AddIntConstant(m, "IPPROTO_UDP", 17); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6470 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6471 | #ifdef IPPROTO_IDP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6472 | PyModule_AddIntMacro(m, IPPROTO_IDP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6473 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6474 | #ifdef IPPROTO_HELLO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6475 | PyModule_AddIntMacro(m, IPPROTO_HELLO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6476 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6477 | #ifdef IPPROTO_ND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6478 | PyModule_AddIntMacro(m, IPPROTO_ND); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6479 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6480 | #ifdef IPPROTO_TP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6481 | PyModule_AddIntMacro(m, IPPROTO_TP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6482 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6483 | #ifdef IPPROTO_IPV6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6484 | PyModule_AddIntMacro(m, IPPROTO_IPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6485 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6486 | #ifdef IPPROTO_ROUTING |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6487 | PyModule_AddIntMacro(m, IPPROTO_ROUTING); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6488 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6489 | #ifdef IPPROTO_FRAGMENT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6490 | PyModule_AddIntMacro(m, IPPROTO_FRAGMENT); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6491 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6492 | #ifdef IPPROTO_RSVP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6493 | PyModule_AddIntMacro(m, IPPROTO_RSVP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6494 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6495 | #ifdef IPPROTO_GRE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6496 | PyModule_AddIntMacro(m, IPPROTO_GRE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6497 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6498 | #ifdef IPPROTO_ESP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6499 | PyModule_AddIntMacro(m, IPPROTO_ESP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6500 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6501 | #ifdef IPPROTO_AH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6502 | PyModule_AddIntMacro(m, IPPROTO_AH); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6503 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6504 | #ifdef IPPROTO_MOBILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6505 | PyModule_AddIntMacro(m, IPPROTO_MOBILE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6506 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6507 | #ifdef IPPROTO_ICMPV6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6508 | PyModule_AddIntMacro(m, IPPROTO_ICMPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6509 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6510 | #ifdef IPPROTO_NONE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6511 | PyModule_AddIntMacro(m, IPPROTO_NONE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6512 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6513 | #ifdef IPPROTO_DSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6514 | PyModule_AddIntMacro(m, IPPROTO_DSTOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6515 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6516 | #ifdef IPPROTO_XTP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6517 | PyModule_AddIntMacro(m, IPPROTO_XTP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6518 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6519 | #ifdef IPPROTO_EON |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6520 | PyModule_AddIntMacro(m, IPPROTO_EON); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6521 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6522 | #ifdef IPPROTO_PIM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6523 | PyModule_AddIntMacro(m, IPPROTO_PIM); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6524 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6525 | #ifdef IPPROTO_IPCOMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6526 | PyModule_AddIntMacro(m, IPPROTO_IPCOMP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6527 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6528 | #ifdef IPPROTO_VRRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6529 | PyModule_AddIntMacro(m, IPPROTO_VRRP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6530 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6531 | #ifdef IPPROTO_SCTP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6532 | PyModule_AddIntMacro(m, IPPROTO_SCTP); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6533 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6534 | #ifdef IPPROTO_BIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6535 | PyModule_AddIntMacro(m, IPPROTO_BIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6536 | #endif |
| 6537 | /**/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6538 | #ifdef IPPROTO_RAW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6539 | PyModule_AddIntMacro(m, IPPROTO_RAW); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6540 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6541 | PyModule_AddIntConstant(m, "IPPROTO_RAW", 255); |
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_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6544 | PyModule_AddIntMacro(m, IPPROTO_MAX); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6545 | #endif |
| 6546 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6547 | #ifdef SYSPROTO_CONTROL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6548 | PyModule_AddIntMacro(m, SYSPROTO_CONTROL); |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6549 | #endif |
| 6550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6551 | /* Some port configuration */ |
| 6552 | #ifdef IPPORT_RESERVED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6553 | PyModule_AddIntMacro(m, IPPORT_RESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6554 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6555 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6556 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6557 | #ifdef IPPORT_USERRESERVED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6558 | PyModule_AddIntMacro(m, IPPORT_USERRESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6559 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6560 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6561 | #endif |
| 6562 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6563 | /* Some reserved IP v.4 addresses */ |
| 6564 | #ifdef INADDR_ANY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6565 | PyModule_AddIntMacro(m, INADDR_ANY); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6566 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6567 | PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6568 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6569 | #ifdef INADDR_BROADCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6570 | PyModule_AddIntMacro(m, INADDR_BROADCAST); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6571 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6572 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6573 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6574 | #ifdef INADDR_LOOPBACK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6575 | PyModule_AddIntMacro(m, INADDR_LOOPBACK); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6576 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6577 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6578 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6579 | #ifdef INADDR_UNSPEC_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6580 | PyModule_AddIntMacro(m, INADDR_UNSPEC_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6581 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6582 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6583 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6584 | #ifdef INADDR_ALLHOSTS_GROUP |
| 6585 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", |
| 6586 | INADDR_ALLHOSTS_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6587 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6588 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6589 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6590 | #ifdef INADDR_MAX_LOCAL_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6591 | PyModule_AddIntMacro(m, INADDR_MAX_LOCAL_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6592 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6593 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6594 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6595 | #ifdef INADDR_NONE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6596 | PyModule_AddIntMacro(m, INADDR_NONE); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6597 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6598 | PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6599 | #endif |
| 6600 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6601 | /* IPv4 [gs]etsockopt options */ |
| 6602 | #ifdef IP_OPTIONS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6603 | PyModule_AddIntMacro(m, IP_OPTIONS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6604 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6605 | #ifdef IP_HDRINCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6606 | PyModule_AddIntMacro(m, IP_HDRINCL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6607 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6608 | #ifdef IP_TOS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6609 | PyModule_AddIntMacro(m, IP_TOS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6610 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6611 | #ifdef IP_TTL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6612 | PyModule_AddIntMacro(m, IP_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6613 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6614 | #ifdef IP_RECVOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6615 | PyModule_AddIntMacro(m, IP_RECVOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6616 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6617 | #ifdef IP_RECVRETOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6618 | PyModule_AddIntMacro(m, IP_RECVRETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6619 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6620 | #ifdef IP_RECVDSTADDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6621 | PyModule_AddIntMacro(m, IP_RECVDSTADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6622 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6623 | #ifdef IP_RETOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6624 | PyModule_AddIntMacro(m, IP_RETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6625 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6626 | #ifdef IP_MULTICAST_IF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6627 | PyModule_AddIntMacro(m, IP_MULTICAST_IF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6628 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6629 | #ifdef IP_MULTICAST_TTL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6630 | PyModule_AddIntMacro(m, IP_MULTICAST_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6631 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6632 | #ifdef IP_MULTICAST_LOOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6633 | PyModule_AddIntMacro(m, IP_MULTICAST_LOOP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6634 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6635 | #ifdef IP_ADD_MEMBERSHIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6636 | PyModule_AddIntMacro(m, IP_ADD_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6637 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6638 | #ifdef IP_DROP_MEMBERSHIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6639 | PyModule_AddIntMacro(m, IP_DROP_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6640 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6641 | #ifdef IP_DEFAULT_MULTICAST_TTL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6642 | PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_TTL); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6643 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6644 | #ifdef IP_DEFAULT_MULTICAST_LOOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6645 | PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_LOOP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6646 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6647 | #ifdef IP_MAX_MEMBERSHIPS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6648 | PyModule_AddIntMacro(m, IP_MAX_MEMBERSHIPS); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6649 | #endif |
Charles-François Natali | 564a42c | 2011-12-17 14:59:56 +0100 | [diff] [blame] | 6650 | #ifdef IP_TRANSPARENT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6651 | PyModule_AddIntMacro(m, IP_TRANSPARENT); |
Charles-François Natali | 564a42c | 2011-12-17 14:59:56 +0100 | [diff] [blame] | 6652 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6653 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6654 | /* IPv6 [gs]etsockopt options, defined in RFC2553 */ |
| 6655 | #ifdef IPV6_JOIN_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6656 | PyModule_AddIntMacro(m, IPV6_JOIN_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6657 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6658 | #ifdef IPV6_LEAVE_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6659 | PyModule_AddIntMacro(m, IPV6_LEAVE_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6660 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6661 | #ifdef IPV6_MULTICAST_HOPS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6662 | PyModule_AddIntMacro(m, IPV6_MULTICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6663 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6664 | #ifdef IPV6_MULTICAST_IF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6665 | PyModule_AddIntMacro(m, IPV6_MULTICAST_IF); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6666 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6667 | #ifdef IPV6_MULTICAST_LOOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6668 | PyModule_AddIntMacro(m, IPV6_MULTICAST_LOOP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6669 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6670 | #ifdef IPV6_UNICAST_HOPS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6671 | PyModule_AddIntMacro(m, IPV6_UNICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6672 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6673 | /* Additional IPV6 socket options, defined in RFC 3493 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6674 | #ifdef IPV6_V6ONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6675 | PyModule_AddIntMacro(m, IPV6_V6ONLY); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6676 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6677 | /* Advanced IPV6 socket options, from RFC 3542 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6678 | #ifdef IPV6_CHECKSUM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6679 | PyModule_AddIntMacro(m, IPV6_CHECKSUM); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6680 | #endif |
| 6681 | #ifdef IPV6_DONTFRAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6682 | PyModule_AddIntMacro(m, IPV6_DONTFRAG); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6683 | #endif |
| 6684 | #ifdef IPV6_DSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6685 | PyModule_AddIntMacro(m, IPV6_DSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6686 | #endif |
| 6687 | #ifdef IPV6_HOPLIMIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6688 | PyModule_AddIntMacro(m, IPV6_HOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6689 | #endif |
| 6690 | #ifdef IPV6_HOPOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6691 | PyModule_AddIntMacro(m, IPV6_HOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6692 | #endif |
| 6693 | #ifdef IPV6_NEXTHOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6694 | PyModule_AddIntMacro(m, IPV6_NEXTHOP); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6695 | #endif |
| 6696 | #ifdef IPV6_PATHMTU |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6697 | PyModule_AddIntMacro(m, IPV6_PATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6698 | #endif |
| 6699 | #ifdef IPV6_PKTINFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6700 | PyModule_AddIntMacro(m, IPV6_PKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6701 | #endif |
| 6702 | #ifdef IPV6_RECVDSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6703 | PyModule_AddIntMacro(m, IPV6_RECVDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6704 | #endif |
| 6705 | #ifdef IPV6_RECVHOPLIMIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6706 | PyModule_AddIntMacro(m, IPV6_RECVHOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6707 | #endif |
| 6708 | #ifdef IPV6_RECVHOPOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6709 | PyModule_AddIntMacro(m, IPV6_RECVHOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6710 | #endif |
| 6711 | #ifdef IPV6_RECVPKTINFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6712 | PyModule_AddIntMacro(m, IPV6_RECVPKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6713 | #endif |
| 6714 | #ifdef IPV6_RECVRTHDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6715 | PyModule_AddIntMacro(m, IPV6_RECVRTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6716 | #endif |
| 6717 | #ifdef IPV6_RECVTCLASS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6718 | PyModule_AddIntMacro(m, IPV6_RECVTCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6719 | #endif |
| 6720 | #ifdef IPV6_RTHDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6721 | PyModule_AddIntMacro(m, IPV6_RTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6722 | #endif |
| 6723 | #ifdef IPV6_RTHDRDSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6724 | PyModule_AddIntMacro(m, IPV6_RTHDRDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6725 | #endif |
| 6726 | #ifdef IPV6_RTHDR_TYPE_0 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6727 | PyModule_AddIntMacro(m, IPV6_RTHDR_TYPE_0); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6728 | #endif |
| 6729 | #ifdef IPV6_RECVPATHMTU |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6730 | PyModule_AddIntMacro(m, IPV6_RECVPATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6731 | #endif |
| 6732 | #ifdef IPV6_TCLASS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6733 | PyModule_AddIntMacro(m, IPV6_TCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6734 | #endif |
| 6735 | #ifdef IPV6_USE_MIN_MTU |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6736 | PyModule_AddIntMacro(m, IPV6_USE_MIN_MTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6737 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6739 | /* TCP options */ |
| 6740 | #ifdef TCP_NODELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6741 | PyModule_AddIntMacro(m, TCP_NODELAY); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6742 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6743 | #ifdef TCP_MAXSEG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6744 | PyModule_AddIntMacro(m, TCP_MAXSEG); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6745 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6746 | #ifdef TCP_CORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6747 | PyModule_AddIntMacro(m, TCP_CORK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6748 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6749 | #ifdef TCP_KEEPIDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6750 | PyModule_AddIntMacro(m, TCP_KEEPIDLE); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6751 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6752 | #ifdef TCP_KEEPINTVL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6753 | PyModule_AddIntMacro(m, TCP_KEEPINTVL); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6754 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6755 | #ifdef TCP_KEEPCNT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6756 | PyModule_AddIntMacro(m, TCP_KEEPCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6757 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6758 | #ifdef TCP_SYNCNT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6759 | PyModule_AddIntMacro(m, TCP_SYNCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6760 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6761 | #ifdef TCP_LINGER2 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6762 | PyModule_AddIntMacro(m, TCP_LINGER2); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6763 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6764 | #ifdef TCP_DEFER_ACCEPT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6765 | PyModule_AddIntMacro(m, TCP_DEFER_ACCEPT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6766 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6767 | #ifdef TCP_WINDOW_CLAMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6768 | PyModule_AddIntMacro(m, TCP_WINDOW_CLAMP); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6769 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6770 | #ifdef TCP_INFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6771 | PyModule_AddIntMacro(m, TCP_INFO); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6772 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6773 | #ifdef TCP_QUICKACK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6774 | PyModule_AddIntMacro(m, TCP_QUICKACK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6775 | #endif |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 6776 | #ifdef TCP_FASTOPEN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6777 | PyModule_AddIntMacro(m, TCP_FASTOPEN); |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 6778 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6779 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6780 | /* IPX options */ |
| 6781 | #ifdef IPX_TYPE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6782 | PyModule_AddIntMacro(m, IPX_TYPE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6783 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 6784 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6785 | /* Reliable Datagram Sockets */ |
| 6786 | #ifdef RDS_CMSG_RDMA_ARGS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6787 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_ARGS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6788 | #endif |
| 6789 | #ifdef RDS_CMSG_RDMA_DEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6790 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_DEST); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6791 | #endif |
| 6792 | #ifdef RDS_CMSG_RDMA_MAP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6793 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_MAP); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6794 | #endif |
| 6795 | #ifdef RDS_CMSG_RDMA_STATUS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6796 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_STATUS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6797 | #endif |
| 6798 | #ifdef RDS_CMSG_RDMA_UPDATE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6799 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_UPDATE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6800 | #endif |
| 6801 | #ifdef RDS_RDMA_READWRITE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6802 | PyModule_AddIntMacro(m, RDS_RDMA_READWRITE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6803 | #endif |
| 6804 | #ifdef RDS_RDMA_FENCE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6805 | PyModule_AddIntMacro(m, RDS_RDMA_FENCE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6806 | #endif |
| 6807 | #ifdef RDS_RDMA_INVALIDATE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6808 | PyModule_AddIntMacro(m, RDS_RDMA_INVALIDATE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6809 | #endif |
| 6810 | #ifdef RDS_RDMA_USE_ONCE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6811 | PyModule_AddIntMacro(m, RDS_RDMA_USE_ONCE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6812 | #endif |
| 6813 | #ifdef RDS_RDMA_DONTWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6814 | PyModule_AddIntMacro(m, RDS_RDMA_DONTWAIT); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6815 | #endif |
| 6816 | #ifdef RDS_RDMA_NOTIFY_ME |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6817 | PyModule_AddIntMacro(m, RDS_RDMA_NOTIFY_ME); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6818 | #endif |
| 6819 | #ifdef RDS_RDMA_SILENT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6820 | PyModule_AddIntMacro(m, RDS_RDMA_SILENT); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6821 | #endif |
| 6822 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6823 | /* get{addr,name}info parameters */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6824 | #ifdef EAI_ADDRFAMILY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6825 | PyModule_AddIntMacro(m, EAI_ADDRFAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6826 | #endif |
| 6827 | #ifdef EAI_AGAIN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6828 | PyModule_AddIntMacro(m, EAI_AGAIN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6829 | #endif |
| 6830 | #ifdef EAI_BADFLAGS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6831 | PyModule_AddIntMacro(m, EAI_BADFLAGS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6832 | #endif |
| 6833 | #ifdef EAI_FAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6834 | PyModule_AddIntMacro(m, EAI_FAIL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6835 | #endif |
| 6836 | #ifdef EAI_FAMILY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6837 | PyModule_AddIntMacro(m, EAI_FAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6838 | #endif |
| 6839 | #ifdef EAI_MEMORY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6840 | PyModule_AddIntMacro(m, EAI_MEMORY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6841 | #endif |
| 6842 | #ifdef EAI_NODATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6843 | PyModule_AddIntMacro(m, EAI_NODATA); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6844 | #endif |
| 6845 | #ifdef EAI_NONAME |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6846 | PyModule_AddIntMacro(m, EAI_NONAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6847 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6848 | #ifdef EAI_OVERFLOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6849 | PyModule_AddIntMacro(m, EAI_OVERFLOW); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6850 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6851 | #ifdef EAI_SERVICE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6852 | PyModule_AddIntMacro(m, EAI_SERVICE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6853 | #endif |
| 6854 | #ifdef EAI_SOCKTYPE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6855 | PyModule_AddIntMacro(m, EAI_SOCKTYPE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6856 | #endif |
| 6857 | #ifdef EAI_SYSTEM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6858 | PyModule_AddIntMacro(m, EAI_SYSTEM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6859 | #endif |
| 6860 | #ifdef EAI_BADHINTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6861 | PyModule_AddIntMacro(m, EAI_BADHINTS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6862 | #endif |
| 6863 | #ifdef EAI_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6864 | PyModule_AddIntMacro(m, EAI_PROTOCOL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6865 | #endif |
| 6866 | #ifdef EAI_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6867 | PyModule_AddIntMacro(m, EAI_MAX); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6868 | #endif |
| 6869 | #ifdef AI_PASSIVE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6870 | PyModule_AddIntMacro(m, AI_PASSIVE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6871 | #endif |
| 6872 | #ifdef AI_CANONNAME |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6873 | PyModule_AddIntMacro(m, AI_CANONNAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6874 | #endif |
| 6875 | #ifdef AI_NUMERICHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6876 | PyModule_AddIntMacro(m, AI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6877 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6878 | #ifdef AI_NUMERICSERV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6879 | PyModule_AddIntMacro(m, AI_NUMERICSERV); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6880 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6881 | #ifdef AI_MASK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6882 | PyModule_AddIntMacro(m, AI_MASK); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6883 | #endif |
| 6884 | #ifdef AI_ALL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6885 | PyModule_AddIntMacro(m, AI_ALL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6886 | #endif |
| 6887 | #ifdef AI_V4MAPPED_CFG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6888 | PyModule_AddIntMacro(m, AI_V4MAPPED_CFG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6889 | #endif |
| 6890 | #ifdef AI_ADDRCONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6891 | PyModule_AddIntMacro(m, AI_ADDRCONFIG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6892 | #endif |
| 6893 | #ifdef AI_V4MAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6894 | PyModule_AddIntMacro(m, AI_V4MAPPED); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6895 | #endif |
| 6896 | #ifdef AI_DEFAULT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6897 | PyModule_AddIntMacro(m, AI_DEFAULT); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6898 | #endif |
| 6899 | #ifdef NI_MAXHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6900 | PyModule_AddIntMacro(m, NI_MAXHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6901 | #endif |
| 6902 | #ifdef NI_MAXSERV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6903 | PyModule_AddIntMacro(m, NI_MAXSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6904 | #endif |
| 6905 | #ifdef NI_NOFQDN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6906 | PyModule_AddIntMacro(m, NI_NOFQDN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6907 | #endif |
| 6908 | #ifdef NI_NUMERICHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6909 | PyModule_AddIntMacro(m, NI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6910 | #endif |
| 6911 | #ifdef NI_NAMEREQD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6912 | PyModule_AddIntMacro(m, NI_NAMEREQD); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6913 | #endif |
| 6914 | #ifdef NI_NUMERICSERV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6915 | PyModule_AddIntMacro(m, NI_NUMERICSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6916 | #endif |
| 6917 | #ifdef NI_DGRAM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6918 | PyModule_AddIntMacro(m, NI_DGRAM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6919 | #endif |
| 6920 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6921 | /* shutdown() parameters */ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6922 | #ifdef SHUT_RD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6923 | PyModule_AddIntMacro(m, SHUT_RD); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6924 | #elif defined(SD_RECEIVE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6925 | PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6926 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6927 | PyModule_AddIntConstant(m, "SHUT_RD", 0); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6928 | #endif |
| 6929 | #ifdef SHUT_WR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6930 | PyModule_AddIntMacro(m, SHUT_WR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6931 | #elif defined(SD_SEND) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6932 | PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6933 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6934 | PyModule_AddIntConstant(m, "SHUT_WR", 1); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6935 | #endif |
| 6936 | #ifdef SHUT_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6937 | PyModule_AddIntMacro(m, SHUT_RDWR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6938 | #elif defined(SD_BOTH) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6939 | PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6940 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6941 | PyModule_AddIntConstant(m, "SHUT_RDWR", 2); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6942 | #endif |
| 6943 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 6944 | #ifdef SIO_RCVALL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6945 | { |
| 6946 | DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS}; |
| 6947 | const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"}; |
| 6948 | int i; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6949 | for(i = 0; i<Py_ARRAY_LENGTH(codes); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6950 | PyObject *tmp; |
| 6951 | tmp = PyLong_FromUnsignedLong(codes[i]); |
| 6952 | if (tmp == NULL) |
| 6953 | return NULL; |
| 6954 | PyModule_AddObject(m, names[i], tmp); |
| 6955 | } |
| 6956 | } |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6957 | PyModule_AddIntMacro(m, RCVALL_OFF); |
| 6958 | PyModule_AddIntMacro(m, RCVALL_ON); |
| 6959 | PyModule_AddIntMacro(m, RCVALL_SOCKETLEVELONLY); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 6960 | #ifdef RCVALL_IPLEVEL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6961 | PyModule_AddIntMacro(m, RCVALL_IPLEVEL); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 6962 | #endif |
| 6963 | #ifdef RCVALL_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6964 | PyModule_AddIntMacro(m, RCVALL_MAX); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 6965 | #endif |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 6966 | #endif /* _MSTCPIP_ */ |
| 6967 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6968 | /* Initialize gethostbyname lock */ |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 6969 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6970 | netdb_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 6971 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6972 | return m; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 6973 | } |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 6974 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6975 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 6976 | #ifndef HAVE_INET_PTON |
Christian Heimes | 96e7b3d | 2007-11-20 06:51:17 +0000 | [diff] [blame] | 6977 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6978 | |
| 6979 | /* Simplistic emulation code for inet_pton that only works for IPv4 */ |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 6980 | /* These are not exposed because they do not set errno properly */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6981 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 6982 | int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6983 | inet_pton(int af, const char *src, void *dst) |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 6984 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6985 | if (af == AF_INET) { |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 6986 | #if (SIZEOF_INT != 4) |
| 6987 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 6988 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6989 | unsigned int packed_addr; |
| 6990 | packed_addr = inet_addr(src); |
| 6991 | if (packed_addr == INADDR_NONE) |
| 6992 | return 0; |
| 6993 | memcpy(dst, &packed_addr, 4); |
| 6994 | return 1; |
| 6995 | } |
| 6996 | /* Should set errno to EAFNOSUPPORT */ |
| 6997 | return -1; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 6998 | } |
| 6999 | |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 7000 | const char * |
| 7001 | 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] | 7002 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7003 | if (af == AF_INET) { |
| 7004 | struct in_addr packed_addr; |
| 7005 | if (size < 16) |
| 7006 | /* Should set errno to ENOSPC. */ |
| 7007 | return NULL; |
| 7008 | memcpy(&packed_addr, src, sizeof(packed_addr)); |
| 7009 | return strncpy(dst, inet_ntoa(packed_addr), size); |
| 7010 | } |
| 7011 | /* Should set errno to EAFNOSUPPORT */ |
| 7012 | return NULL; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7013 | } |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 7014 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7015 | #endif |
Christian Heimes | b615069 | 2007-11-15 23:37:07 +0000 | [diff] [blame] | 7016 | #endif |