Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1 | /* Socket module */ |
| 2 | |
| 3 | /* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5 | This module provides an interface to Berkeley socket IPC. |
| 6 | |
| 7 | Limitations: |
| 8 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 9 | - Only AF_INET, AF_INET6 and AF_UNIX address families are supported in a |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 10 | portable manner, though AF_PACKET, AF_NETLINK and AF_TIPC are supported |
| 11 | under Linux. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 12 | - No read/write operations (use sendall/recv or makefile instead). |
| 13 | - Additional restrictions apply on some non-Unix platforms (compensated |
| 14 | for by socket.py). |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 16 | Module interface: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 17 | |
Andrew Svetlov | 0832af6 | 2012-12-18 23:10:48 +0200 | [diff] [blame] | 18 | - socket.error: exception raised for socket specific errors, alias for OSError |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 19 | - socket.gaierror: exception raised for getaddrinfo/getnameinfo errors, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 20 | a subclass of socket.error |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 21 | - socket.herror: exception raised for gethostby* errors, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | a subclass of socket.error |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 23 | - socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd') |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 24 | - socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...]) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 25 | - socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com') |
Guido van Rossum | 25405c7 | 1996-12-19 16:42:52 +0000 | [diff] [blame] | 26 | - socket.getprotobyname(protocolname) --> protocol number |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 27 | - socket.getservbyname(servicename[, protocolname]) --> port number |
| 28 | - socket.getservbyport(portnumber[, protocolname]) --> service name |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 29 | - socket.socket([family[, type [, proto, fileno]]]) --> new socket object |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 30 | (fileno specifies a pre-existing socket file descriptor) |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 31 | - socket.socketpair([family[, type [, proto]]]) --> (socket, socket) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 32 | - socket.ntohs(16 bit value) --> new int object |
| 33 | - socket.ntohl(32 bit value) --> new int object |
| 34 | - socket.htons(16 bit value) --> new int object |
| 35 | - socket.htonl(32 bit value) --> new int object |
Berker Peksag | a6ec5ee | 2014-06-30 11:28:40 +0300 | [diff] [blame] | 36 | - socket.getaddrinfo(host, port [, family, type, proto, flags]) |
| 37 | --> List of (family, type, proto, canonname, sockaddr) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 38 | - socket.getnameinfo(sockaddr, flags) --> (host, port) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 39 | - socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h> |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 40 | - socket.has_ipv6: boolean value indicating if IPv6 is supported |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 41 | - socket.inet_aton(IP address) -> 32-bit packed IP representation |
| 42 | - socket.inet_ntoa(packed IP) -> IP address string |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 43 | - socket.getdefaulttimeout() -> None | float |
| 44 | - socket.setdefaulttimeout(None | float) |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 45 | - socket.if_nameindex() -> list of tuples (if_index, if_name) |
| 46 | - socket.if_nametoindex(name) -> corresponding interface index |
| 47 | - socket.if_indextoname(index) -> corresponding interface name |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 48 | - an Internet socket address is a pair (hostname, port) |
| 49 | where hostname can be anything recognized by gethostbyname() |
| 50 | (including the dd.dd.dd.dd notation) and port is in host byte order |
| 51 | - where a hostname is returned, the dd.dd.dd.dd notation is used |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 52 | - a UNIX domain socket address is a string specifying the pathname |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 53 | - an AF_PACKET socket address is a tuple containing a string |
| 54 | specifying the ethernet interface and an integer specifying |
| 55 | the Ethernet protocol number to be received. For example: |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 56 | ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple |
Andrew M. Kuchling | b8e1717 | 2004-07-10 23:39:35 +0000 | [diff] [blame] | 57 | specify packet-type and ha-type/addr. |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 58 | - an AF_TIPC socket address is expressed as |
| 59 | (addr_type, v1, v2, v3 [, scope]); where addr_type can be one of: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, and TIPC_ADDR_ID; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 61 | and scope can be one of: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and TIPC_NODE_SCOPE. |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 63 | The meaning of v1, v2 and v3 depends on the value of addr_type: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | if addr_type is TIPC_ADDR_NAME: |
| 65 | v1 is the server type |
| 66 | v2 is the port identifier |
| 67 | v3 is ignored |
| 68 | if addr_type is TIPC_ADDR_NAMESEQ: |
| 69 | v1 is the server type |
| 70 | v2 is the lower port number |
| 71 | v3 is the upper port number |
| 72 | if addr_type is TIPC_ADDR_ID: |
| 73 | v1 is the node |
| 74 | v2 is the ref |
| 75 | v3 is ignored |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 76 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 78 | Local naming conventions: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 80 | - names starting with sock_ are socket object methods |
| 81 | - names starting with socket_ are module-level functions |
| 82 | - names starting with PySocket are exported through socketmodule.h |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 84 | */ |
| 85 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 86 | #ifdef __APPLE__ |
| 87 | /* |
| 88 | * inet_aton is not available on OSX 10.3, yet we want to use a binary |
| 89 | * that was build on 10.4 or later to work on that release, weak linking |
| 90 | * comes to the rescue. |
| 91 | */ |
| 92 | # pragma weak inet_aton |
| 93 | #endif |
| 94 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 95 | #include "Python.h" |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 96 | #include "structmember.h" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 97 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 98 | /* Socket object documentation */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 99 | PyDoc_STRVAR(sock_doc, |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 100 | "socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None) -> socket object\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 101 | \n\ |
| 102 | Open a socket of the given type. The family argument specifies the\n\ |
| 103 | address family; it defaults to AF_INET. The type argument specifies\n\ |
| 104 | whether this is a stream (SOCK_STREAM, this is the default)\n\ |
| 105 | or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\ |
| 106 | specifying the default protocol. Keyword arguments are accepted.\n\ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 107 | The socket is created as non-inheritable.\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 108 | \n\ |
| 109 | A socket object represents one endpoint of a network connection.\n\ |
| 110 | \n\ |
| 111 | Methods of socket objects (keyword arguments not allowed):\n\ |
| 112 | \n\ |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 113 | _accept() -- accept connection, returning new socket fd and client address\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 114 | bind(addr) -- bind the socket to a local address\n\ |
| 115 | close() -- close the socket\n\ |
| 116 | connect(addr) -- connect the socket to a remote address\n\ |
| 117 | connect_ex(addr) -- connect, return an error code instead of an exception\n\ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 118 | dup() -- return a new socket fd duplicated from fileno()\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 119 | fileno() -- return underlying file descriptor\n\ |
| 120 | getpeername() -- return remote address [*]\n\ |
| 121 | getsockname() -- return local address\n\ |
| 122 | getsockopt(level, optname[, buflen]) -- get socket options\n\ |
| 123 | gettimeout() -- return timeout or None\n\ |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 124 | listen([n]) -- start listening for incoming connections\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 125 | recv(buflen[, flags]) -- receive data\n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 126 | recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 127 | recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 128 | recvfrom_into(buffer[, nbytes, [, flags])\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 129 | -- receive data and sender\'s address (into a buffer)\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 130 | sendall(data[, flags]) -- send all data\n\ |
| 131 | send(data[, flags]) -- send data, may not send all of it\n\ |
| 132 | sendto(data[, flags], addr) -- send data to a given address\n\ |
| 133 | setblocking(0 | 1) -- set or clear the blocking I/O flag\n\ |
| 134 | setsockopt(level, optname, value) -- set socket options\n\ |
| 135 | settimeout(None | float) -- set or clear the timeout\n\ |
| 136 | shutdown(how) -- shut down traffic in one or both directions\n\ |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 137 | if_nameindex() -- return all network interface indices and names\n\ |
Gregory P. Smith | b6471db | 2011-05-22 22:47:55 -0700 | [diff] [blame] | 138 | if_nametoindex(name) -- return the corresponding interface index\n\ |
| 139 | if_indextoname(index) -- return the corresponding interface name\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 140 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 141 | [*] not available on all platforms!"); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 142 | |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 143 | /* XXX This is a terrible mess of platform-dependent preprocessor hacks. |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 144 | I hope some day someone can clean this up please... */ |
| 145 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 146 | /* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure |
| 147 | script doesn't get this right, so we hardcode some platform checks below. |
| 148 | On the other hand, not all Linux versions agree, so there the settings |
| 149 | computed by the configure script are needed! */ |
| 150 | |
| 151 | #ifndef linux |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 152 | # undef HAVE_GETHOSTBYNAME_R_3_ARG |
| 153 | # undef HAVE_GETHOSTBYNAME_R_5_ARG |
| 154 | # undef HAVE_GETHOSTBYNAME_R_6_ARG |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 155 | #endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 156 | |
Victor Stinner | 710d27e | 2011-08-23 10:57:32 +0200 | [diff] [blame] | 157 | #if defined(__OpenBSD__) |
| 158 | # include <sys/uio.h> |
| 159 | #endif |
| 160 | |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 161 | #ifndef WITH_THREAD |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 162 | # undef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 163 | #endif |
| 164 | |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 165 | #ifdef HAVE_GETHOSTBYNAME_R |
Jesus Cea | 736e7fc | 2011-03-14 17:36:54 +0100 | [diff] [blame] | 166 | # if defined(_AIX) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 167 | # define HAVE_GETHOSTBYNAME_R_3_ARG |
| 168 | # elif defined(__sun) || defined(__sgi) |
| 169 | # define HAVE_GETHOSTBYNAME_R_5_ARG |
| 170 | # elif defined(linux) |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 171 | /* Rely on the configure script */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 172 | # else |
| 173 | # undef HAVE_GETHOSTBYNAME_R |
| 174 | # endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 175 | #endif |
| 176 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 177 | #if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \ |
| 178 | !defined(MS_WINDOWS) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 179 | # define USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 180 | #endif |
| 181 | |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 182 | /* To use __FreeBSD_version */ |
| 183 | #ifdef HAVE_SYS_PARAM_H |
| 184 | #include <sys/param.h> |
| 185 | #endif |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 186 | /* On systems on which getaddrinfo() is believed to not be thread-safe, |
Just van Rossum | 09aecd7 | 2003-05-09 08:03:44 +0000 | [diff] [blame] | 187 | (this includes the getaddrinfo emulation) protect access with a lock. */ |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 188 | #if defined(WITH_THREAD) && (defined(__APPLE__) || \ |
| 189 | (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 190 | defined(__OpenBSD__) || defined(__NetBSD__) || \ |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 191 | !defined(HAVE_GETADDRINFO)) |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 192 | #define USE_GETADDRINFO_LOCK |
| 193 | #endif |
| 194 | |
| 195 | #ifdef USE_GETADDRINFO_LOCK |
| 196 | #define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1); |
| 197 | #define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock); |
| 198 | #else |
| 199 | #define ACQUIRE_GETADDRINFO_LOCK |
| 200 | #define RELEASE_GETADDRINFO_LOCK |
| 201 | #endif |
| 202 | |
| 203 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 204 | # include "pythread.h" |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 205 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 206 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 207 | #if defined(PYCC_VACPP) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 208 | # include <types.h> |
| 209 | # include <io.h> |
| 210 | # include <sys/ioctl.h> |
| 211 | # include <utils.h> |
| 212 | # include <ctype.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 213 | #endif |
| 214 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 215 | #ifdef __APPLE__ |
| 216 | # include <sys/ioctl.h> |
| 217 | #endif |
| 218 | |
| 219 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 220 | #if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 221 | /* make sure that the reentrant (gethostbyaddr_r etc) |
| 222 | functions are declared correctly if compiling with |
| 223 | MIPSPro 7.x in ANSI C mode (default) */ |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 224 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 225 | /* XXX Using _SGIAPI is the wrong thing, |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 226 | but I don't know what the right thing is. */ |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 227 | #undef _SGIAPI /* to avoid warning */ |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 228 | #define _SGIAPI 1 |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 229 | |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 230 | #undef _XOPEN_SOURCE |
| 231 | #include <sys/socket.h> |
| 232 | #include <sys/types.h> |
| 233 | #include <netinet/in.h> |
| 234 | #ifdef _SS_ALIGNSIZE |
| 235 | #define HAVE_GETADDRINFO 1 |
| 236 | #define HAVE_GETNAMEINFO 1 |
| 237 | #endif |
| 238 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 239 | #define HAVE_INET_PTON |
| 240 | #include <netdb.h> |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 241 | #endif |
| 242 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 243 | /* Irix 6.5 fails to define this variable at all. This is needed |
| 244 | for both GCC and SGI's compiler. I'd say that the SGI headers |
Georg Brandl | dbd8339 | 2006-02-20 09:42:33 +0000 | [diff] [blame] | 245 | are just busted. Same thing for Solaris. */ |
| 246 | #if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN) |
Anthony Baxter | bab23cf | 2003-10-04 08:00:49 +0000 | [diff] [blame] | 247 | #define INET_ADDRSTRLEN 16 |
| 248 | #endif |
| 249 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 250 | /* Generic includes */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 251 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 252 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 253 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 254 | |
Gregory P. Smith | 3b1f2c3 | 2011-05-15 12:18:23 -0700 | [diff] [blame] | 255 | #ifdef HAVE_SYS_SOCKET_H |
| 256 | #include <sys/socket.h> |
| 257 | #endif |
| 258 | |
| 259 | #ifdef HAVE_NET_IF_H |
| 260 | #include <net/if.h> |
| 261 | #endif |
| 262 | |
Marc-André Lemburg | 976ade6 | 2002-02-16 18:47:07 +0000 | [diff] [blame] | 263 | /* Generic socket object definitions and includes */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 264 | #define PySocket_BUILDING_SOCKET |
Marc-André Lemburg | bb8b78b | 2002-02-16 18:44:52 +0000 | [diff] [blame] | 265 | #include "socketmodule.h" |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 266 | |
| 267 | /* Addressing includes */ |
| 268 | |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 269 | #ifndef MS_WINDOWS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 270 | |
| 271 | /* Non-MS WINDOWS includes */ |
| 272 | # include <netdb.h> |
Nick Coghlan | 4ecf6cf | 2011-08-22 15:45:19 +1000 | [diff] [blame] | 273 | # include <unistd.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 274 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 275 | /* Headers needed for inet_ntoa() and inet_addr() */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 276 | # include <arpa/inet.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 277 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 278 | # include <fcntl.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 279 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 280 | #else |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 281 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 282 | /* MS_WINDOWS includes */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 283 | # ifdef HAVE_FCNTL_H |
| 284 | # include <fcntl.h> |
| 285 | # endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 286 | |
Steve Dower | 65e4cb1 | 2014-11-22 12:54:57 -0800 | [diff] [blame] | 287 | #if defined(_MSC_VER) && _MSC_VER >= 1800 |
| 288 | /* Provides the IsWindows7SP1OrGreater() function */ |
| 289 | #include <VersionHelpers.h> |
| 290 | #endif |
| 291 | |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 292 | #endif |
| 293 | |
Skip Montanaro | 7befb99 | 2004-02-10 16:50:21 +0000 | [diff] [blame] | 294 | #include <stddef.h> |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 295 | |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 296 | #ifndef O_NONBLOCK |
| 297 | # define O_NONBLOCK O_NDELAY |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 298 | #endif |
| 299 | |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 300 | /* include Python's addrinfo.h unless it causes trouble */ |
| 301 | #if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE) |
| 302 | /* Do not include addinfo.h on some newer IRIX versions. |
| 303 | * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21, |
| 304 | * for example, but not by 6.5.10. |
| 305 | */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 306 | #elif defined(_MSC_VER) && _MSC_VER>1201 |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 307 | /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and |
| 308 | * EAI_* constants are defined in (the already included) ws2tcpip.h. |
| 309 | */ |
| 310 | #else |
| 311 | # include "addrinfo.h" |
| 312 | #endif |
Jason Tishler | c246cb7 | 2004-08-09 13:25:59 +0000 | [diff] [blame] | 313 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 314 | #ifndef HAVE_INET_PTON |
Christian Heimes | 96e7b3d | 2007-11-20 06:51:17 +0000 | [diff] [blame] | 315 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 316 | int inet_pton(int af, const char *src, void *dst); |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 317 | const char *inet_ntop(int af, const void *src, char *dst, socklen_t size); |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 318 | #endif |
Christian Heimes | b615069 | 2007-11-15 23:37:07 +0000 | [diff] [blame] | 319 | #endif |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 320 | |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 321 | #ifdef __APPLE__ |
| 322 | /* On OS X, getaddrinfo returns no error indication of lookup |
| 323 | failure, so we must use the emulation instead of the libinfo |
| 324 | implementation. Unfortunately, performing an autoconf test |
| 325 | for this bug would require DNS access for the machine performing |
| 326 | the configuration, which is not acceptable. Therefore, we |
| 327 | determine the bug just by checking for __APPLE__. If this bug |
| 328 | gets ever fixed, perhaps checking for sys/version.h would be |
| 329 | appropriate, which is 10/0 on the system with the bug. */ |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 330 | #ifndef HAVE_GETNAMEINFO |
| 331 | /* This bug seems to be fixed in Jaguar. Ths easiest way I could |
| 332 | Find to check for Jaguar is that it has getnameinfo(), which |
| 333 | older releases don't have */ |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 334 | #undef HAVE_GETADDRINFO |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 335 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 336 | |
| 337 | #ifdef HAVE_INET_ATON |
| 338 | #define USE_INET_ATON_WEAKLINK |
| 339 | #endif |
| 340 | |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 341 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 342 | |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 343 | /* I know this is a bad practice, but it is the easiest... */ |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 344 | #if !defined(HAVE_GETADDRINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 345 | /* avoid clashes with the C library definition of the symbol. */ |
| 346 | #define getaddrinfo fake_getaddrinfo |
| 347 | #define gai_strerror fake_gai_strerror |
| 348 | #define freeaddrinfo fake_freeaddrinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 349 | #include "getaddrinfo.c" |
| 350 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 351 | #if !defined(HAVE_GETNAMEINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 352 | #define getnameinfo fake_getnameinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 353 | #include "getnameinfo.c" |
| 354 | #endif |
| 355 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 356 | #ifdef MS_WINDOWS |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 357 | #define SOCKETCLOSE closesocket |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 358 | #endif |
| 359 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 360 | #ifdef MS_WIN32 |
Kristján Valur Jónsson | 310052c | 2012-03-31 13:35:00 +0000 | [diff] [blame] | 361 | #undef EAFNOSUPPORT |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 362 | #define EAFNOSUPPORT WSAEAFNOSUPPORT |
| 363 | #define snprintf _snprintf |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 364 | #endif |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 365 | |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 366 | #ifndef SOCKETCLOSE |
| 367 | #define SOCKETCLOSE close |
| 368 | #endif |
| 369 | |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 370 | #if (defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H)) && !defined(__NetBSD__) && !defined(__DragonFly__) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 371 | #define USE_BLUETOOTH 1 |
| 372 | #if defined(__FreeBSD__) |
| 373 | #define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP |
| 374 | #define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 375 | #define BTPROTO_HCI BLUETOOTH_PROTO_HCI |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 376 | #define SOL_HCI SOL_HCI_RAW |
| 377 | #define HCI_FILTER SO_HCI_RAW_FILTER |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 378 | #define sockaddr_l2 sockaddr_l2cap |
| 379 | #define sockaddr_rc sockaddr_rfcomm |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 380 | #define hci_dev hci_node |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 381 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb) |
| 382 | #define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 383 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 384 | #elif defined(__NetBSD__) || defined(__DragonFly__) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 385 | #define sockaddr_l2 sockaddr_bt |
| 386 | #define sockaddr_rc sockaddr_bt |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 387 | #define sockaddr_hci sockaddr_bt |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 388 | #define sockaddr_sco sockaddr_bt |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 389 | #define SOL_HCI BTPROTO_HCI |
| 390 | #define HCI_DATA_DIR SO_HCI_DIRECTION |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 391 | #define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb) |
| 392 | #define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 393 | #define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 394 | #define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 395 | #else |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 396 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb) |
| 397 | #define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 398 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 399 | #define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb) |
| 400 | #endif |
| 401 | #endif |
| 402 | |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 403 | /* Convert "sock_addr_t *" to "struct sockaddr *". */ |
| 404 | #define SAS2SA(x) (&((x)->sa)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 405 | |
Martin v. Löwis | e941617 | 2003-05-03 10:12:45 +0000 | [diff] [blame] | 406 | /* |
| 407 | * Constants for getnameinfo() |
| 408 | */ |
| 409 | #if !defined(NI_MAXHOST) |
| 410 | #define NI_MAXHOST 1025 |
| 411 | #endif |
| 412 | #if !defined(NI_MAXSERV) |
| 413 | #define NI_MAXSERV 32 |
| 414 | #endif |
| 415 | |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 416 | #ifndef INVALID_SOCKET /* MS defines this */ |
| 417 | #define INVALID_SOCKET (-1) |
| 418 | #endif |
| 419 | |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 420 | #ifndef INADDR_NONE |
| 421 | #define INADDR_NONE (-1) |
| 422 | #endif |
| 423 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 424 | /* XXX There's a problem here: *static* functions are not supposed to have |
| 425 | a Py prefix (or use CapitalizedWords). Later... */ |
| 426 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 427 | /* Global variable holding the exception type for errors detected |
| 428 | by this module (but not argument type or memory errors, etc.). */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 429 | static PyObject *socket_herror; |
| 430 | static PyObject *socket_gaierror; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 431 | static PyObject *socket_timeout; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 432 | |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 433 | /* A forward reference to the socket type object. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 434 | The sock_type variable contains pointers to various functions, |
| 435 | some of which call new_sockobject(), which uses sock_type, so |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 436 | there has to be a circular reference. */ |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 437 | static PyTypeObject sock_type; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 438 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 439 | #if defined(HAVE_POLL_H) |
| 440 | #include <poll.h> |
| 441 | #elif defined(HAVE_SYS_POLL_H) |
| 442 | #include <sys/poll.h> |
| 443 | #endif |
| 444 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 445 | /* Largest value to try to store in a socklen_t (used when handling |
| 446 | ancillary data). POSIX requires socklen_t to hold at least |
| 447 | (2**31)-1 and recommends against storing larger values, but |
| 448 | socklen_t was originally int in the BSD interface, so to be on the |
| 449 | safe side we use the smaller of (2**31)-1 and INT_MAX. */ |
| 450 | #if INT_MAX > 0x7fffffff |
| 451 | #define SOCKLEN_T_LIMIT 0x7fffffff |
| 452 | #else |
| 453 | #define SOCKLEN_T_LIMIT INT_MAX |
| 454 | #endif |
| 455 | |
Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 456 | #ifdef HAVE_POLL |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 457 | /* Instead of select(), we'll use poll() since poll() works on any fd. */ |
| 458 | #define IS_SELECTABLE(s) 1 |
| 459 | /* Can we call select() with this socket without a buffer overrun? */ |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 460 | #else |
Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 461 | /* If there's no timeout left, we don't have to call select, so it's a safe, |
| 462 | * little white lie. */ |
| 463 | #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] | 464 | #endif |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 465 | |
| 466 | static PyObject* |
| 467 | select_error(void) |
| 468 | { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 469 | PyErr_SetString(PyExc_OSError, "unable to select on socket"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | return NULL; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 473 | #ifdef MS_WINDOWS |
Antoine Pitrou | c8f0196 | 2010-09-28 22:03:27 +0000 | [diff] [blame] | 474 | #ifndef WSAEAGAIN |
| 475 | #define WSAEAGAIN WSAEWOULDBLOCK |
| 476 | #endif |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 477 | #define CHECK_ERRNO(expected) \ |
| 478 | (WSAGetLastError() == WSA ## expected) |
| 479 | #else |
| 480 | #define CHECK_ERRNO(expected) \ |
| 481 | (errno == expected) |
| 482 | #endif |
| 483 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 484 | #ifdef MS_WINDOWS |
| 485 | /* Does WSASocket() support the WSA_FLAG_NO_HANDLE_INHERIT flag? */ |
| 486 | static int support_wsa_no_inherit = -1; |
| 487 | #endif |
| 488 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 489 | /* Convenience function to raise an error according to errno |
| 490 | and return a NULL pointer from a function. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 491 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 492 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 493 | set_error(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 494 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 495 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | int err_no = WSAGetLastError(); |
| 497 | /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which |
| 498 | recognizes the error codes used by both GetLastError() and |
| 499 | WSAGetLastError */ |
| 500 | if (err_no) |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 501 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, err_no); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 502 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 503 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 504 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 507 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 508 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 509 | set_herror(int h_error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 510 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 511 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 512 | |
| 513 | #ifdef HAVE_HSTRERROR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 514 | v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 515 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 516 | v = Py_BuildValue("(is)", h_error, "host not found"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 517 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | if (v != NULL) { |
| 519 | PyErr_SetObject(socket_herror, v); |
| 520 | Py_DECREF(v); |
| 521 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 523 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | |
| 527 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 528 | set_gaierror(int error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 529 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 530 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 531 | |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 532 | #ifdef EAI_SYSTEM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 533 | /* EAI_SYSTEM is not available on Windows XP. */ |
| 534 | if (error == EAI_SYSTEM) |
| 535 | return set_error(); |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 536 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 537 | |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 538 | #ifdef HAVE_GAI_STRERROR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 | v = Py_BuildValue("(is)", error, gai_strerror(error)); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 540 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 541 | v = Py_BuildValue("(is)", error, "getaddrinfo failed"); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 542 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 543 | if (v != NULL) { |
| 544 | PyErr_SetObject(socket_gaierror, v); |
| 545 | Py_DECREF(v); |
| 546 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 548 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 551 | /* Function to perform the setting of socket blocking mode |
| 552 | internally. block = (1 | 0). */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 553 | static int |
| 554 | internal_setblocking(PySocketSockObject *s, int block) |
| 555 | { |
Victor Stinner | b6dab6b | 2014-07-23 22:56:55 +0200 | [diff] [blame] | 556 | #ifdef MS_WINDOWS |
| 557 | u_long arg; |
| 558 | #endif |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 559 | #if !defined(MS_WINDOWS) \ |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 560 | && !((defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO))) |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 561 | int delay_flag, new_delay_flag; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 562 | #endif |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 563 | #ifdef SOCK_NONBLOCK |
| 564 | if (block) |
| 565 | s->sock_type &= (~SOCK_NONBLOCK); |
| 566 | else |
| 567 | s->sock_type |= SOCK_NONBLOCK; |
| 568 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 569 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 570 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 571 | #ifndef MS_WINDOWS |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 572 | #if (defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 573 | block = !block; |
| 574 | ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block); |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 575 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 576 | delay_flag = fcntl(s->sock_fd, F_GETFL, 0); |
| 577 | if (block) |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 578 | new_delay_flag = delay_flag & (~O_NONBLOCK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 579 | else |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 580 | new_delay_flag = delay_flag | O_NONBLOCK; |
| 581 | if (new_delay_flag != delay_flag) |
| 582 | fcntl(s->sock_fd, F_SETFL, new_delay_flag); |
| 583 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 584 | #else /* MS_WINDOWS */ |
Victor Stinner | b6dab6b | 2014-07-23 22:56:55 +0200 | [diff] [blame] | 585 | arg = !block; |
| 586 | ioctlsocket(s->sock_fd, FIONBIO, &arg); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 587 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 588 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 589 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 590 | /* Since these don't return anything */ |
| 591 | return 1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 594 | /* Do a select()/poll() on the socket, if necessary (sock_timeout > 0). |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 595 | The argument writing indicates the direction. |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 596 | This does not raise an exception; we'll let our caller do that |
| 597 | after they've reacquired the interpreter lock. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 598 | Returns 1 on timeout, -1 on error, 0 otherwise. */ |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 599 | static int |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 600 | internal_select_ex(PySocketSockObject *s, int writing, double interval) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 601 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 602 | int n; |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 604 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
| 605 | if (s->sock_timeout <= 0.0) |
| 606 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 608 | /* Guard against closed socket */ |
| 609 | if (s->sock_fd < 0) |
| 610 | return 0; |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 611 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 612 | /* Handling this condition here simplifies the select loops */ |
| 613 | if (interval < 0.0) |
| 614 | return 1; |
| 615 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 616 | /* Prefer poll, if available, since you can poll() any fd |
| 617 | * which can't be done with select(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 618 | #ifdef HAVE_POLL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | { |
| 620 | struct pollfd pollfd; |
| 621 | int timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 622 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | pollfd.fd = s->sock_fd; |
| 624 | pollfd.events = writing ? POLLOUT : POLLIN; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 625 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 626 | /* s->sock_timeout is in seconds, timeout in ms */ |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 627 | timeout = (int)(interval * 1000 + 0.5); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 628 | n = poll(&pollfd, 1, timeout); |
| 629 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 630 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 631 | { |
| 632 | /* Construct the arguments to select */ |
| 633 | fd_set fds; |
| 634 | struct timeval tv; |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 635 | tv.tv_sec = (int)interval; |
| 636 | tv.tv_usec = (int)((interval - tv.tv_sec) * 1e6); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 637 | FD_ZERO(&fds); |
| 638 | FD_SET(s->sock_fd, &fds); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 640 | /* See if the socket is ready */ |
| 641 | if (writing) |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 642 | n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 643 | NULL, &fds, NULL, &tv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 644 | else |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 645 | n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 646 | &fds, NULL, NULL, &tv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 648 | #endif |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 649 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 650 | if (n < 0) |
| 651 | return -1; |
| 652 | if (n == 0) |
| 653 | return 1; |
| 654 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 657 | static int |
| 658 | internal_select(PySocketSockObject *s, int writing) |
| 659 | { |
| 660 | return internal_select_ex(s, writing, s->sock_timeout); |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | Two macros for automatic retry of select() in case of false positives |
| 665 | (for example, select() could indicate a socket is ready for reading |
| 666 | but the data then discarded by the OS because of a wrong checksum). |
| 667 | Here is an example of use: |
| 668 | |
| 669 | BEGIN_SELECT_LOOP(s) |
| 670 | Py_BEGIN_ALLOW_THREADS |
| 671 | timeout = internal_select_ex(s, 0, interval); |
| 672 | if (!timeout) |
| 673 | outlen = recv(s->sock_fd, cbuf, len, flags); |
| 674 | Py_END_ALLOW_THREADS |
| 675 | if (timeout == 1) { |
| 676 | PyErr_SetString(socket_timeout, "timed out"); |
| 677 | return -1; |
| 678 | } |
| 679 | END_SELECT_LOOP(s) |
| 680 | */ |
| 681 | |
| 682 | #define BEGIN_SELECT_LOOP(s) \ |
| 683 | { \ |
| 684 | _PyTime_timeval now, deadline = {0, 0}; \ |
| 685 | double interval = s->sock_timeout; \ |
| 686 | int has_timeout = s->sock_timeout > 0.0; \ |
| 687 | if (has_timeout) { \ |
Victor Stinner | ae58649 | 2014-09-02 23:18:25 +0200 | [diff] [blame] | 688 | _PyTime_monotonic(&now); \ |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 689 | deadline = now; \ |
| 690 | _PyTime_ADD_SECONDS(deadline, s->sock_timeout); \ |
| 691 | } \ |
| 692 | while (1) { \ |
| 693 | errno = 0; \ |
| 694 | |
| 695 | #define END_SELECT_LOOP(s) \ |
| 696 | if (!has_timeout || \ |
| 697 | (!CHECK_ERRNO(EWOULDBLOCK) && !CHECK_ERRNO(EAGAIN))) \ |
| 698 | break; \ |
Victor Stinner | ae58649 | 2014-09-02 23:18:25 +0200 | [diff] [blame] | 699 | _PyTime_monotonic(&now); \ |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 700 | interval = _PyTime_INTERVAL(now, deadline); \ |
| 701 | } \ |
| 702 | } \ |
| 703 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 704 | /* Initialize a new socket object. */ |
| 705 | |
Tim Peters | a12b4cf | 2002-07-18 22:38:44 +0000 | [diff] [blame] | 706 | static double defaulttimeout = -1.0; /* Default timeout for new sockets */ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 707 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 708 | static void |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 709 | init_sockobject(PySocketSockObject *s, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 710 | SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 711 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 712 | s->sock_fd = fd; |
| 713 | s->sock_family = family; |
| 714 | s->sock_type = type; |
| 715 | s->sock_proto = proto; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 716 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 717 | s->errorhandler = &set_error; |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 718 | #ifdef SOCK_NONBLOCK |
| 719 | if (type & SOCK_NONBLOCK) |
| 720 | s->sock_timeout = 0.0; |
| 721 | else |
| 722 | #endif |
| 723 | { |
| 724 | s->sock_timeout = defaulttimeout; |
| 725 | if (defaulttimeout >= 0.0) |
| 726 | internal_setblocking(s, 0); |
| 727 | } |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 728 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 732 | /* Create a new socket object. |
| 733 | This just creates the object and initializes it. |
| 734 | If the creation fails, return NULL and set an exception (implicit |
| 735 | in NEWOBJ()). */ |
| 736 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 737 | static PySocketSockObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 738 | new_sockobject(SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 739 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 740 | PySocketSockObject *s; |
| 741 | s = (PySocketSockObject *) |
| 742 | PyType_GenericNew(&sock_type, NULL, NULL); |
| 743 | if (s != NULL) |
| 744 | init_sockobject(s, fd, family, type, proto); |
| 745 | return s; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 748 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 749 | /* Lock to allow python interpreter to continue, but only allow one |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 750 | thread to be in gethostbyname or getaddrinfo */ |
| 751 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Matthias Klose | d182a6c | 2012-08-14 17:29:04 +0200 | [diff] [blame] | 752 | static PyThread_type_lock netdb_lock; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 753 | #endif |
| 754 | |
| 755 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 756 | /* Convert a string specifying a host name or one of a few symbolic |
| 757 | names to a numeric IP address. This usually calls gethostbyname() |
| 758 | to do the work; the names "" and "<broadcast>" are special. |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 759 | Return the length (IPv4 should be 4 bytes), or negative if |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 760 | an error occurred; then an exception is raised. */ |
| 761 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 762 | static int |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 763 | 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] | 764 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 765 | struct addrinfo hints, *res; |
| 766 | int error; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 767 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 768 | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
| 769 | if (name[0] == '\0') { |
| 770 | int siz; |
| 771 | memset(&hints, 0, sizeof(hints)); |
| 772 | hints.ai_family = af; |
| 773 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
| 774 | hints.ai_flags = AI_PASSIVE; |
| 775 | Py_BEGIN_ALLOW_THREADS |
| 776 | ACQUIRE_GETADDRINFO_LOCK |
| 777 | error = getaddrinfo(NULL, "0", &hints, &res); |
| 778 | Py_END_ALLOW_THREADS |
| 779 | /* We assume that those thread-unsafe getaddrinfo() versions |
| 780 | *are* safe regarding their return value, ie. that a |
| 781 | subsequent call to getaddrinfo() does not destroy the |
| 782 | outcome of the first call. */ |
| 783 | RELEASE_GETADDRINFO_LOCK |
| 784 | if (error) { |
| 785 | set_gaierror(error); |
| 786 | return -1; |
| 787 | } |
| 788 | switch (res->ai_family) { |
| 789 | case AF_INET: |
| 790 | siz = 4; |
| 791 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 792 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 793 | case AF_INET6: |
| 794 | siz = 16; |
| 795 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 796 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 797 | default: |
| 798 | freeaddrinfo(res); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 799 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 800 | "unsupported address family"); |
| 801 | return -1; |
| 802 | } |
| 803 | if (res->ai_next) { |
| 804 | freeaddrinfo(res); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 805 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 806 | "wildcard resolved to multiple address"); |
| 807 | return -1; |
| 808 | } |
| 809 | if (res->ai_addrlen < addr_ret_size) |
| 810 | addr_ret_size = res->ai_addrlen; |
| 811 | memcpy(addr_ret, res->ai_addr, addr_ret_size); |
| 812 | freeaddrinfo(res); |
| 813 | return siz; |
| 814 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 815 | /* special-case broadcast - inet_addr() below can return INADDR_NONE for |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 816 | * this */ |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 817 | if (strcmp(name, "255.255.255.255") == 0 || |
| 818 | strcmp(name, "<broadcast>") == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 819 | struct sockaddr_in *sin; |
| 820 | if (af != AF_INET && af != AF_UNSPEC) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 821 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 822 | "address family mismatched"); |
| 823 | return -1; |
| 824 | } |
| 825 | sin = (struct sockaddr_in *)addr_ret; |
| 826 | memset((void *) sin, '\0', sizeof(*sin)); |
| 827 | sin->sin_family = AF_INET; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 828 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 829 | sin->sin_len = sizeof(*sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 830 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 831 | sin->sin_addr.s_addr = INADDR_BROADCAST; |
| 832 | return sizeof(sin->sin_addr); |
| 833 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 834 | |
| 835 | /* avoid a name resolution in case of numeric address */ |
| 836 | #ifdef HAVE_INET_PTON |
| 837 | /* check for an IPv4 address */ |
| 838 | if (af == AF_UNSPEC || af == AF_INET) { |
| 839 | struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret; |
| 840 | memset(sin, 0, sizeof(*sin)); |
| 841 | if (inet_pton(AF_INET, name, &sin->sin_addr) > 0) { |
| 842 | sin->sin_family = AF_INET; |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 843 | #ifdef HAVE_SOCKADDR_SA_LEN |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 844 | sin->sin_len = sizeof(*sin); |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 845 | #endif |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 846 | return 4; |
| 847 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 848 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 849 | #ifdef ENABLE_IPV6 |
| 850 | /* check for an IPv6 address - if the address contains a scope ID, we |
| 851 | * fallback to getaddrinfo(), which can handle translation from interface |
| 852 | * name to interface index */ |
| 853 | if ((af == AF_UNSPEC || af == AF_INET6) && !strchr(name, '%')) { |
| 854 | struct sockaddr_in6 *sin = (struct sockaddr_in6 *)addr_ret; |
| 855 | memset(sin, 0, sizeof(*sin)); |
| 856 | if (inet_pton(AF_INET6, name, &sin->sin6_addr) > 0) { |
| 857 | sin->sin6_family = AF_INET6; |
| 858 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 859 | sin->sin6_len = sizeof(*sin); |
| 860 | #endif |
| 861 | return 16; |
| 862 | } |
| 863 | } |
| 864 | #endif /* ENABLE_IPV6 */ |
| 865 | #else /* HAVE_INET_PTON */ |
| 866 | /* check for an IPv4 address */ |
| 867 | if (af == AF_INET || af == AF_UNSPEC) { |
| 868 | struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret; |
| 869 | memset(sin, 0, sizeof(*sin)); |
| 870 | if ((sin->sin_addr.s_addr = inet_addr(name)) != INADDR_NONE) { |
| 871 | sin->sin_family = AF_INET; |
| 872 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 873 | sin->sin_len = sizeof(*sin); |
| 874 | #endif |
| 875 | return 4; |
| 876 | } |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 877 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 878 | #endif /* HAVE_INET_PTON */ |
| 879 | |
| 880 | /* perform a name resolution */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 881 | memset(&hints, 0, sizeof(hints)); |
| 882 | hints.ai_family = af; |
| 883 | Py_BEGIN_ALLOW_THREADS |
| 884 | ACQUIRE_GETADDRINFO_LOCK |
| 885 | error = getaddrinfo(name, NULL, &hints, &res); |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 886 | #if defined(__digital__) && defined(__unix__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 887 | if (error == EAI_NONAME && af == AF_UNSPEC) { |
| 888 | /* On Tru64 V5.1, numeric-to-addr conversion fails |
| 889 | if no address family is given. Assume IPv4 for now.*/ |
| 890 | hints.ai_family = AF_INET; |
| 891 | error = getaddrinfo(name, NULL, &hints, &res); |
| 892 | } |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 893 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 894 | Py_END_ALLOW_THREADS |
| 895 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 896 | if (error) { |
| 897 | set_gaierror(error); |
| 898 | return -1; |
| 899 | } |
| 900 | if (res->ai_addrlen < addr_ret_size) |
| 901 | addr_ret_size = res->ai_addrlen; |
| 902 | memcpy((char *) addr_ret, res->ai_addr, addr_ret_size); |
| 903 | freeaddrinfo(res); |
| 904 | switch (addr_ret->sa_family) { |
| 905 | case AF_INET: |
| 906 | return 4; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 907 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 908 | case AF_INET6: |
| 909 | return 16; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 910 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 911 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 912 | PyErr_SetString(PyExc_OSError, "unknown address family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 913 | return -1; |
| 914 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 917 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 918 | /* Create a string object representing an IP address. |
| 919 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 920 | size numbers). */ |
| 921 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 922 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 923 | makeipaddr(struct sockaddr *addr, int addrlen) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 924 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 925 | char buf[NI_MAXHOST]; |
| 926 | int error; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 927 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 928 | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, |
| 929 | NI_NUMERICHOST); |
| 930 | if (error) { |
| 931 | set_gaierror(error); |
| 932 | return NULL; |
| 933 | } |
| 934 | return PyUnicode_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 938 | #ifdef USE_BLUETOOTH |
| 939 | /* Convert a string representation of a Bluetooth address into a numeric |
| 940 | address. Returns the length (6), or raises an exception and returns -1 if |
| 941 | an error occurred. */ |
| 942 | |
| 943 | static int |
| 944 | setbdaddr(char *name, bdaddr_t *bdaddr) |
| 945 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 946 | unsigned int b0, b1, b2, b3, b4, b5; |
| 947 | char ch; |
| 948 | int n; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 949 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 950 | n = sscanf(name, "%X:%X:%X:%X:%X:%X%c", |
| 951 | &b5, &b4, &b3, &b2, &b1, &b0, &ch); |
| 952 | if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) { |
| 953 | bdaddr->b[0] = b0; |
| 954 | bdaddr->b[1] = b1; |
| 955 | bdaddr->b[2] = b2; |
| 956 | bdaddr->b[3] = b3; |
| 957 | bdaddr->b[4] = b4; |
| 958 | bdaddr->b[5] = b5; |
| 959 | return 6; |
| 960 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 961 | PyErr_SetString(PyExc_OSError, "bad bluetooth address"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 962 | return -1; |
| 963 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | /* Create a string representation of the Bluetooth address. This is always a |
| 967 | string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal |
| 968 | value (zero padded if necessary). */ |
| 969 | |
| 970 | static PyObject * |
| 971 | makebdaddr(bdaddr_t *bdaddr) |
| 972 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 973 | char buf[(6 * 2) + 5 + 1]; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 974 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 975 | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 976 | bdaddr->b[5], bdaddr->b[4], bdaddr->b[3], |
| 977 | bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]); |
| 978 | return PyUnicode_FromString(buf); |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 979 | } |
| 980 | #endif |
| 981 | |
| 982 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 983 | /* Create an object representing the given socket address, |
| 984 | suitable for passing it back to bind(), connect() etc. |
| 985 | The family field of the sockaddr structure is inspected |
| 986 | to determine what kind of address it really is. */ |
| 987 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 988 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 989 | static PyObject * |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 990 | 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] | 991 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 992 | if (addrlen == 0) { |
| 993 | /* No address -- may be recvfrom() from known socket */ |
| 994 | Py_INCREF(Py_None); |
| 995 | return Py_None; |
| 996 | } |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 997 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 998 | switch (addr->sa_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 999 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1000 | case AF_INET: |
| 1001 | { |
| 1002 | struct sockaddr_in *a; |
| 1003 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1004 | PyObject *ret = NULL; |
| 1005 | if (addrobj) { |
| 1006 | a = (struct sockaddr_in *)addr; |
| 1007 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 1008 | Py_DECREF(addrobj); |
| 1009 | } |
| 1010 | return ret; |
| 1011 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1012 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1013 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1014 | case AF_UNIX: |
| 1015 | { |
| 1016 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1017 | #ifdef linux |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1018 | if (a->sun_path[0] == 0) { /* Linux abstract namespace */ |
| 1019 | addrlen -= offsetof(struct sockaddr_un, sun_path); |
Charles-François Natali | b10c71d | 2013-05-21 10:45:46 +0200 | [diff] [blame] | 1020 | return PyBytes_FromStringAndSize(a->sun_path, addrlen); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1021 | } |
| 1022 | else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1023 | #endif /* linux */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1024 | { |
| 1025 | /* regular NULL-terminated string */ |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1026 | return PyUnicode_DecodeFSDefault(a->sun_path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | } |
| 1028 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1029 | #endif /* AF_UNIX */ |
| 1030 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1031 | #if defined(AF_NETLINK) |
| 1032 | case AF_NETLINK: |
| 1033 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1034 | struct sockaddr_nl *a = (struct sockaddr_nl *) addr; |
| 1035 | return Py_BuildValue("II", a->nl_pid, a->nl_groups); |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1036 | } |
| 1037 | #endif /* AF_NETLINK */ |
| 1038 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1039 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1040 | case AF_INET6: |
| 1041 | { |
| 1042 | struct sockaddr_in6 *a; |
| 1043 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1044 | PyObject *ret = NULL; |
| 1045 | if (addrobj) { |
| 1046 | a = (struct sockaddr_in6 *)addr; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1047 | ret = Py_BuildValue("OiII", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1048 | addrobj, |
| 1049 | ntohs(a->sin6_port), |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1050 | ntohl(a->sin6_flowinfo), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1051 | a->sin6_scope_id); |
| 1052 | Py_DECREF(addrobj); |
| 1053 | } |
| 1054 | return ret; |
| 1055 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1056 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1057 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1058 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1059 | case AF_BLUETOOTH: |
| 1060 | switch (proto) { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1061 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1062 | case BTPROTO_L2CAP: |
| 1063 | { |
| 1064 | struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr; |
| 1065 | PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr)); |
| 1066 | PyObject *ret = NULL; |
| 1067 | if (addrobj) { |
| 1068 | ret = Py_BuildValue("Oi", |
| 1069 | addrobj, |
| 1070 | _BT_L2_MEMB(a, psm)); |
| 1071 | Py_DECREF(addrobj); |
| 1072 | } |
| 1073 | return ret; |
| 1074 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1075 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1076 | case BTPROTO_RFCOMM: |
| 1077 | { |
| 1078 | struct sockaddr_rc *a = (struct sockaddr_rc *) addr; |
| 1079 | PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr)); |
| 1080 | PyObject *ret = NULL; |
| 1081 | if (addrobj) { |
| 1082 | ret = Py_BuildValue("Oi", |
| 1083 | addrobj, |
| 1084 | _BT_RC_MEMB(a, channel)); |
| 1085 | Py_DECREF(addrobj); |
| 1086 | } |
| 1087 | return ret; |
| 1088 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1089 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1090 | case BTPROTO_HCI: |
| 1091 | { |
| 1092 | struct sockaddr_hci *a = (struct sockaddr_hci *) addr; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1093 | #if defined(__NetBSD__) || defined(__DragonFly__) |
| 1094 | return makebdaddr(&_BT_HCI_MEMB(a, bdaddr)); |
| 1095 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1096 | PyObject *ret = NULL; |
| 1097 | ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev)); |
| 1098 | return ret; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1099 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1100 | } |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1101 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1102 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1103 | case BTPROTO_SCO: |
| 1104 | { |
| 1105 | struct sockaddr_sco *a = (struct sockaddr_sco *) addr; |
| 1106 | return makebdaddr(&_BT_SCO_MEMB(a, bdaddr)); |
| 1107 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1108 | #endif |
| 1109 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1110 | default: |
| 1111 | PyErr_SetString(PyExc_ValueError, |
| 1112 | "Unknown Bluetooth protocol"); |
| 1113 | return NULL; |
| 1114 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1115 | #endif |
| 1116 | |
Antoine Pitrou | b156a46 | 2010-10-27 20:13:57 +0000 | [diff] [blame] | 1117 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1118 | case AF_PACKET: |
| 1119 | { |
| 1120 | struct sockaddr_ll *a = (struct sockaddr_ll *)addr; |
| 1121 | char *ifname = ""; |
| 1122 | struct ifreq ifr; |
| 1123 | /* need to look up interface name give index */ |
| 1124 | if (a->sll_ifindex) { |
| 1125 | ifr.ifr_ifindex = a->sll_ifindex; |
| 1126 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
| 1127 | ifname = ifr.ifr_name; |
| 1128 | } |
| 1129 | return Py_BuildValue("shbhy#", |
| 1130 | ifname, |
| 1131 | ntohs(a->sll_protocol), |
| 1132 | a->sll_pkttype, |
| 1133 | a->sll_hatype, |
| 1134 | a->sll_addr, |
| 1135 | a->sll_halen); |
| 1136 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1137 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1138 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1139 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1140 | case AF_TIPC: |
| 1141 | { |
| 1142 | struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr; |
| 1143 | if (a->addrtype == TIPC_ADDR_NAMESEQ) { |
| 1144 | return Py_BuildValue("IIIII", |
| 1145 | a->addrtype, |
| 1146 | a->addr.nameseq.type, |
| 1147 | a->addr.nameseq.lower, |
| 1148 | a->addr.nameseq.upper, |
| 1149 | a->scope); |
| 1150 | } else if (a->addrtype == TIPC_ADDR_NAME) { |
| 1151 | return Py_BuildValue("IIIII", |
| 1152 | a->addrtype, |
| 1153 | a->addr.name.name.type, |
| 1154 | a->addr.name.name.instance, |
| 1155 | a->addr.name.name.instance, |
| 1156 | a->scope); |
| 1157 | } else if (a->addrtype == TIPC_ADDR_ID) { |
| 1158 | return Py_BuildValue("IIIII", |
| 1159 | a->addrtype, |
| 1160 | a->addr.id.node, |
| 1161 | a->addr.id.ref, |
| 1162 | 0, |
| 1163 | a->scope); |
| 1164 | } else { |
| 1165 | PyErr_SetString(PyExc_ValueError, |
| 1166 | "Invalid address type"); |
| 1167 | return NULL; |
| 1168 | } |
| 1169 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1170 | #endif |
| 1171 | |
Vinay Sajip | ecfc98c | 2014-03-20 12:42:42 +0000 | [diff] [blame] | 1172 | #ifdef AF_CAN |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1173 | case AF_CAN: |
| 1174 | { |
| 1175 | struct sockaddr_can *a = (struct sockaddr_can *)addr; |
| 1176 | char *ifname = ""; |
| 1177 | struct ifreq ifr; |
| 1178 | /* need to look up interface name given index */ |
| 1179 | if (a->can_ifindex) { |
| 1180 | ifr.ifr_ifindex = a->can_ifindex; |
| 1181 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
| 1182 | ifname = ifr.ifr_name; |
| 1183 | } |
| 1184 | |
| 1185 | return Py_BuildValue("O&h", PyUnicode_DecodeFSDefault, |
| 1186 | ifname, |
| 1187 | a->can_family); |
| 1188 | } |
| 1189 | #endif |
| 1190 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1191 | #ifdef PF_SYSTEM |
| 1192 | case PF_SYSTEM: |
| 1193 | switch(proto) { |
| 1194 | #ifdef SYSPROTO_CONTROL |
| 1195 | case SYSPROTO_CONTROL: |
| 1196 | { |
| 1197 | struct sockaddr_ctl *a = (struct sockaddr_ctl *)addr; |
| 1198 | return Py_BuildValue("(II)", a->sc_id, a->sc_unit); |
| 1199 | } |
| 1200 | #endif |
| 1201 | default: |
| 1202 | PyErr_SetString(PyExc_ValueError, |
| 1203 | "Invalid address type"); |
| 1204 | return 0; |
| 1205 | } |
| 1206 | #endif |
| 1207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1208 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1210 | default: |
| 1211 | /* If we don't know the address family, don't raise an |
| 1212 | exception -- return it as an (int, bytes) tuple. */ |
| 1213 | return Py_BuildValue("iy#", |
| 1214 | addr->sa_family, |
| 1215 | addr->sa_data, |
| 1216 | sizeof(addr->sa_data)); |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1218 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1221 | /* Helper for getsockaddrarg: bypass IDNA for ASCII-only host names |
| 1222 | (in particular, numeric IP addresses). */ |
| 1223 | struct maybe_idna { |
| 1224 | PyObject *obj; |
| 1225 | char *buf; |
| 1226 | }; |
| 1227 | |
| 1228 | static void |
| 1229 | idna_cleanup(struct maybe_idna *data) |
| 1230 | { |
| 1231 | Py_CLEAR(data->obj); |
| 1232 | } |
| 1233 | |
| 1234 | static int |
| 1235 | idna_converter(PyObject *obj, struct maybe_idna *data) |
| 1236 | { |
| 1237 | size_t len; |
| 1238 | PyObject *obj2, *obj3; |
| 1239 | if (obj == NULL) { |
| 1240 | idna_cleanup(data); |
| 1241 | return 1; |
| 1242 | } |
| 1243 | data->obj = NULL; |
| 1244 | len = -1; |
| 1245 | if (PyBytes_Check(obj)) { |
| 1246 | data->buf = PyBytes_AsString(obj); |
| 1247 | len = PyBytes_Size(obj); |
| 1248 | } |
| 1249 | else if (PyByteArray_Check(obj)) { |
| 1250 | data->buf = PyByteArray_AsString(obj); |
| 1251 | len = PyByteArray_Size(obj); |
| 1252 | } |
| 1253 | else if (PyUnicode_Check(obj) && PyUnicode_READY(obj) == 0 && PyUnicode_IS_COMPACT_ASCII(obj)) { |
| 1254 | data->buf = PyUnicode_DATA(obj); |
| 1255 | len = PyUnicode_GET_LENGTH(obj); |
| 1256 | } |
| 1257 | else { |
| 1258 | obj2 = PyUnicode_FromObject(obj); |
| 1259 | if (!obj2) { |
| 1260 | PyErr_Format(PyExc_TypeError, "string or unicode text buffer expected, not %s", |
| 1261 | obj->ob_type->tp_name); |
| 1262 | return 0; |
| 1263 | } |
| 1264 | obj3 = PyUnicode_AsEncodedString(obj2, "idna", NULL); |
| 1265 | Py_DECREF(obj2); |
| 1266 | if (!obj3) { |
| 1267 | PyErr_SetString(PyExc_TypeError, "encoding of hostname failed"); |
| 1268 | return 0; |
| 1269 | } |
| 1270 | if (!PyBytes_Check(obj3)) { |
Martin v. Löwis | 17fd1e1 | 2014-08-05 16:13:50 +0200 | [diff] [blame] | 1271 | Py_DECREF(obj3); |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1272 | PyErr_SetString(PyExc_TypeError, "encoding of hostname failed to return bytes"); |
| 1273 | return 0; |
| 1274 | } |
| 1275 | data->obj = obj3; |
| 1276 | data->buf = PyBytes_AS_STRING(obj3); |
| 1277 | len = PyBytes_GET_SIZE(obj3); |
| 1278 | } |
| 1279 | if (strlen(data->buf) != len) { |
| 1280 | Py_CLEAR(data->obj); |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1281 | 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] | 1282 | return 0; |
| 1283 | } |
| 1284 | return Py_CLEANUP_SUPPORTED; |
Victor Stinner | 1a62a68 | 2014-08-17 19:33:28 +0200 | [diff] [blame] | 1285 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1286 | |
| 1287 | /* Parse a socket address argument according to the socket object's |
| 1288 | address family. Return 1 if the address was in the proper format, |
| 1289 | 0 of not. The address is returned through addr_ret, its length |
| 1290 | through len_ret. */ |
| 1291 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1292 | static int |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1293 | getsockaddrarg(PySocketSockObject *s, PyObject *args, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1294 | struct sockaddr *addr_ret, int *len_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1295 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1296 | switch (s->sock_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1297 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1298 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1299 | case AF_UNIX: |
| 1300 | { |
| 1301 | struct sockaddr_un* addr; |
| 1302 | char *path; |
| 1303 | int len; |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1304 | int retval = 0; |
| 1305 | |
| 1306 | /* PEP 383. Not using PyUnicode_FSConverter since we need to |
| 1307 | allow embedded nulls on Linux. */ |
| 1308 | if (PyUnicode_Check(args)) { |
| 1309 | if ((args = PyUnicode_EncodeFSDefault(args)) == NULL) |
| 1310 | return 0; |
| 1311 | } |
| 1312 | else |
| 1313 | Py_INCREF(args); |
| 1314 | if (!PyArg_Parse(args, "y#", &path, &len)) |
| 1315 | goto unix_out; |
Victor Stinner | 1a62a68 | 2014-08-17 19:33:28 +0200 | [diff] [blame] | 1316 | assert(len >= 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1318 | addr = (struct sockaddr_un*)addr_ret; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1319 | #ifdef linux |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1320 | if (len > 0 && path[0] == 0) { |
| 1321 | /* Linux abstract namespace extension */ |
Victor Stinner | 1a62a68 | 2014-08-17 19:33:28 +0200 | [diff] [blame] | 1322 | if ((size_t)len > sizeof addr->sun_path) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1323 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1324 | "AF_UNIX path too long"); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1325 | goto unix_out; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1326 | } |
| 1327 | } |
| 1328 | else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1329 | #endif /* linux */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1330 | { |
| 1331 | /* regular NULL-terminated string */ |
Victor Stinner | 1a62a68 | 2014-08-17 19:33:28 +0200 | [diff] [blame] | 1332 | if ((size_t)len >= sizeof addr->sun_path) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1333 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1334 | "AF_UNIX path too long"); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1335 | goto unix_out; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1336 | } |
| 1337 | addr->sun_path[len] = 0; |
| 1338 | } |
| 1339 | addr->sun_family = s->sock_family; |
| 1340 | memcpy(addr->sun_path, path, len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1341 | *len_ret = len + offsetof(struct sockaddr_un, sun_path); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1342 | retval = 1; |
| 1343 | unix_out: |
| 1344 | Py_DECREF(args); |
| 1345 | return retval; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1346 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1347 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1348 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1349 | #if defined(AF_NETLINK) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1350 | case AF_NETLINK: |
| 1351 | { |
| 1352 | struct sockaddr_nl* addr; |
| 1353 | int pid, groups; |
| 1354 | addr = (struct sockaddr_nl *)addr_ret; |
| 1355 | if (!PyTuple_Check(args)) { |
| 1356 | PyErr_Format( |
| 1357 | PyExc_TypeError, |
| 1358 | "getsockaddrarg: " |
| 1359 | "AF_NETLINK address must be tuple, not %.500s", |
| 1360 | Py_TYPE(args)->tp_name); |
| 1361 | return 0; |
| 1362 | } |
| 1363 | if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups)) |
| 1364 | return 0; |
| 1365 | addr->nl_family = AF_NETLINK; |
| 1366 | addr->nl_pid = pid; |
| 1367 | addr->nl_groups = groups; |
| 1368 | *len_ret = sizeof(*addr); |
| 1369 | return 1; |
| 1370 | } |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1371 | #endif |
| 1372 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 1373 | #ifdef AF_RDS |
| 1374 | case AF_RDS: |
| 1375 | /* RDS sockets use sockaddr_in: fall-through */ |
| 1376 | #endif |
| 1377 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1378 | case AF_INET: |
| 1379 | { |
| 1380 | struct sockaddr_in* addr; |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1381 | struct maybe_idna host = {NULL, NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1382 | int port, result; |
| 1383 | if (!PyTuple_Check(args)) { |
| 1384 | PyErr_Format( |
| 1385 | PyExc_TypeError, |
| 1386 | "getsockaddrarg: " |
| 1387 | "AF_INET address must be tuple, not %.500s", |
| 1388 | Py_TYPE(args)->tp_name); |
| 1389 | return 0; |
| 1390 | } |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1391 | if (!PyArg_ParseTuple(args, "O&i:getsockaddrarg", |
| 1392 | idna_converter, &host, &port)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1393 | return 0; |
| 1394 | addr=(struct sockaddr_in*)addr_ret; |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1395 | result = setipaddr(host.buf, (struct sockaddr *)addr, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1396 | sizeof(*addr), AF_INET); |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1397 | idna_cleanup(&host); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1398 | if (result < 0) |
| 1399 | return 0; |
| 1400 | if (port < 0 || port > 0xffff) { |
| 1401 | PyErr_SetString( |
| 1402 | PyExc_OverflowError, |
| 1403 | "getsockaddrarg: port must be 0-65535."); |
| 1404 | return 0; |
| 1405 | } |
| 1406 | addr->sin_family = AF_INET; |
| 1407 | addr->sin_port = htons((short)port); |
| 1408 | *len_ret = sizeof *addr; |
| 1409 | return 1; |
| 1410 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1411 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1412 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1413 | case AF_INET6: |
| 1414 | { |
| 1415 | struct sockaddr_in6* addr; |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1416 | struct maybe_idna host = {NULL, NULL}; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1417 | int port, result; |
| 1418 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1419 | flowinfo = scope_id = 0; |
| 1420 | if (!PyTuple_Check(args)) { |
| 1421 | PyErr_Format( |
| 1422 | PyExc_TypeError, |
| 1423 | "getsockaddrarg: " |
| 1424 | "AF_INET6 address must be tuple, not %.500s", |
| 1425 | Py_TYPE(args)->tp_name); |
| 1426 | return 0; |
| 1427 | } |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1428 | if (!PyArg_ParseTuple(args, "O&i|II", |
| 1429 | idna_converter, &host, &port, &flowinfo, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1430 | &scope_id)) { |
| 1431 | return 0; |
| 1432 | } |
| 1433 | addr = (struct sockaddr_in6*)addr_ret; |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1434 | result = setipaddr(host.buf, (struct sockaddr *)addr, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1435 | sizeof(*addr), AF_INET6); |
Martin v. Löwis | eb1c28a | 2014-08-05 16:10:38 +0200 | [diff] [blame] | 1436 | idna_cleanup(&host); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1437 | if (result < 0) |
| 1438 | return 0; |
| 1439 | if (port < 0 || port > 0xffff) { |
| 1440 | PyErr_SetString( |
| 1441 | PyExc_OverflowError, |
| 1442 | "getsockaddrarg: port must be 0-65535."); |
| 1443 | return 0; |
| 1444 | } |
Charles-François Natali | 9b0c006 | 2012-06-23 10:17:05 +0200 | [diff] [blame] | 1445 | if (flowinfo > 0xfffff) { |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1446 | PyErr_SetString( |
| 1447 | PyExc_OverflowError, |
| 1448 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 1449 | return 0; |
| 1450 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1451 | addr->sin6_family = s->sock_family; |
| 1452 | addr->sin6_port = htons((short)port); |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1453 | addr->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1454 | addr->sin6_scope_id = scope_id; |
| 1455 | *len_ret = sizeof *addr; |
| 1456 | return 1; |
| 1457 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1458 | #endif |
| 1459 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1460 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1461 | case AF_BLUETOOTH: |
| 1462 | { |
| 1463 | switch (s->sock_proto) { |
| 1464 | case BTPROTO_L2CAP: |
| 1465 | { |
| 1466 | struct sockaddr_l2 *addr; |
| 1467 | char *straddr; |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1469 | addr = (struct sockaddr_l2 *)addr_ret; |
| 1470 | memset(addr, 0, sizeof(struct sockaddr_l2)); |
| 1471 | _BT_L2_MEMB(addr, family) = AF_BLUETOOTH; |
| 1472 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1473 | &_BT_L2_MEMB(addr, psm))) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1474 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1475 | "wrong format"); |
| 1476 | return 0; |
| 1477 | } |
| 1478 | if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0) |
| 1479 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1480 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1481 | *len_ret = sizeof *addr; |
| 1482 | return 1; |
| 1483 | } |
| 1484 | case BTPROTO_RFCOMM: |
| 1485 | { |
| 1486 | struct sockaddr_rc *addr; |
| 1487 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1488 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1489 | addr = (struct sockaddr_rc *)addr_ret; |
| 1490 | _BT_RC_MEMB(addr, family) = AF_BLUETOOTH; |
| 1491 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1492 | &_BT_RC_MEMB(addr, channel))) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1493 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1494 | "wrong format"); |
| 1495 | return 0; |
| 1496 | } |
| 1497 | if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0) |
| 1498 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1500 | *len_ret = sizeof *addr; |
| 1501 | return 1; |
| 1502 | } |
| 1503 | case BTPROTO_HCI: |
| 1504 | { |
| 1505 | struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1506 | #if defined(__NetBSD__) || defined(__DragonFly__) |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1507 | char *straddr = PyBytes_AS_STRING(args); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1508 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1509 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1510 | if (straddr == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1511 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1512 | "wrong format"); |
| 1513 | return 0; |
| 1514 | } |
| 1515 | if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0) |
| 1516 | return 0; |
| 1517 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1518 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
| 1519 | if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1520 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1521 | "wrong format"); |
| 1522 | return 0; |
| 1523 | } |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1524 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1525 | *len_ret = sizeof *addr; |
| 1526 | return 1; |
| 1527 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1528 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1529 | case BTPROTO_SCO: |
| 1530 | { |
| 1531 | struct sockaddr_sco *addr; |
| 1532 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1533 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1534 | addr = (struct sockaddr_sco *)addr_ret; |
| 1535 | _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; |
| 1536 | if (!PyBytes_Check(args)) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1537 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1538 | "wrong format"); |
| 1539 | return 0; |
| 1540 | } |
| 1541 | straddr = PyBytes_AS_STRING(args); |
| 1542 | if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0) |
| 1543 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1544 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1545 | *len_ret = sizeof *addr; |
| 1546 | return 1; |
| 1547 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1548 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1549 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1550 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: unknown Bluetooth protocol"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1551 | return 0; |
| 1552 | } |
| 1553 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1554 | #endif |
| 1555 | |
Antoine Pitrou | b156a46 | 2010-10-27 20:13:57 +0000 | [diff] [blame] | 1556 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1557 | case AF_PACKET: |
| 1558 | { |
| 1559 | struct sockaddr_ll* addr; |
| 1560 | struct ifreq ifr; |
| 1561 | char *interfaceName; |
| 1562 | int protoNumber; |
| 1563 | int hatype = 0; |
| 1564 | int pkttype = 0; |
| 1565 | char *haddr = NULL; |
| 1566 | unsigned int halen = 0; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1568 | if (!PyTuple_Check(args)) { |
| 1569 | PyErr_Format( |
| 1570 | PyExc_TypeError, |
| 1571 | "getsockaddrarg: " |
| 1572 | "AF_PACKET address must be tuple, not %.500s", |
| 1573 | Py_TYPE(args)->tp_name); |
| 1574 | return 0; |
| 1575 | } |
| 1576 | if (!PyArg_ParseTuple(args, "si|iiy#", &interfaceName, |
| 1577 | &protoNumber, &pkttype, &hatype, |
| 1578 | &haddr, &halen)) |
| 1579 | return 0; |
| 1580 | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name)); |
| 1581 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
| 1582 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
| 1583 | s->errorhandler(); |
| 1584 | return 0; |
| 1585 | } |
| 1586 | if (halen > 8) { |
| 1587 | PyErr_SetString(PyExc_ValueError, |
| 1588 | "Hardware address must be 8 bytes or less"); |
| 1589 | return 0; |
| 1590 | } |
| 1591 | if (protoNumber < 0 || protoNumber > 0xffff) { |
| 1592 | PyErr_SetString( |
| 1593 | PyExc_OverflowError, |
| 1594 | "getsockaddrarg: protoNumber must be 0-65535."); |
| 1595 | return 0; |
| 1596 | } |
| 1597 | addr = (struct sockaddr_ll*)addr_ret; |
| 1598 | addr->sll_family = AF_PACKET; |
| 1599 | addr->sll_protocol = htons((short)protoNumber); |
| 1600 | addr->sll_ifindex = ifr.ifr_ifindex; |
| 1601 | addr->sll_pkttype = pkttype; |
| 1602 | addr->sll_hatype = hatype; |
| 1603 | if (halen != 0) { |
| 1604 | memcpy(&addr->sll_addr, haddr, halen); |
| 1605 | } |
| 1606 | addr->sll_halen = halen; |
| 1607 | *len_ret = sizeof *addr; |
| 1608 | return 1; |
| 1609 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1610 | #endif |
| 1611 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1612 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1613 | case AF_TIPC: |
| 1614 | { |
| 1615 | unsigned int atype, v1, v2, v3; |
| 1616 | unsigned int scope = TIPC_CLUSTER_SCOPE; |
| 1617 | struct sockaddr_tipc *addr; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1619 | if (!PyTuple_Check(args)) { |
| 1620 | PyErr_Format( |
| 1621 | PyExc_TypeError, |
| 1622 | "getsockaddrarg: " |
| 1623 | "AF_TIPC address must be tuple, not %.500s", |
| 1624 | Py_TYPE(args)->tp_name); |
| 1625 | return 0; |
| 1626 | } |
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 | if (!PyArg_ParseTuple(args, |
| 1629 | "IIII|I;Invalid TIPC address format", |
| 1630 | &atype, &v1, &v2, &v3, &scope)) |
| 1631 | return 0; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1632 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1633 | addr = (struct sockaddr_tipc *) addr_ret; |
| 1634 | memset(addr, 0, sizeof(struct sockaddr_tipc)); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1636 | addr->family = AF_TIPC; |
| 1637 | addr->scope = scope; |
| 1638 | addr->addrtype = atype; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1640 | if (atype == TIPC_ADDR_NAMESEQ) { |
| 1641 | addr->addr.nameseq.type = v1; |
| 1642 | addr->addr.nameseq.lower = v2; |
| 1643 | addr->addr.nameseq.upper = v3; |
| 1644 | } else if (atype == TIPC_ADDR_NAME) { |
| 1645 | addr->addr.name.name.type = v1; |
| 1646 | addr->addr.name.name.instance = v2; |
| 1647 | } else if (atype == TIPC_ADDR_ID) { |
| 1648 | addr->addr.id.node = v1; |
| 1649 | addr->addr.id.ref = v2; |
| 1650 | } else { |
| 1651 | /* Shouldn't happen */ |
| 1652 | PyErr_SetString(PyExc_TypeError, "Invalid address type"); |
| 1653 | return 0; |
| 1654 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1656 | *len_ret = sizeof(*addr); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1658 | return 1; |
| 1659 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1660 | #endif |
| 1661 | |
Vinay Sajip | ed6783f | 2014-03-21 11:44:32 +0000 | [diff] [blame] | 1662 | #if defined(AF_CAN) && defined(CAN_RAW) && defined(CAN_BCM) |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1663 | case AF_CAN: |
| 1664 | switch (s->sock_proto) { |
| 1665 | case CAN_RAW: |
Charles-François Natali | 773e42d | 2013-02-05 19:42:01 +0100 | [diff] [blame] | 1666 | /* fall-through */ |
| 1667 | case CAN_BCM: |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1668 | { |
| 1669 | struct sockaddr_can *addr; |
| 1670 | PyObject *interfaceName; |
| 1671 | struct ifreq ifr; |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1672 | Py_ssize_t len; |
| 1673 | |
Benjamin Peterson | 18b7191 | 2013-05-16 15:29:44 -0500 | [diff] [blame] | 1674 | addr = (struct sockaddr_can *)addr_ret; |
| 1675 | |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1676 | if (!PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter, |
| 1677 | &interfaceName)) |
| 1678 | return 0; |
| 1679 | |
| 1680 | len = PyBytes_GET_SIZE(interfaceName); |
| 1681 | |
| 1682 | if (len == 0) { |
| 1683 | ifr.ifr_ifindex = 0; |
Victor Stinner | 1a62a68 | 2014-08-17 19:33:28 +0200 | [diff] [blame] | 1684 | } else if ((size_t)len < sizeof(ifr.ifr_name)) { |
Christian Heimes | 15b6885 | 2012-09-10 01:25:50 +0200 | [diff] [blame] | 1685 | strncpy(ifr.ifr_name, PyBytes_AS_STRING(interfaceName), sizeof(ifr.ifr_name)); |
| 1686 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1687 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
| 1688 | s->errorhandler(); |
| 1689 | Py_DECREF(interfaceName); |
| 1690 | return 0; |
| 1691 | } |
| 1692 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1693 | PyErr_SetString(PyExc_OSError, |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1694 | "AF_CAN interface name too long"); |
| 1695 | Py_DECREF(interfaceName); |
| 1696 | return 0; |
| 1697 | } |
| 1698 | |
| 1699 | addr->can_family = AF_CAN; |
| 1700 | addr->can_ifindex = ifr.ifr_ifindex; |
| 1701 | |
| 1702 | *len_ret = sizeof(*addr); |
| 1703 | Py_DECREF(interfaceName); |
| 1704 | return 1; |
| 1705 | } |
| 1706 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1707 | PyErr_SetString(PyExc_OSError, |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1708 | "getsockaddrarg: unsupported CAN protocol"); |
| 1709 | return 0; |
| 1710 | } |
| 1711 | #endif |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1712 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1713 | #ifdef PF_SYSTEM |
| 1714 | case PF_SYSTEM: |
| 1715 | switch (s->sock_proto) { |
| 1716 | #ifdef SYSPROTO_CONTROL |
| 1717 | case SYSPROTO_CONTROL: |
| 1718 | { |
| 1719 | struct sockaddr_ctl *addr; |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1720 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1721 | addr = (struct sockaddr_ctl *)addr_ret; |
| 1722 | addr->sc_family = AF_SYSTEM; |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1723 | addr->ss_sysaddr = AF_SYS_CONTROL; |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1724 | |
| 1725 | if (PyUnicode_Check(args)) { |
| 1726 | struct ctl_info info; |
| 1727 | PyObject *ctl_name; |
| 1728 | |
| 1729 | if (!PyArg_Parse(args, "O&", |
| 1730 | PyUnicode_FSConverter, &ctl_name)) { |
| 1731 | return 0; |
| 1732 | } |
| 1733 | |
| 1734 | if (PyBytes_GET_SIZE(ctl_name) > sizeof(info.ctl_name)) { |
| 1735 | PyErr_SetString(PyExc_ValueError, |
| 1736 | "provided string is too long"); |
| 1737 | Py_DECREF(ctl_name); |
| 1738 | return 0; |
| 1739 | } |
| 1740 | strncpy(info.ctl_name, PyBytes_AS_STRING(ctl_name), |
| 1741 | sizeof(info.ctl_name)); |
| 1742 | Py_DECREF(ctl_name); |
| 1743 | |
| 1744 | if (ioctl(s->sock_fd, CTLIOCGINFO, &info)) { |
| 1745 | PyErr_SetString(PyExc_OSError, |
| 1746 | "cannot find kernel control with provided name"); |
| 1747 | return 0; |
| 1748 | } |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1749 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1750 | addr->sc_id = info.ctl_id; |
| 1751 | addr->sc_unit = 0; |
| 1752 | } else if (!PyArg_ParseTuple(args, "II", |
| 1753 | &(addr->sc_id), &(addr->sc_unit))) { |
| 1754 | PyErr_SetString(PyExc_TypeError, "getsockaddrarg: " |
| 1755 | "expected str or tuple of two ints"); |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1756 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1757 | return 0; |
| 1758 | } |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1759 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1760 | *len_ret = sizeof(*addr); |
| 1761 | return 1; |
| 1762 | } |
| 1763 | #endif |
| 1764 | default: |
| 1765 | PyErr_SetString(PyExc_OSError, |
| 1766 | "getsockaddrarg: unsupported PF_SYSTEM protocol"); |
| 1767 | return 0; |
| 1768 | } |
| 1769 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1771 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1773 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1774 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: bad family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1775 | return 0; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1776 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1777 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1780 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1781 | /* 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] | 1782 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 1783 | through len_ret. */ |
| 1784 | |
| 1785 | static int |
Peter Schneider-Kamp | 618e25d | 2000-07-11 23:00:12 +0000 | [diff] [blame] | 1786 | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1787 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1788 | switch (s->sock_family) { |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1789 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1790 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1791 | case AF_UNIX: |
| 1792 | { |
| 1793 | *len_ret = sizeof (struct sockaddr_un); |
| 1794 | return 1; |
| 1795 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1796 | #endif /* AF_UNIX */ |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1797 | #if defined(AF_NETLINK) |
| 1798 | case AF_NETLINK: |
| 1799 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1800 | *len_ret = sizeof (struct sockaddr_nl); |
| 1801 | return 1; |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1802 | } |
| 1803 | #endif |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1804 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 1805 | #ifdef AF_RDS |
| 1806 | case AF_RDS: |
| 1807 | /* RDS sockets use sockaddr_in: fall-through */ |
| 1808 | #endif |
| 1809 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1810 | case AF_INET: |
| 1811 | { |
| 1812 | *len_ret = sizeof (struct sockaddr_in); |
| 1813 | return 1; |
| 1814 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1815 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1816 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1817 | case AF_INET6: |
| 1818 | { |
| 1819 | *len_ret = sizeof (struct sockaddr_in6); |
| 1820 | return 1; |
| 1821 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1822 | #endif |
| 1823 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1824 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1825 | case AF_BLUETOOTH: |
| 1826 | { |
| 1827 | switch(s->sock_proto) |
| 1828 | { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1829 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1830 | case BTPROTO_L2CAP: |
| 1831 | *len_ret = sizeof (struct sockaddr_l2); |
| 1832 | return 1; |
| 1833 | case BTPROTO_RFCOMM: |
| 1834 | *len_ret = sizeof (struct sockaddr_rc); |
| 1835 | return 1; |
| 1836 | case BTPROTO_HCI: |
| 1837 | *len_ret = sizeof (struct sockaddr_hci); |
| 1838 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1839 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1840 | case BTPROTO_SCO: |
| 1841 | *len_ret = sizeof (struct sockaddr_sco); |
| 1842 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1843 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1844 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1845 | PyErr_SetString(PyExc_OSError, "getsockaddrlen: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1846 | "unknown BT protocol"); |
| 1847 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1848 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1849 | } |
| 1850 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1851 | #endif |
| 1852 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1853 | #ifdef HAVE_NETPACKET_PACKET_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1854 | case AF_PACKET: |
| 1855 | { |
| 1856 | *len_ret = sizeof (struct sockaddr_ll); |
| 1857 | return 1; |
| 1858 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1859 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1860 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1861 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1862 | case AF_TIPC: |
| 1863 | { |
| 1864 | *len_ret = sizeof (struct sockaddr_tipc); |
| 1865 | return 1; |
| 1866 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1867 | #endif |
| 1868 | |
Vinay Sajip | ecfc98c | 2014-03-20 12:42:42 +0000 | [diff] [blame] | 1869 | #ifdef AF_CAN |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1870 | case AF_CAN: |
| 1871 | { |
| 1872 | *len_ret = sizeof (struct sockaddr_can); |
| 1873 | return 1; |
| 1874 | } |
| 1875 | #endif |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1876 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1877 | #ifdef PF_SYSTEM |
| 1878 | case PF_SYSTEM: |
| 1879 | switch(s->sock_proto) { |
| 1880 | #ifdef SYSPROTO_CONTROL |
| 1881 | case SYSPROTO_CONTROL: |
| 1882 | *len_ret = sizeof (struct sockaddr_ctl); |
| 1883 | return 1; |
| 1884 | #endif |
| 1885 | default: |
| 1886 | PyErr_SetString(PyExc_OSError, "getsockaddrlen: " |
| 1887 | "unknown PF_SYSTEM protocol"); |
| 1888 | return 0; |
| 1889 | } |
| 1890 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1891 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1892 | /* More cases here... */ |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1893 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1894 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1895 | PyErr_SetString(PyExc_OSError, "getsockaddrlen: bad family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1896 | return 0; |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1897 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1898 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 1902 | /* Support functions for the sendmsg() and recvmsg[_into]() methods. |
| 1903 | Currently, these methods are only compiled if the RFC 2292/3542 |
| 1904 | CMSG_LEN() macro is available. Older systems seem to have used |
| 1905 | sizeof(struct cmsghdr) + (length) where CMSG_LEN() is used now, so |
| 1906 | it may be possible to define CMSG_LEN() that way if it's not |
| 1907 | provided. Some architectures might need extra padding after the |
| 1908 | cmsghdr, however, and CMSG_LEN() would have to take account of |
| 1909 | this. */ |
| 1910 | #ifdef CMSG_LEN |
| 1911 | /* If length is in range, set *result to CMSG_LEN(length) and return |
| 1912 | true; otherwise, return false. */ |
| 1913 | static int |
| 1914 | get_CMSG_LEN(size_t length, size_t *result) |
| 1915 | { |
| 1916 | size_t tmp; |
| 1917 | |
| 1918 | if (length > (SOCKLEN_T_LIMIT - CMSG_LEN(0))) |
| 1919 | return 0; |
| 1920 | tmp = CMSG_LEN(length); |
| 1921 | if (tmp > SOCKLEN_T_LIMIT || tmp < length) |
| 1922 | return 0; |
| 1923 | *result = tmp; |
| 1924 | return 1; |
| 1925 | } |
| 1926 | |
| 1927 | #ifdef CMSG_SPACE |
| 1928 | /* If length is in range, set *result to CMSG_SPACE(length) and return |
| 1929 | true; otherwise, return false. */ |
| 1930 | static int |
| 1931 | get_CMSG_SPACE(size_t length, size_t *result) |
| 1932 | { |
| 1933 | size_t tmp; |
| 1934 | |
| 1935 | /* Use CMSG_SPACE(1) here in order to take account of the padding |
| 1936 | necessary before *and* after the data. */ |
| 1937 | if (length > (SOCKLEN_T_LIMIT - CMSG_SPACE(1))) |
| 1938 | return 0; |
| 1939 | tmp = CMSG_SPACE(length); |
| 1940 | if (tmp > SOCKLEN_T_LIMIT || tmp < length) |
| 1941 | return 0; |
| 1942 | *result = tmp; |
| 1943 | return 1; |
| 1944 | } |
| 1945 | #endif |
| 1946 | |
| 1947 | /* Return true iff msg->msg_controllen is valid, cmsgh is a valid |
| 1948 | pointer in msg->msg_control with at least "space" bytes after it, |
| 1949 | and its cmsg_len member inside the buffer. */ |
| 1950 | static int |
| 1951 | cmsg_min_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t space) |
| 1952 | { |
| 1953 | size_t cmsg_offset; |
| 1954 | static const size_t cmsg_len_end = (offsetof(struct cmsghdr, cmsg_len) + |
| 1955 | sizeof(cmsgh->cmsg_len)); |
| 1956 | |
Charles-François Natali | 466517d | 2011-08-28 18:23:43 +0200 | [diff] [blame] | 1957 | /* Note that POSIX allows msg_controllen to be of signed type. */ |
Brett Cannon | b05cbe6 | 2014-01-07 17:01:01 -0500 | [diff] [blame] | 1958 | if (cmsgh == NULL || msg->msg_control == NULL) |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 1959 | return 0; |
Brett Cannon | b05cbe6 | 2014-01-07 17:01:01 -0500 | [diff] [blame] | 1960 | /* Note that POSIX allows msg_controllen to be of a signed type. This is |
| 1961 | annoying under OS X as it's unsigned there and so it triggers a |
| 1962 | tautological comparison warning under Clang when compared against 0. |
| 1963 | Since the check is valid on other platforms, silence the warning under |
| 1964 | Clang. */ |
| 1965 | #ifdef __clang__ |
| 1966 | #pragma clang diagnostic push |
| 1967 | #pragma clang diagnostic ignored "-Wtautological-compare" |
| 1968 | #endif |
Serhiy Storchaka | caa01f8 | 2015-03-05 23:42:24 +0200 | [diff] [blame^] | 1969 | #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))) |
Serhiy Storchaka | b48af34 | 2015-02-26 15:27:57 +0200 | [diff] [blame] | 1970 | #pragma GCC diagnostic push |
| 1971 | #pragma GCC diagnostic ignored "-Wtype-limits" |
| 1972 | #endif |
Brett Cannon | b05cbe6 | 2014-01-07 17:01:01 -0500 | [diff] [blame] | 1973 | if (msg->msg_controllen < 0) |
| 1974 | return 0; |
Serhiy Storchaka | caa01f8 | 2015-03-05 23:42:24 +0200 | [diff] [blame^] | 1975 | #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))) |
Serhiy Storchaka | b48af34 | 2015-02-26 15:27:57 +0200 | [diff] [blame] | 1976 | #pragma GCC diagnostic pop |
| 1977 | #endif |
Brett Cannon | b05cbe6 | 2014-01-07 17:01:01 -0500 | [diff] [blame] | 1978 | #ifdef __clang__ |
| 1979 | #pragma clang diagnostic pop |
| 1980 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 1981 | if (space < cmsg_len_end) |
| 1982 | space = cmsg_len_end; |
| 1983 | cmsg_offset = (char *)cmsgh - (char *)msg->msg_control; |
| 1984 | return (cmsg_offset <= (size_t)-1 - space && |
| 1985 | cmsg_offset + space <= msg->msg_controllen); |
| 1986 | } |
| 1987 | |
| 1988 | /* If pointer CMSG_DATA(cmsgh) is in buffer msg->msg_control, set |
| 1989 | *space to number of bytes following it in the buffer and return |
| 1990 | true; otherwise, return false. Assumes cmsgh, msg->msg_control and |
| 1991 | msg->msg_controllen are valid. */ |
| 1992 | static int |
| 1993 | get_cmsg_data_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *space) |
| 1994 | { |
| 1995 | size_t data_offset; |
| 1996 | char *data_ptr; |
| 1997 | |
| 1998 | if ((data_ptr = (char *)CMSG_DATA(cmsgh)) == NULL) |
| 1999 | return 0; |
| 2000 | data_offset = data_ptr - (char *)msg->msg_control; |
| 2001 | if (data_offset > msg->msg_controllen) |
| 2002 | return 0; |
| 2003 | *space = msg->msg_controllen - data_offset; |
| 2004 | return 1; |
| 2005 | } |
| 2006 | |
| 2007 | /* If cmsgh is invalid or not contained in the buffer pointed to by |
| 2008 | msg->msg_control, return -1. If cmsgh is valid and its associated |
| 2009 | data is entirely contained in the buffer, set *data_len to the |
| 2010 | length of the associated data and return 0. If only part of the |
| 2011 | associated data is contained in the buffer but cmsgh is otherwise |
| 2012 | valid, set *data_len to the length contained in the buffer and |
| 2013 | return 1. */ |
| 2014 | static int |
| 2015 | get_cmsg_data_len(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *data_len) |
| 2016 | { |
| 2017 | size_t space, cmsg_data_len; |
| 2018 | |
| 2019 | if (!cmsg_min_space(msg, cmsgh, CMSG_LEN(0)) || |
| 2020 | cmsgh->cmsg_len < CMSG_LEN(0)) |
| 2021 | return -1; |
| 2022 | cmsg_data_len = cmsgh->cmsg_len - CMSG_LEN(0); |
| 2023 | if (!get_cmsg_data_space(msg, cmsgh, &space)) |
| 2024 | return -1; |
| 2025 | if (space >= cmsg_data_len) { |
| 2026 | *data_len = cmsg_data_len; |
| 2027 | return 0; |
| 2028 | } |
| 2029 | *data_len = space; |
| 2030 | return 1; |
| 2031 | } |
| 2032 | #endif /* CMSG_LEN */ |
| 2033 | |
| 2034 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2035 | /* s._accept() -> (fd, address) */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2036 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2037 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2038 | sock_accept(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2039 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2040 | sock_addr_t addrbuf; |
| 2041 | SOCKET_T newfd = INVALID_SOCKET; |
| 2042 | socklen_t addrlen; |
| 2043 | PyObject *sock = NULL; |
| 2044 | PyObject *addr = NULL; |
| 2045 | PyObject *res = NULL; |
| 2046 | int timeout; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2047 | int async_err = 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2048 | #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) |
| 2049 | /* accept4() is available on Linux 2.6.28+ and glibc 2.10 */ |
| 2050 | static int accept4_works = -1; |
| 2051 | #endif |
| 2052 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2053 | if (!getsockaddrlen(s, &addrlen)) |
| 2054 | return NULL; |
| 2055 | memset(&addrbuf, 0, addrlen); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2056 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2057 | if (!IS_SELECTABLE(s)) |
| 2058 | return select_error(); |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2059 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2060 | BEGIN_SELECT_LOOP(s) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2061 | do { |
| 2062 | Py_BEGIN_ALLOW_THREADS |
| 2063 | timeout = internal_select_ex(s, 0, interval); |
| 2064 | if (!timeout) { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2065 | #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2066 | if (accept4_works != 0) { |
| 2067 | newfd = accept4(s->sock_fd, SAS2SA(&addrbuf), &addrlen, |
| 2068 | SOCK_CLOEXEC); |
| 2069 | if (newfd == INVALID_SOCKET && accept4_works == -1) { |
| 2070 | /* On Linux older than 2.6.28, accept4() fails with ENOSYS */ |
| 2071 | accept4_works = (errno != ENOSYS); |
| 2072 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2073 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2074 | if (accept4_works == 0) |
| 2075 | newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2076 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2077 | newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2078 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2079 | } |
| 2080 | Py_END_ALLOW_THREADS |
| 2081 | } while (newfd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2082 | if (timeout == 1) { |
| 2083 | PyErr_SetString(socket_timeout, "timed out"); |
| 2084 | return NULL; |
| 2085 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2086 | END_SELECT_LOOP(s) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2087 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2088 | if (newfd == INVALID_SOCKET) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2089 | return (!async_err) ? s->errorhandler() : NULL; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2090 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2091 | #ifdef MS_WINDOWS |
| 2092 | if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) { |
| 2093 | PyErr_SetFromWindowsErr(0); |
| 2094 | SOCKETCLOSE(newfd); |
| 2095 | goto finally; |
| 2096 | } |
| 2097 | #else |
| 2098 | |
| 2099 | #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) |
| 2100 | if (!accept4_works) |
| 2101 | #endif |
| 2102 | { |
| 2103 | if (_Py_set_inheritable(newfd, 0, NULL) < 0) { |
| 2104 | SOCKETCLOSE(newfd); |
| 2105 | goto finally; |
| 2106 | } |
| 2107 | } |
| 2108 | #endif |
| 2109 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2110 | sock = PyLong_FromSocket_t(newfd); |
| 2111 | if (sock == NULL) { |
| 2112 | SOCKETCLOSE(newfd); |
| 2113 | goto finally; |
| 2114 | } |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2115 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2116 | addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 2117 | addrlen, s->sock_proto); |
| 2118 | if (addr == NULL) |
| 2119 | goto finally; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2121 | res = PyTuple_Pack(2, sock, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2122 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2123 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2124 | Py_XDECREF(sock); |
| 2125 | Py_XDECREF(addr); |
| 2126 | return res; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2127 | } |
| 2128 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2129 | PyDoc_STRVAR(accept_doc, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2130 | "_accept() -> (integer, address info)\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2131 | \n\ |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2132 | Wait for an incoming connection. Return a new socket file descriptor\n\ |
| 2133 | representing the connection, and the address of the client.\n\ |
| 2134 | For IP sockets, the address info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2135 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2136 | /* s.setblocking(flag) method. Argument: |
| 2137 | False -- non-blocking mode; same as settimeout(0) |
| 2138 | True -- blocking mode; same as settimeout(None) |
| 2139 | */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2140 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2141 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2142 | sock_setblocking(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2143 | { |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 2144 | long block; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2145 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2146 | block = PyLong_AsLong(arg); |
| 2147 | if (block == -1 && PyErr_Occurred()) |
| 2148 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2149 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2150 | s->sock_timeout = block ? -1.0 : 0.0; |
| 2151 | internal_setblocking(s, block); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2153 | Py_INCREF(Py_None); |
| 2154 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2155 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2156 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2157 | PyDoc_STRVAR(setblocking_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2158 | "setblocking(flag)\n\ |
| 2159 | \n\ |
| 2160 | 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] | 2161 | setblocking(True) is equivalent to settimeout(None);\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2162 | setblocking(False) is equivalent to settimeout(0.0)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2163 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2164 | /* s.settimeout(timeout) method. Argument: |
| 2165 | None -- no timeout, blocking mode; same as setblocking(True) |
| 2166 | 0.0 -- non-blocking mode; same as setblocking(False) |
| 2167 | > 0 -- timeout mode; operations time out after timeout seconds |
| 2168 | < 0 -- illegal; raises an exception |
| 2169 | */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2170 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2171 | sock_settimeout(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2172 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2173 | double timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2175 | if (arg == Py_None) |
| 2176 | timeout = -1.0; |
| 2177 | else { |
| 2178 | timeout = PyFloat_AsDouble(arg); |
| 2179 | if (timeout < 0.0) { |
| 2180 | if (!PyErr_Occurred()) |
| 2181 | PyErr_SetString(PyExc_ValueError, |
| 2182 | "Timeout value out of range"); |
| 2183 | return NULL; |
| 2184 | } |
| 2185 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2186 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2187 | s->sock_timeout = timeout; |
| 2188 | internal_setblocking(s, timeout < 0.0); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2189 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2190 | Py_INCREF(Py_None); |
| 2191 | return Py_None; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2194 | PyDoc_STRVAR(settimeout_doc, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2195 | "settimeout(timeout)\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2196 | \n\ |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2197 | Set a timeout on socket operations. 'timeout' can be a float,\n\ |
| 2198 | giving in seconds, or None. Setting a timeout of None disables\n\ |
| 2199 | the timeout feature and is equivalent to setblocking(1).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2200 | Setting a timeout of zero is the same as setblocking(0)."); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2201 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2202 | /* s.gettimeout() method. |
| 2203 | Returns the timeout associated with a socket. */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2204 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2205 | sock_gettimeout(PySocketSockObject *s) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2206 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2207 | if (s->sock_timeout < 0.0) { |
| 2208 | Py_INCREF(Py_None); |
| 2209 | return Py_None; |
| 2210 | } |
| 2211 | else |
| 2212 | return PyFloat_FromDouble(s->sock_timeout); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2213 | } |
| 2214 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2215 | PyDoc_STRVAR(gettimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2216 | "gettimeout() -> timeout\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2217 | \n\ |
Ezio Melotti | 388c945 | 2011-08-14 08:28:57 +0300 | [diff] [blame] | 2218 | Returns the timeout in seconds (float) associated with socket \n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2219 | 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] | 2220 | operations are disabled."); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2221 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 2222 | /* s.setsockopt() method. |
| 2223 | With an integer third argument, sets an integer option. |
| 2224 | With a string third argument, sets an option from a buffer; |
| 2225 | use optional built-in module 'struct' to encode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2226 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2227 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2228 | sock_setsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2229 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2230 | int level; |
| 2231 | int optname; |
| 2232 | int res; |
| 2233 | char *buf; |
| 2234 | int buflen; |
| 2235 | int flag; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2237 | if (PyArg_ParseTuple(args, "iii:setsockopt", |
| 2238 | &level, &optname, &flag)) { |
| 2239 | buf = (char *) &flag; |
| 2240 | buflen = sizeof flag; |
| 2241 | } |
| 2242 | else { |
| 2243 | PyErr_Clear(); |
| 2244 | if (!PyArg_ParseTuple(args, "iiy#:setsockopt", |
| 2245 | &level, &optname, &buf, &buflen)) |
| 2246 | return NULL; |
| 2247 | } |
| 2248 | res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen); |
| 2249 | if (res < 0) |
| 2250 | return s->errorhandler(); |
| 2251 | Py_INCREF(Py_None); |
| 2252 | return Py_None; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2255 | PyDoc_STRVAR(setsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2256 | "setsockopt(level, option, value)\n\ |
| 2257 | \n\ |
| 2258 | 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] | 2259 | The value argument can either be an integer or a string."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2260 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2261 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 2262 | /* s.getsockopt() method. |
| 2263 | With two arguments, retrieves an integer option. |
| 2264 | With a third integer argument, retrieves a string buffer of that size; |
| 2265 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2266 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2267 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2268 | sock_getsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2269 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2270 | int level; |
| 2271 | int optname; |
| 2272 | int res; |
| 2273 | PyObject *buf; |
| 2274 | socklen_t buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2275 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2276 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt", |
| 2277 | &level, &optname, &buflen)) |
| 2278 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2280 | if (buflen == 0) { |
| 2281 | int flag = 0; |
| 2282 | socklen_t flagsize = sizeof flag; |
| 2283 | res = getsockopt(s->sock_fd, level, optname, |
| 2284 | (void *)&flag, &flagsize); |
| 2285 | if (res < 0) |
| 2286 | return s->errorhandler(); |
| 2287 | return PyLong_FromLong(flag); |
| 2288 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2289 | if (buflen <= 0 || buflen > 1024) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2290 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2291 | "getsockopt buflen out of range"); |
| 2292 | return NULL; |
| 2293 | } |
| 2294 | buf = PyBytes_FromStringAndSize((char *)NULL, buflen); |
| 2295 | if (buf == NULL) |
| 2296 | return NULL; |
| 2297 | res = getsockopt(s->sock_fd, level, optname, |
| 2298 | (void *)PyBytes_AS_STRING(buf), &buflen); |
| 2299 | if (res < 0) { |
| 2300 | Py_DECREF(buf); |
| 2301 | return s->errorhandler(); |
| 2302 | } |
| 2303 | _PyBytes_Resize(&buf, buflen); |
| 2304 | return buf; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2307 | PyDoc_STRVAR(getsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2308 | "getsockopt(level, option[, buffersize]) -> value\n\ |
| 2309 | \n\ |
| 2310 | Get a socket option. See the Unix manual for level and option.\n\ |
| 2311 | 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] | 2312 | string of that length; otherwise it is an integer."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2313 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2314 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2315 | /* s.bind(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2316 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2317 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2318 | sock_bind(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2319 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2320 | sock_addr_t addrbuf; |
| 2321 | int addrlen; |
| 2322 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2323 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2324 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2325 | return NULL; |
| 2326 | Py_BEGIN_ALLOW_THREADS |
| 2327 | res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen); |
| 2328 | Py_END_ALLOW_THREADS |
| 2329 | if (res < 0) |
| 2330 | return s->errorhandler(); |
| 2331 | Py_INCREF(Py_None); |
| 2332 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2335 | PyDoc_STRVAR(bind_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2336 | "bind(address)\n\ |
| 2337 | \n\ |
| 2338 | 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] | 2339 | 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] | 2340 | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2341 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2342 | |
| 2343 | /* s.close() method. |
| 2344 | Set the file descriptor to -1 so operations tried subsequently |
| 2345 | will surely fail. */ |
| 2346 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2347 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2348 | sock_close(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2349 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2350 | SOCKET_T fd; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2351 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2352 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 2353 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 2354 | * for more details. |
| 2355 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2356 | if ((fd = s->sock_fd) != -1) { |
| 2357 | s->sock_fd = -1; |
| 2358 | Py_BEGIN_ALLOW_THREADS |
| 2359 | (void) SOCKETCLOSE(fd); |
| 2360 | Py_END_ALLOW_THREADS |
| 2361 | } |
| 2362 | Py_INCREF(Py_None); |
| 2363 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2366 | PyDoc_STRVAR(close_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2367 | "close()\n\ |
| 2368 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2369 | Close the socket. It cannot be used after this call."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2370 | |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2371 | static PyObject * |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2372 | sock_detach(PySocketSockObject *s) |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2373 | { |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2374 | SOCKET_T fd = s->sock_fd; |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2375 | s->sock_fd = -1; |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2376 | return PyLong_FromSocket_t(fd); |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2379 | PyDoc_STRVAR(detach_doc, |
| 2380 | "detach()\n\ |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2381 | \n\ |
Guido van Rossum | 638aebd | 2013-12-04 11:50:09 -0800 | [diff] [blame] | 2382 | Close the socket object without closing the underlying file descriptor.\n\ |
| 2383 | 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] | 2384 | can be reused for other purposes. The file descriptor is returned."); |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2385 | |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2386 | static int |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2387 | internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2388 | int *timeoutp) |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2389 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2390 | int res, timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2391 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2392 | timeout = 0; |
| 2393 | res = connect(s->sock_fd, addr, addrlen); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2394 | |
| 2395 | #ifdef MS_WINDOWS |
| 2396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2397 | if (s->sock_timeout > 0.0) { |
| 2398 | if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK && |
| 2399 | IS_SELECTABLE(s)) { |
| 2400 | /* This is a mess. Best solution: trust select */ |
| 2401 | fd_set fds; |
| 2402 | fd_set fds_exc; |
| 2403 | struct timeval tv; |
| 2404 | tv.tv_sec = (int)s->sock_timeout; |
| 2405 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 2406 | FD_ZERO(&fds); |
| 2407 | FD_SET(s->sock_fd, &fds); |
| 2408 | FD_ZERO(&fds_exc); |
| 2409 | FD_SET(s->sock_fd, &fds_exc); |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2410 | res = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 2411 | NULL, &fds, &fds_exc, &tv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2412 | if (res == 0) { |
| 2413 | res = WSAEWOULDBLOCK; |
| 2414 | timeout = 1; |
| 2415 | } else if (res > 0) { |
| 2416 | if (FD_ISSET(s->sock_fd, &fds)) |
| 2417 | /* The socket is in the writable set - this |
| 2418 | means connected */ |
| 2419 | res = 0; |
| 2420 | else { |
| 2421 | /* As per MS docs, we need to call getsockopt() |
| 2422 | to get the underlying error */ |
| 2423 | int res_size = sizeof res; |
| 2424 | /* It must be in the exception set */ |
| 2425 | assert(FD_ISSET(s->sock_fd, &fds_exc)); |
| 2426 | if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR, |
| 2427 | (char *)&res, &res_size)) |
| 2428 | /* getsockopt also clears WSAGetLastError, |
| 2429 | so reset it back. */ |
| 2430 | WSASetLastError(res); |
| 2431 | else |
| 2432 | res = WSAGetLastError(); |
| 2433 | } |
| 2434 | } |
| 2435 | /* else if (res < 0) an error occurred */ |
| 2436 | } |
| 2437 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2439 | if (res < 0) |
| 2440 | res = WSAGetLastError(); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2441 | |
| 2442 | #else |
| 2443 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2444 | if (s->sock_timeout > 0.0) { |
| 2445 | if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) { |
| 2446 | timeout = internal_select(s, 1); |
| 2447 | if (timeout == 0) { |
| 2448 | /* Bug #1019808: in case of an EINPROGRESS, |
| 2449 | use getsockopt(SO_ERROR) to get the real |
| 2450 | error. */ |
| 2451 | socklen_t res_size = sizeof res; |
| 2452 | (void)getsockopt(s->sock_fd, SOL_SOCKET, |
| 2453 | SO_ERROR, &res, &res_size); |
| 2454 | if (res == EISCONN) |
| 2455 | res = 0; |
| 2456 | errno = res; |
| 2457 | } |
| 2458 | else if (timeout == -1) { |
| 2459 | res = errno; /* had error */ |
| 2460 | } |
| 2461 | else |
| 2462 | res = EWOULDBLOCK; /* timed out */ |
| 2463 | } |
| 2464 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2466 | if (res < 0) |
| 2467 | res = errno; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2468 | |
| 2469 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2470 | *timeoutp = timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2471 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2472 | return res; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2473 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2474 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2475 | /* s.connect(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2476 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2477 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2478 | sock_connect(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2479 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2480 | sock_addr_t addrbuf; |
| 2481 | int addrlen; |
| 2482 | int res; |
| 2483 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2484 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2485 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2486 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2487 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2488 | Py_BEGIN_ALLOW_THREADS |
| 2489 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 2490 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2491 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2492 | if (timeout == 1) { |
| 2493 | PyErr_SetString(socket_timeout, "timed out"); |
| 2494 | return NULL; |
| 2495 | } |
| 2496 | if (res != 0) |
| 2497 | return s->errorhandler(); |
| 2498 | Py_INCREF(Py_None); |
| 2499 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2500 | } |
| 2501 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2502 | PyDoc_STRVAR(connect_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2503 | "connect(address)\n\ |
| 2504 | \n\ |
| 2505 | 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] | 2506 | is a pair (host, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2507 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2508 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2509 | /* s.connect_ex(sockaddr) method */ |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2510 | |
| 2511 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2512 | sock_connect_ex(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2513 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2514 | sock_addr_t addrbuf; |
| 2515 | int addrlen; |
| 2516 | int res; |
| 2517 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2518 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2519 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2520 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2521 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2522 | Py_BEGIN_ALLOW_THREADS |
| 2523 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 2524 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2525 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2526 | /* Signals are not errors (though they may raise exceptions). Adapted |
| 2527 | from PyErr_SetFromErrnoWithFilenameObject(). */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2528 | if (res == EINTR && PyErr_CheckSignals()) |
| 2529 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2530 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2531 | return PyLong_FromLong((long) res); |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2534 | PyDoc_STRVAR(connect_ex_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2535 | "connect_ex(address) -> errno\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2536 | \n\ |
| 2537 | 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] | 2538 | instead of raising an exception when an error occurs."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2539 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2540 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2541 | /* s.fileno() method */ |
| 2542 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2543 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2544 | sock_fileno(PySocketSockObject *s) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2545 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2546 | return PyLong_FromSocket_t(s->sock_fd); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2547 | } |
| 2548 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2549 | PyDoc_STRVAR(fileno_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2550 | "fileno() -> integer\n\ |
| 2551 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2552 | Return the integer file descriptor of the socket."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2553 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2554 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2555 | /* s.getsockname() method */ |
| 2556 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2557 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2558 | sock_getsockname(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2559 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2560 | sock_addr_t addrbuf; |
| 2561 | int res; |
| 2562 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2564 | if (!getsockaddrlen(s, &addrlen)) |
| 2565 | return NULL; |
| 2566 | memset(&addrbuf, 0, addrlen); |
| 2567 | Py_BEGIN_ALLOW_THREADS |
| 2568 | res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2569 | Py_END_ALLOW_THREADS |
| 2570 | if (res < 0) |
| 2571 | return s->errorhandler(); |
| 2572 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2573 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2576 | PyDoc_STRVAR(getsockname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2577 | "getsockname() -> address info\n\ |
| 2578 | \n\ |
| 2579 | 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] | 2580 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2581 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2582 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2583 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2584 | /* s.getpeername() method */ |
| 2585 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2586 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2587 | sock_getpeername(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2588 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2589 | sock_addr_t addrbuf; |
| 2590 | int res; |
| 2591 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2592 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2593 | if (!getsockaddrlen(s, &addrlen)) |
| 2594 | return NULL; |
| 2595 | memset(&addrbuf, 0, addrlen); |
| 2596 | Py_BEGIN_ALLOW_THREADS |
| 2597 | res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2598 | Py_END_ALLOW_THREADS |
| 2599 | if (res < 0) |
| 2600 | return s->errorhandler(); |
| 2601 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2602 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2603 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2604 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2605 | PyDoc_STRVAR(getpeername_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2606 | "getpeername() -> address info\n\ |
| 2607 | \n\ |
| 2608 | 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] | 2609 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2610 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2611 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2612 | |
| 2613 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2614 | /* s.listen(n) method */ |
| 2615 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2616 | static PyObject * |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2617 | sock_listen(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2618 | { |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2619 | /* We try to choose a default backlog high enough to avoid connection drops |
| 2620 | * for common workloads, yet not too high to limit resource usage. */ |
| 2621 | int backlog = Py_MIN(SOMAXCONN, 128); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2622 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2623 | |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2624 | if (!PyArg_ParseTuple(args, "|i:listen", &backlog)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2625 | return NULL; |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2626 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2627 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 1be815a | 2011-05-10 19:16:29 +0200 | [diff] [blame] | 2628 | /* To avoid problems on systems that don't allow a negative backlog |
| 2629 | * (which doesn't make sense anyway) we force a minimum value of 0. */ |
| 2630 | if (backlog < 0) |
| 2631 | backlog = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2632 | res = listen(s->sock_fd, backlog); |
| 2633 | Py_END_ALLOW_THREADS |
| 2634 | if (res < 0) |
| 2635 | return s->errorhandler(); |
| 2636 | Py_INCREF(Py_None); |
| 2637 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2638 | } |
| 2639 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2640 | PyDoc_STRVAR(listen_doc, |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2641 | "listen([backlog])\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2642 | \n\ |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 2643 | Enable a server to accept connections. If backlog is specified, it must be\n\ |
| 2644 | 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] | 2645 | 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] | 2646 | connections. If not specified, a default reasonable value is chosen."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2647 | |
| 2648 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2649 | /* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2650 | * 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] | 2651 | * 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] | 2652 | * 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] | 2653 | * 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] | 2654 | * also possible that we return a number of bytes smaller than the request |
| 2655 | * bytes. |
| 2656 | */ |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2657 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2658 | static Py_ssize_t |
| 2659 | 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] | 2660 | { |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2661 | Py_ssize_t outlen = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2662 | int timeout; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2663 | int async_err = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2664 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2665 | if (!IS_SELECTABLE(s)) { |
| 2666 | select_error(); |
| 2667 | return -1; |
| 2668 | } |
| 2669 | if (len == 0) { |
| 2670 | /* If 0 bytes were requested, do nothing. */ |
| 2671 | return 0; |
| 2672 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2673 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2674 | BEGIN_SELECT_LOOP(s) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2675 | do { |
| 2676 | Py_BEGIN_ALLOW_THREADS |
| 2677 | timeout = internal_select_ex(s, 0, interval); |
| 2678 | if (!timeout) { |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 2679 | #ifdef MS_WINDOWS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2680 | if (len > INT_MAX) |
| 2681 | len = INT_MAX; |
| 2682 | outlen = recv(s->sock_fd, cbuf, (int)len, flags); |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 2683 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2684 | outlen = recv(s->sock_fd, cbuf, len, flags); |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 2685 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2686 | } |
| 2687 | Py_END_ALLOW_THREADS |
| 2688 | } while (outlen < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2689 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2690 | if (timeout == 1) { |
| 2691 | PyErr_SetString(socket_timeout, "timed out"); |
| 2692 | return -1; |
| 2693 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2694 | END_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2695 | if (outlen < 0) { |
| 2696 | /* Note: the call to errorhandler() ALWAYS indirectly returned |
| 2697 | NULL, so ignore its return value */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2698 | if (!async_err) |
| 2699 | s->errorhandler(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2700 | return -1; |
| 2701 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2702 | return outlen; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2705 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2706 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2707 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2708 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2709 | sock_recv(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2710 | { |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2711 | Py_ssize_t recvlen, outlen; |
| 2712 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2713 | PyObject *buf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2714 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2715 | if (!PyArg_ParseTuple(args, "n|i:recv", &recvlen, &flags)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2716 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2718 | if (recvlen < 0) { |
| 2719 | PyErr_SetString(PyExc_ValueError, |
| 2720 | "negative buffersize in recv"); |
| 2721 | return NULL; |
| 2722 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2723 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2724 | /* Allocate a new string. */ |
| 2725 | buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
| 2726 | if (buf == NULL) |
| 2727 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2729 | /* Call the guts */ |
| 2730 | outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags); |
| 2731 | if (outlen < 0) { |
| 2732 | /* An error occurred, release the string and return an |
| 2733 | error. */ |
| 2734 | Py_DECREF(buf); |
| 2735 | return NULL; |
| 2736 | } |
| 2737 | if (outlen != recvlen) { |
| 2738 | /* We did not read as many bytes as we anticipated, resize the |
| 2739 | string if possible and be successful. */ |
| 2740 | _PyBytes_Resize(&buf, outlen); |
| 2741 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2742 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2743 | return buf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2744 | } |
| 2745 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2746 | PyDoc_STRVAR(recv_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2747 | "recv(buffersize[, flags]) -> data\n\ |
| 2748 | \n\ |
| 2749 | Receive up to buffersize bytes from the socket. For the optional flags\n\ |
| 2750 | argument, see the Unix manual. When no data is available, block until\n\ |
| 2751 | 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] | 2752 | 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] | 2753 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2754 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2755 | /* s.recv_into(buffer, [nbytes [,flags]]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2756 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2757 | static PyObject* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2758 | sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2759 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2760 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2761 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2762 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2763 | Py_buffer pbuf; |
| 2764 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2765 | Py_ssize_t buflen, readlen, recvlen = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2766 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2767 | /* Get the buffer's memory */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2768 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recv_into", kwlist, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2769 | &pbuf, &recvlen, &flags)) |
| 2770 | return NULL; |
| 2771 | buf = pbuf.buf; |
| 2772 | buflen = pbuf.len; |
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 | if (recvlen < 0) { |
| 2775 | PyBuffer_Release(&pbuf); |
| 2776 | PyErr_SetString(PyExc_ValueError, |
| 2777 | "negative buffersize in recv_into"); |
| 2778 | return NULL; |
| 2779 | } |
| 2780 | if (recvlen == 0) { |
| 2781 | /* If nbytes was not specified, use the buffer's length */ |
| 2782 | recvlen = buflen; |
| 2783 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2784 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2785 | /* Check if the buffer is large enough */ |
| 2786 | if (buflen < recvlen) { |
| 2787 | PyBuffer_Release(&pbuf); |
| 2788 | PyErr_SetString(PyExc_ValueError, |
| 2789 | "buffer too small for requested bytes"); |
| 2790 | return NULL; |
| 2791 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2793 | /* Call the guts */ |
| 2794 | readlen = sock_recv_guts(s, buf, recvlen, flags); |
| 2795 | if (readlen < 0) { |
| 2796 | /* Return an error. */ |
| 2797 | PyBuffer_Release(&pbuf); |
| 2798 | return NULL; |
| 2799 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2800 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2801 | PyBuffer_Release(&pbuf); |
| 2802 | /* Return the number of bytes read. Note that we do not do anything |
| 2803 | special here in the case that readlen < recvlen. */ |
| 2804 | return PyLong_FromSsize_t(readlen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2805 | } |
| 2806 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2807 | PyDoc_STRVAR(recv_into_doc, |
| 2808 | "recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2809 | \n\ |
| 2810 | A version of recv() that stores its data into a buffer rather than creating \n\ |
| 2811 | a new string. Receive up to buffersize bytes from the socket. If buffersize \n\ |
| 2812 | is not specified (or 0), receive up to the size available in the given buffer.\n\ |
| 2813 | \n\ |
| 2814 | See recv() for documentation about the flags."); |
| 2815 | |
| 2816 | |
| 2817 | /* |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 2818 | * This is the guts of the recvfrom() and recvfrom_into() methods, which reads |
| 2819 | * into a char buffer. If you have any inc/def ref to do to the objects that |
| 2820 | * 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] | 2821 | * 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] | 2822 | * that it is also possible that we return a number of bytes smaller than the |
| 2823 | * request bytes. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2824 | * |
| 2825 | * 'addr' is a return value for the address object. Note that you must decref |
| 2826 | * it yourself. |
| 2827 | */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2828 | static Py_ssize_t |
| 2829 | 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] | 2830 | PyObject** addr) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2831 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2832 | sock_addr_t addrbuf; |
| 2833 | int timeout; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2834 | Py_ssize_t n = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2835 | socklen_t addrlen; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2836 | int async_err = 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2837 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2838 | *addr = NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2839 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2840 | if (!getsockaddrlen(s, &addrlen)) |
| 2841 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2842 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2843 | if (!IS_SELECTABLE(s)) { |
| 2844 | select_error(); |
| 2845 | return -1; |
| 2846 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2847 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2848 | BEGIN_SELECT_LOOP(s) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2849 | do { |
| 2850 | Py_BEGIN_ALLOW_THREADS |
| 2851 | memset(&addrbuf, 0, addrlen); |
| 2852 | timeout = internal_select_ex(s, 0, interval); |
| 2853 | if (!timeout) { |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 2854 | #ifdef MS_WINDOWS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2855 | if (len > INT_MAX) |
| 2856 | len = INT_MAX; |
| 2857 | n = recvfrom(s->sock_fd, cbuf, (int)len, flags, |
| 2858 | (void *) &addrbuf, &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2859 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2860 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2861 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2862 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2863 | } |
| 2864 | Py_END_ALLOW_THREADS |
| 2865 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2866 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2867 | if (timeout == 1) { |
| 2868 | PyErr_SetString(socket_timeout, "timed out"); |
| 2869 | return -1; |
| 2870 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2871 | END_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2872 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2873 | if (!async_err) |
| 2874 | s->errorhandler(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2875 | return -1; |
| 2876 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2877 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2878 | if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 2879 | addrlen, s->sock_proto))) |
| 2880 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2882 | return n; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
| 2885 | /* s.recvfrom(nbytes [,flags]) method */ |
| 2886 | |
| 2887 | static PyObject * |
| 2888 | sock_recvfrom(PySocketSockObject *s, PyObject *args) |
| 2889 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2890 | PyObject *buf = NULL; |
| 2891 | PyObject *addr = NULL; |
| 2892 | PyObject *ret = NULL; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2893 | int flags = 0; |
| 2894 | Py_ssize_t recvlen, outlen; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2895 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2896 | if (!PyArg_ParseTuple(args, "n|i:recvfrom", &recvlen, &flags)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2897 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2899 | if (recvlen < 0) { |
| 2900 | PyErr_SetString(PyExc_ValueError, |
| 2901 | "negative buffersize in recvfrom"); |
| 2902 | return NULL; |
| 2903 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2904 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2905 | buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
| 2906 | if (buf == NULL) |
| 2907 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2908 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2909 | outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf), |
| 2910 | recvlen, flags, &addr); |
| 2911 | if (outlen < 0) { |
| 2912 | goto finally; |
| 2913 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2915 | if (outlen != recvlen) { |
| 2916 | /* We did not read as many bytes as we anticipated, resize the |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 2917 | string if possible and be successful. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2918 | if (_PyBytes_Resize(&buf, outlen) < 0) |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 2919 | /* Oopsy, not so successful after all. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2920 | goto finally; |
| 2921 | } |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2922 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2923 | ret = PyTuple_Pack(2, buf, addr); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2924 | |
| 2925 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2926 | Py_XDECREF(buf); |
| 2927 | Py_XDECREF(addr); |
| 2928 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2931 | PyDoc_STRVAR(recvfrom_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2932 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
| 2933 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2934 | 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] | 2935 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2936 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2937 | /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2938 | |
| 2939 | static PyObject * |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2940 | sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2941 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2942 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2943 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2944 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2945 | Py_buffer pbuf; |
| 2946 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2947 | Py_ssize_t readlen, buflen, recvlen = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2949 | PyObject *addr = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2950 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2951 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recvfrom_into", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2952 | kwlist, &pbuf, |
| 2953 | &recvlen, &flags)) |
| 2954 | return NULL; |
| 2955 | buf = pbuf.buf; |
| 2956 | buflen = pbuf.len; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2957 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2958 | if (recvlen < 0) { |
| 2959 | PyBuffer_Release(&pbuf); |
| 2960 | PyErr_SetString(PyExc_ValueError, |
| 2961 | "negative buffersize in recvfrom_into"); |
| 2962 | return NULL; |
| 2963 | } |
| 2964 | if (recvlen == 0) { |
| 2965 | /* If nbytes was not specified, use the buffer's length */ |
| 2966 | recvlen = buflen; |
Benjamin Peterson | c6b37e2 | 2014-01-13 23:14:42 -0500 | [diff] [blame] | 2967 | } else if (recvlen > buflen) { |
| 2968 | PyBuffer_Release(&pbuf); |
| 2969 | PyErr_SetString(PyExc_ValueError, |
| 2970 | "nbytes is greater than the length of the buffer"); |
| 2971 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2972 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2973 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2974 | readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr); |
| 2975 | if (readlen < 0) { |
| 2976 | PyBuffer_Release(&pbuf); |
| 2977 | /* Return an error */ |
| 2978 | Py_XDECREF(addr); |
| 2979 | return NULL; |
| 2980 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2981 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2982 | PyBuffer_Release(&pbuf); |
| 2983 | /* Return the number of bytes read and the address. Note that we do |
| 2984 | not do anything special here in the case that readlen < recvlen. */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2985 | return Py_BuildValue("nN", readlen, addr); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2986 | } |
| 2987 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2988 | PyDoc_STRVAR(recvfrom_into_doc, |
| 2989 | "recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2990 | \n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2991 | 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] | 2992 | |
| 2993 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2994 | /* The sendmsg() and recvmsg[_into]() methods require a working |
| 2995 | CMSG_LEN(). See the comment near get_CMSG_LEN(). */ |
| 2996 | #ifdef CMSG_LEN |
| 2997 | /* |
| 2998 | * Call recvmsg() with the supplied iovec structures, flags, and |
| 2999 | * ancillary data buffer size (controllen). Returns the tuple return |
| 3000 | * value for recvmsg() or recvmsg_into(), with the first item provided |
| 3001 | * by the supplied makeval() function. makeval() will be called with |
| 3002 | * the length read and makeval_data as arguments, and must return a |
| 3003 | * new reference (which will be decrefed if there is a subsequent |
| 3004 | * error). On error, closes any file descriptors received via |
| 3005 | * SCM_RIGHTS. |
| 3006 | */ |
| 3007 | static PyObject * |
| 3008 | sock_recvmsg_guts(PySocketSockObject *s, struct iovec *iov, int iovlen, |
| 3009 | int flags, Py_ssize_t controllen, |
| 3010 | PyObject *(*makeval)(ssize_t, void *), void *makeval_data) |
| 3011 | { |
| 3012 | ssize_t bytes_received = -1; |
| 3013 | int timeout; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3014 | int async_err = 0; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3015 | sock_addr_t addrbuf; |
| 3016 | socklen_t addrbuflen; |
Charles-François Natali | e9e95ae | 2011-08-24 21:40:53 +0200 | [diff] [blame] | 3017 | struct msghdr msg = {0}; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3018 | PyObject *cmsg_list = NULL, *retval = NULL; |
| 3019 | void *controlbuf = NULL; |
| 3020 | struct cmsghdr *cmsgh; |
| 3021 | size_t cmsgdatalen = 0; |
| 3022 | int cmsg_status; |
| 3023 | |
| 3024 | /* XXX: POSIX says that msg_name and msg_namelen "shall be |
| 3025 | ignored" when the socket is connected (Linux fills them in |
| 3026 | anyway for AF_UNIX sockets at least). Normally msg_namelen |
| 3027 | seems to be set to 0 if there's no address, but try to |
| 3028 | initialize msg_name to something that won't be mistaken for a |
| 3029 | real address if that doesn't happen. */ |
| 3030 | if (!getsockaddrlen(s, &addrbuflen)) |
| 3031 | return NULL; |
| 3032 | memset(&addrbuf, 0, addrbuflen); |
| 3033 | SAS2SA(&addrbuf)->sa_family = AF_UNSPEC; |
| 3034 | |
| 3035 | if (controllen < 0 || controllen > SOCKLEN_T_LIMIT) { |
| 3036 | PyErr_SetString(PyExc_ValueError, |
| 3037 | "invalid ancillary data buffer length"); |
| 3038 | return NULL; |
| 3039 | } |
| 3040 | if (controllen > 0 && (controlbuf = PyMem_Malloc(controllen)) == NULL) |
| 3041 | return PyErr_NoMemory(); |
| 3042 | |
| 3043 | /* Make the system call. */ |
| 3044 | if (!IS_SELECTABLE(s)) { |
| 3045 | select_error(); |
| 3046 | goto finally; |
| 3047 | } |
| 3048 | |
| 3049 | BEGIN_SELECT_LOOP(s) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3050 | do { |
| 3051 | Py_BEGIN_ALLOW_THREADS; |
| 3052 | msg.msg_name = SAS2SA(&addrbuf); |
| 3053 | msg.msg_namelen = addrbuflen; |
| 3054 | msg.msg_iov = iov; |
| 3055 | msg.msg_iovlen = iovlen; |
| 3056 | msg.msg_control = controlbuf; |
| 3057 | msg.msg_controllen = controllen; |
| 3058 | timeout = internal_select_ex(s, 0, interval); |
| 3059 | if (!timeout) |
| 3060 | bytes_received = recvmsg(s->sock_fd, &msg, flags); |
| 3061 | Py_END_ALLOW_THREADS; |
| 3062 | if (timeout == 1) { |
| 3063 | PyErr_SetString(socket_timeout, "timed out"); |
| 3064 | goto finally; |
| 3065 | } |
| 3066 | } while (bytes_received < 0 && errno == EINTR && |
| 3067 | !(async_err = PyErr_CheckSignals())); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3068 | END_SELECT_LOOP(s) |
| 3069 | |
| 3070 | if (bytes_received < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3071 | if (!async_err) |
| 3072 | s->errorhandler(); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3073 | goto finally; |
| 3074 | } |
| 3075 | |
| 3076 | /* Make list of (level, type, data) tuples from control messages. */ |
| 3077 | if ((cmsg_list = PyList_New(0)) == NULL) |
| 3078 | goto err_closefds; |
| 3079 | /* Check for empty ancillary data as old CMSG_FIRSTHDR() |
| 3080 | implementations didn't do so. */ |
| 3081 | for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL); |
| 3082 | cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) { |
| 3083 | PyObject *bytes, *tuple; |
| 3084 | int tmp; |
| 3085 | |
| 3086 | cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen); |
| 3087 | if (cmsg_status != 0) { |
| 3088 | if (PyErr_WarnEx(PyExc_RuntimeWarning, |
| 3089 | "received malformed or improperly-truncated " |
| 3090 | "ancillary data", 1) == -1) |
| 3091 | goto err_closefds; |
| 3092 | } |
| 3093 | if (cmsg_status < 0) |
| 3094 | break; |
| 3095 | if (cmsgdatalen > PY_SSIZE_T_MAX) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3096 | PyErr_SetString(PyExc_OSError, "control message too long"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3097 | goto err_closefds; |
| 3098 | } |
| 3099 | |
| 3100 | bytes = PyBytes_FromStringAndSize((char *)CMSG_DATA(cmsgh), |
| 3101 | cmsgdatalen); |
| 3102 | tuple = Py_BuildValue("iiN", (int)cmsgh->cmsg_level, |
| 3103 | (int)cmsgh->cmsg_type, bytes); |
| 3104 | if (tuple == NULL) |
| 3105 | goto err_closefds; |
| 3106 | tmp = PyList_Append(cmsg_list, tuple); |
| 3107 | Py_DECREF(tuple); |
| 3108 | if (tmp != 0) |
| 3109 | goto err_closefds; |
| 3110 | |
| 3111 | if (cmsg_status != 0) |
| 3112 | break; |
| 3113 | } |
| 3114 | |
| 3115 | retval = Py_BuildValue("NOiN", |
| 3116 | (*makeval)(bytes_received, makeval_data), |
| 3117 | cmsg_list, |
| 3118 | (int)msg.msg_flags, |
| 3119 | makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 3120 | ((msg.msg_namelen > addrbuflen) ? |
| 3121 | addrbuflen : msg.msg_namelen), |
| 3122 | s->sock_proto)); |
| 3123 | if (retval == NULL) |
| 3124 | goto err_closefds; |
| 3125 | |
| 3126 | finally: |
| 3127 | Py_XDECREF(cmsg_list); |
| 3128 | PyMem_Free(controlbuf); |
| 3129 | return retval; |
| 3130 | |
| 3131 | err_closefds: |
| 3132 | #ifdef SCM_RIGHTS |
| 3133 | /* Close all descriptors coming from SCM_RIGHTS, so they don't leak. */ |
| 3134 | for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL); |
| 3135 | cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) { |
| 3136 | cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen); |
| 3137 | if (cmsg_status < 0) |
| 3138 | break; |
| 3139 | if (cmsgh->cmsg_level == SOL_SOCKET && |
| 3140 | cmsgh->cmsg_type == SCM_RIGHTS) { |
| 3141 | size_t numfds; |
| 3142 | int *fdp; |
| 3143 | |
| 3144 | numfds = cmsgdatalen / sizeof(int); |
| 3145 | fdp = (int *)CMSG_DATA(cmsgh); |
| 3146 | while (numfds-- > 0) |
| 3147 | close(*fdp++); |
| 3148 | } |
| 3149 | if (cmsg_status != 0) |
| 3150 | break; |
| 3151 | } |
| 3152 | #endif /* SCM_RIGHTS */ |
| 3153 | goto finally; |
| 3154 | } |
| 3155 | |
| 3156 | |
| 3157 | static PyObject * |
| 3158 | makeval_recvmsg(ssize_t received, void *data) |
| 3159 | { |
| 3160 | PyObject **buf = data; |
| 3161 | |
| 3162 | if (received < PyBytes_GET_SIZE(*buf)) |
| 3163 | _PyBytes_Resize(buf, received); |
| 3164 | Py_XINCREF(*buf); |
| 3165 | return *buf; |
| 3166 | } |
| 3167 | |
| 3168 | /* s.recvmsg(bufsize[, ancbufsize[, flags]]) method */ |
| 3169 | |
| 3170 | static PyObject * |
| 3171 | sock_recvmsg(PySocketSockObject *s, PyObject *args) |
| 3172 | { |
| 3173 | Py_ssize_t bufsize, ancbufsize = 0; |
| 3174 | int flags = 0; |
| 3175 | struct iovec iov; |
| 3176 | PyObject *buf = NULL, *retval = NULL; |
| 3177 | |
| 3178 | if (!PyArg_ParseTuple(args, "n|ni:recvmsg", &bufsize, &ancbufsize, &flags)) |
| 3179 | return NULL; |
| 3180 | |
| 3181 | if (bufsize < 0) { |
| 3182 | PyErr_SetString(PyExc_ValueError, "negative buffer size in recvmsg()"); |
| 3183 | return NULL; |
| 3184 | } |
| 3185 | if ((buf = PyBytes_FromStringAndSize(NULL, bufsize)) == NULL) |
| 3186 | return NULL; |
| 3187 | iov.iov_base = PyBytes_AS_STRING(buf); |
| 3188 | iov.iov_len = bufsize; |
| 3189 | |
| 3190 | /* Note that we're passing a pointer to *our pointer* to the bytes |
| 3191 | object here (&buf); makeval_recvmsg() may incref the object, or |
| 3192 | deallocate it and set our pointer to NULL. */ |
| 3193 | retval = sock_recvmsg_guts(s, &iov, 1, flags, ancbufsize, |
| 3194 | &makeval_recvmsg, &buf); |
| 3195 | Py_XDECREF(buf); |
| 3196 | return retval; |
| 3197 | } |
| 3198 | |
| 3199 | PyDoc_STRVAR(recvmsg_doc, |
| 3200 | "recvmsg(bufsize[, ancbufsize[, flags]]) -> (data, ancdata, msg_flags, address)\n\ |
| 3201 | \n\ |
| 3202 | Receive normal data (up to bufsize bytes) and ancillary data from the\n\ |
| 3203 | socket. The ancbufsize argument sets the size in bytes of the\n\ |
| 3204 | internal buffer used to receive the ancillary data; it defaults to 0,\n\ |
| 3205 | meaning that no ancillary data will be received. Appropriate buffer\n\ |
| 3206 | sizes for ancillary data can be calculated using CMSG_SPACE() or\n\ |
| 3207 | CMSG_LEN(), and items which do not fit into the buffer might be\n\ |
| 3208 | truncated or discarded. The flags argument defaults to 0 and has the\n\ |
| 3209 | same meaning as for recv().\n\ |
| 3210 | \n\ |
| 3211 | The return value is a 4-tuple: (data, ancdata, msg_flags, address).\n\ |
| 3212 | The data item is a bytes object holding the non-ancillary data\n\ |
| 3213 | received. The ancdata item is a list of zero or more tuples\n\ |
| 3214 | (cmsg_level, cmsg_type, cmsg_data) representing the ancillary data\n\ |
| 3215 | (control messages) received: cmsg_level and cmsg_type are integers\n\ |
| 3216 | specifying the protocol level and protocol-specific type respectively,\n\ |
| 3217 | and cmsg_data is a bytes object holding the associated data. The\n\ |
| 3218 | msg_flags item is the bitwise OR of various flags indicating\n\ |
| 3219 | conditions on the received message; see your system documentation for\n\ |
| 3220 | details. If the receiving socket is unconnected, address is the\n\ |
| 3221 | address of the sending socket, if available; otherwise, its value is\n\ |
| 3222 | unspecified.\n\ |
| 3223 | \n\ |
| 3224 | If recvmsg() raises an exception after the system call returns, it\n\ |
| 3225 | will first attempt to close any file descriptors received via the\n\ |
| 3226 | SCM_RIGHTS mechanism."); |
| 3227 | |
| 3228 | |
| 3229 | static PyObject * |
| 3230 | makeval_recvmsg_into(ssize_t received, void *data) |
| 3231 | { |
| 3232 | return PyLong_FromSsize_t(received); |
| 3233 | } |
| 3234 | |
| 3235 | /* s.recvmsg_into(buffers[, ancbufsize[, flags]]) method */ |
| 3236 | |
| 3237 | static PyObject * |
| 3238 | sock_recvmsg_into(PySocketSockObject *s, PyObject *args) |
| 3239 | { |
| 3240 | Py_ssize_t ancbufsize = 0; |
| 3241 | int flags = 0; |
| 3242 | struct iovec *iovs = NULL; |
| 3243 | Py_ssize_t i, nitems, nbufs = 0; |
| 3244 | Py_buffer *bufs = NULL; |
| 3245 | PyObject *buffers_arg, *fast, *retval = NULL; |
| 3246 | |
| 3247 | if (!PyArg_ParseTuple(args, "O|ni:recvmsg_into", |
| 3248 | &buffers_arg, &ancbufsize, &flags)) |
| 3249 | return NULL; |
| 3250 | |
| 3251 | if ((fast = PySequence_Fast(buffers_arg, |
| 3252 | "recvmsg_into() argument 1 must be an " |
| 3253 | "iterable")) == NULL) |
| 3254 | return NULL; |
| 3255 | nitems = PySequence_Fast_GET_SIZE(fast); |
| 3256 | if (nitems > INT_MAX) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3257 | PyErr_SetString(PyExc_OSError, "recvmsg_into() argument 1 is too long"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3258 | goto finally; |
| 3259 | } |
| 3260 | |
| 3261 | /* Fill in an iovec for each item, and save the Py_buffer |
| 3262 | structs to release afterwards. */ |
| 3263 | if (nitems > 0 && ((iovs = PyMem_New(struct iovec, nitems)) == NULL || |
| 3264 | (bufs = PyMem_New(Py_buffer, nitems)) == NULL)) { |
| 3265 | PyErr_NoMemory(); |
| 3266 | goto finally; |
| 3267 | } |
| 3268 | for (; nbufs < nitems; nbufs++) { |
| 3269 | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(fast, nbufs), |
| 3270 | "w*;recvmsg_into() argument 1 must be an iterable " |
| 3271 | "of single-segment read-write buffers", |
| 3272 | &bufs[nbufs])) |
| 3273 | goto finally; |
| 3274 | iovs[nbufs].iov_base = bufs[nbufs].buf; |
| 3275 | iovs[nbufs].iov_len = bufs[nbufs].len; |
| 3276 | } |
| 3277 | |
| 3278 | retval = sock_recvmsg_guts(s, iovs, nitems, flags, ancbufsize, |
| 3279 | &makeval_recvmsg_into, NULL); |
| 3280 | finally: |
| 3281 | for (i = 0; i < nbufs; i++) |
| 3282 | PyBuffer_Release(&bufs[i]); |
| 3283 | PyMem_Free(bufs); |
| 3284 | PyMem_Free(iovs); |
| 3285 | Py_DECREF(fast); |
| 3286 | return retval; |
| 3287 | } |
| 3288 | |
| 3289 | PyDoc_STRVAR(recvmsg_into_doc, |
| 3290 | "recvmsg_into(buffers[, ancbufsize[, flags]]) -> (nbytes, ancdata, msg_flags, address)\n\ |
| 3291 | \n\ |
| 3292 | Receive normal data and ancillary data from the socket, scattering the\n\ |
| 3293 | non-ancillary data into a series of buffers. The buffers argument\n\ |
| 3294 | must be an iterable of objects that export writable buffers\n\ |
| 3295 | (e.g. bytearray objects); these will be filled with successive chunks\n\ |
| 3296 | of the non-ancillary data until it has all been written or there are\n\ |
| 3297 | no more buffers. The ancbufsize argument sets the size in bytes of\n\ |
| 3298 | the internal buffer used to receive the ancillary data; it defaults to\n\ |
| 3299 | 0, meaning that no ancillary data will be received. Appropriate\n\ |
| 3300 | buffer sizes for ancillary data can be calculated using CMSG_SPACE()\n\ |
| 3301 | or CMSG_LEN(), and items which do not fit into the buffer might be\n\ |
| 3302 | truncated or discarded. The flags argument defaults to 0 and has the\n\ |
| 3303 | same meaning as for recv().\n\ |
| 3304 | \n\ |
| 3305 | The return value is a 4-tuple: (nbytes, ancdata, msg_flags, address).\n\ |
| 3306 | The nbytes item is the total number of bytes of non-ancillary data\n\ |
| 3307 | written into the buffers. The ancdata item is a list of zero or more\n\ |
| 3308 | tuples (cmsg_level, cmsg_type, cmsg_data) representing the ancillary\n\ |
| 3309 | data (control messages) received: cmsg_level and cmsg_type are\n\ |
| 3310 | integers specifying the protocol level and protocol-specific type\n\ |
| 3311 | respectively, and cmsg_data is a bytes object holding the associated\n\ |
| 3312 | data. The msg_flags item is the bitwise OR of various flags\n\ |
| 3313 | indicating conditions on the received message; see your system\n\ |
| 3314 | documentation for details. If the receiving socket is unconnected,\n\ |
| 3315 | address is the address of the sending socket, if available; otherwise,\n\ |
| 3316 | its value is unspecified.\n\ |
| 3317 | \n\ |
| 3318 | If recvmsg_into() raises an exception after the system call returns,\n\ |
| 3319 | it will first attempt to close any file descriptors received via the\n\ |
| 3320 | SCM_RIGHTS mechanism."); |
| 3321 | #endif /* CMSG_LEN */ |
| 3322 | |
| 3323 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 3324 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3325 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3326 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3327 | sock_send(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3328 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3329 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3330 | Py_ssize_t len, n = -1; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3331 | int async_err = 0; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3332 | int flags = 0, timeout; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3333 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3335 | if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags)) |
| 3336 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3337 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3338 | if (!IS_SELECTABLE(s)) { |
| 3339 | PyBuffer_Release(&pbuf); |
| 3340 | return select_error(); |
| 3341 | } |
| 3342 | buf = pbuf.buf; |
| 3343 | len = pbuf.len; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 3344 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3345 | BEGIN_SELECT_LOOP(s) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3346 | do { |
| 3347 | Py_BEGIN_ALLOW_THREADS |
| 3348 | timeout = internal_select_ex(s, 1, interval); |
| 3349 | if (!timeout) { |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 3350 | #ifdef MS_WINDOWS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3351 | if (len > INT_MAX) |
| 3352 | len = INT_MAX; |
| 3353 | n = send(s->sock_fd, buf, (int)len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3354 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3355 | n = send(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3356 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3357 | } |
| 3358 | Py_END_ALLOW_THREADS |
| 3359 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3360 | if (timeout == 1) { |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3361 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3362 | PyErr_SetString(socket_timeout, "timed out"); |
| 3363 | return NULL; |
| 3364 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3365 | END_SELECT_LOOP(s) |
| 3366 | |
| 3367 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3368 | if (n < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3369 | return (!async_err) ? s->errorhandler() : NULL; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3370 | return PyLong_FromSsize_t(n); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3371 | } |
| 3372 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3373 | PyDoc_STRVAR(send_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3374 | "send(data[, flags]) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3375 | \n\ |
| 3376 | 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] | 3377 | 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] | 3378 | 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] | 3379 | |
| 3380 | |
| 3381 | /* s.sendall(data [,flags]) method */ |
| 3382 | |
| 3383 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3384 | sock_sendall(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3385 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3386 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3387 | Py_ssize_t len, n = -1; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3388 | int async_err = 0; |
| 3389 | int flags = 0, timeout; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3390 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3391 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3392 | if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags)) |
| 3393 | return NULL; |
| 3394 | buf = pbuf.buf; |
| 3395 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3397 | if (!IS_SELECTABLE(s)) { |
| 3398 | PyBuffer_Release(&pbuf); |
| 3399 | return select_error(); |
| 3400 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 3401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3402 | do { |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3403 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3404 | timeout = internal_select(s, 1); |
| 3405 | n = -1; |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3406 | if (!timeout) { |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 3407 | #ifdef MS_WINDOWS |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 3408 | if (len > INT_MAX) |
| 3409 | len = INT_MAX; |
| 3410 | n = send(s->sock_fd, buf, (int)len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3411 | #else |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3412 | n = send(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3413 | #endif |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3414 | } |
| 3415 | Py_END_ALLOW_THREADS |
| 3416 | if (timeout == 1) { |
| 3417 | PyBuffer_Release(&pbuf); |
| 3418 | PyErr_SetString(socket_timeout, "timed out"); |
| 3419 | return NULL; |
| 3420 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3421 | if (n >= 0) { |
| 3422 | buf += n; |
| 3423 | len -= n; |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3424 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3425 | } while (len > 0 && (n >= 0 || errno == EINTR) && |
| 3426 | !(async_err = PyErr_CheckSignals())); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3427 | PyBuffer_Release(&pbuf); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3428 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3429 | if (n < 0 || async_err) |
| 3430 | return (!async_err) ? s->errorhandler() : NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3431 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3432 | Py_INCREF(Py_None); |
| 3433 | return Py_None; |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3434 | } |
| 3435 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3436 | PyDoc_STRVAR(sendall_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3437 | "sendall(data[, flags])\n\ |
| 3438 | \n\ |
| 3439 | Send a data string to the socket. For the optional flags\n\ |
| 3440 | argument, see the Unix manual. This calls send() repeatedly\n\ |
| 3441 | 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] | 3442 | to tell how much data has been sent."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3443 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3444 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 3445 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3446 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3447 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3448 | sock_sendto(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3449 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3450 | Py_buffer pbuf; |
| 3451 | PyObject *addro; |
| 3452 | char *buf; |
Antoine Pitrou | 5e98141 | 2011-03-17 22:38:37 +0100 | [diff] [blame] | 3453 | Py_ssize_t len, arglen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3454 | sock_addr_t addrbuf; |
| 3455 | int addrlen, n = -1, flags, timeout; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3456 | int async_err = 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3457 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3458 | flags = 0; |
Antoine Pitrou | 5e98141 | 2011-03-17 22:38:37 +0100 | [diff] [blame] | 3459 | arglen = PyTuple_Size(args); |
| 3460 | switch (arglen) { |
| 3461 | case 2: |
| 3462 | PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro); |
| 3463 | break; |
| 3464 | case 3: |
| 3465 | PyArg_ParseTuple(args, "y*iO:sendto", |
| 3466 | &pbuf, &flags, &addro); |
| 3467 | break; |
| 3468 | default: |
| 3469 | PyErr_Format(PyExc_TypeError, |
| 3470 | "sendto() takes 2 or 3 arguments (%d given)", |
| 3471 | arglen); |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 3472 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3473 | } |
Antoine Pitrou | 5e98141 | 2011-03-17 22:38:37 +0100 | [diff] [blame] | 3474 | if (PyErr_Occurred()) |
| 3475 | return NULL; |
| 3476 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3477 | buf = pbuf.buf; |
| 3478 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3479 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3480 | if (!IS_SELECTABLE(s)) { |
| 3481 | PyBuffer_Release(&pbuf); |
| 3482 | return select_error(); |
| 3483 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 3484 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3485 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) { |
| 3486 | PyBuffer_Release(&pbuf); |
| 3487 | return NULL; |
| 3488 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3489 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3490 | BEGIN_SELECT_LOOP(s) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3491 | do { |
| 3492 | Py_BEGIN_ALLOW_THREADS |
| 3493 | timeout = internal_select_ex(s, 1, interval); |
| 3494 | if (!timeout) { |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 3495 | #ifdef MS_WINDOWS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3496 | if (len > INT_MAX) |
| 3497 | len = INT_MAX; |
| 3498 | n = sendto(s->sock_fd, buf, (int)len, flags, |
| 3499 | SAS2SA(&addrbuf), addrlen); |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 3500 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3501 | n = sendto(s->sock_fd, buf, len, flags, |
| 3502 | SAS2SA(&addrbuf), addrlen); |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 3503 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3504 | } |
| 3505 | Py_END_ALLOW_THREADS |
| 3506 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3508 | if (timeout == 1) { |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3509 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3510 | PyErr_SetString(socket_timeout, "timed out"); |
| 3511 | return NULL; |
| 3512 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3513 | END_SELECT_LOOP(s) |
| 3514 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3515 | if (n < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3516 | return (!async_err) ? s->errorhandler() : NULL; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3517 | return PyLong_FromSsize_t(n); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3520 | PyDoc_STRVAR(sendto_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3521 | "sendto(data[, flags], address) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3522 | \n\ |
| 3523 | 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] | 3524 | For IP sockets, the address is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3525 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3526 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3527 | /* The sendmsg() and recvmsg[_into]() methods require a working |
| 3528 | CMSG_LEN(). See the comment near get_CMSG_LEN(). */ |
| 3529 | #ifdef CMSG_LEN |
| 3530 | /* s.sendmsg(buffers[, ancdata[, flags[, address]]]) method */ |
| 3531 | |
| 3532 | static PyObject * |
| 3533 | sock_sendmsg(PySocketSockObject *s, PyObject *args) |
| 3534 | { |
| 3535 | Py_ssize_t i, ndataparts, ndatabufs = 0, ncmsgs, ncmsgbufs = 0; |
| 3536 | Py_buffer *databufs = NULL; |
| 3537 | struct iovec *iovs = NULL; |
| 3538 | sock_addr_t addrbuf; |
Charles-François Natali | e9e95ae | 2011-08-24 21:40:53 +0200 | [diff] [blame] | 3539 | struct msghdr msg = {0}; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3540 | struct cmsginfo { |
| 3541 | int level; |
| 3542 | int type; |
| 3543 | Py_buffer data; |
| 3544 | } *cmsgs = NULL; |
| 3545 | void *controlbuf = NULL; |
| 3546 | size_t controllen, controllen_last; |
| 3547 | ssize_t bytes_sent = -1; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3548 | int async_err = 0; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3549 | int addrlen, timeout, flags = 0; |
| 3550 | PyObject *data_arg, *cmsg_arg = NULL, *addr_arg = NULL, *data_fast = NULL, |
| 3551 | *cmsg_fast = NULL, *retval = NULL; |
| 3552 | |
| 3553 | if (!PyArg_ParseTuple(args, "O|OiO:sendmsg", |
| 3554 | &data_arg, &cmsg_arg, &flags, &addr_arg)) |
| 3555 | return NULL; |
| 3556 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3557 | /* Parse destination address. */ |
| 3558 | if (addr_arg != NULL && addr_arg != Py_None) { |
| 3559 | if (!getsockaddrarg(s, addr_arg, SAS2SA(&addrbuf), &addrlen)) |
| 3560 | goto finally; |
| 3561 | msg.msg_name = &addrbuf; |
| 3562 | msg.msg_namelen = addrlen; |
| 3563 | } |
| 3564 | |
| 3565 | /* Fill in an iovec for each message part, and save the Py_buffer |
| 3566 | structs to release afterwards. */ |
| 3567 | if ((data_fast = PySequence_Fast(data_arg, |
| 3568 | "sendmsg() argument 1 must be an " |
| 3569 | "iterable")) == NULL) |
| 3570 | goto finally; |
| 3571 | ndataparts = PySequence_Fast_GET_SIZE(data_fast); |
| 3572 | if (ndataparts > INT_MAX) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3573 | PyErr_SetString(PyExc_OSError, "sendmsg() argument 1 is too long"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3574 | goto finally; |
| 3575 | } |
| 3576 | msg.msg_iovlen = ndataparts; |
| 3577 | if (ndataparts > 0 && |
| 3578 | ((msg.msg_iov = iovs = PyMem_New(struct iovec, ndataparts)) == NULL || |
| 3579 | (databufs = PyMem_New(Py_buffer, ndataparts)) == NULL)) { |
| 3580 | PyErr_NoMemory(); |
| 3581 | goto finally; |
| 3582 | } |
| 3583 | for (; ndatabufs < ndataparts; ndatabufs++) { |
| 3584 | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(data_fast, ndatabufs), |
| 3585 | "y*;sendmsg() argument 1 must be an iterable of " |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 3586 | "bytes-like objects", |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3587 | &databufs[ndatabufs])) |
| 3588 | goto finally; |
| 3589 | iovs[ndatabufs].iov_base = databufs[ndatabufs].buf; |
| 3590 | iovs[ndatabufs].iov_len = databufs[ndatabufs].len; |
| 3591 | } |
| 3592 | |
| 3593 | if (cmsg_arg == NULL) |
| 3594 | ncmsgs = 0; |
| 3595 | else { |
| 3596 | if ((cmsg_fast = PySequence_Fast(cmsg_arg, |
| 3597 | "sendmsg() argument 2 must be an " |
| 3598 | "iterable")) == NULL) |
| 3599 | goto finally; |
| 3600 | ncmsgs = PySequence_Fast_GET_SIZE(cmsg_fast); |
| 3601 | } |
| 3602 | |
| 3603 | #ifndef CMSG_SPACE |
| 3604 | if (ncmsgs > 1) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3605 | PyErr_SetString(PyExc_OSError, |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3606 | "sending multiple control messages is not supported " |
| 3607 | "on this system"); |
| 3608 | goto finally; |
| 3609 | } |
| 3610 | #endif |
| 3611 | /* Save level, type and Py_buffer for each control message, |
| 3612 | and calculate total size. */ |
| 3613 | if (ncmsgs > 0 && (cmsgs = PyMem_New(struct cmsginfo, ncmsgs)) == NULL) { |
| 3614 | PyErr_NoMemory(); |
| 3615 | goto finally; |
| 3616 | } |
| 3617 | controllen = controllen_last = 0; |
| 3618 | while (ncmsgbufs < ncmsgs) { |
| 3619 | size_t bufsize, space; |
| 3620 | |
| 3621 | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(cmsg_fast, ncmsgbufs), |
| 3622 | "(iiy*):[sendmsg() ancillary data items]", |
| 3623 | &cmsgs[ncmsgbufs].level, |
| 3624 | &cmsgs[ncmsgbufs].type, |
| 3625 | &cmsgs[ncmsgbufs].data)) |
| 3626 | goto finally; |
| 3627 | bufsize = cmsgs[ncmsgbufs++].data.len; |
| 3628 | |
| 3629 | #ifdef CMSG_SPACE |
| 3630 | if (!get_CMSG_SPACE(bufsize, &space)) { |
| 3631 | #else |
| 3632 | if (!get_CMSG_LEN(bufsize, &space)) { |
| 3633 | #endif |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3634 | PyErr_SetString(PyExc_OSError, "ancillary data item too large"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3635 | goto finally; |
| 3636 | } |
| 3637 | controllen += space; |
| 3638 | if (controllen > SOCKLEN_T_LIMIT || controllen < controllen_last) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3639 | PyErr_SetString(PyExc_OSError, "too much ancillary data"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3640 | goto finally; |
| 3641 | } |
| 3642 | controllen_last = controllen; |
| 3643 | } |
| 3644 | |
| 3645 | /* Construct ancillary data block from control message info. */ |
| 3646 | if (ncmsgbufs > 0) { |
| 3647 | struct cmsghdr *cmsgh = NULL; |
| 3648 | |
| 3649 | if ((msg.msg_control = controlbuf = |
| 3650 | PyMem_Malloc(controllen)) == NULL) { |
| 3651 | PyErr_NoMemory(); |
| 3652 | goto finally; |
| 3653 | } |
| 3654 | msg.msg_controllen = controllen; |
| 3655 | |
| 3656 | /* Need to zero out the buffer as a workaround for glibc's |
| 3657 | CMSG_NXTHDR() implementation. After getting the pointer to |
| 3658 | the next header, it checks its (uninitialized) cmsg_len |
| 3659 | member to see if the "message" fits in the buffer, and |
| 3660 | returns NULL if it doesn't. Zero-filling the buffer |
Terry Jan Reedy | 0f84764 | 2013-03-11 18:34:00 -0400 | [diff] [blame] | 3661 | ensures that this doesn't happen. */ |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3662 | memset(controlbuf, 0, controllen); |
| 3663 | |
| 3664 | for (i = 0; i < ncmsgbufs; i++) { |
| 3665 | size_t msg_len, data_len = cmsgs[i].data.len; |
| 3666 | int enough_space = 0; |
| 3667 | |
| 3668 | cmsgh = (i == 0) ? CMSG_FIRSTHDR(&msg) : CMSG_NXTHDR(&msg, cmsgh); |
| 3669 | if (cmsgh == NULL) { |
| 3670 | PyErr_Format(PyExc_RuntimeError, |
| 3671 | "unexpected NULL result from %s()", |
| 3672 | (i == 0) ? "CMSG_FIRSTHDR" : "CMSG_NXTHDR"); |
| 3673 | goto finally; |
| 3674 | } |
| 3675 | if (!get_CMSG_LEN(data_len, &msg_len)) { |
| 3676 | PyErr_SetString(PyExc_RuntimeError, |
| 3677 | "item size out of range for CMSG_LEN()"); |
| 3678 | goto finally; |
| 3679 | } |
| 3680 | if (cmsg_min_space(&msg, cmsgh, msg_len)) { |
| 3681 | size_t space; |
| 3682 | |
| 3683 | cmsgh->cmsg_len = msg_len; |
| 3684 | if (get_cmsg_data_space(&msg, cmsgh, &space)) |
| 3685 | enough_space = (space >= data_len); |
| 3686 | } |
| 3687 | if (!enough_space) { |
| 3688 | PyErr_SetString(PyExc_RuntimeError, |
| 3689 | "ancillary data does not fit in calculated " |
| 3690 | "space"); |
| 3691 | goto finally; |
| 3692 | } |
| 3693 | cmsgh->cmsg_level = cmsgs[i].level; |
| 3694 | cmsgh->cmsg_type = cmsgs[i].type; |
| 3695 | memcpy(CMSG_DATA(cmsgh), cmsgs[i].data.buf, data_len); |
| 3696 | } |
| 3697 | } |
| 3698 | |
| 3699 | /* Make the system call. */ |
| 3700 | if (!IS_SELECTABLE(s)) { |
| 3701 | select_error(); |
| 3702 | goto finally; |
| 3703 | } |
| 3704 | |
| 3705 | BEGIN_SELECT_LOOP(s) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3706 | do { |
| 3707 | Py_BEGIN_ALLOW_THREADS; |
| 3708 | timeout = internal_select_ex(s, 1, interval); |
| 3709 | if (!timeout) |
| 3710 | bytes_sent = sendmsg(s->sock_fd, &msg, flags); |
| 3711 | Py_END_ALLOW_THREADS; |
| 3712 | if (timeout == 1) { |
| 3713 | PyErr_SetString(socket_timeout, "timed out"); |
| 3714 | goto finally; |
| 3715 | } |
| 3716 | } while (bytes_sent < 0 && errno == EINTR && |
| 3717 | !(async_err = PyErr_CheckSignals())); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3718 | END_SELECT_LOOP(s) |
| 3719 | |
| 3720 | if (bytes_sent < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3721 | if (!async_err) |
| 3722 | s->errorhandler(); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3723 | goto finally; |
| 3724 | } |
| 3725 | retval = PyLong_FromSsize_t(bytes_sent); |
| 3726 | |
| 3727 | finally: |
| 3728 | PyMem_Free(controlbuf); |
| 3729 | for (i = 0; i < ncmsgbufs; i++) |
| 3730 | PyBuffer_Release(&cmsgs[i].data); |
| 3731 | PyMem_Free(cmsgs); |
| 3732 | Py_XDECREF(cmsg_fast); |
| 3733 | for (i = 0; i < ndatabufs; i++) |
| 3734 | PyBuffer_Release(&databufs[i]); |
| 3735 | PyMem_Free(databufs); |
| 3736 | PyMem_Free(iovs); |
| 3737 | Py_XDECREF(data_fast); |
| 3738 | return retval; |
| 3739 | } |
| 3740 | |
| 3741 | PyDoc_STRVAR(sendmsg_doc, |
| 3742 | "sendmsg(buffers[, ancdata[, flags[, address]]]) -> count\n\ |
| 3743 | \n\ |
| 3744 | Send normal and ancillary data to the socket, gathering the\n\ |
| 3745 | non-ancillary data from a series of buffers and concatenating it into\n\ |
| 3746 | a single message. The buffers argument specifies the non-ancillary\n\ |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 3747 | data as an iterable of bytes-like objects (e.g. bytes objects).\n\ |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3748 | The ancdata argument specifies the ancillary data (control messages)\n\ |
| 3749 | as an iterable of zero or more tuples (cmsg_level, cmsg_type,\n\ |
| 3750 | cmsg_data), where cmsg_level and cmsg_type are integers specifying the\n\ |
| 3751 | protocol level and protocol-specific type respectively, and cmsg_data\n\ |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 3752 | is a bytes-like object holding the associated data. The flags\n\ |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3753 | argument defaults to 0 and has the same meaning as for send(). If\n\ |
| 3754 | address is supplied and not None, it sets a destination address for\n\ |
| 3755 | the message. The return value is the number of bytes of non-ancillary\n\ |
| 3756 | data sent."); |
| 3757 | #endif /* CMSG_LEN */ |
| 3758 | |
| 3759 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3760 | /* s.shutdown(how) method */ |
| 3761 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3762 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3763 | sock_shutdown(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3764 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3765 | int how; |
| 3766 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 3767 | |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 3768 | how = _PyLong_AsInt(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3769 | if (how == -1 && PyErr_Occurred()) |
| 3770 | return NULL; |
| 3771 | Py_BEGIN_ALLOW_THREADS |
| 3772 | res = shutdown(s->sock_fd, how); |
| 3773 | Py_END_ALLOW_THREADS |
| 3774 | if (res < 0) |
| 3775 | return s->errorhandler(); |
| 3776 | Py_INCREF(Py_None); |
| 3777 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3778 | } |
| 3779 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3780 | PyDoc_STRVAR(shutdown_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3781 | "shutdown(flag)\n\ |
| 3782 | \n\ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 3783 | Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\ |
| 3784 | 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] | 3785 | |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 3786 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3787 | static PyObject* |
| 3788 | sock_ioctl(PySocketSockObject *s, PyObject *arg) |
| 3789 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3790 | unsigned long cmd = SIO_RCVALL; |
| 3791 | PyObject *argO; |
| 3792 | DWORD recv; |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3793 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3794 | if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO)) |
| 3795 | return NULL; |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3797 | switch (cmd) { |
| 3798 | case SIO_RCVALL: { |
| 3799 | unsigned int option = RCVALL_ON; |
| 3800 | if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option)) |
| 3801 | return NULL; |
| 3802 | if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option), |
| 3803 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 3804 | return set_error(); |
| 3805 | } |
| 3806 | return PyLong_FromUnsignedLong(recv); } |
| 3807 | case SIO_KEEPALIVE_VALS: { |
| 3808 | struct tcp_keepalive ka; |
| 3809 | if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd, |
| 3810 | &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval)) |
| 3811 | return NULL; |
| 3812 | if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka), |
| 3813 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 3814 | return set_error(); |
| 3815 | } |
| 3816 | return PyLong_FromUnsignedLong(recv); } |
| 3817 | default: |
| 3818 | PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd); |
| 3819 | return NULL; |
| 3820 | } |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3821 | } |
| 3822 | PyDoc_STRVAR(sock_ioctl_doc, |
| 3823 | "ioctl(cmd, option) -> long\n\ |
| 3824 | \n\ |
Kristján Valur Jónsson | 847ec75 | 2009-09-27 21:10:38 +0000 | [diff] [blame] | 3825 | Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\ |
| 3826 | SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\ |
| 3827 | 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] | 3828 | #endif |
| 3829 | |
| 3830 | #if defined(MS_WINDOWS) |
| 3831 | static PyObject* |
| 3832 | sock_share(PySocketSockObject *s, PyObject *arg) |
| 3833 | { |
| 3834 | WSAPROTOCOL_INFO info; |
| 3835 | DWORD processId; |
| 3836 | int result; |
| 3837 | |
| 3838 | if (!PyArg_ParseTuple(arg, "I", &processId)) |
| 3839 | return NULL; |
| 3840 | |
| 3841 | Py_BEGIN_ALLOW_THREADS |
| 3842 | result = WSADuplicateSocket(s->sock_fd, processId, &info); |
| 3843 | Py_END_ALLOW_THREADS |
| 3844 | if (result == SOCKET_ERROR) |
| 3845 | return set_error(); |
| 3846 | return PyBytes_FromStringAndSize((const char*)&info, sizeof(info)); |
| 3847 | } |
| 3848 | PyDoc_STRVAR(sock_share_doc, |
| 3849 | "share(process_id) -> bytes\n\ |
| 3850 | \n\ |
| 3851 | Share the socket with another process. The target process id\n\ |
| 3852 | must be provided and the resulting bytes object passed to the target\n\ |
| 3853 | process. There the shared socket can be instantiated by calling\n\ |
| 3854 | socket.fromshare()."); |
| 3855 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3856 | |
| 3857 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3858 | |
| 3859 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3860 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3861 | static PyMethodDef sock_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3862 | {"_accept", (PyCFunction)sock_accept, METH_NOARGS, |
| 3863 | accept_doc}, |
| 3864 | {"bind", (PyCFunction)sock_bind, METH_O, |
| 3865 | bind_doc}, |
| 3866 | {"close", (PyCFunction)sock_close, METH_NOARGS, |
| 3867 | close_doc}, |
| 3868 | {"connect", (PyCFunction)sock_connect, METH_O, |
| 3869 | connect_doc}, |
| 3870 | {"connect_ex", (PyCFunction)sock_connect_ex, METH_O, |
| 3871 | connect_ex_doc}, |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 3872 | {"detach", (PyCFunction)sock_detach, METH_NOARGS, |
| 3873 | detach_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3874 | {"fileno", (PyCFunction)sock_fileno, METH_NOARGS, |
| 3875 | fileno_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3876 | #ifdef HAVE_GETPEERNAME |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3877 | {"getpeername", (PyCFunction)sock_getpeername, |
| 3878 | METH_NOARGS, getpeername_doc}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 3879 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3880 | {"getsockname", (PyCFunction)sock_getsockname, |
| 3881 | METH_NOARGS, getsockname_doc}, |
| 3882 | {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS, |
| 3883 | getsockopt_doc}, |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 3884 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3885 | {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS, |
| 3886 | sock_ioctl_doc}, |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3887 | #endif |
Kristján Valur Jónsson | 10f383a | 2012-04-07 11:23:31 +0000 | [diff] [blame] | 3888 | #if defined(MS_WINDOWS) |
| 3889 | {"share", (PyCFunction)sock_share, METH_VARARGS, |
| 3890 | sock_share_doc}, |
| 3891 | #endif |
Charles-François Natali | 644b8f5 | 2014-05-22 19:45:39 +0100 | [diff] [blame] | 3892 | {"listen", (PyCFunction)sock_listen, METH_VARARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3893 | listen_doc}, |
| 3894 | {"recv", (PyCFunction)sock_recv, METH_VARARGS, |
| 3895 | recv_doc}, |
| 3896 | {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS, |
| 3897 | recv_into_doc}, |
| 3898 | {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS, |
| 3899 | recvfrom_doc}, |
| 3900 | {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS, |
| 3901 | recvfrom_into_doc}, |
| 3902 | {"send", (PyCFunction)sock_send, METH_VARARGS, |
| 3903 | send_doc}, |
| 3904 | {"sendall", (PyCFunction)sock_sendall, METH_VARARGS, |
| 3905 | sendall_doc}, |
| 3906 | {"sendto", (PyCFunction)sock_sendto, METH_VARARGS, |
| 3907 | sendto_doc}, |
| 3908 | {"setblocking", (PyCFunction)sock_setblocking, METH_O, |
| 3909 | setblocking_doc}, |
| 3910 | {"settimeout", (PyCFunction)sock_settimeout, METH_O, |
| 3911 | settimeout_doc}, |
| 3912 | {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS, |
| 3913 | gettimeout_doc}, |
| 3914 | {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS, |
| 3915 | setsockopt_doc}, |
| 3916 | {"shutdown", (PyCFunction)sock_shutdown, METH_O, |
| 3917 | shutdown_doc}, |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3918 | #ifdef CMSG_LEN |
| 3919 | {"recvmsg", (PyCFunction)sock_recvmsg, METH_VARARGS, |
| 3920 | recvmsg_doc}, |
| 3921 | {"recvmsg_into", (PyCFunction)sock_recvmsg_into, METH_VARARGS, |
| 3922 | recvmsg_into_doc,}, |
| 3923 | {"sendmsg", (PyCFunction)sock_sendmsg, METH_VARARGS, |
| 3924 | sendmsg_doc}, |
| 3925 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3926 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3927 | }; |
| 3928 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3929 | /* SockObject members */ |
| 3930 | static PyMemberDef sock_memberlist[] = { |
| 3931 | {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"}, |
| 3932 | {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"}, |
| 3933 | {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"}, |
| 3934 | {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"}, |
| 3935 | {0}, |
| 3936 | }; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3937 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3938 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3939 | First close the file description. */ |
| 3940 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3941 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3942 | sock_dealloc(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3943 | { |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 3944 | if (s->sock_fd != -1) { |
| 3945 | PyObject *exc, *val, *tb; |
| 3946 | Py_ssize_t old_refcount = Py_REFCNT(s); |
| 3947 | ++Py_REFCNT(s); |
| 3948 | PyErr_Fetch(&exc, &val, &tb); |
| 3949 | if (PyErr_WarnFormat(PyExc_ResourceWarning, 1, |
| 3950 | "unclosed %R", s)) |
| 3951 | /* Spurious errors can appear at shutdown */ |
| 3952 | if (PyErr_ExceptionMatches(PyExc_Warning)) |
| 3953 | PyErr_WriteUnraisable((PyObject *) s); |
| 3954 | PyErr_Restore(exc, val, tb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3955 | (void) SOCKETCLOSE(s->sock_fd); |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 3956 | Py_REFCNT(s) = old_refcount; |
| 3957 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3958 | Py_TYPE(s)->tp_free((PyObject *)s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3959 | } |
| 3960 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3961 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3962 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3963 | sock_repr(PySocketSockObject *s) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3964 | { |
Victor Stinner | e254e53 | 2014-07-26 14:36:55 +0200 | [diff] [blame] | 3965 | long sock_fd; |
| 3966 | /* On Windows, this test is needed because SOCKET_T is unsigned */ |
| 3967 | if (s->sock_fd == INVALID_SOCKET) { |
| 3968 | sock_fd = -1; |
| 3969 | } |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 3970 | #if SIZEOF_SOCKET_T > SIZEOF_LONG |
Victor Stinner | e254e53 | 2014-07-26 14:36:55 +0200 | [diff] [blame] | 3971 | else if (s->sock_fd > LONG_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3972 | /* this can occur on Win64, and actually there is a special |
| 3973 | ugly printf formatter for decimal pointer length integer |
| 3974 | printing, only bother if necessary*/ |
| 3975 | PyErr_SetString(PyExc_OverflowError, |
| 3976 | "no printf formatter to display " |
| 3977 | "the socket descriptor in decimal"); |
| 3978 | return NULL; |
| 3979 | } |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 3980 | #endif |
Victor Stinner | e254e53 | 2014-07-26 14:36:55 +0200 | [diff] [blame] | 3981 | else |
| 3982 | sock_fd = (long)s->sock_fd; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3983 | return PyUnicode_FromFormat( |
| 3984 | "<socket object, fd=%ld, family=%d, type=%d, proto=%d>", |
Victor Stinner | e254e53 | 2014-07-26 14:36:55 +0200 | [diff] [blame] | 3985 | sock_fd, s->sock_family, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3986 | s->sock_type, |
| 3987 | s->sock_proto); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3988 | } |
| 3989 | |
| 3990 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3991 | /* Create a new, uninitialized socket object. */ |
| 3992 | |
| 3993 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3994 | sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3995 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3996 | PyObject *new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3997 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3998 | new = type->tp_alloc(type, 0); |
| 3999 | if (new != NULL) { |
| 4000 | ((PySocketSockObject *)new)->sock_fd = -1; |
| 4001 | ((PySocketSockObject *)new)->sock_timeout = -1.0; |
| 4002 | ((PySocketSockObject *)new)->errorhandler = &set_error; |
| 4003 | } |
| 4004 | return new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4005 | } |
| 4006 | |
| 4007 | |
| 4008 | /* Initialize a new socket object. */ |
| 4009 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4010 | #ifdef SOCK_CLOEXEC |
| 4011 | /* socket() and socketpair() fail with EINVAL on Linux kernel older |
| 4012 | * than 2.6.27 if SOCK_CLOEXEC flag is set in the socket type. */ |
| 4013 | static int sock_cloexec_works = -1; |
| 4014 | #endif |
| 4015 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4016 | /*ARGSUSED*/ |
| 4017 | static int |
Andrew MacIntyre | 7aec4a2 | 2002-06-13 11:53:52 +0000 | [diff] [blame] | 4018 | sock_initobj(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4019 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4020 | PySocketSockObject *s = (PySocketSockObject *)self; |
| 4021 | PyObject *fdobj = NULL; |
| 4022 | SOCKET_T fd = INVALID_SOCKET; |
| 4023 | int family = AF_INET, type = SOCK_STREAM, proto = 0; |
| 4024 | static char *keywords[] = {"family", "type", "proto", "fileno", 0}; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4025 | #ifndef MS_WINDOWS |
| 4026 | #ifdef SOCK_CLOEXEC |
| 4027 | int *atomic_flag_works = &sock_cloexec_works; |
| 4028 | #else |
| 4029 | int *atomic_flag_works = NULL; |
| 4030 | #endif |
| 4031 | #endif |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4032 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4033 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 4034 | "|iiiO:socket", keywords, |
| 4035 | &family, &type, &proto, &fdobj)) |
| 4036 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4037 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4038 | if (fdobj != NULL && fdobj != Py_None) { |
Kristján Valur Jónsson | 10f383a | 2012-04-07 11:23:31 +0000 | [diff] [blame] | 4039 | #ifdef MS_WINDOWS |
| 4040 | /* recreate a socket that was duplicated */ |
| 4041 | if (PyBytes_Check(fdobj)) { |
| 4042 | WSAPROTOCOL_INFO info; |
| 4043 | if (PyBytes_GET_SIZE(fdobj) != sizeof(info)) { |
| 4044 | PyErr_Format(PyExc_ValueError, |
| 4045 | "socket descriptor string has wrong size, " |
| 4046 | "should be %zu bytes.", sizeof(info)); |
| 4047 | return -1; |
| 4048 | } |
| 4049 | memcpy(&info, PyBytes_AS_STRING(fdobj), sizeof(info)); |
| 4050 | Py_BEGIN_ALLOW_THREADS |
| 4051 | fd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, |
| 4052 | FROM_PROTOCOL_INFO, &info, 0, WSA_FLAG_OVERLAPPED); |
| 4053 | Py_END_ALLOW_THREADS |
| 4054 | if (fd == INVALID_SOCKET) { |
| 4055 | set_error(); |
| 4056 | return -1; |
| 4057 | } |
| 4058 | family = info.iAddressFamily; |
| 4059 | type = info.iSocketType; |
| 4060 | proto = info.iProtocol; |
| 4061 | } |
| 4062 | else |
| 4063 | #endif |
| 4064 | { |
| 4065 | fd = PyLong_AsSocket_t(fdobj); |
| 4066 | if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
| 4067 | return -1; |
| 4068 | if (fd == INVALID_SOCKET) { |
| 4069 | PyErr_SetString(PyExc_ValueError, |
| 4070 | "can't use invalid socket value"); |
| 4071 | return -1; |
| 4072 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4073 | } |
| 4074 | } |
| 4075 | else { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4076 | #ifdef MS_WINDOWS |
| 4077 | /* Windows implementation */ |
| 4078 | #ifndef WSA_FLAG_NO_HANDLE_INHERIT |
| 4079 | #define WSA_FLAG_NO_HANDLE_INHERIT 0x80 |
| 4080 | #endif |
| 4081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4082 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4083 | if (support_wsa_no_inherit) { |
| 4084 | fd = WSASocket(family, type, proto, |
| 4085 | NULL, 0, |
| 4086 | WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT); |
| 4087 | if (fd == INVALID_SOCKET) { |
| 4088 | /* Windows 7 or Windows 2008 R2 without SP1 or the hotfix */ |
| 4089 | support_wsa_no_inherit = 0; |
| 4090 | fd = socket(family, type, proto); |
| 4091 | } |
| 4092 | } |
| 4093 | else { |
| 4094 | fd = socket(family, type, proto); |
| 4095 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4096 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4097 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4098 | if (fd == INVALID_SOCKET) { |
| 4099 | set_error(); |
| 4100 | return -1; |
| 4101 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4102 | |
| 4103 | if (!support_wsa_no_inherit) { |
| 4104 | if (!SetHandleInformation((HANDLE)fd, HANDLE_FLAG_INHERIT, 0)) { |
| 4105 | closesocket(fd); |
| 4106 | PyErr_SetFromWindowsErr(0); |
| 4107 | return -1; |
| 4108 | } |
| 4109 | } |
| 4110 | #else |
| 4111 | /* UNIX */ |
| 4112 | Py_BEGIN_ALLOW_THREADS |
| 4113 | #ifdef SOCK_CLOEXEC |
| 4114 | if (sock_cloexec_works != 0) { |
| 4115 | fd = socket(family, type | SOCK_CLOEXEC, proto); |
| 4116 | if (sock_cloexec_works == -1) { |
| 4117 | if (fd >= 0) { |
| 4118 | sock_cloexec_works = 1; |
| 4119 | } |
| 4120 | else if (errno == EINVAL) { |
| 4121 | /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */ |
| 4122 | sock_cloexec_works = 0; |
| 4123 | fd = socket(family, type, proto); |
| 4124 | } |
| 4125 | } |
| 4126 | } |
| 4127 | else |
| 4128 | #endif |
| 4129 | { |
| 4130 | fd = socket(family, type, proto); |
| 4131 | } |
| 4132 | Py_END_ALLOW_THREADS |
| 4133 | |
| 4134 | if (fd == INVALID_SOCKET) { |
| 4135 | set_error(); |
| 4136 | return -1; |
| 4137 | } |
| 4138 | |
| 4139 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 4140 | SOCKETCLOSE(fd); |
| 4141 | return -1; |
| 4142 | } |
| 4143 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4144 | } |
| 4145 | init_sockobject(s, fd, family, type, proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4146 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4147 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4148 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
| 4151 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4152 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4153 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4154 | static PyTypeObject sock_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4155 | PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */ |
| 4156 | "_socket.socket", /* tp_name */ |
| 4157 | sizeof(PySocketSockObject), /* tp_basicsize */ |
| 4158 | 0, /* tp_itemsize */ |
| 4159 | (destructor)sock_dealloc, /* tp_dealloc */ |
| 4160 | 0, /* tp_print */ |
| 4161 | 0, /* tp_getattr */ |
| 4162 | 0, /* tp_setattr */ |
| 4163 | 0, /* tp_reserved */ |
| 4164 | (reprfunc)sock_repr, /* tp_repr */ |
| 4165 | 0, /* tp_as_number */ |
| 4166 | 0, /* tp_as_sequence */ |
| 4167 | 0, /* tp_as_mapping */ |
| 4168 | 0, /* tp_hash */ |
| 4169 | 0, /* tp_call */ |
| 4170 | 0, /* tp_str */ |
| 4171 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4172 | 0, /* tp_setattro */ |
| 4173 | 0, /* tp_as_buffer */ |
| 4174 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 4175 | sock_doc, /* tp_doc */ |
| 4176 | 0, /* tp_traverse */ |
| 4177 | 0, /* tp_clear */ |
| 4178 | 0, /* tp_richcompare */ |
| 4179 | 0, /* tp_weaklistoffset */ |
| 4180 | 0, /* tp_iter */ |
| 4181 | 0, /* tp_iternext */ |
| 4182 | sock_methods, /* tp_methods */ |
| 4183 | sock_memberlist, /* tp_members */ |
| 4184 | 0, /* tp_getset */ |
| 4185 | 0, /* tp_base */ |
| 4186 | 0, /* tp_dict */ |
| 4187 | 0, /* tp_descr_get */ |
| 4188 | 0, /* tp_descr_set */ |
| 4189 | 0, /* tp_dictoffset */ |
| 4190 | sock_initobj, /* tp_init */ |
| 4191 | PyType_GenericAlloc, /* tp_alloc */ |
| 4192 | sock_new, /* tp_new */ |
| 4193 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4194 | }; |
| 4195 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4196 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 4197 | /* Python interface to gethostname(). */ |
| 4198 | |
| 4199 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4200 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4201 | socket_gethostname(PyObject *self, PyObject *unused) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 4202 | { |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4203 | #ifdef MS_WINDOWS |
| 4204 | /* Don't use winsock's gethostname, as this returns the ANSI |
| 4205 | version of the hostname, whereas we need a Unicode string. |
| 4206 | Otherwise, gethostname apparently also returns the DNS name. */ |
Hirokazu Yamamoto | 09fff7a | 2010-11-05 17:24:13 +0000 | [diff] [blame] | 4207 | wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 4208 | DWORD size = Py_ARRAY_LENGTH(buf); |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4209 | wchar_t *name; |
Hirokazu Yamamoto | 09fff7a | 2010-11-05 17:24:13 +0000 | [diff] [blame] | 4210 | PyObject *result; |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4211 | |
| 4212 | if (GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size)) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 4213 | return PyUnicode_FromWideChar(buf, size); |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4214 | |
| 4215 | if (GetLastError() != ERROR_MORE_DATA) |
| 4216 | return PyErr_SetFromWindowsErr(0); |
| 4217 | |
| 4218 | if (size == 0) |
| 4219 | return PyUnicode_New(0, 0); |
| 4220 | |
| 4221 | /* MSDN says ERROR_MORE_DATA may occur because DNS allows longer |
| 4222 | names */ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 4223 | name = PyMem_New(wchar_t, size); |
| 4224 | if (!name) { |
| 4225 | PyErr_NoMemory(); |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4226 | return NULL; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 4227 | } |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4228 | if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname, |
| 4229 | name, |
| 4230 | &size)) |
| 4231 | { |
| 4232 | PyMem_Free(name); |
| 4233 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4234 | } |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4235 | |
| 4236 | result = PyUnicode_FromWideChar(name, size); |
| 4237 | PyMem_Free(name); |
| 4238 | return result; |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4239 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4240 | char buf[1024]; |
| 4241 | int res; |
| 4242 | Py_BEGIN_ALLOW_THREADS |
| 4243 | res = gethostname(buf, (int) sizeof buf - 1); |
| 4244 | Py_END_ALLOW_THREADS |
| 4245 | if (res < 0) |
| 4246 | return set_error(); |
| 4247 | buf[sizeof buf - 1] = '\0'; |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4248 | return PyUnicode_DecodeFSDefault(buf); |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4249 | #endif |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 4250 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 4251 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4252 | PyDoc_STRVAR(gethostname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4253 | "gethostname() -> string\n\ |
| 4254 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4255 | Return the current host name."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4256 | |
Antoine Pitrou | 061cfb5 | 2011-02-28 22:25:22 +0000 | [diff] [blame] | 4257 | #ifdef HAVE_SETHOSTNAME |
| 4258 | PyDoc_STRVAR(sethostname_doc, |
| 4259 | "sethostname(name)\n\n\ |
| 4260 | Sets the hostname to name."); |
| 4261 | |
| 4262 | static PyObject * |
| 4263 | socket_sethostname(PyObject *self, PyObject *args) |
| 4264 | { |
| 4265 | PyObject *hnobj; |
| 4266 | Py_buffer buf; |
| 4267 | int res, flag = 0; |
| 4268 | |
Christian Heimes | d2774c7 | 2013-06-19 02:06:29 +0200 | [diff] [blame] | 4269 | #ifdef _AIX |
| 4270 | /* issue #18259, not declared in any useful header file */ |
| 4271 | extern int sethostname(const char *, size_t); |
| 4272 | #endif |
| 4273 | |
Antoine Pitrou | 061cfb5 | 2011-02-28 22:25:22 +0000 | [diff] [blame] | 4274 | if (!PyArg_ParseTuple(args, "S:sethostname", &hnobj)) { |
| 4275 | PyErr_Clear(); |
| 4276 | if (!PyArg_ParseTuple(args, "O&:sethostname", |
| 4277 | PyUnicode_FSConverter, &hnobj)) |
| 4278 | return NULL; |
| 4279 | flag = 1; |
| 4280 | } |
| 4281 | res = PyObject_GetBuffer(hnobj, &buf, PyBUF_SIMPLE); |
| 4282 | if (!res) { |
| 4283 | res = sethostname(buf.buf, buf.len); |
| 4284 | PyBuffer_Release(&buf); |
| 4285 | } |
| 4286 | if (flag) |
| 4287 | Py_DECREF(hnobj); |
| 4288 | if (res) |
| 4289 | return set_error(); |
| 4290 | Py_RETURN_NONE; |
| 4291 | } |
| 4292 | #endif |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 4293 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4294 | /* Python interface to gethostbyname(name). */ |
| 4295 | |
| 4296 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4297 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4298 | socket_gethostbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4299 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4300 | char *name; |
| 4301 | sock_addr_t addrbuf; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4302 | PyObject *ret = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4303 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4304 | if (!PyArg_ParseTuple(args, "et:gethostbyname", "idna", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4305 | return NULL; |
| 4306 | if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4307 | goto finally; |
| 4308 | ret = makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in)); |
| 4309 | finally: |
| 4310 | PyMem_Free(name); |
| 4311 | return ret; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4312 | } |
| 4313 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4314 | PyDoc_STRVAR(gethostbyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4315 | "gethostbyname(host) -> address\n\ |
| 4316 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4317 | 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] | 4318 | |
| 4319 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4320 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
| 4321 | |
| 4322 | static PyObject * |
Victor Stinner | 1a62a68 | 2014-08-17 19:33:28 +0200 | [diff] [blame] | 4323 | 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] | 4324 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4325 | char **pch; |
| 4326 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 4327 | PyObject *name_list = (PyObject *)NULL; |
| 4328 | PyObject *addr_list = (PyObject *)NULL; |
| 4329 | PyObject *tmp; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4330 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4331 | if (h == NULL) { |
| 4332 | /* Let's get real error message to return */ |
| 4333 | set_herror(h_errno); |
| 4334 | return NULL; |
| 4335 | } |
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 | if (h->h_addrtype != af) { |
| 4338 | /* Let's get real error message to return */ |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 4339 | errno = EAFNOSUPPORT; |
| 4340 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4341 | return NULL; |
| 4342 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4343 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4344 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4345 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4346 | case AF_INET: |
| 4347 | if (alen < sizeof(struct sockaddr_in)) |
| 4348 | return NULL; |
| 4349 | break; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4350 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4351 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4352 | case AF_INET6: |
| 4353 | if (alen < sizeof(struct sockaddr_in6)) |
| 4354 | return NULL; |
| 4355 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4356 | #endif |
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 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4360 | if ((name_list = PyList_New(0)) == NULL) |
| 4361 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4362 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4363 | if ((addr_list = PyList_New(0)) == NULL) |
| 4364 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4366 | /* SF #1511317: h_aliases can be NULL */ |
| 4367 | if (h->h_aliases) { |
| 4368 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 4369 | int status; |
| 4370 | tmp = PyUnicode_FromString(*pch); |
| 4371 | if (tmp == NULL) |
| 4372 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4374 | status = PyList_Append(name_list, tmp); |
| 4375 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4377 | if (status) |
| 4378 | goto err; |
| 4379 | } |
| 4380 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4381 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4382 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 4383 | int status; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4384 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4385 | switch (af) { |
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 | case AF_INET: |
| 4388 | { |
| 4389 | struct sockaddr_in sin; |
| 4390 | memset(&sin, 0, sizeof(sin)); |
| 4391 | sin.sin_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4392 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4393 | sin.sin_len = sizeof(sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4394 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4395 | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); |
| 4396 | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4398 | if (pch == h->h_addr_list && alen >= sizeof(sin)) |
| 4399 | memcpy((char *) addr, &sin, sizeof(sin)); |
| 4400 | break; |
| 4401 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4402 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4403 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4404 | case AF_INET6: |
| 4405 | { |
| 4406 | struct sockaddr_in6 sin6; |
| 4407 | memset(&sin6, 0, sizeof(sin6)); |
| 4408 | sin6.sin6_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4409 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4410 | sin6.sin6_len = sizeof(sin6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4411 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4412 | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); |
| 4413 | tmp = makeipaddr((struct sockaddr *)&sin6, |
| 4414 | sizeof(sin6)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4415 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4416 | if (pch == h->h_addr_list && alen >= sizeof(sin6)) |
| 4417 | memcpy((char *) addr, &sin6, sizeof(sin6)); |
| 4418 | break; |
| 4419 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4420 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4421 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4422 | default: /* can't happen */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4423 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4424 | "unsupported address family"); |
| 4425 | return NULL; |
| 4426 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4427 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4428 | if (tmp == NULL) |
| 4429 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4431 | status = PyList_Append(addr_list, tmp); |
| 4432 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4434 | if (status) |
| 4435 | goto err; |
| 4436 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4437 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4438 | 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] | 4439 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4440 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4441 | Py_XDECREF(name_list); |
| 4442 | Py_XDECREF(addr_list); |
| 4443 | return rtn_tuple; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4444 | } |
| 4445 | |
| 4446 | |
| 4447 | /* Python interface to gethostbyname_ex(name). */ |
| 4448 | |
| 4449 | /*ARGSUSED*/ |
| 4450 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4451 | socket_gethostbyname_ex(PyObject *self, PyObject *args) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4452 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4453 | char *name; |
| 4454 | struct hostent *h; |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4455 | sock_addr_t addr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4456 | struct sockaddr *sa; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4457 | PyObject *ret = NULL; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4458 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4459 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4460 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4461 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4462 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4463 | char buf[16384]; |
| 4464 | int buf_len = (sizeof buf) - 1; |
| 4465 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4466 | #endif |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4467 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4468 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4469 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4470 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4471 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4472 | if (!PyArg_ParseTuple(args, "et:gethostbyname_ex", "idna", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4473 | return NULL; |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4474 | if (setipaddr(name, SAS2SA(&addr), sizeof(addr), AF_INET) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4475 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4476 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4477 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4478 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4479 | gethostbyname_r(name, &hp_allocated, buf, buf_len, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4480 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4481 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4482 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4483 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4484 | memset((void *) &data, '\0', sizeof(data)); |
| 4485 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 4486 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4487 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4488 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4489 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4490 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4491 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4492 | h = gethostbyname(name); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4493 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4494 | Py_END_ALLOW_THREADS |
| 4495 | /* Some C libraries would require addr.__ss_family instead of |
| 4496 | addr.ss_family. |
| 4497 | Therefore, we cast the sockaddr_storage into sockaddr to |
| 4498 | access sa_family. */ |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4499 | sa = SAS2SA(&addr); |
| 4500 | ret = gethost_common(h, SAS2SA(&addr), sizeof(addr), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4501 | sa->sa_family); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4502 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4503 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 4504 | #endif |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4505 | finally: |
| 4506 | PyMem_Free(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4507 | return ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4508 | } |
| 4509 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4510 | PyDoc_STRVAR(ghbn_ex_doc, |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4511 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
| 4512 | \n\ |
| 4513 | 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] | 4514 | 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] | 4515 | |
| 4516 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4517 | /* Python interface to gethostbyaddr(IP). */ |
| 4518 | |
| 4519 | /*ARGSUSED*/ |
| 4520 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4521 | socket_gethostbyaddr(PyObject *self, PyObject *args) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4522 | { |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4523 | sock_addr_t addr; |
| 4524 | struct sockaddr *sa = SAS2SA(&addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4525 | char *ip_num; |
| 4526 | struct hostent *h; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4527 | PyObject *ret = NULL; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4528 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4529 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4530 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4531 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4532 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4533 | /* glibcs up to 2.10 assume that the buf argument to |
| 4534 | gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc |
| 4535 | does not ensure. The attribute below instructs the compiler |
| 4536 | to maintain this alignment. */ |
| 4537 | char buf[16384] Py_ALIGNED(8); |
| 4538 | int buf_len = (sizeof buf) - 1; |
| 4539 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4540 | #endif |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4541 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4542 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4543 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4544 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4545 | char *ap; |
| 4546 | int al; |
| 4547 | int af; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4548 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4549 | if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4550 | return NULL; |
| 4551 | af = AF_UNSPEC; |
| 4552 | if (setipaddr(ip_num, sa, sizeof(addr), af) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4553 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4554 | af = sa->sa_family; |
| 4555 | ap = NULL; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 4556 | /* al = 0; */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4557 | switch (af) { |
| 4558 | case AF_INET: |
| 4559 | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr; |
| 4560 | al = sizeof(((struct sockaddr_in *)sa)->sin_addr); |
| 4561 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4562 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4563 | case AF_INET6: |
| 4564 | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr; |
| 4565 | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); |
| 4566 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4567 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4568 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4569 | PyErr_SetString(PyExc_OSError, "unsupported address family"); |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4570 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4571 | } |
| 4572 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4573 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4574 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4575 | gethostbyaddr_r(ap, al, af, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4576 | &hp_allocated, buf, buf_len, |
| 4577 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4578 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4579 | h = gethostbyaddr_r(ap, al, af, |
| 4580 | &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4581 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4582 | memset((void *) &data, '\0', sizeof(data)); |
| 4583 | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); |
| 4584 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4585 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4586 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4587 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4588 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4589 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4590 | h = gethostbyaddr(ap, al, af); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4591 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4592 | Py_END_ALLOW_THREADS |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4593 | ret = gethost_common(h, SAS2SA(&addr), sizeof(addr), af); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4594 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4595 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4596 | #endif |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4597 | finally: |
| 4598 | PyMem_Free(ip_num); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4599 | return ret; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4600 | } |
| 4601 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4602 | PyDoc_STRVAR(gethostbyaddr_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4603 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
| 4604 | \n\ |
| 4605 | 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] | 4606 | 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] | 4607 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4608 | |
| 4609 | /* Python interface to getservbyname(name). |
| 4610 | This only returns the port number, since the other info is already |
| 4611 | known or not useful (like the list of aliases). */ |
| 4612 | |
| 4613 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4614 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4615 | socket_getservbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4616 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4617 | char *name, *proto=NULL; |
| 4618 | struct servent *sp; |
| 4619 | if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto)) |
| 4620 | return NULL; |
| 4621 | Py_BEGIN_ALLOW_THREADS |
| 4622 | sp = getservbyname(name, proto); |
| 4623 | Py_END_ALLOW_THREADS |
| 4624 | if (sp == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4625 | PyErr_SetString(PyExc_OSError, "service/proto not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4626 | return NULL; |
| 4627 | } |
| 4628 | return PyLong_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4631 | PyDoc_STRVAR(getservbyname_doc, |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4632 | "getservbyname(servicename[, protocolname]) -> integer\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4633 | \n\ |
| 4634 | Return a port number from a service name and protocol name.\n\ |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4635 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 4636 | otherwise any protocol will match."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4637 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4638 | |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4639 | /* Python interface to getservbyport(port). |
| 4640 | This only returns the service name, since the other info is already |
| 4641 | known or not useful (like the list of aliases). */ |
| 4642 | |
| 4643 | /*ARGSUSED*/ |
| 4644 | static PyObject * |
| 4645 | socket_getservbyport(PyObject *self, PyObject *args) |
| 4646 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4647 | int port; |
| 4648 | char *proto=NULL; |
| 4649 | struct servent *sp; |
| 4650 | if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto)) |
| 4651 | return NULL; |
| 4652 | if (port < 0 || port > 0xffff) { |
| 4653 | PyErr_SetString( |
| 4654 | PyExc_OverflowError, |
| 4655 | "getservbyport: port must be 0-65535."); |
| 4656 | return NULL; |
| 4657 | } |
| 4658 | Py_BEGIN_ALLOW_THREADS |
| 4659 | sp = getservbyport(htons((short)port), proto); |
| 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, "port/proto not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4663 | return NULL; |
| 4664 | } |
| 4665 | return PyUnicode_FromString(sp->s_name); |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4666 | } |
| 4667 | |
| 4668 | PyDoc_STRVAR(getservbyport_doc, |
| 4669 | "getservbyport(port[, protocolname]) -> string\n\ |
| 4670 | \n\ |
| 4671 | Return the service name from a port number and protocol name.\n\ |
| 4672 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 4673 | otherwise any protocol will match."); |
| 4674 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4675 | /* Python interface to getprotobyname(name). |
| 4676 | This only returns the protocol number, since the other info is |
| 4677 | already known or not useful (like the list of aliases). */ |
| 4678 | |
| 4679 | /*ARGSUSED*/ |
| 4680 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4681 | socket_getprotobyname(PyObject *self, PyObject *args) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4682 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4683 | char *name; |
| 4684 | struct protoent *sp; |
| 4685 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) |
| 4686 | return NULL; |
| 4687 | Py_BEGIN_ALLOW_THREADS |
| 4688 | sp = getprotobyname(name); |
| 4689 | Py_END_ALLOW_THREADS |
| 4690 | if (sp == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4691 | PyErr_SetString(PyExc_OSError, "protocol not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4692 | return NULL; |
| 4693 | } |
| 4694 | return PyLong_FromLong((long) sp->p_proto); |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4695 | } |
| 4696 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4697 | PyDoc_STRVAR(getprotobyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4698 | "getprotobyname(name) -> integer\n\ |
| 4699 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4700 | Return the protocol number for the named protocol. (Rarely used.)"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4701 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4702 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4703 | #ifndef NO_DUP |
| 4704 | /* dup() function for socket fds */ |
| 4705 | |
| 4706 | static PyObject * |
| 4707 | socket_dup(PyObject *self, PyObject *fdobj) |
| 4708 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4709 | SOCKET_T fd, newfd; |
| 4710 | PyObject *newfdobj; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4711 | #ifdef MS_WINDOWS |
| 4712 | WSAPROTOCOL_INFO info; |
| 4713 | #endif |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4714 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4715 | fd = PyLong_AsSocket_t(fdobj); |
| 4716 | if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
| 4717 | return NULL; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4718 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4719 | #ifdef MS_WINDOWS |
| 4720 | if (WSADuplicateSocket(fd, GetCurrentProcessId(), &info)) |
| 4721 | return set_error(); |
| 4722 | |
| 4723 | newfd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, |
| 4724 | FROM_PROTOCOL_INFO, |
| 4725 | &info, 0, WSA_FLAG_OVERLAPPED); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4726 | if (newfd == INVALID_SOCKET) |
| 4727 | return set_error(); |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4728 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4729 | if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) { |
| 4730 | closesocket(newfd); |
| 4731 | PyErr_SetFromWindowsErr(0); |
| 4732 | return NULL; |
| 4733 | } |
| 4734 | #else |
| 4735 | /* On UNIX, dup can be used to duplicate the file descriptor of a socket */ |
| 4736 | newfd = _Py_dup(fd); |
| 4737 | if (newfd == INVALID_SOCKET) |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 4738 | return NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4739 | #endif |
| 4740 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4741 | newfdobj = PyLong_FromSocket_t(newfd); |
| 4742 | if (newfdobj == NULL) |
| 4743 | SOCKETCLOSE(newfd); |
| 4744 | return newfdobj; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4745 | } |
| 4746 | |
| 4747 | PyDoc_STRVAR(dup_doc, |
| 4748 | "dup(integer) -> integer\n\ |
| 4749 | \n\ |
| 4750 | Duplicate an integer socket file descriptor. This is like os.dup(), but for\n\ |
| 4751 | sockets; on some platforms os.dup() won't work for socket file descriptors."); |
| 4752 | #endif |
| 4753 | |
| 4754 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4755 | #ifdef HAVE_SOCKETPAIR |
| 4756 | /* Create a pair of sockets using the socketpair() function. |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 4757 | Arguments as for socket() except the default family is AF_UNIX if |
Dave Cole | e8bbfe4 | 2004-08-26 00:51:16 +0000 | [diff] [blame] | 4758 | defined on the platform; otherwise, the default is AF_INET. */ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4759 | |
| 4760 | /*ARGSUSED*/ |
| 4761 | static PyObject * |
| 4762 | socket_socketpair(PyObject *self, PyObject *args) |
| 4763 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4764 | PySocketSockObject *s0 = NULL, *s1 = NULL; |
| 4765 | SOCKET_T sv[2]; |
| 4766 | int family, type = SOCK_STREAM, proto = 0; |
| 4767 | PyObject *res = NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4768 | #ifdef SOCK_CLOEXEC |
| 4769 | int *atomic_flag_works = &sock_cloexec_works; |
| 4770 | #else |
| 4771 | int *atomic_flag_works = NULL; |
| 4772 | #endif |
| 4773 | int ret; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4774 | |
| 4775 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4776 | family = AF_UNIX; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4777 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4778 | family = AF_INET; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4779 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4780 | if (!PyArg_ParseTuple(args, "|iii:socketpair", |
| 4781 | &family, &type, &proto)) |
| 4782 | return NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4783 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4784 | /* Create a pair of socket fds */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4785 | Py_BEGIN_ALLOW_THREADS |
| 4786 | #ifdef SOCK_CLOEXEC |
| 4787 | if (sock_cloexec_works != 0) { |
| 4788 | ret = socketpair(family, type | SOCK_CLOEXEC, proto, sv); |
| 4789 | if (sock_cloexec_works == -1) { |
| 4790 | if (ret >= 0) { |
| 4791 | sock_cloexec_works = 1; |
| 4792 | } |
| 4793 | else if (errno == EINVAL) { |
| 4794 | /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */ |
| 4795 | sock_cloexec_works = 0; |
| 4796 | ret = socketpair(family, type, proto, sv); |
| 4797 | } |
| 4798 | } |
| 4799 | } |
| 4800 | else |
| 4801 | #endif |
| 4802 | { |
| 4803 | ret = socketpair(family, type, proto, sv); |
| 4804 | } |
| 4805 | Py_END_ALLOW_THREADS |
| 4806 | |
| 4807 | if (ret < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4808 | return set_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4809 | |
| 4810 | if (_Py_set_inheritable(sv[0], 0, atomic_flag_works) < 0) |
| 4811 | goto finally; |
| 4812 | if (_Py_set_inheritable(sv[1], 0, atomic_flag_works) < 0) |
| 4813 | goto finally; |
| 4814 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4815 | s0 = new_sockobject(sv[0], family, type, proto); |
| 4816 | if (s0 == NULL) |
| 4817 | goto finally; |
| 4818 | s1 = new_sockobject(sv[1], family, type, proto); |
| 4819 | if (s1 == NULL) |
| 4820 | goto finally; |
| 4821 | res = PyTuple_Pack(2, s0, s1); |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4822 | |
| 4823 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4824 | if (res == NULL) { |
| 4825 | if (s0 == NULL) |
| 4826 | SOCKETCLOSE(sv[0]); |
| 4827 | if (s1 == NULL) |
| 4828 | SOCKETCLOSE(sv[1]); |
| 4829 | } |
| 4830 | Py_XDECREF(s0); |
| 4831 | Py_XDECREF(s1); |
| 4832 | return res; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4833 | } |
| 4834 | |
| 4835 | PyDoc_STRVAR(socketpair_doc, |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4836 | "socketpair([family[, type [, proto]]]) -> (socket object, socket object)\n\ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4837 | \n\ |
| 4838 | Create a pair of socket objects from the sockets returned by the platform\n\ |
| 4839 | socketpair() function.\n\ |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 4840 | 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] | 4841 | 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] | 4842 | |
| 4843 | #endif /* HAVE_SOCKETPAIR */ |
| 4844 | |
| 4845 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4846 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4847 | socket_ntohs(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4848 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4849 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4850 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4851 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { |
| 4852 | return NULL; |
| 4853 | } |
| 4854 | if (x1 < 0) { |
| 4855 | PyErr_SetString(PyExc_OverflowError, |
| 4856 | "can't convert negative number to unsigned long"); |
| 4857 | return NULL; |
| 4858 | } |
| 4859 | x2 = (unsigned int)ntohs((unsigned short)x1); |
| 4860 | return PyLong_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4861 | } |
| 4862 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4863 | PyDoc_STRVAR(ntohs_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4864 | "ntohs(integer) -> integer\n\ |
| 4865 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4866 | Convert a 16-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4867 | |
| 4868 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4869 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4870 | socket_ntohl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4871 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4872 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4873 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4874 | if (PyLong_Check(arg)) { |
| 4875 | x = PyLong_AsUnsignedLong(arg); |
| 4876 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 4877 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4878 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4879 | { |
| 4880 | unsigned long y; |
| 4881 | /* only want the trailing 32 bits */ |
| 4882 | y = x & 0xFFFFFFFFUL; |
| 4883 | if (y ^ x) |
| 4884 | return PyErr_Format(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4885 | "int larger than 32 bits"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4886 | x = y; |
| 4887 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4888 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4889 | } |
| 4890 | else |
| 4891 | return PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4892 | "expected int, %s found", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4893 | Py_TYPE(arg)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4894 | return PyLong_FromUnsignedLong(ntohl(x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4895 | } |
| 4896 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4897 | PyDoc_STRVAR(ntohl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4898 | "ntohl(integer) -> integer\n\ |
| 4899 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4900 | Convert a 32-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4901 | |
| 4902 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4903 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4904 | socket_htons(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4905 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4906 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4907 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4908 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) { |
| 4909 | return NULL; |
| 4910 | } |
| 4911 | if (x1 < 0) { |
| 4912 | PyErr_SetString(PyExc_OverflowError, |
| 4913 | "can't convert negative number to unsigned long"); |
| 4914 | return NULL; |
| 4915 | } |
| 4916 | x2 = (unsigned int)htons((unsigned short)x1); |
| 4917 | return PyLong_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4918 | } |
| 4919 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4920 | PyDoc_STRVAR(htons_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4921 | "htons(integer) -> integer\n\ |
| 4922 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4923 | Convert a 16-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4924 | |
| 4925 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4926 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4927 | socket_htonl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4928 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4929 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4930 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4931 | if (PyLong_Check(arg)) { |
| 4932 | x = PyLong_AsUnsignedLong(arg); |
| 4933 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 4934 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4935 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4936 | { |
| 4937 | unsigned long y; |
| 4938 | /* only want the trailing 32 bits */ |
| 4939 | y = x & 0xFFFFFFFFUL; |
| 4940 | if (y ^ x) |
| 4941 | return PyErr_Format(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4942 | "int larger than 32 bits"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4943 | x = y; |
| 4944 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4945 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4946 | } |
| 4947 | else |
| 4948 | return PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4949 | "expected int, %s found", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4950 | Py_TYPE(arg)->tp_name); |
| 4951 | return PyLong_FromUnsignedLong(htonl((unsigned long)x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4954 | PyDoc_STRVAR(htonl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4955 | "htonl(integer) -> integer\n\ |
| 4956 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4957 | Convert a 32-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4958 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4959 | /* socket.inet_aton() and socket.inet_ntoa() functions. */ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4960 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4961 | PyDoc_STRVAR(inet_aton_doc, |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 4962 | "inet_aton(string) -> bytes giving packed 32-bit IP representation\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4963 | \n\ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 4964 | 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] | 4965 | binary format used in low-level network functions."); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4966 | |
| 4967 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4968 | socket_inet_aton(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4969 | { |
Neal Norwitz | 88f115b | 2003-02-13 02:15:42 +0000 | [diff] [blame] | 4970 | #ifdef HAVE_INET_ATON |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4971 | struct in_addr buf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4972 | #endif |
| 4973 | |
| 4974 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 4975 | #if (SIZEOF_INT != 4) |
| 4976 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 4977 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4978 | /* Have to use inet_addr() instead */ |
| 4979 | unsigned int packed_addr; |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 4980 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4981 | char *ip_addr; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 4982 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4983 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) |
| 4984 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 4985 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 4986 | |
| 4987 | #ifdef HAVE_INET_ATON |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4988 | |
| 4989 | #ifdef USE_INET_ATON_WEAKLINK |
| 4990 | if (inet_aton != NULL) { |
| 4991 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4992 | if (inet_aton(ip_addr, &buf)) |
| 4993 | return PyBytes_FromStringAndSize((char *)(&buf), |
| 4994 | sizeof(buf)); |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 4995 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4996 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4997 | "illegal IP address string passed to inet_aton"); |
| 4998 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 4999 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5000 | #ifdef USE_INET_ATON_WEAKLINK |
| 5001 | } else { |
| 5002 | #endif |
| 5003 | |
| 5004 | #endif |
| 5005 | |
| 5006 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
| 5007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5008 | /* special-case this address as inet_addr might return INADDR_NONE |
| 5009 | * for this */ |
| 5010 | if (strcmp(ip_addr, "255.255.255.255") == 0) { |
Charles-François Natali | 24aa041 | 2013-08-31 14:48:25 +0200 | [diff] [blame] | 5011 | packed_addr = INADDR_BROADCAST; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5012 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5013 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5014 | packed_addr = inet_addr(ip_addr); |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 5015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5016 | if (packed_addr == INADDR_NONE) { /* invalid address */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5017 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5018 | "illegal IP address string passed to inet_aton"); |
| 5019 | return NULL; |
| 5020 | } |
| 5021 | } |
| 5022 | return PyBytes_FromStringAndSize((char *) &packed_addr, |
| 5023 | sizeof(packed_addr)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5024 | |
| 5025 | #ifdef USE_INET_ATON_WEAKLINK |
| 5026 | } |
| 5027 | #endif |
| 5028 | |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 5029 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5030 | } |
| 5031 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5032 | PyDoc_STRVAR(inet_ntoa_doc, |
Fred Drake | e066134 | 2000-03-07 14:05:16 +0000 | [diff] [blame] | 5033 | "inet_ntoa(packed_ip) -> ip_address_string\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5034 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5035 | 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] | 5036 | |
| 5037 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5038 | socket_inet_ntoa(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5039 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5040 | char *packed_str; |
| 5041 | int addr_len; |
| 5042 | struct in_addr packed_addr; |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5043 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5044 | if (!PyArg_ParseTuple(args, "y#:inet_ntoa", &packed_str, &addr_len)) { |
| 5045 | return NULL; |
| 5046 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 5047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5048 | if (addr_len != sizeof(packed_addr)) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5049 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5050 | "packed IP wrong length for inet_ntoa"); |
| 5051 | return NULL; |
| 5052 | } |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5053 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5054 | memcpy(&packed_addr, packed_str, addr_len); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5055 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5056 | return PyUnicode_FromString(inet_ntoa(packed_addr)); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 5057 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5058 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5059 | #if defined(HAVE_INET_PTON) || defined(MS_WINDOWS) |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5060 | |
| 5061 | PyDoc_STRVAR(inet_pton_doc, |
| 5062 | "inet_pton(af, ip) -> packed IP address string\n\ |
| 5063 | \n\ |
| 5064 | Convert an IP address from string format to a packed string suitable\n\ |
| 5065 | for use with low-level network functions."); |
| 5066 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5067 | #endif |
| 5068 | |
| 5069 | #ifdef HAVE_INET_PTON |
| 5070 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5071 | static PyObject * |
| 5072 | socket_inet_pton(PyObject *self, PyObject *args) |
| 5073 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5074 | int af; |
| 5075 | char* ip; |
| 5076 | int retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5077 | #ifdef ENABLE_IPV6 |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 5078 | 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] | 5079 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5080 | char packed[sizeof(struct in_addr)]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5081 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5082 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 5083 | return NULL; |
| 5084 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5085 | |
Martin v. Löwis | 04697e8 | 2004-06-02 12:35:29 +0000 | [diff] [blame] | 5086 | #if !defined(ENABLE_IPV6) && defined(AF_INET6) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5087 | if(af == AF_INET6) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5088 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5089 | "can't use AF_INET6, IPv6 is disabled"); |
| 5090 | return NULL; |
| 5091 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5092 | #endif |
Martin v. Löwis | 1064909 | 2003-08-05 06:25:06 +0000 | [diff] [blame] | 5093 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5094 | retval = inet_pton(af, ip, packed); |
| 5095 | if (retval < 0) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5096 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5097 | return NULL; |
| 5098 | } else if (retval == 0) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5099 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5100 | "illegal IP address string passed to inet_pton"); |
| 5101 | return NULL; |
| 5102 | } else if (af == AF_INET) { |
| 5103 | return PyBytes_FromStringAndSize(packed, |
| 5104 | sizeof(struct in_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5105 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5106 | } else if (af == AF_INET6) { |
| 5107 | return PyBytes_FromStringAndSize(packed, |
| 5108 | sizeof(struct in6_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5109 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5110 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5111 | PyErr_SetString(PyExc_OSError, "unknown address family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5112 | return NULL; |
| 5113 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5114 | } |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5115 | #elif defined(MS_WINDOWS) |
| 5116 | |
| 5117 | static PyObject * |
| 5118 | socket_inet_pton(PyObject *self, PyObject *args) |
| 5119 | { |
| 5120 | int af; |
| 5121 | char* ip; |
| 5122 | struct sockaddr_in6 addr; |
| 5123 | INT ret, size; |
| 5124 | |
| 5125 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 5126 | return NULL; |
| 5127 | } |
| 5128 | |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5129 | size = sizeof(addr); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5130 | ret = WSAStringToAddressA(ip, af, NULL, (LPSOCKADDR)&addr, &size); |
| 5131 | |
| 5132 | if (ret) { |
| 5133 | PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError()); |
| 5134 | return NULL; |
| 5135 | } else if(af == AF_INET) { |
| 5136 | struct sockaddr_in *addr4 = (struct sockaddr_in*)&addr; |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5137 | return PyBytes_FromStringAndSize((const char *)&(addr4->sin_addr), |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5138 | sizeof(addr4->sin_addr)); |
| 5139 | } else if (af == AF_INET6) { |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5140 | return PyBytes_FromStringAndSize((const char *)&(addr.sin6_addr), |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5141 | sizeof(addr.sin6_addr)); |
| 5142 | } else { |
| 5143 | PyErr_SetString(PyExc_OSError, "unknown address family"); |
| 5144 | return NULL; |
| 5145 | } |
| 5146 | } |
| 5147 | |
| 5148 | #endif |
| 5149 | |
| 5150 | #if defined(HAVE_INET_PTON) || defined(MS_WINDOWS) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5151 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5152 | PyDoc_STRVAR(inet_ntop_doc, |
| 5153 | "inet_ntop(af, packed_ip) -> string formatted IP address\n\ |
| 5154 | \n\ |
| 5155 | Convert a packed IP address of the given family to string format."); |
| 5156 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5157 | #endif |
| 5158 | |
| 5159 | |
| 5160 | #ifdef HAVE_INET_PTON |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5161 | static PyObject * |
| 5162 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 5163 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5164 | int af; |
| 5165 | char* packed; |
| 5166 | int len; |
| 5167 | const char* retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5168 | #ifdef ENABLE_IPV6 |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 5169 | char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5170 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5171 | char ip[INET_ADDRSTRLEN + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5172 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5174 | /* Guarantee NUL-termination for PyUnicode_FromString() below */ |
| 5175 | memset((void *) &ip[0], '\0', sizeof(ip)); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5177 | if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) { |
| 5178 | return NULL; |
| 5179 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5180 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5181 | if (af == AF_INET) { |
| 5182 | if (len != sizeof(struct in_addr)) { |
| 5183 | PyErr_SetString(PyExc_ValueError, |
| 5184 | "invalid length of packed IP address string"); |
| 5185 | return NULL; |
| 5186 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5187 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5188 | } else if (af == AF_INET6) { |
| 5189 | if (len != sizeof(struct in6_addr)) { |
| 5190 | PyErr_SetString(PyExc_ValueError, |
| 5191 | "invalid length of packed IP address string"); |
| 5192 | return NULL; |
| 5193 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5194 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5195 | } else { |
| 5196 | PyErr_Format(PyExc_ValueError, |
| 5197 | "unknown address family %d", af); |
| 5198 | return NULL; |
| 5199 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5201 | retval = inet_ntop(af, packed, ip, sizeof(ip)); |
| 5202 | if (!retval) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5203 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5204 | return NULL; |
| 5205 | } else { |
| 5206 | return PyUnicode_FromString(retval); |
| 5207 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5209 | /* NOTREACHED */ |
| 5210 | PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop"); |
| 5211 | return NULL; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5212 | } |
| 5213 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5214 | #elif defined(MS_WINDOWS) |
| 5215 | |
| 5216 | static PyObject * |
| 5217 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 5218 | { |
| 5219 | int af; |
| 5220 | char* packed; |
| 5221 | int len; |
| 5222 | struct sockaddr_in6 addr; |
| 5223 | DWORD addrlen, ret, retlen; |
Jason R. Coombs | 8ec784c | 2013-11-10 13:43:22 -0500 | [diff] [blame] | 5224 | #ifdef ENABLE_IPV6 |
| 5225 | char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
| 5226 | #else |
| 5227 | char ip[INET_ADDRSTRLEN + 1]; |
| 5228 | #endif |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5229 | |
| 5230 | /* Guarantee NUL-termination for PyUnicode_FromString() below */ |
| 5231 | memset((void *) &ip[0], '\0', sizeof(ip)); |
| 5232 | |
| 5233 | if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) { |
| 5234 | return NULL; |
| 5235 | } |
| 5236 | |
| 5237 | if (af == AF_INET) { |
| 5238 | struct sockaddr_in * addr4 = (struct sockaddr_in *)&addr; |
| 5239 | |
| 5240 | if (len != sizeof(struct in_addr)) { |
| 5241 | PyErr_SetString(PyExc_ValueError, |
| 5242 | "invalid length of packed IP address string"); |
| 5243 | return NULL; |
| 5244 | } |
| 5245 | memset(addr4, 0, sizeof(struct sockaddr_in)); |
| 5246 | addr4->sin_family = AF_INET; |
| 5247 | memcpy(&(addr4->sin_addr), packed, sizeof(addr4->sin_addr)); |
| 5248 | addrlen = sizeof(struct sockaddr_in); |
| 5249 | } else if (af == AF_INET6) { |
| 5250 | if (len != sizeof(struct in6_addr)) { |
| 5251 | PyErr_SetString(PyExc_ValueError, |
| 5252 | "invalid length of packed IP address string"); |
| 5253 | return NULL; |
| 5254 | } |
| 5255 | |
| 5256 | memset(&addr, 0, sizeof(addr)); |
| 5257 | addr.sin6_family = AF_INET6; |
| 5258 | memcpy(&(addr.sin6_addr), packed, sizeof(addr.sin6_addr)); |
| 5259 | addrlen = sizeof(addr); |
| 5260 | } else { |
| 5261 | PyErr_Format(PyExc_ValueError, |
| 5262 | "unknown address family %d", af); |
| 5263 | return NULL; |
| 5264 | } |
| 5265 | |
| 5266 | retlen = sizeof(ip); |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5267 | ret = WSAAddressToStringA((struct sockaddr*)&addr, addrlen, NULL, |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5268 | ip, &retlen); |
| 5269 | |
| 5270 | if (ret) { |
| 5271 | PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError()); |
| 5272 | return NULL; |
| 5273 | } else { |
| 5274 | return PyUnicode_FromString(ip); |
| 5275 | } |
| 5276 | } |
| 5277 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5278 | #endif /* HAVE_INET_PTON */ |
| 5279 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5280 | /* Python interface to getaddrinfo(host, port). */ |
| 5281 | |
| 5282 | /*ARGSUSED*/ |
| 5283 | static PyObject * |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5284 | socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5285 | { |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 5286 | static char* kwnames[] = {"host", "port", "family", "type", "proto", |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5287 | "flags", 0}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5288 | struct addrinfo hints, *res; |
| 5289 | struct addrinfo *res0 = NULL; |
| 5290 | PyObject *hobj = NULL; |
| 5291 | PyObject *pobj = (PyObject *)NULL; |
| 5292 | char pbuf[30]; |
| 5293 | char *hptr, *pptr; |
| 5294 | int family, socktype, protocol, flags; |
| 5295 | int error; |
| 5296 | PyObject *all = (PyObject *)NULL; |
| 5297 | PyObject *idna = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5298 | |
Georg Brandl | 6083a4b | 2013-10-14 06:51:46 +0200 | [diff] [blame] | 5299 | socktype = protocol = flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5300 | family = AF_UNSPEC; |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 5301 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iiii:getaddrinfo", |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5302 | kwnames, &hobj, &pobj, &family, &socktype, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5303 | &protocol, &flags)) { |
| 5304 | return NULL; |
| 5305 | } |
| 5306 | if (hobj == Py_None) { |
| 5307 | hptr = NULL; |
| 5308 | } else if (PyUnicode_Check(hobj)) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5309 | _Py_IDENTIFIER(encode); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 5310 | |
| 5311 | idna = _PyObject_CallMethodId(hobj, &PyId_encode, "s", "idna"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5312 | if (!idna) |
| 5313 | return NULL; |
| 5314 | assert(PyBytes_Check(idna)); |
| 5315 | hptr = PyBytes_AS_STRING(idna); |
| 5316 | } else if (PyBytes_Check(hobj)) { |
| 5317 | hptr = PyBytes_AsString(hobj); |
| 5318 | } else { |
| 5319 | PyErr_SetString(PyExc_TypeError, |
| 5320 | "getaddrinfo() argument 1 must be string or None"); |
| 5321 | return NULL; |
| 5322 | } |
| 5323 | if (PyLong_CheckExact(pobj)) { |
| 5324 | long value = PyLong_AsLong(pobj); |
| 5325 | if (value == -1 && PyErr_Occurred()) |
| 5326 | goto err; |
| 5327 | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value); |
| 5328 | pptr = pbuf; |
| 5329 | } else if (PyUnicode_Check(pobj)) { |
| 5330 | pptr = _PyUnicode_AsString(pobj); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 5331 | if (pptr == NULL) |
| 5332 | goto err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5333 | } else if (PyBytes_Check(pobj)) { |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 5334 | pptr = PyBytes_AS_STRING(pobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5335 | } else if (pobj == Py_None) { |
| 5336 | pptr = (char *)NULL; |
| 5337 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5338 | PyErr_SetString(PyExc_OSError, "Int or String expected"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5339 | goto err; |
| 5340 | } |
Ronald Oussoren | a822d366 | 2013-06-10 10:36:28 +0200 | [diff] [blame] | 5341 | #if defined(__APPLE__) && defined(AI_NUMERICSERV) |
Ronald Oussoren | 27a4ac5 | 2013-05-24 13:47:37 +0200 | [diff] [blame] | 5342 | if ((flags & AI_NUMERICSERV) && (pptr == NULL || (pptr[0] == '0' && pptr[1] == 0))) { |
| 5343 | /* On OSX upto at least OSX 10.8 getaddrinfo crashes |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 5344 | * if AI_NUMERICSERV is set and the servname is NULL or "0". |
| 5345 | * This workaround avoids a segfault in libsystem. |
| 5346 | */ |
Ronald Oussoren | 27a4ac5 | 2013-05-24 13:47:37 +0200 | [diff] [blame] | 5347 | pptr = "00"; |
| 5348 | } |
| 5349 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5350 | memset(&hints, 0, sizeof(hints)); |
| 5351 | hints.ai_family = family; |
| 5352 | hints.ai_socktype = socktype; |
| 5353 | hints.ai_protocol = protocol; |
| 5354 | hints.ai_flags = flags; |
| 5355 | Py_BEGIN_ALLOW_THREADS |
| 5356 | ACQUIRE_GETADDRINFO_LOCK |
| 5357 | error = getaddrinfo(hptr, pptr, &hints, &res0); |
| 5358 | Py_END_ALLOW_THREADS |
| 5359 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 5360 | if (error) { |
| 5361 | set_gaierror(error); |
| 5362 | goto err; |
| 5363 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5364 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5365 | if ((all = PyList_New(0)) == NULL) |
| 5366 | goto err; |
| 5367 | for (res = res0; res; res = res->ai_next) { |
| 5368 | PyObject *single; |
| 5369 | PyObject *addr = |
| 5370 | makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol); |
| 5371 | if (addr == NULL) |
| 5372 | goto err; |
| 5373 | single = Py_BuildValue("iiisO", res->ai_family, |
| 5374 | res->ai_socktype, res->ai_protocol, |
| 5375 | res->ai_canonname ? res->ai_canonname : "", |
| 5376 | addr); |
| 5377 | Py_DECREF(addr); |
| 5378 | if (single == NULL) |
| 5379 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5381 | if (PyList_Append(all, single)) |
| 5382 | goto err; |
| 5383 | Py_XDECREF(single); |
| 5384 | } |
| 5385 | Py_XDECREF(idna); |
| 5386 | if (res0) |
| 5387 | freeaddrinfo(res0); |
| 5388 | return all; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5389 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5390 | Py_XDECREF(all); |
| 5391 | Py_XDECREF(idna); |
| 5392 | if (res0) |
| 5393 | freeaddrinfo(res0); |
| 5394 | return (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5395 | } |
| 5396 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5397 | PyDoc_STRVAR(getaddrinfo_doc, |
Berker Peksag | a6ec5ee | 2014-06-30 11:28:40 +0300 | [diff] [blame] | 5398 | "getaddrinfo(host, port [, family, type, proto, flags])\n\ |
| 5399 | -> list of (family, type, proto, canonname, sockaddr)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5400 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5401 | Resolve host and port into addrinfo struct."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5402 | |
| 5403 | /* Python interface to getnameinfo(sa, flags). */ |
| 5404 | |
| 5405 | /*ARGSUSED*/ |
| 5406 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5407 | socket_getnameinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5408 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5409 | PyObject *sa = (PyObject *)NULL; |
| 5410 | int flags; |
| 5411 | char *hostp; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5412 | int port; |
| 5413 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5414 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; |
| 5415 | struct addrinfo hints, *res = NULL; |
| 5416 | int error; |
| 5417 | PyObject *ret = (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5418 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5419 | flags = flowinfo = scope_id = 0; |
| 5420 | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) |
| 5421 | return NULL; |
| 5422 | if (!PyTuple_Check(sa)) { |
| 5423 | PyErr_SetString(PyExc_TypeError, |
| 5424 | "getnameinfo() argument 1 must be a tuple"); |
| 5425 | return NULL; |
| 5426 | } |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5427 | if (!PyArg_ParseTuple(sa, "si|II", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5428 | &hostp, &port, &flowinfo, &scope_id)) |
| 5429 | return NULL; |
Charles-François Natali | 9b0c006 | 2012-06-23 10:17:05 +0200 | [diff] [blame] | 5430 | if (flowinfo > 0xfffff) { |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5431 | PyErr_SetString(PyExc_OverflowError, |
| 5432 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 5433 | return NULL; |
| 5434 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5435 | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); |
| 5436 | memset(&hints, 0, sizeof(hints)); |
| 5437 | hints.ai_family = AF_UNSPEC; |
| 5438 | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ |
Martin v. Löwis | 112c0f3 | 2010-08-25 07:38:15 +0000 | [diff] [blame] | 5439 | hints.ai_flags = AI_NUMERICHOST; /* don't do any name resolution */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5440 | Py_BEGIN_ALLOW_THREADS |
| 5441 | ACQUIRE_GETADDRINFO_LOCK |
| 5442 | error = getaddrinfo(hostp, pbuf, &hints, &res); |
| 5443 | Py_END_ALLOW_THREADS |
| 5444 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 5445 | if (error) { |
| 5446 | set_gaierror(error); |
| 5447 | goto fail; |
| 5448 | } |
| 5449 | if (res->ai_next) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5450 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5451 | "sockaddr resolved to multiple addresses"); |
| 5452 | goto fail; |
| 5453 | } |
| 5454 | switch (res->ai_family) { |
| 5455 | case AF_INET: |
| 5456 | { |
| 5457 | if (PyTuple_GET_SIZE(sa) != 2) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5458 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5459 | "IPv4 sockaddr must be 2 tuple"); |
| 5460 | goto fail; |
| 5461 | } |
| 5462 | break; |
| 5463 | } |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 5464 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5465 | case AF_INET6: |
| 5466 | { |
| 5467 | struct sockaddr_in6 *sin6; |
| 5468 | sin6 = (struct sockaddr_in6 *)res->ai_addr; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5469 | sin6->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5470 | sin6->sin6_scope_id = scope_id; |
| 5471 | break; |
| 5472 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5473 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5474 | } |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 5475 | error = getnameinfo(res->ai_addr, (socklen_t) res->ai_addrlen, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5476 | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); |
| 5477 | if (error) { |
| 5478 | set_gaierror(error); |
| 5479 | goto fail; |
| 5480 | } |
| 5481 | ret = Py_BuildValue("ss", hbuf, pbuf); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5482 | |
| 5483 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5484 | if (res) |
| 5485 | freeaddrinfo(res); |
| 5486 | return ret; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5487 | } |
| 5488 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5489 | PyDoc_STRVAR(getnameinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5490 | "getnameinfo(sockaddr, flags) --> (host, port)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5491 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5492 | Get host and port for a sockaddr."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5493 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5494 | |
| 5495 | /* Python API to getting and setting the default timeout value. */ |
| 5496 | |
| 5497 | static PyObject * |
| 5498 | socket_getdefaulttimeout(PyObject *self) |
| 5499 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5500 | if (defaulttimeout < 0.0) { |
| 5501 | Py_INCREF(Py_None); |
| 5502 | return Py_None; |
| 5503 | } |
| 5504 | else |
| 5505 | return PyFloat_FromDouble(defaulttimeout); |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5506 | } |
| 5507 | |
| 5508 | PyDoc_STRVAR(getdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5509 | "getdefaulttimeout() -> timeout\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5510 | \n\ |
Ezio Melotti | 388c945 | 2011-08-14 08:28:57 +0300 | [diff] [blame] | 5511 | 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] | 5512 | A value of None indicates that new socket objects have no timeout.\n\ |
| 5513 | When the socket module is first imported, the default is None."); |
| 5514 | |
| 5515 | static PyObject * |
| 5516 | socket_setdefaulttimeout(PyObject *self, PyObject *arg) |
| 5517 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5518 | double timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5520 | if (arg == Py_None) |
| 5521 | timeout = -1.0; |
| 5522 | else { |
| 5523 | timeout = PyFloat_AsDouble(arg); |
| 5524 | if (timeout < 0.0) { |
| 5525 | if (!PyErr_Occurred()) |
| 5526 | PyErr_SetString(PyExc_ValueError, |
| 5527 | "Timeout value out of range"); |
| 5528 | return NULL; |
| 5529 | } |
| 5530 | } |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5531 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5532 | defaulttimeout = timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5533 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5534 | Py_INCREF(Py_None); |
| 5535 | return Py_None; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5536 | } |
| 5537 | |
| 5538 | PyDoc_STRVAR(setdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5539 | "setdefaulttimeout(timeout)\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5540 | \n\ |
Ezio Melotti | 388c945 | 2011-08-14 08:28:57 +0300 | [diff] [blame] | 5541 | 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] | 5542 | A value of None indicates that new socket objects have no timeout.\n\ |
| 5543 | When the socket module is first imported, the default is None."); |
| 5544 | |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5545 | #ifdef HAVE_IF_NAMEINDEX |
| 5546 | /* Python API for getting interface indices and names */ |
| 5547 | |
| 5548 | static PyObject * |
| 5549 | socket_if_nameindex(PyObject *self, PyObject *arg) |
| 5550 | { |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5551 | PyObject *list; |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5552 | int i; |
| 5553 | struct if_nameindex *ni; |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 5554 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5555 | ni = if_nameindex(); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5556 | if (ni == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5557 | PyErr_SetFromErrno(PyExc_OSError); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5558 | return NULL; |
| 5559 | } |
| 5560 | |
| 5561 | list = PyList_New(0); |
| 5562 | if (list == NULL) { |
| 5563 | if_freenameindex(ni); |
| 5564 | return NULL; |
| 5565 | } |
| 5566 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5567 | for (i = 0; ni[i].if_index != 0 && i < INT_MAX; i++) { |
| 5568 | PyObject *ni_tuple = Py_BuildValue("IO&", |
| 5569 | ni[i].if_index, PyUnicode_DecodeFSDefault, ni[i].if_name); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5570 | |
| 5571 | if (ni_tuple == NULL || PyList_Append(list, ni_tuple) == -1) { |
| 5572 | Py_XDECREF(ni_tuple); |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5573 | Py_DECREF(list); |
| 5574 | if_freenameindex(ni); |
| 5575 | return NULL; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5576 | } |
| 5577 | Py_DECREF(ni_tuple); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5578 | } |
| 5579 | |
| 5580 | if_freenameindex(ni); |
| 5581 | return list; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5582 | } |
| 5583 | |
| 5584 | PyDoc_STRVAR(if_nameindex_doc, |
| 5585 | "if_nameindex()\n\ |
| 5586 | \n\ |
| 5587 | Returns a list of network interface information (index, name) tuples."); |
| 5588 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5589 | static PyObject * |
| 5590 | socket_if_nametoindex(PyObject *self, PyObject *args) |
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 | PyObject *oname; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5593 | unsigned long index; |
| 5594 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5595 | if (!PyArg_ParseTuple(args, "O&:if_nametoindex", |
| 5596 | PyUnicode_FSConverter, &oname)) |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5597 | return NULL; |
| 5598 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5599 | index = if_nametoindex(PyBytes_AS_STRING(oname)); |
| 5600 | Py_DECREF(oname); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5601 | if (index == 0) { |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5602 | /* if_nametoindex() doesn't set errno */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5603 | PyErr_SetString(PyExc_OSError, "no interface with this name"); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5604 | return NULL; |
| 5605 | } |
| 5606 | |
| 5607 | return PyLong_FromUnsignedLong(index); |
| 5608 | } |
| 5609 | |
| 5610 | PyDoc_STRVAR(if_nametoindex_doc, |
| 5611 | "if_nametoindex(if_name)\n\ |
| 5612 | \n\ |
| 5613 | Returns the interface index corresponding to the interface name if_name."); |
| 5614 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5615 | static PyObject * |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5616 | socket_if_indextoname(PyObject *self, PyObject *arg) |
| 5617 | { |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5618 | unsigned long index; |
| 5619 | char name[IF_NAMESIZE + 1]; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5620 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5621 | index = PyLong_AsUnsignedLong(arg); |
| 5622 | if (index == (unsigned long) -1) |
| 5623 | return NULL; |
| 5624 | |
| 5625 | if (if_indextoname(index, name) == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5626 | PyErr_SetFromErrno(PyExc_OSError); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5627 | return NULL; |
| 5628 | } |
| 5629 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5630 | return PyUnicode_DecodeFSDefault(name); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5631 | } |
| 5632 | |
| 5633 | PyDoc_STRVAR(if_indextoname_doc, |
| 5634 | "if_indextoname(if_index)\n\ |
| 5635 | \n\ |
| 5636 | Returns the interface name corresponding to the interface index if_index."); |
| 5637 | |
| 5638 | #endif /* HAVE_IF_NAMEINDEX */ |
| 5639 | |
| 5640 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 5641 | #ifdef CMSG_LEN |
| 5642 | /* Python interface to CMSG_LEN(length). */ |
| 5643 | |
| 5644 | static PyObject * |
| 5645 | socket_CMSG_LEN(PyObject *self, PyObject *args) |
| 5646 | { |
| 5647 | Py_ssize_t length; |
| 5648 | size_t result; |
| 5649 | |
| 5650 | if (!PyArg_ParseTuple(args, "n:CMSG_LEN", &length)) |
| 5651 | return NULL; |
| 5652 | if (length < 0 || !get_CMSG_LEN(length, &result)) { |
| 5653 | PyErr_Format(PyExc_OverflowError, "CMSG_LEN() argument out of range"); |
| 5654 | return NULL; |
| 5655 | } |
| 5656 | return PyLong_FromSize_t(result); |
| 5657 | } |
| 5658 | |
| 5659 | PyDoc_STRVAR(CMSG_LEN_doc, |
| 5660 | "CMSG_LEN(length) -> control message length\n\ |
| 5661 | \n\ |
| 5662 | Return the total length, without trailing padding, of an ancillary\n\ |
| 5663 | data item with associated data of the given length. This value can\n\ |
| 5664 | often be used as the buffer size for recvmsg() to receive a single\n\ |
| 5665 | item of ancillary data, but RFC 3542 requires portable applications to\n\ |
| 5666 | use CMSG_SPACE() and thus include space for padding, even when the\n\ |
| 5667 | item will be the last in the buffer. Raises OverflowError if length\n\ |
| 5668 | is outside the permissible range of values."); |
| 5669 | |
| 5670 | |
| 5671 | #ifdef CMSG_SPACE |
| 5672 | /* Python interface to CMSG_SPACE(length). */ |
| 5673 | |
| 5674 | static PyObject * |
| 5675 | socket_CMSG_SPACE(PyObject *self, PyObject *args) |
| 5676 | { |
| 5677 | Py_ssize_t length; |
| 5678 | size_t result; |
| 5679 | |
| 5680 | if (!PyArg_ParseTuple(args, "n:CMSG_SPACE", &length)) |
| 5681 | return NULL; |
| 5682 | if (length < 0 || !get_CMSG_SPACE(length, &result)) { |
| 5683 | PyErr_SetString(PyExc_OverflowError, |
| 5684 | "CMSG_SPACE() argument out of range"); |
| 5685 | return NULL; |
| 5686 | } |
| 5687 | return PyLong_FromSize_t(result); |
| 5688 | } |
| 5689 | |
| 5690 | PyDoc_STRVAR(CMSG_SPACE_doc, |
| 5691 | "CMSG_SPACE(length) -> buffer size\n\ |
| 5692 | \n\ |
| 5693 | Return the buffer size needed for recvmsg() to receive an ancillary\n\ |
| 5694 | data item with associated data of the given length, along with any\n\ |
| 5695 | trailing padding. The buffer space needed to receive multiple items\n\ |
| 5696 | is the sum of the CMSG_SPACE() values for their associated data\n\ |
| 5697 | lengths. Raises OverflowError if length is outside the permissible\n\ |
| 5698 | range of values."); |
| 5699 | #endif /* CMSG_SPACE */ |
| 5700 | #endif /* CMSG_LEN */ |
| 5701 | |
| 5702 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 5703 | /* List of functions exported by this module. */ |
| 5704 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5705 | static PyMethodDef socket_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5706 | {"gethostbyname", socket_gethostbyname, |
| 5707 | METH_VARARGS, gethostbyname_doc}, |
| 5708 | {"gethostbyname_ex", socket_gethostbyname_ex, |
| 5709 | METH_VARARGS, ghbn_ex_doc}, |
| 5710 | {"gethostbyaddr", socket_gethostbyaddr, |
| 5711 | METH_VARARGS, gethostbyaddr_doc}, |
| 5712 | {"gethostname", socket_gethostname, |
| 5713 | METH_NOARGS, gethostname_doc}, |
Antoine Pitrou | 061cfb5 | 2011-02-28 22:25:22 +0000 | [diff] [blame] | 5714 | #ifdef HAVE_SETHOSTNAME |
| 5715 | {"sethostname", socket_sethostname, |
| 5716 | METH_VARARGS, sethostname_doc}, |
| 5717 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5718 | {"getservbyname", socket_getservbyname, |
| 5719 | METH_VARARGS, getservbyname_doc}, |
| 5720 | {"getservbyport", socket_getservbyport, |
| 5721 | METH_VARARGS, getservbyport_doc}, |
| 5722 | {"getprotobyname", socket_getprotobyname, |
| 5723 | METH_VARARGS, getprotobyname_doc}, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 5724 | #ifndef NO_DUP |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5725 | {"dup", socket_dup, |
| 5726 | METH_O, dup_doc}, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 5727 | #endif |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 5728 | #ifdef HAVE_SOCKETPAIR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5729 | {"socketpair", socket_socketpair, |
| 5730 | METH_VARARGS, socketpair_doc}, |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 5731 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5732 | {"ntohs", socket_ntohs, |
| 5733 | METH_VARARGS, ntohs_doc}, |
| 5734 | {"ntohl", socket_ntohl, |
| 5735 | METH_O, ntohl_doc}, |
| 5736 | {"htons", socket_htons, |
| 5737 | METH_VARARGS, htons_doc}, |
| 5738 | {"htonl", socket_htonl, |
| 5739 | METH_O, htonl_doc}, |
| 5740 | {"inet_aton", socket_inet_aton, |
| 5741 | METH_VARARGS, inet_aton_doc}, |
| 5742 | {"inet_ntoa", socket_inet_ntoa, |
| 5743 | METH_VARARGS, inet_ntoa_doc}, |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5744 | #if defined(HAVE_INET_PTON) || defined(MS_WINDOWS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5745 | {"inet_pton", socket_inet_pton, |
| 5746 | METH_VARARGS, inet_pton_doc}, |
| 5747 | {"inet_ntop", socket_inet_ntop, |
| 5748 | METH_VARARGS, inet_ntop_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5749 | #endif |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5750 | {"getaddrinfo", (PyCFunction)socket_getaddrinfo, |
| 5751 | METH_VARARGS | METH_KEYWORDS, getaddrinfo_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5752 | {"getnameinfo", socket_getnameinfo, |
| 5753 | METH_VARARGS, getnameinfo_doc}, |
| 5754 | {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout, |
| 5755 | METH_NOARGS, getdefaulttimeout_doc}, |
| 5756 | {"setdefaulttimeout", socket_setdefaulttimeout, |
| 5757 | METH_O, setdefaulttimeout_doc}, |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5758 | #ifdef HAVE_IF_NAMEINDEX |
| 5759 | {"if_nameindex", socket_if_nameindex, |
| 5760 | METH_NOARGS, if_nameindex_doc}, |
| 5761 | {"if_nametoindex", socket_if_nametoindex, |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5762 | METH_VARARGS, if_nametoindex_doc}, |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5763 | {"if_indextoname", socket_if_indextoname, |
| 5764 | METH_O, if_indextoname_doc}, |
| 5765 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 5766 | #ifdef CMSG_LEN |
| 5767 | {"CMSG_LEN", socket_CMSG_LEN, |
| 5768 | METH_VARARGS, CMSG_LEN_doc}, |
| 5769 | #ifdef CMSG_SPACE |
| 5770 | {"CMSG_SPACE", socket_CMSG_SPACE, |
| 5771 | METH_VARARGS, CMSG_SPACE_doc}, |
| 5772 | #endif |
| 5773 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5774 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5775 | }; |
| 5776 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 5777 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5778 | #ifdef MS_WINDOWS |
| 5779 | #define OS_INIT_DEFINED |
| 5780 | |
| 5781 | /* Additional initialization and cleanup for Windows */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5782 | |
| 5783 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5784 | os_cleanup(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5785 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5786 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5787 | } |
| 5788 | |
| 5789 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5790 | os_init(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5791 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5792 | WSADATA WSAData; |
| 5793 | int ret; |
| 5794 | ret = WSAStartup(0x0101, &WSAData); |
| 5795 | switch (ret) { |
| 5796 | case 0: /* No error */ |
| 5797 | Py_AtExit(os_cleanup); |
| 5798 | return 1; /* Success */ |
| 5799 | case WSASYSNOTREADY: |
| 5800 | PyErr_SetString(PyExc_ImportError, |
| 5801 | "WSAStartup failed: network not ready"); |
| 5802 | break; |
| 5803 | case WSAVERNOTSUPPORTED: |
| 5804 | case WSAEINVAL: |
| 5805 | PyErr_SetString( |
| 5806 | PyExc_ImportError, |
| 5807 | "WSAStartup failed: requested version not supported"); |
| 5808 | break; |
| 5809 | default: |
| 5810 | PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret); |
| 5811 | break; |
| 5812 | } |
| 5813 | return 0; /* Failure */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5814 | } |
| 5815 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 5816 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5817 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5818 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5819 | |
| 5820 | #ifndef OS_INIT_DEFINED |
| 5821 | static int |
| 5822 | os_init(void) |
| 5823 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5824 | return 1; /* Success */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5825 | } |
| 5826 | #endif |
| 5827 | |
| 5828 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5829 | /* C API table - always add new things to the end for binary |
| 5830 | compatibility. */ |
| 5831 | static |
| 5832 | PySocketModule_APIObject PySocketModuleAPI = |
| 5833 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5834 | &sock_type, |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 5835 | NULL, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5836 | NULL |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5837 | }; |
| 5838 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5839 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5840 | /* Initialize the _socket module. |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5841 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5842 | This module is actually called "_socket", and there's a wrapper |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 5843 | "socket.py" which implements some additional functionality. |
| 5844 | The import of "_socket" may fail with an ImportError exception if |
| 5845 | os-specific initialization fails. On Windows, this does WINSOCK |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 5846 | initialization. When WINSOCK is initialized successfully, a call to |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 5847 | WSACleanup() is scheduled to be made at exit time. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5848 | */ |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5849 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5850 | PyDoc_STRVAR(socket_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5851 | "Implementation module for socket operations.\n\ |
| 5852 | \n\ |
| 5853 | See the socket module for documentation."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5854 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5855 | static struct PyModuleDef socketmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5856 | PyModuleDef_HEAD_INIT, |
| 5857 | PySocket_MODULE_NAME, |
| 5858 | socket_doc, |
| 5859 | -1, |
| 5860 | socket_methods, |
| 5861 | NULL, |
| 5862 | NULL, |
| 5863 | NULL, |
| 5864 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5865 | }; |
| 5866 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 5867 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5868 | PyInit__socket(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5869 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5870 | PyObject *m, *has_ipv6; |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5871 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5872 | if (!os_init()) |
| 5873 | return NULL; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5874 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5875 | #ifdef MS_WINDOWS |
| 5876 | if (support_wsa_no_inherit == -1) { |
Steve Dower | 65e4cb1 | 2014-11-22 12:54:57 -0800 | [diff] [blame] | 5877 | #if defined(_MSC_VER) && _MSC_VER >= 1800 |
| 5878 | support_wsa_no_inherit = IsWindows7SP1OrGreater(); |
| 5879 | #else |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5880 | DWORD version = GetVersion(); |
| 5881 | DWORD major = (DWORD)LOBYTE(LOWORD(version)); |
| 5882 | DWORD minor = (DWORD)HIBYTE(LOWORD(version)); |
| 5883 | /* need Windows 7 SP1, 2008 R2 SP1 or later */ |
Steve Dower | 65e4cb1 | 2014-11-22 12:54:57 -0800 | [diff] [blame] | 5884 | support_wsa_no_inherit = major > 6 || (major == 6 && minor >= 1); |
| 5885 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5886 | } |
| 5887 | #endif |
| 5888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5889 | Py_TYPE(&sock_type) = &PyType_Type; |
| 5890 | m = PyModule_Create(&socketmodule); |
| 5891 | if (m == NULL) |
| 5892 | return NULL; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5893 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5894 | Py_INCREF(PyExc_OSError); |
| 5895 | PySocketModuleAPI.error = PyExc_OSError; |
| 5896 | Py_INCREF(PyExc_OSError); |
| 5897 | PyModule_AddObject(m, "error", PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5898 | socket_herror = PyErr_NewException("socket.herror", |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5899 | PyExc_OSError, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5900 | if (socket_herror == NULL) |
| 5901 | return NULL; |
| 5902 | Py_INCREF(socket_herror); |
| 5903 | PyModule_AddObject(m, "herror", socket_herror); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5904 | socket_gaierror = PyErr_NewException("socket.gaierror", PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5905 | NULL); |
| 5906 | if (socket_gaierror == NULL) |
| 5907 | return NULL; |
| 5908 | Py_INCREF(socket_gaierror); |
| 5909 | PyModule_AddObject(m, "gaierror", socket_gaierror); |
| 5910 | socket_timeout = PyErr_NewException("socket.timeout", |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5911 | PyExc_OSError, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5912 | if (socket_timeout == NULL) |
| 5913 | return NULL; |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 5914 | PySocketModuleAPI.timeout_error = socket_timeout; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5915 | Py_INCREF(socket_timeout); |
| 5916 | PyModule_AddObject(m, "timeout", socket_timeout); |
| 5917 | Py_INCREF((PyObject *)&sock_type); |
| 5918 | if (PyModule_AddObject(m, "SocketType", |
| 5919 | (PyObject *)&sock_type) != 0) |
| 5920 | return NULL; |
| 5921 | Py_INCREF((PyObject *)&sock_type); |
| 5922 | if (PyModule_AddObject(m, "socket", |
| 5923 | (PyObject *)&sock_type) != 0) |
| 5924 | return NULL; |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5925 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5926 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5927 | has_ipv6 = Py_True; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5928 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5929 | has_ipv6 = Py_False; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5930 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5931 | Py_INCREF(has_ipv6); |
| 5932 | PyModule_AddObject(m, "has_ipv6", has_ipv6); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5933 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5934 | /* Export C API */ |
| 5935 | if (PyModule_AddObject(m, PySocket_CAPI_NAME, |
| 5936 | PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL) |
| 5937 | ) != 0) |
| 5938 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5939 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5940 | /* Address families (we only support AF_INET and AF_UNIX) */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5941 | #ifdef AF_UNSPEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5942 | PyModule_AddIntMacro(m, AF_UNSPEC); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5943 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5944 | PyModule_AddIntMacro(m, AF_INET); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5945 | #ifdef AF_INET6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5946 | PyModule_AddIntMacro(m, AF_INET6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5947 | #endif /* AF_INET6 */ |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 5948 | #if defined(AF_UNIX) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5949 | PyModule_AddIntMacro(m, AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5950 | #endif /* AF_UNIX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5951 | #ifdef AF_AX25 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5952 | /* Amateur Radio AX.25 */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5953 | PyModule_AddIntMacro(m, AF_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5954 | #endif |
| 5955 | #ifdef AF_IPX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5956 | PyModule_AddIntMacro(m, AF_IPX); /* Novell IPX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5957 | #endif |
| 5958 | #ifdef AF_APPLETALK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5959 | /* Appletalk DDP */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5960 | PyModule_AddIntMacro(m, AF_APPLETALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5961 | #endif |
| 5962 | #ifdef AF_NETROM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5963 | /* Amateur radio NetROM */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5964 | PyModule_AddIntMacro(m, AF_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5965 | #endif |
| 5966 | #ifdef AF_BRIDGE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5967 | /* Multiprotocol bridge */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5968 | PyModule_AddIntMacro(m, AF_BRIDGE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5969 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5970 | #ifdef AF_ATMPVC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5971 | /* ATM PVCs */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5972 | PyModule_AddIntMacro(m, AF_ATMPVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5973 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5974 | #ifdef AF_AAL5 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5975 | /* Reserved for Werner's ATM */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5976 | PyModule_AddIntMacro(m, AF_AAL5); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5977 | #endif |
| 5978 | #ifdef AF_X25 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5979 | /* Reserved for X.25 project */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5980 | PyModule_AddIntMacro(m, AF_X25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5981 | #endif |
| 5982 | #ifdef AF_INET6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5983 | PyModule_AddIntMacro(m, AF_INET6); /* IP version 6 */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5984 | #endif |
| 5985 | #ifdef AF_ROSE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5986 | /* Amateur Radio X.25 PLP */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5987 | PyModule_AddIntMacro(m, AF_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5988 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5989 | #ifdef AF_DECnet |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5990 | /* Reserved for DECnet project */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5991 | PyModule_AddIntMacro(m, AF_DECnet); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5992 | #endif |
| 5993 | #ifdef AF_NETBEUI |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5994 | /* Reserved for 802.2LLC project */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5995 | PyModule_AddIntMacro(m, AF_NETBEUI); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5996 | #endif |
| 5997 | #ifdef AF_SECURITY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5998 | /* Security callback pseudo AF */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5999 | PyModule_AddIntMacro(m, AF_SECURITY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6000 | #endif |
| 6001 | #ifdef AF_KEY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6002 | /* PF_KEY key management API */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6003 | PyModule_AddIntMacro(m, AF_KEY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6004 | #endif |
| 6005 | #ifdef AF_NETLINK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6006 | /* */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6007 | PyModule_AddIntMacro(m, AF_NETLINK); |
| 6008 | PyModule_AddIntMacro(m, NETLINK_ROUTE); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6009 | #ifdef NETLINK_SKIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6010 | PyModule_AddIntMacro(m, NETLINK_SKIP); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6011 | #endif |
| 6012 | #ifdef NETLINK_W1 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6013 | PyModule_AddIntMacro(m, NETLINK_W1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6014 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6015 | PyModule_AddIntMacro(m, NETLINK_USERSOCK); |
| 6016 | PyModule_AddIntMacro(m, NETLINK_FIREWALL); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 6017 | #ifdef NETLINK_TCPDIAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6018 | PyModule_AddIntMacro(m, NETLINK_TCPDIAG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 6019 | #endif |
| 6020 | #ifdef NETLINK_NFLOG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6021 | PyModule_AddIntMacro(m, NETLINK_NFLOG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 6022 | #endif |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 6023 | #ifdef NETLINK_XFRM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6024 | PyModule_AddIntMacro(m, NETLINK_XFRM); |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 6025 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6026 | #ifdef NETLINK_ARPD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6027 | PyModule_AddIntMacro(m, NETLINK_ARPD); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6028 | #endif |
| 6029 | #ifdef NETLINK_ROUTE6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6030 | PyModule_AddIntMacro(m, NETLINK_ROUTE6); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6031 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6032 | PyModule_AddIntMacro(m, NETLINK_IP6_FW); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6033 | #ifdef NETLINK_DNRTMSG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6034 | PyModule_AddIntMacro(m, NETLINK_DNRTMSG); |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 6035 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6036 | #ifdef NETLINK_TAPBASE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6037 | PyModule_AddIntMacro(m, NETLINK_TAPBASE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6038 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6039 | #endif /* AF_NETLINK */ |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6040 | #ifdef AF_ROUTE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6041 | /* Alias to emulate 4.4BSD */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6042 | PyModule_AddIntMacro(m, AF_ROUTE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6043 | #endif |
Giampaolo Rodola' | 80e1c43 | 2013-05-21 21:02:04 +0200 | [diff] [blame] | 6044 | #ifdef AF_LINK |
| 6045 | PyModule_AddIntMacro(m, AF_LINK); |
| 6046 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6047 | #ifdef AF_ASH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6048 | /* Ash */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6049 | PyModule_AddIntMacro(m, AF_ASH); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6050 | #endif |
| 6051 | #ifdef AF_ECONET |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6052 | /* Acorn Econet */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6053 | PyModule_AddIntMacro(m, AF_ECONET); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6054 | #endif |
| 6055 | #ifdef AF_ATMSVC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6056 | /* ATM SVCs */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6057 | PyModule_AddIntMacro(m, AF_ATMSVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6058 | #endif |
| 6059 | #ifdef AF_SNA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6060 | /* Linux SNA Project (nutters!) */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6061 | PyModule_AddIntMacro(m, AF_SNA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6062 | #endif |
| 6063 | #ifdef AF_IRDA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6064 | /* IRDA sockets */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6065 | PyModule_AddIntMacro(m, AF_IRDA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6066 | #endif |
| 6067 | #ifdef AF_PPPOX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6068 | /* PPPoX sockets */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6069 | PyModule_AddIntMacro(m, AF_PPPOX); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6070 | #endif |
| 6071 | #ifdef AF_WANPIPE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6072 | /* Wanpipe API Sockets */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6073 | PyModule_AddIntMacro(m, AF_WANPIPE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6074 | #endif |
| 6075 | #ifdef AF_LLC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6076 | /* Linux LLC */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6077 | PyModule_AddIntMacro(m, AF_LLC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 6078 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 6079 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 6080 | #ifdef USE_BLUETOOTH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6081 | PyModule_AddIntMacro(m, AF_BLUETOOTH); |
| 6082 | PyModule_AddIntMacro(m, BTPROTO_L2CAP); |
| 6083 | PyModule_AddIntMacro(m, BTPROTO_HCI); |
| 6084 | PyModule_AddIntMacro(m, SOL_HCI); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 6085 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6086 | PyModule_AddIntMacro(m, HCI_FILTER); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 6087 | #endif |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 6088 | #if !defined(__FreeBSD__) |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 6089 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6090 | PyModule_AddIntMacro(m, HCI_TIME_STAMP); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 6091 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6092 | PyModule_AddIntMacro(m, HCI_DATA_DIR); |
| 6093 | PyModule_AddIntMacro(m, BTPROTO_SCO); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 6094 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6095 | PyModule_AddIntMacro(m, BTPROTO_RFCOMM); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6096 | PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00"); |
| 6097 | 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] | 6098 | #endif |
| 6099 | |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6100 | #ifdef AF_CAN |
| 6101 | /* Controller Area Network */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6102 | PyModule_AddIntMacro(m, AF_CAN); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6103 | #endif |
| 6104 | #ifdef PF_CAN |
| 6105 | /* Controller Area Network */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6106 | PyModule_AddIntMacro(m, PF_CAN); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6107 | #endif |
| 6108 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6109 | /* Reliable Datagram Sockets */ |
| 6110 | #ifdef AF_RDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6111 | PyModule_AddIntMacro(m, AF_RDS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6112 | #endif |
| 6113 | #ifdef PF_RDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6114 | PyModule_AddIntMacro(m, PF_RDS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6115 | #endif |
| 6116 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6117 | /* Kernel event messages */ |
| 6118 | #ifdef PF_SYSTEM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6119 | PyModule_AddIntMacro(m, PF_SYSTEM); |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6120 | #endif |
| 6121 | #ifdef AF_SYSTEM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6122 | PyModule_AddIntMacro(m, AF_SYSTEM); |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6123 | #endif |
| 6124 | |
Antoine Pitrou | b156a46 | 2010-10-27 20:13:57 +0000 | [diff] [blame] | 6125 | #ifdef AF_PACKET |
| 6126 | PyModule_AddIntMacro(m, AF_PACKET); |
| 6127 | #endif |
| 6128 | #ifdef PF_PACKET |
| 6129 | PyModule_AddIntMacro(m, PF_PACKET); |
| 6130 | #endif |
| 6131 | #ifdef PACKET_HOST |
| 6132 | PyModule_AddIntMacro(m, PACKET_HOST); |
| 6133 | #endif |
| 6134 | #ifdef PACKET_BROADCAST |
| 6135 | PyModule_AddIntMacro(m, PACKET_BROADCAST); |
| 6136 | #endif |
| 6137 | #ifdef PACKET_MULTICAST |
| 6138 | PyModule_AddIntMacro(m, PACKET_MULTICAST); |
| 6139 | #endif |
| 6140 | #ifdef PACKET_OTHERHOST |
| 6141 | PyModule_AddIntMacro(m, PACKET_OTHERHOST); |
| 6142 | #endif |
| 6143 | #ifdef PACKET_OUTGOING |
| 6144 | PyModule_AddIntMacro(m, PACKET_OUTGOING); |
| 6145 | #endif |
| 6146 | #ifdef PACKET_LOOPBACK |
| 6147 | PyModule_AddIntMacro(m, PACKET_LOOPBACK); |
| 6148 | #endif |
| 6149 | #ifdef PACKET_FASTROUTE |
| 6150 | PyModule_AddIntMacro(m, PACKET_FASTROUTE); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 6151 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6152 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6153 | #ifdef HAVE_LINUX_TIPC_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6154 | PyModule_AddIntMacro(m, AF_TIPC); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6155 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6156 | /* for addresses */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6157 | PyModule_AddIntMacro(m, TIPC_ADDR_NAMESEQ); |
| 6158 | PyModule_AddIntMacro(m, TIPC_ADDR_NAME); |
| 6159 | PyModule_AddIntMacro(m, TIPC_ADDR_ID); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6160 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6161 | PyModule_AddIntMacro(m, TIPC_ZONE_SCOPE); |
| 6162 | PyModule_AddIntMacro(m, TIPC_CLUSTER_SCOPE); |
| 6163 | PyModule_AddIntMacro(m, TIPC_NODE_SCOPE); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6165 | /* for setsockopt() */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6166 | PyModule_AddIntMacro(m, SOL_TIPC); |
| 6167 | PyModule_AddIntMacro(m, TIPC_IMPORTANCE); |
| 6168 | PyModule_AddIntMacro(m, TIPC_SRC_DROPPABLE); |
| 6169 | PyModule_AddIntMacro(m, TIPC_DEST_DROPPABLE); |
| 6170 | PyModule_AddIntMacro(m, TIPC_CONN_TIMEOUT); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6171 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6172 | PyModule_AddIntMacro(m, TIPC_LOW_IMPORTANCE); |
| 6173 | PyModule_AddIntMacro(m, TIPC_MEDIUM_IMPORTANCE); |
| 6174 | PyModule_AddIntMacro(m, TIPC_HIGH_IMPORTANCE); |
| 6175 | PyModule_AddIntMacro(m, TIPC_CRITICAL_IMPORTANCE); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6177 | /* for subscriptions */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6178 | PyModule_AddIntMacro(m, TIPC_SUB_PORTS); |
| 6179 | PyModule_AddIntMacro(m, TIPC_SUB_SERVICE); |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 6180 | #ifdef TIPC_SUB_CANCEL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6181 | /* doesn't seem to be available everywhere */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6182 | PyModule_AddIntMacro(m, TIPC_SUB_CANCEL); |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 6183 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6184 | PyModule_AddIntMacro(m, TIPC_WAIT_FOREVER); |
| 6185 | PyModule_AddIntMacro(m, TIPC_PUBLISHED); |
| 6186 | PyModule_AddIntMacro(m, TIPC_WITHDRAWN); |
| 6187 | PyModule_AddIntMacro(m, TIPC_SUBSCR_TIMEOUT); |
| 6188 | PyModule_AddIntMacro(m, TIPC_CFG_SRV); |
| 6189 | PyModule_AddIntMacro(m, TIPC_TOP_SRV); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6190 | #endif |
| 6191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6192 | /* Socket types */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6193 | PyModule_AddIntMacro(m, SOCK_STREAM); |
| 6194 | PyModule_AddIntMacro(m, SOCK_DGRAM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 6195 | /* We have incomplete socket support. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6196 | PyModule_AddIntMacro(m, SOCK_RAW); |
| 6197 | PyModule_AddIntMacro(m, SOCK_SEQPACKET); |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 6198 | #if defined(SOCK_RDM) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6199 | PyModule_AddIntMacro(m, SOCK_RDM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 6200 | #endif |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 6201 | #ifdef SOCK_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6202 | PyModule_AddIntMacro(m, SOCK_CLOEXEC); |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 6203 | #endif |
| 6204 | #ifdef SOCK_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6205 | PyModule_AddIntMacro(m, SOCK_NONBLOCK); |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 6206 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6208 | #ifdef SO_DEBUG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6209 | PyModule_AddIntMacro(m, SO_DEBUG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6210 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6211 | #ifdef SO_ACCEPTCONN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6212 | PyModule_AddIntMacro(m, SO_ACCEPTCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6213 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6214 | #ifdef SO_REUSEADDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6215 | PyModule_AddIntMacro(m, SO_REUSEADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6216 | #endif |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 6217 | #ifdef SO_EXCLUSIVEADDRUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6218 | PyModule_AddIntMacro(m, SO_EXCLUSIVEADDRUSE); |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 6219 | #endif |
| 6220 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6221 | #ifdef SO_KEEPALIVE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6222 | PyModule_AddIntMacro(m, SO_KEEPALIVE); |
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_DONTROUTE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6225 | PyModule_AddIntMacro(m, SO_DONTROUTE); |
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_BROADCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6228 | PyModule_AddIntMacro(m, SO_BROADCAST); |
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_USELOOPBACK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6231 | PyModule_AddIntMacro(m, SO_USELOOPBACK); |
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_LINGER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6234 | PyModule_AddIntMacro(m, SO_LINGER); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6235 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6236 | #ifdef SO_OOBINLINE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6237 | PyModule_AddIntMacro(m, SO_OOBINLINE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6238 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6239 | #ifdef SO_REUSEPORT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6240 | PyModule_AddIntMacro(m, SO_REUSEPORT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6241 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6242 | #ifdef SO_SNDBUF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6243 | PyModule_AddIntMacro(m, SO_SNDBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6244 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6245 | #ifdef SO_RCVBUF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6246 | PyModule_AddIntMacro(m, SO_RCVBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6247 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6248 | #ifdef SO_SNDLOWAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6249 | PyModule_AddIntMacro(m, SO_SNDLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6250 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6251 | #ifdef SO_RCVLOWAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6252 | PyModule_AddIntMacro(m, SO_RCVLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6253 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6254 | #ifdef SO_SNDTIMEO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6255 | PyModule_AddIntMacro(m, SO_SNDTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6256 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6257 | #ifdef SO_RCVTIMEO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6258 | PyModule_AddIntMacro(m, SO_RCVTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6259 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6260 | #ifdef SO_ERROR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6261 | PyModule_AddIntMacro(m, SO_ERROR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6262 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6263 | #ifdef SO_TYPE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6264 | PyModule_AddIntMacro(m, SO_TYPE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6265 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6266 | #ifdef SO_SETFIB |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6267 | PyModule_AddIntMacro(m, SO_SETFIB); |
Larry Hastings | f0f3795 | 2010-04-02 11:47:10 +0000 | [diff] [blame] | 6268 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6269 | #ifdef SO_PASSCRED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6270 | PyModule_AddIntMacro(m, SO_PASSCRED); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6271 | #endif |
| 6272 | #ifdef SO_PEERCRED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6273 | PyModule_AddIntMacro(m, SO_PEERCRED); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6274 | #endif |
| 6275 | #ifdef LOCAL_PEERCRED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6276 | PyModule_AddIntMacro(m, LOCAL_PEERCRED); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6277 | #endif |
Victor Stinner | cb98bed | 2011-10-25 13:45:48 +0200 | [diff] [blame] | 6278 | #ifdef SO_BINDTODEVICE |
| 6279 | PyModule_AddIntMacro(m, SO_BINDTODEVICE); |
| 6280 | #endif |
Benjamin Peterson | 1314ef7 | 2013-11-27 09:18:54 -0600 | [diff] [blame] | 6281 | #ifdef SO_PRIORITY |
| 6282 | PyModule_AddIntMacro(m, SO_PRIORITY); |
| 6283 | #endif |
Charles-François Natali | ea07eb9 | 2014-09-18 23:18:46 +0100 | [diff] [blame] | 6284 | #ifdef SO_MARK |
| 6285 | PyModule_AddIntMacro(m, SO_MARK); |
| 6286 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6288 | /* Maximum number of connections for "listen" */ |
| 6289 | #ifdef SOMAXCONN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6290 | PyModule_AddIntMacro(m, SOMAXCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6291 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6292 | PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6293 | #endif |
| 6294 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6295 | /* Ancilliary message types */ |
| 6296 | #ifdef SCM_RIGHTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6297 | PyModule_AddIntMacro(m, SCM_RIGHTS); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6298 | #endif |
| 6299 | #ifdef SCM_CREDENTIALS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6300 | PyModule_AddIntMacro(m, SCM_CREDENTIALS); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6301 | #endif |
| 6302 | #ifdef SCM_CREDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6303 | PyModule_AddIntMacro(m, SCM_CREDS); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6304 | #endif |
| 6305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6306 | /* Flags for send, recv */ |
| 6307 | #ifdef MSG_OOB |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6308 | PyModule_AddIntMacro(m, MSG_OOB); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6309 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6310 | #ifdef MSG_PEEK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6311 | PyModule_AddIntMacro(m, MSG_PEEK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6312 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6313 | #ifdef MSG_DONTROUTE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6314 | PyModule_AddIntMacro(m, MSG_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6315 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6316 | #ifdef MSG_DONTWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6317 | PyModule_AddIntMacro(m, MSG_DONTWAIT); |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 6318 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6319 | #ifdef MSG_EOR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6320 | PyModule_AddIntMacro(m, MSG_EOR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6321 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6322 | #ifdef MSG_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6323 | PyModule_AddIntMacro(m, MSG_TRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6324 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6325 | #ifdef MSG_CTRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6326 | PyModule_AddIntMacro(m, MSG_CTRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6327 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6328 | #ifdef MSG_WAITALL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6329 | PyModule_AddIntMacro(m, MSG_WAITALL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6330 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6331 | #ifdef MSG_BTAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6332 | PyModule_AddIntMacro(m, MSG_BTAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6333 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6334 | #ifdef MSG_ETAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6335 | PyModule_AddIntMacro(m, MSG_ETAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6336 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6337 | #ifdef MSG_NOSIGNAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6338 | PyModule_AddIntMacro(m, MSG_NOSIGNAL); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6339 | #endif |
| 6340 | #ifdef MSG_NOTIFICATION |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6341 | PyModule_AddIntMacro(m, MSG_NOTIFICATION); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6342 | #endif |
| 6343 | #ifdef MSG_CMSG_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6344 | PyModule_AddIntMacro(m, MSG_CMSG_CLOEXEC); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6345 | #endif |
| 6346 | #ifdef MSG_ERRQUEUE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6347 | PyModule_AddIntMacro(m, MSG_ERRQUEUE); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6348 | #endif |
| 6349 | #ifdef MSG_CONFIRM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6350 | PyModule_AddIntMacro(m, MSG_CONFIRM); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6351 | #endif |
| 6352 | #ifdef MSG_MORE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6353 | PyModule_AddIntMacro(m, MSG_MORE); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6354 | #endif |
| 6355 | #ifdef MSG_EOF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6356 | PyModule_AddIntMacro(m, MSG_EOF); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6357 | #endif |
| 6358 | #ifdef MSG_BCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6359 | PyModule_AddIntMacro(m, MSG_BCAST); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6360 | #endif |
| 6361 | #ifdef MSG_MCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6362 | PyModule_AddIntMacro(m, MSG_MCAST); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6363 | #endif |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 6364 | #ifdef MSG_FASTOPEN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6365 | PyModule_AddIntMacro(m, MSG_FASTOPEN); |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 6366 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6367 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6368 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 6369 | #ifdef SOL_SOCKET |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6370 | PyModule_AddIntMacro(m, SOL_SOCKET); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6371 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6372 | #ifdef SOL_IP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6373 | PyModule_AddIntMacro(m, SOL_IP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6374 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6375 | PyModule_AddIntConstant(m, "SOL_IP", 0); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6376 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6377 | #ifdef SOL_IPX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6378 | PyModule_AddIntMacro(m, SOL_IPX); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6379 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6380 | #ifdef SOL_AX25 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6381 | PyModule_AddIntMacro(m, SOL_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6382 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6383 | #ifdef SOL_ATALK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6384 | PyModule_AddIntMacro(m, SOL_ATALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6385 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6386 | #ifdef SOL_NETROM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6387 | PyModule_AddIntMacro(m, SOL_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6388 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6389 | #ifdef SOL_ROSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6390 | PyModule_AddIntMacro(m, SOL_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6391 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6392 | #ifdef SOL_TCP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6393 | PyModule_AddIntMacro(m, SOL_TCP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6394 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6395 | PyModule_AddIntConstant(m, "SOL_TCP", 6); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6396 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6397 | #ifdef SOL_UDP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6398 | PyModule_AddIntMacro(m, SOL_UDP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6399 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6400 | PyModule_AddIntConstant(m, "SOL_UDP", 17); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6401 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6402 | #ifdef SOL_CAN_BASE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6403 | PyModule_AddIntMacro(m, SOL_CAN_BASE); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6404 | #endif |
| 6405 | #ifdef SOL_CAN_RAW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6406 | PyModule_AddIntMacro(m, SOL_CAN_RAW); |
| 6407 | PyModule_AddIntMacro(m, CAN_RAW); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6408 | #endif |
| 6409 | #ifdef HAVE_LINUX_CAN_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6410 | PyModule_AddIntMacro(m, CAN_EFF_FLAG); |
| 6411 | PyModule_AddIntMacro(m, CAN_RTR_FLAG); |
| 6412 | PyModule_AddIntMacro(m, CAN_ERR_FLAG); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6413 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6414 | PyModule_AddIntMacro(m, CAN_SFF_MASK); |
| 6415 | PyModule_AddIntMacro(m, CAN_EFF_MASK); |
| 6416 | PyModule_AddIntMacro(m, CAN_ERR_MASK); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6417 | #endif |
| 6418 | #ifdef HAVE_LINUX_CAN_RAW_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6419 | PyModule_AddIntMacro(m, CAN_RAW_FILTER); |
| 6420 | PyModule_AddIntMacro(m, CAN_RAW_ERR_FILTER); |
| 6421 | PyModule_AddIntMacro(m, CAN_RAW_LOOPBACK); |
| 6422 | PyModule_AddIntMacro(m, CAN_RAW_RECV_OWN_MSGS); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6423 | #endif |
Charles-François Natali | 773e42d | 2013-02-05 19:42:01 +0100 | [diff] [blame] | 6424 | #ifdef HAVE_LINUX_CAN_BCM_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6425 | PyModule_AddIntMacro(m, CAN_BCM); |
Charles-François Natali | 773e42d | 2013-02-05 19:42:01 +0100 | [diff] [blame] | 6426 | PyModule_AddIntConstant(m, "CAN_BCM_TX_SETUP", TX_SETUP); |
| 6427 | PyModule_AddIntConstant(m, "CAN_BCM_TX_DELETE", TX_DELETE); |
| 6428 | PyModule_AddIntConstant(m, "CAN_BCM_TX_READ", TX_READ); |
| 6429 | PyModule_AddIntConstant(m, "CAN_BCM_TX_SEND", TX_SEND); |
| 6430 | PyModule_AddIntConstant(m, "CAN_BCM_RX_SETUP", RX_SETUP); |
| 6431 | PyModule_AddIntConstant(m, "CAN_BCM_RX_DELETE", RX_DELETE); |
| 6432 | PyModule_AddIntConstant(m, "CAN_BCM_RX_READ", RX_READ); |
| 6433 | PyModule_AddIntConstant(m, "CAN_BCM_TX_STATUS", TX_STATUS); |
| 6434 | PyModule_AddIntConstant(m, "CAN_BCM_TX_EXPIRED", TX_EXPIRED); |
| 6435 | PyModule_AddIntConstant(m, "CAN_BCM_RX_STATUS", RX_STATUS); |
| 6436 | PyModule_AddIntConstant(m, "CAN_BCM_RX_TIMEOUT", RX_TIMEOUT); |
| 6437 | PyModule_AddIntConstant(m, "CAN_BCM_RX_CHANGED", RX_CHANGED); |
| 6438 | #endif |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6439 | #ifdef SOL_RDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6440 | PyModule_AddIntMacro(m, SOL_RDS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6441 | #endif |
| 6442 | #ifdef RDS_CANCEL_SENT_TO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6443 | PyModule_AddIntMacro(m, RDS_CANCEL_SENT_TO); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6444 | #endif |
| 6445 | #ifdef RDS_GET_MR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6446 | PyModule_AddIntMacro(m, RDS_GET_MR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6447 | #endif |
| 6448 | #ifdef RDS_FREE_MR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6449 | PyModule_AddIntMacro(m, RDS_FREE_MR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6450 | #endif |
| 6451 | #ifdef RDS_RECVERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6452 | PyModule_AddIntMacro(m, RDS_RECVERR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6453 | #endif |
| 6454 | #ifdef RDS_CONG_MONITOR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6455 | PyModule_AddIntMacro(m, RDS_CONG_MONITOR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6456 | #endif |
| 6457 | #ifdef RDS_GET_MR_FOR_DEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6458 | PyModule_AddIntMacro(m, RDS_GET_MR_FOR_DEST); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6459 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6460 | #ifdef IPPROTO_IP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6461 | PyModule_AddIntMacro(m, IPPROTO_IP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6462 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6463 | PyModule_AddIntConstant(m, "IPPROTO_IP", 0); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6464 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6465 | #ifdef IPPROTO_HOPOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6466 | PyModule_AddIntMacro(m, IPPROTO_HOPOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6467 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6468 | #ifdef IPPROTO_ICMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6469 | PyModule_AddIntMacro(m, IPPROTO_ICMP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6470 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6471 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6472 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6473 | #ifdef IPPROTO_IGMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6474 | PyModule_AddIntMacro(m, IPPROTO_IGMP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6475 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6476 | #ifdef IPPROTO_GGP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6477 | PyModule_AddIntMacro(m, IPPROTO_GGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6478 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6479 | #ifdef IPPROTO_IPV4 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6480 | PyModule_AddIntMacro(m, IPPROTO_IPV4); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6481 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6482 | #ifdef IPPROTO_IPV6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6483 | PyModule_AddIntMacro(m, IPPROTO_IPV6); |
Martin v. Löwis | a0f1734 | 2003-10-03 13:56:20 +0000 | [diff] [blame] | 6484 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6485 | #ifdef IPPROTO_IPIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6486 | PyModule_AddIntMacro(m, IPPROTO_IPIP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6487 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6488 | #ifdef IPPROTO_TCP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6489 | PyModule_AddIntMacro(m, IPPROTO_TCP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6490 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6491 | PyModule_AddIntConstant(m, "IPPROTO_TCP", 6); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6492 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6493 | #ifdef IPPROTO_EGP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6494 | PyModule_AddIntMacro(m, IPPROTO_EGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6495 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6496 | #ifdef IPPROTO_PUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6497 | PyModule_AddIntMacro(m, IPPROTO_PUP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6498 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6499 | #ifdef IPPROTO_UDP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6500 | PyModule_AddIntMacro(m, IPPROTO_UDP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6501 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6502 | PyModule_AddIntConstant(m, "IPPROTO_UDP", 17); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6503 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6504 | #ifdef IPPROTO_IDP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6505 | PyModule_AddIntMacro(m, IPPROTO_IDP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6506 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6507 | #ifdef IPPROTO_HELLO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6508 | PyModule_AddIntMacro(m, IPPROTO_HELLO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6509 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6510 | #ifdef IPPROTO_ND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6511 | PyModule_AddIntMacro(m, IPPROTO_ND); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6512 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6513 | #ifdef IPPROTO_TP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6514 | PyModule_AddIntMacro(m, IPPROTO_TP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6515 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6516 | #ifdef IPPROTO_IPV6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6517 | PyModule_AddIntMacro(m, IPPROTO_IPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6518 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6519 | #ifdef IPPROTO_ROUTING |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6520 | PyModule_AddIntMacro(m, IPPROTO_ROUTING); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6521 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6522 | #ifdef IPPROTO_FRAGMENT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6523 | PyModule_AddIntMacro(m, IPPROTO_FRAGMENT); |
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_RSVP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6526 | PyModule_AddIntMacro(m, IPPROTO_RSVP); |
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_GRE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6529 | PyModule_AddIntMacro(m, IPPROTO_GRE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6530 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6531 | #ifdef IPPROTO_ESP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6532 | PyModule_AddIntMacro(m, IPPROTO_ESP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6533 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6534 | #ifdef IPPROTO_AH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6535 | PyModule_AddIntMacro(m, IPPROTO_AH); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6536 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6537 | #ifdef IPPROTO_MOBILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6538 | PyModule_AddIntMacro(m, IPPROTO_MOBILE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6539 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6540 | #ifdef IPPROTO_ICMPV6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6541 | PyModule_AddIntMacro(m, IPPROTO_ICMPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6542 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6543 | #ifdef IPPROTO_NONE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6544 | PyModule_AddIntMacro(m, IPPROTO_NONE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6545 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6546 | #ifdef IPPROTO_DSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6547 | PyModule_AddIntMacro(m, IPPROTO_DSTOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6548 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6549 | #ifdef IPPROTO_XTP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6550 | PyModule_AddIntMacro(m, IPPROTO_XTP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6551 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6552 | #ifdef IPPROTO_EON |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6553 | PyModule_AddIntMacro(m, IPPROTO_EON); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6554 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6555 | #ifdef IPPROTO_PIM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6556 | PyModule_AddIntMacro(m, IPPROTO_PIM); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6557 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6558 | #ifdef IPPROTO_IPCOMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6559 | PyModule_AddIntMacro(m, IPPROTO_IPCOMP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6560 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6561 | #ifdef IPPROTO_VRRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6562 | PyModule_AddIntMacro(m, IPPROTO_VRRP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6563 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6564 | #ifdef IPPROTO_SCTP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6565 | PyModule_AddIntMacro(m, IPPROTO_SCTP); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6566 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6567 | #ifdef IPPROTO_BIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6568 | PyModule_AddIntMacro(m, IPPROTO_BIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6569 | #endif |
| 6570 | /**/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6571 | #ifdef IPPROTO_RAW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6572 | PyModule_AddIntMacro(m, IPPROTO_RAW); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6573 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6574 | PyModule_AddIntConstant(m, "IPPROTO_RAW", 255); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6575 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6576 | #ifdef IPPROTO_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6577 | PyModule_AddIntMacro(m, IPPROTO_MAX); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6578 | #endif |
| 6579 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6580 | #ifdef SYSPROTO_CONTROL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6581 | PyModule_AddIntMacro(m, SYSPROTO_CONTROL); |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6582 | #endif |
| 6583 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6584 | /* Some port configuration */ |
| 6585 | #ifdef IPPORT_RESERVED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6586 | PyModule_AddIntMacro(m, IPPORT_RESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6587 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6588 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6589 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6590 | #ifdef IPPORT_USERRESERVED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6591 | PyModule_AddIntMacro(m, IPPORT_USERRESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6592 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6593 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6594 | #endif |
| 6595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6596 | /* Some reserved IP v.4 addresses */ |
| 6597 | #ifdef INADDR_ANY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6598 | PyModule_AddIntMacro(m, INADDR_ANY); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6599 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6600 | PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6601 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6602 | #ifdef INADDR_BROADCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6603 | PyModule_AddIntMacro(m, INADDR_BROADCAST); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6604 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6605 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6606 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6607 | #ifdef INADDR_LOOPBACK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6608 | PyModule_AddIntMacro(m, INADDR_LOOPBACK); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6609 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6610 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6611 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6612 | #ifdef INADDR_UNSPEC_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6613 | PyModule_AddIntMacro(m, INADDR_UNSPEC_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6614 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6615 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6616 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6617 | #ifdef INADDR_ALLHOSTS_GROUP |
| 6618 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", |
| 6619 | INADDR_ALLHOSTS_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6620 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6621 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6622 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6623 | #ifdef INADDR_MAX_LOCAL_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6624 | PyModule_AddIntMacro(m, INADDR_MAX_LOCAL_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6625 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6626 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6627 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6628 | #ifdef INADDR_NONE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6629 | PyModule_AddIntMacro(m, INADDR_NONE); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6630 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6631 | PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6632 | #endif |
| 6633 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6634 | /* IPv4 [gs]etsockopt options */ |
| 6635 | #ifdef IP_OPTIONS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6636 | PyModule_AddIntMacro(m, IP_OPTIONS); |
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_HDRINCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6639 | PyModule_AddIntMacro(m, IP_HDRINCL); |
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_TOS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6642 | PyModule_AddIntMacro(m, IP_TOS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6643 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6644 | #ifdef IP_TTL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6645 | PyModule_AddIntMacro(m, IP_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6646 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6647 | #ifdef IP_RECVOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6648 | PyModule_AddIntMacro(m, IP_RECVOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6649 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6650 | #ifdef IP_RECVRETOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6651 | PyModule_AddIntMacro(m, IP_RECVRETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6652 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6653 | #ifdef IP_RECVDSTADDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6654 | PyModule_AddIntMacro(m, IP_RECVDSTADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6655 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6656 | #ifdef IP_RETOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6657 | PyModule_AddIntMacro(m, IP_RETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6658 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6659 | #ifdef IP_MULTICAST_IF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6660 | PyModule_AddIntMacro(m, IP_MULTICAST_IF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6661 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6662 | #ifdef IP_MULTICAST_TTL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6663 | PyModule_AddIntMacro(m, IP_MULTICAST_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6664 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6665 | #ifdef IP_MULTICAST_LOOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6666 | PyModule_AddIntMacro(m, IP_MULTICAST_LOOP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6667 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6668 | #ifdef IP_ADD_MEMBERSHIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6669 | PyModule_AddIntMacro(m, IP_ADD_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6670 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6671 | #ifdef IP_DROP_MEMBERSHIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6672 | PyModule_AddIntMacro(m, IP_DROP_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6673 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6674 | #ifdef IP_DEFAULT_MULTICAST_TTL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6675 | PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_TTL); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6676 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6677 | #ifdef IP_DEFAULT_MULTICAST_LOOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6678 | PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_LOOP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6679 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6680 | #ifdef IP_MAX_MEMBERSHIPS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6681 | PyModule_AddIntMacro(m, IP_MAX_MEMBERSHIPS); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6682 | #endif |
Charles-François Natali | 564a42c | 2011-12-17 14:59:56 +0100 | [diff] [blame] | 6683 | #ifdef IP_TRANSPARENT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6684 | PyModule_AddIntMacro(m, IP_TRANSPARENT); |
Charles-François Natali | 564a42c | 2011-12-17 14:59:56 +0100 | [diff] [blame] | 6685 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6686 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6687 | /* IPv6 [gs]etsockopt options, defined in RFC2553 */ |
| 6688 | #ifdef IPV6_JOIN_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6689 | PyModule_AddIntMacro(m, IPV6_JOIN_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6690 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6691 | #ifdef IPV6_LEAVE_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6692 | PyModule_AddIntMacro(m, IPV6_LEAVE_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6693 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6694 | #ifdef IPV6_MULTICAST_HOPS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6695 | PyModule_AddIntMacro(m, IPV6_MULTICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6696 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6697 | #ifdef IPV6_MULTICAST_IF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6698 | PyModule_AddIntMacro(m, IPV6_MULTICAST_IF); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6699 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6700 | #ifdef IPV6_MULTICAST_LOOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6701 | PyModule_AddIntMacro(m, IPV6_MULTICAST_LOOP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6702 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6703 | #ifdef IPV6_UNICAST_HOPS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6704 | PyModule_AddIntMacro(m, IPV6_UNICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6705 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6706 | /* Additional IPV6 socket options, defined in RFC 3493 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6707 | #ifdef IPV6_V6ONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6708 | PyModule_AddIntMacro(m, IPV6_V6ONLY); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6709 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6710 | /* Advanced IPV6 socket options, from RFC 3542 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6711 | #ifdef IPV6_CHECKSUM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6712 | PyModule_AddIntMacro(m, IPV6_CHECKSUM); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6713 | #endif |
| 6714 | #ifdef IPV6_DONTFRAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6715 | PyModule_AddIntMacro(m, IPV6_DONTFRAG); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6716 | #endif |
| 6717 | #ifdef IPV6_DSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6718 | PyModule_AddIntMacro(m, IPV6_DSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6719 | #endif |
| 6720 | #ifdef IPV6_HOPLIMIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6721 | PyModule_AddIntMacro(m, IPV6_HOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6722 | #endif |
| 6723 | #ifdef IPV6_HOPOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6724 | PyModule_AddIntMacro(m, IPV6_HOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6725 | #endif |
| 6726 | #ifdef IPV6_NEXTHOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6727 | PyModule_AddIntMacro(m, IPV6_NEXTHOP); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6728 | #endif |
| 6729 | #ifdef IPV6_PATHMTU |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6730 | PyModule_AddIntMacro(m, IPV6_PATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6731 | #endif |
| 6732 | #ifdef IPV6_PKTINFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6733 | PyModule_AddIntMacro(m, IPV6_PKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6734 | #endif |
| 6735 | #ifdef IPV6_RECVDSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6736 | PyModule_AddIntMacro(m, IPV6_RECVDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6737 | #endif |
| 6738 | #ifdef IPV6_RECVHOPLIMIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6739 | PyModule_AddIntMacro(m, IPV6_RECVHOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6740 | #endif |
| 6741 | #ifdef IPV6_RECVHOPOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6742 | PyModule_AddIntMacro(m, IPV6_RECVHOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6743 | #endif |
| 6744 | #ifdef IPV6_RECVPKTINFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6745 | PyModule_AddIntMacro(m, IPV6_RECVPKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6746 | #endif |
| 6747 | #ifdef IPV6_RECVRTHDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6748 | PyModule_AddIntMacro(m, IPV6_RECVRTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6749 | #endif |
| 6750 | #ifdef IPV6_RECVTCLASS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6751 | PyModule_AddIntMacro(m, IPV6_RECVTCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6752 | #endif |
| 6753 | #ifdef IPV6_RTHDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6754 | PyModule_AddIntMacro(m, IPV6_RTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6755 | #endif |
| 6756 | #ifdef IPV6_RTHDRDSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6757 | PyModule_AddIntMacro(m, IPV6_RTHDRDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6758 | #endif |
| 6759 | #ifdef IPV6_RTHDR_TYPE_0 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6760 | PyModule_AddIntMacro(m, IPV6_RTHDR_TYPE_0); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6761 | #endif |
| 6762 | #ifdef IPV6_RECVPATHMTU |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6763 | PyModule_AddIntMacro(m, IPV6_RECVPATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6764 | #endif |
| 6765 | #ifdef IPV6_TCLASS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6766 | PyModule_AddIntMacro(m, IPV6_TCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6767 | #endif |
| 6768 | #ifdef IPV6_USE_MIN_MTU |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6769 | PyModule_AddIntMacro(m, IPV6_USE_MIN_MTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6770 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6771 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6772 | /* TCP options */ |
| 6773 | #ifdef TCP_NODELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6774 | PyModule_AddIntMacro(m, TCP_NODELAY); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6775 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6776 | #ifdef TCP_MAXSEG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6777 | PyModule_AddIntMacro(m, TCP_MAXSEG); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6778 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6779 | #ifdef TCP_CORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6780 | PyModule_AddIntMacro(m, TCP_CORK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6781 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6782 | #ifdef TCP_KEEPIDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6783 | PyModule_AddIntMacro(m, TCP_KEEPIDLE); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6784 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6785 | #ifdef TCP_KEEPINTVL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6786 | PyModule_AddIntMacro(m, TCP_KEEPINTVL); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6787 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6788 | #ifdef TCP_KEEPCNT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6789 | PyModule_AddIntMacro(m, TCP_KEEPCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6790 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6791 | #ifdef TCP_SYNCNT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6792 | PyModule_AddIntMacro(m, TCP_SYNCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6793 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6794 | #ifdef TCP_LINGER2 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6795 | PyModule_AddIntMacro(m, TCP_LINGER2); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6796 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6797 | #ifdef TCP_DEFER_ACCEPT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6798 | PyModule_AddIntMacro(m, TCP_DEFER_ACCEPT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6799 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6800 | #ifdef TCP_WINDOW_CLAMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6801 | PyModule_AddIntMacro(m, TCP_WINDOW_CLAMP); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6802 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6803 | #ifdef TCP_INFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6804 | PyModule_AddIntMacro(m, TCP_INFO); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6805 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6806 | #ifdef TCP_QUICKACK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6807 | PyModule_AddIntMacro(m, TCP_QUICKACK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6808 | #endif |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 6809 | #ifdef TCP_FASTOPEN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6810 | PyModule_AddIntMacro(m, TCP_FASTOPEN); |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 6811 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6812 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6813 | /* IPX options */ |
| 6814 | #ifdef IPX_TYPE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6815 | PyModule_AddIntMacro(m, IPX_TYPE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6816 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 6817 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6818 | /* Reliable Datagram Sockets */ |
| 6819 | #ifdef RDS_CMSG_RDMA_ARGS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6820 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_ARGS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6821 | #endif |
| 6822 | #ifdef RDS_CMSG_RDMA_DEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6823 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_DEST); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6824 | #endif |
| 6825 | #ifdef RDS_CMSG_RDMA_MAP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6826 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_MAP); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6827 | #endif |
| 6828 | #ifdef RDS_CMSG_RDMA_STATUS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6829 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_STATUS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6830 | #endif |
| 6831 | #ifdef RDS_CMSG_RDMA_UPDATE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6832 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_UPDATE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6833 | #endif |
| 6834 | #ifdef RDS_RDMA_READWRITE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6835 | PyModule_AddIntMacro(m, RDS_RDMA_READWRITE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6836 | #endif |
| 6837 | #ifdef RDS_RDMA_FENCE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6838 | PyModule_AddIntMacro(m, RDS_RDMA_FENCE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6839 | #endif |
| 6840 | #ifdef RDS_RDMA_INVALIDATE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6841 | PyModule_AddIntMacro(m, RDS_RDMA_INVALIDATE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6842 | #endif |
| 6843 | #ifdef RDS_RDMA_USE_ONCE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6844 | PyModule_AddIntMacro(m, RDS_RDMA_USE_ONCE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6845 | #endif |
| 6846 | #ifdef RDS_RDMA_DONTWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6847 | PyModule_AddIntMacro(m, RDS_RDMA_DONTWAIT); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6848 | #endif |
| 6849 | #ifdef RDS_RDMA_NOTIFY_ME |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6850 | PyModule_AddIntMacro(m, RDS_RDMA_NOTIFY_ME); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6851 | #endif |
| 6852 | #ifdef RDS_RDMA_SILENT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6853 | PyModule_AddIntMacro(m, RDS_RDMA_SILENT); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6854 | #endif |
| 6855 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6856 | /* get{addr,name}info parameters */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6857 | #ifdef EAI_ADDRFAMILY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6858 | PyModule_AddIntMacro(m, EAI_ADDRFAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6859 | #endif |
| 6860 | #ifdef EAI_AGAIN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6861 | PyModule_AddIntMacro(m, EAI_AGAIN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6862 | #endif |
| 6863 | #ifdef EAI_BADFLAGS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6864 | PyModule_AddIntMacro(m, EAI_BADFLAGS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6865 | #endif |
| 6866 | #ifdef EAI_FAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6867 | PyModule_AddIntMacro(m, EAI_FAIL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6868 | #endif |
| 6869 | #ifdef EAI_FAMILY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6870 | PyModule_AddIntMacro(m, EAI_FAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6871 | #endif |
| 6872 | #ifdef EAI_MEMORY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6873 | PyModule_AddIntMacro(m, EAI_MEMORY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6874 | #endif |
| 6875 | #ifdef EAI_NODATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6876 | PyModule_AddIntMacro(m, EAI_NODATA); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6877 | #endif |
| 6878 | #ifdef EAI_NONAME |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6879 | PyModule_AddIntMacro(m, EAI_NONAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6880 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6881 | #ifdef EAI_OVERFLOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6882 | PyModule_AddIntMacro(m, EAI_OVERFLOW); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6883 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6884 | #ifdef EAI_SERVICE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6885 | PyModule_AddIntMacro(m, EAI_SERVICE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6886 | #endif |
| 6887 | #ifdef EAI_SOCKTYPE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6888 | PyModule_AddIntMacro(m, EAI_SOCKTYPE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6889 | #endif |
| 6890 | #ifdef EAI_SYSTEM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6891 | PyModule_AddIntMacro(m, EAI_SYSTEM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6892 | #endif |
| 6893 | #ifdef EAI_BADHINTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6894 | PyModule_AddIntMacro(m, EAI_BADHINTS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6895 | #endif |
| 6896 | #ifdef EAI_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6897 | PyModule_AddIntMacro(m, EAI_PROTOCOL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6898 | #endif |
| 6899 | #ifdef EAI_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6900 | PyModule_AddIntMacro(m, EAI_MAX); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6901 | #endif |
| 6902 | #ifdef AI_PASSIVE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6903 | PyModule_AddIntMacro(m, AI_PASSIVE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6904 | #endif |
| 6905 | #ifdef AI_CANONNAME |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6906 | PyModule_AddIntMacro(m, AI_CANONNAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6907 | #endif |
| 6908 | #ifdef AI_NUMERICHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6909 | PyModule_AddIntMacro(m, AI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6910 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6911 | #ifdef AI_NUMERICSERV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6912 | PyModule_AddIntMacro(m, AI_NUMERICSERV); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6913 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6914 | #ifdef AI_MASK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6915 | PyModule_AddIntMacro(m, AI_MASK); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6916 | #endif |
| 6917 | #ifdef AI_ALL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6918 | PyModule_AddIntMacro(m, AI_ALL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6919 | #endif |
| 6920 | #ifdef AI_V4MAPPED_CFG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6921 | PyModule_AddIntMacro(m, AI_V4MAPPED_CFG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6922 | #endif |
| 6923 | #ifdef AI_ADDRCONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6924 | PyModule_AddIntMacro(m, AI_ADDRCONFIG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6925 | #endif |
| 6926 | #ifdef AI_V4MAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6927 | PyModule_AddIntMacro(m, AI_V4MAPPED); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6928 | #endif |
| 6929 | #ifdef AI_DEFAULT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6930 | PyModule_AddIntMacro(m, AI_DEFAULT); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6931 | #endif |
| 6932 | #ifdef NI_MAXHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6933 | PyModule_AddIntMacro(m, NI_MAXHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6934 | #endif |
| 6935 | #ifdef NI_MAXSERV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6936 | PyModule_AddIntMacro(m, NI_MAXSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6937 | #endif |
| 6938 | #ifdef NI_NOFQDN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6939 | PyModule_AddIntMacro(m, NI_NOFQDN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6940 | #endif |
| 6941 | #ifdef NI_NUMERICHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6942 | PyModule_AddIntMacro(m, NI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6943 | #endif |
| 6944 | #ifdef NI_NAMEREQD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6945 | PyModule_AddIntMacro(m, NI_NAMEREQD); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6946 | #endif |
| 6947 | #ifdef NI_NUMERICSERV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6948 | PyModule_AddIntMacro(m, NI_NUMERICSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6949 | #endif |
| 6950 | #ifdef NI_DGRAM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6951 | PyModule_AddIntMacro(m, NI_DGRAM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6952 | #endif |
| 6953 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6954 | /* shutdown() parameters */ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6955 | #ifdef SHUT_RD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6956 | PyModule_AddIntMacro(m, SHUT_RD); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6957 | #elif defined(SD_RECEIVE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6958 | PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6959 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6960 | PyModule_AddIntConstant(m, "SHUT_RD", 0); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6961 | #endif |
| 6962 | #ifdef SHUT_WR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6963 | PyModule_AddIntMacro(m, SHUT_WR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6964 | #elif defined(SD_SEND) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6965 | PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6966 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6967 | PyModule_AddIntConstant(m, "SHUT_WR", 1); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6968 | #endif |
| 6969 | #ifdef SHUT_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6970 | PyModule_AddIntMacro(m, SHUT_RDWR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6971 | #elif defined(SD_BOTH) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6972 | PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6973 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6974 | PyModule_AddIntConstant(m, "SHUT_RDWR", 2); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6975 | #endif |
| 6976 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 6977 | #ifdef SIO_RCVALL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6978 | { |
| 6979 | DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS}; |
| 6980 | const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"}; |
| 6981 | int i; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6982 | for(i = 0; i<Py_ARRAY_LENGTH(codes); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6983 | PyObject *tmp; |
| 6984 | tmp = PyLong_FromUnsignedLong(codes[i]); |
| 6985 | if (tmp == NULL) |
| 6986 | return NULL; |
| 6987 | PyModule_AddObject(m, names[i], tmp); |
| 6988 | } |
| 6989 | } |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6990 | PyModule_AddIntMacro(m, RCVALL_OFF); |
| 6991 | PyModule_AddIntMacro(m, RCVALL_ON); |
| 6992 | PyModule_AddIntMacro(m, RCVALL_SOCKETLEVELONLY); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 6993 | #ifdef RCVALL_IPLEVEL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6994 | PyModule_AddIntMacro(m, RCVALL_IPLEVEL); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 6995 | #endif |
| 6996 | #ifdef RCVALL_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6997 | PyModule_AddIntMacro(m, RCVALL_MAX); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 6998 | #endif |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 6999 | #endif /* _MSTCPIP_ */ |
| 7000 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7001 | /* Initialize gethostbyname lock */ |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 7002 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7003 | netdb_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 7004 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7005 | return m; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 7006 | } |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7007 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 7008 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7009 | #ifndef HAVE_INET_PTON |
Christian Heimes | 96e7b3d | 2007-11-20 06:51:17 +0000 | [diff] [blame] | 7010 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 7011 | |
| 7012 | /* Simplistic emulation code for inet_pton that only works for IPv4 */ |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 7013 | /* These are not exposed because they do not set errno properly */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 7014 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 7015 | int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 7016 | inet_pton(int af, const char *src, void *dst) |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7017 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7018 | if (af == AF_INET) { |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 7019 | #if (SIZEOF_INT != 4) |
| 7020 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 7021 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7022 | unsigned int packed_addr; |
| 7023 | packed_addr = inet_addr(src); |
| 7024 | if (packed_addr == INADDR_NONE) |
| 7025 | return 0; |
| 7026 | memcpy(dst, &packed_addr, 4); |
| 7027 | return 1; |
| 7028 | } |
| 7029 | /* Should set errno to EAFNOSUPPORT */ |
| 7030 | return -1; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7031 | } |
| 7032 | |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 7033 | const char * |
| 7034 | 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] | 7035 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7036 | if (af == AF_INET) { |
| 7037 | struct in_addr packed_addr; |
| 7038 | if (size < 16) |
| 7039 | /* Should set errno to ENOSPC. */ |
| 7040 | return NULL; |
| 7041 | memcpy(&packed_addr, src, sizeof(packed_addr)); |
| 7042 | return strncpy(dst, inet_ntoa(packed_addr), size); |
| 7043 | } |
| 7044 | /* Should set errno to EAFNOSUPPORT */ |
| 7045 | return NULL; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7046 | } |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 7047 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 7048 | #endif |
Christian Heimes | b615069 | 2007-11-15 23:37:07 +0000 | [diff] [blame] | 7049 | #endif |